]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/catsearch.c
1a02f235b9ae487f137f8b7b34653a651595e8bb
[netatalk.git] / etc / afpd / catsearch.c
1 /* 
2  * Netatalk 2002 (c)
3  * Copyright (C) 1990, 1993 Regents of The University of Michigan
4  * All Rights Reserved. See COPYRIGHT
5  */
6
7
8 /*
9  * This file contains FPCatSearch implementation. FPCatSearch performs
10  * file/directory search based on specified criteria. It is used by client
11  * to perform fast searches on (propably) big volumes. So, it has to be
12  * pretty fast.
13  *
14  * This implementation bypasses most of adouble/libatalk stuff as long as
15  * possible and does a standard filesystem search. It calls higher-level
16  * libatalk/afpd functions only when it is really needed, mainly while
17  * returning some non-UNIX information or filtering by non-UNIX criteria.
18  *
19  * Initial version written by Rafal Lewczuk <rlewczuk@pronet.pl>
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <syslog.h>
32 #include <unistd.h>
33
34 #if STDC_HEADERS
35 #include <string.h>
36 #else
37 #ifndef HAVE_MEMCPY
38 #define memcpy(d,s,n) bcopy ((s), (d), (n))
39 #define memmove(d,s,n) bcopy ((s), (d), (n))
40 #endif /* ! HAVE_MEMCPY */
41 #endif
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/file.h>
46 #include <netinet/in.h>
47
48 #include <netatalk/endian.h>
49 #include <atalk/afp.h>
50 #include <atalk/adouble.h>
51 #ifdef CNID_DB
52 #include <atalk/cnid.h>
53 #endif /* DID_MTAB */
54 #include "desktop.h"
55 #include "directory.h"
56 #include "file.h"
57 #include "volume.h"
58 #include "globals.h"
59 #include "filedir.h"
60 #include "fork.h"
61
62 #ifdef DID_MTAB
63 #include "parse-mtab.h"
64 #endif /* DID_MTAB */
65
66 #ifdef WITH_CATSEARCH
67
68 struct finderinfo {
69         u_int32_t f_type;
70         u_int32_t creator;
71         u_int8_t attrs;    /* File attributes (8 bits)*/
72         u_int8_t label;    /* Label (8 bits)*/
73         char reserved[22]; /* Unknown (at least for now...) */
74 };
75
76 /* Known attributes:
77  * 0x04 - has a custom icon
78  * 0x20 - name/icon is locked
79  * 0x40 - is invisible
80  * 0x80 - is alias
81  *
82  * Known labels:
83  * 0x02 - project 2
84  * 0x04 - project 1
85  * 0x06 - personal
86  * 0x08 - cool
87  * 0x0a - in progress
88  * 0x0c - hot
89  * 0x0e - essential
90  */
91
92 /* This is our search-criteria structure. */
93 struct scrit {
94         u_int32_t rbitmap;          /* Request bitmap - which values should we check ? */
95         u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
96         u_int16_t attr;             /* File attributes */
97         time_t cdate;               /* Creation date */
98         time_t mdate;               /* Last modification date */
99         time_t bdate;               /* Last backup date */
100         u_int32_t pdid;             /* Parent DID */
101         u_int16_t offcnt;           /* Offspring count */
102         struct finderinfo finfo;    /* Finder info */
103         char lname[32];             /* Long name */
104 };
105
106 /*
107  * Directory tree search is recursive by its nature. But AFP specification
108  * requires FPCatSearch to pause after returning n results and be able to
109  * resume the search later. So we have to do recursive search using flat
110  * (iterative) algorithm and remember all directories to look into in an
111  * stack-like structure. The structure below is one item of directory stack.
112  *
113  */
114 struct dsitem {
115         char *lname;     /* Long name */
116         struct dir *dir; /* Structure describing this directory */
117         int pidx;        /* Parent's dsitem structure index. */
118         int checked;     /* Have we checked this directory ? */
119         char *path;      /* UNIX path to this directory */
120 };
121  
122
123 #define DS_BSIZE 128
124 static int cur_pos = 0;    /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
125 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
126 static int save_cidx = -1; /* Saved index of currently scanned directory. */
127
128 static struct dsitem *dstack = NULL; /* Directory stack data... */
129 static int dssize = 0;               /* Directory stack (allocated) size... */
130 static int dsidx = 0;                /* First free item index... */
131
132 static struct scrit c1, c2;          /* search criteria */
133
134 /* Puts new item onto directory stack. */
135 static int addstack(char *lname, struct dir *dir, int pidx)
136 {
137         struct dsitem *ds;
138
139         /* check if we have some space on stack... */
140         if (dsidx >= dssize) {
141                 dssize += DS_BSIZE;
142                 dstack = realloc(dstack, dssize * sizeof(struct dsitem));       
143                 if (dstack == NULL)
144                         return -1;
145         }
146
147         /* Put new element. Allocate and copy lname and path. */
148         ds = dstack + dsidx++;
149         ds->lname = strdup(lname);
150         ds->dir = dir;
151         ds->pidx = pidx;
152         if (pidx >= 0) {
153                 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
154                 strcpy(ds->path, dstack[pidx].path);
155                 strcat(ds->path, "/");
156                 strcat(ds->path, ds->lname);
157         }
158
159         ds->checked = 0;
160
161         return 0;
162 }
163
164 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
165 static int reducestack()
166 {
167         int r;
168         if (save_cidx != -1) {
169                 r = save_cidx;
170                 save_cidx = -1;
171                 return r;
172         }
173
174         while (dsidx > 0) {
175                 if (dstack[dsidx-1].checked) {
176                         dsidx--;
177                         free(dstack[dsidx].lname);
178                         free(dstack[dsidx].path);
179                         /* Check if we need to free (or release) dir structures */
180                 } else
181                         return dsidx - 1;
182         } // while
183         return -1;
184 } /* reducestack() */
185
186 /* Clears directory stack. */
187 static void clearstack() 
188 {
189         save_cidx = -1;
190         while (dsidx > 0) {
191                 dsidx--;
192                 free(dstack[dsidx].lname);
193                 free(dstack[dsidx].path);
194                 /* Check if we need to free (or release) dir structures */
195         }
196 } /* clearstack() */
197
198 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
199 static int resolve_dir(struct vol *vol, int cidx)
200 {
201         struct dir *dir, *curdir;
202         struct stat statbuf;
203
204         if (dstack[cidx].dir != NULL)
205                 return 1;
206
207         if (dstack[cidx].pidx < 0)
208                 return 0;
209
210         if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
211                return 0;
212
213         curdir = dstack[dstack[cidx].pidx].dir;
214         dir = curdir->d_child;
215         while (dir) {
216                 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
217                         break;
218                 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
219         } /* while */
220
221         if (!dir)
222                 if (stat(dstack[cidx].path, &statbuf)==-1) {
223                         syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
224                         return 0;
225                 }
226
227         if (!dir && ((dir = adddir(vol, curdir, dstack[cidx].lname, strlen(dstack[cidx].lname),
228                                                 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
229                         return 0;
230         dstack[cidx].dir = dir;
231
232         return 1;
233 } /* resolve_dir */
234
235 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
236 static struct adouble *adl_lkup(struct vol *vol, char *upath, int cidx)
237 {
238         static struct adouble ad;
239         struct adouble *adp;
240         char *mpath = utompath(vol, upath);
241         struct ofork *of;
242
243 /*
244         //if (dstack[cidx].dir == NULL && !resolve_dir(vol, cidx))
245         //      return NULL;
246
247         //if ((of = of_findname(vol, dstack[cidx].dir, mpath))) {
248         //      adp = of->of_ad;
249         //} else {
250         */
251                 memset(&ad, 0, sizeof(ad));
252                 adp = &ad;
253         /* } */
254
255         if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
256                 return NULL;
257         } 
258         return adp;     
259 }
260
261 #define CATPBIT_PARTIAL 31
262 /* Criteria checker. This function returns a 2-bit value. */
263 /* bit 0 means if checked file meets given criteria. */
264 /* bit 1 means if it is a directory and we should descent into it. */
265 /* uname - UNIX name 
266  * fname - our fname (translated to UNIX)
267  * cidx - index in directory stack
268  */
269 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
270         int r = 0;
271         struct stat sbuf;
272         u_int16_t attr;
273         struct finderinfo *finfo = NULL;
274         struct adouble *adp = NULL;
275         time_t c_date, b_date;
276
277         if (stat(uname, &sbuf) < 0)
278                 return 0;
279         
280         if (S_ISDIR(sbuf.st_mode))
281                 r = 2;
282
283         /* Kind of optimization: 
284          * -- first check things we've already have - filename
285          * -- last check things we get from ad_open()
286          */
287
288         /* Check for filename */
289         if (c1.rbitmap & (1<<DIRPBIT_LNAME)) { 
290                 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
291                         if (strstr(fname, c1.lname) == NULL)
292                                 goto crit_check_ret;
293                 } else
294                         if (strcmp(fname, c1.lname) != 0)
295                                 goto crit_check_ret;
296         } /* if (c1.rbitmap & ... */
297
298
299         /* FIXME */
300         if ((unsigned)c2.mdate > 0x7fffffff)
301                 c2.mdate = 0x7fffffff;
302         if ((unsigned)c2.cdate > 0x7fffffff)
303                 c2.cdate = 0x7fffffff;
304         if ((unsigned)c2.bdate > 0x7fffffff)
305                 c2.bdate = 0x7fffffff;
306
307         /* Check for modification date FIXME: should we look at adouble structure ? */
308         if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) 
309                 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
310                         goto crit_check_ret;
311
312         /* Check for creation date... */
313         if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
314                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
315                         if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
316                                 c_date = AD_DATE_TO_UNIX(c_date);
317                         else c_date = sbuf.st_mtime;
318                 } else c_date = sbuf.st_mtime;
319                 if (c_date < c1.cdate || c_date > c2.cdate)
320                         goto crit_check_ret;
321         }
322
323         /* Check for backup date... */
324         if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
325                 if (adp || (adp == adl_lkup(vol, uname, cidx))) {
326                         if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
327                                 b_date = AD_DATE_TO_UNIX(b_date);
328                         else b_date = sbuf.st_mtime;
329                 } else b_date = sbuf.st_mtime;
330                 if (b_date < c1.bdate || b_date > c2.bdate)
331                         goto crit_check_ret;
332         }
333                                 
334         /* Check attributes */
335         if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
336                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
337                         ad_getattr(adp, &attr);
338                         if ((attr & c2.attr) != c1.attr)
339                                 goto crit_check_ret;
340                 } else goto crit_check_ret;
341                 
342
343         /* Check file type ID */
344         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0)
345                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
346                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
347                         if (finfo->f_type != c1.finfo.f_type)
348                                 goto crit_check_ret;
349                 } else goto crit_check_ret;
350
351         /* Check creator ID */
352         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0)
353                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
354                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
355                         if (finfo->creator != c1.finfo.creator)
356                                 goto crit_check_ret;
357                 } else goto crit_check_ret;
358         
359         /* Check finder info attributes */
360         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
361                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
362                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
363                         if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
364                                 goto crit_check_ret;
365                 } else goto crit_check_ret;
366
367         /* Check label */
368         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
369                 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
370                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
371                         if ((finfo->label & c2.finfo.label) != c1.finfo.label)
372                                 goto crit_check_ret;
373                 } else goto crit_check_ret;
374         
375         /* FIXME: Attributes check ! */
376         
377         /* All criteria are met. */
378         r |= 1;
379 crit_check_ret:
380         if (adp != NULL)
381                 ad_close(adp, ADFLAGS_HF);
382         return r;
383 }  
384
385
386 /* Adds an item to resultset. */
387 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
388 {
389         char *p = *rbuf;
390         int l = fname != NULL ? strlen(fname) : 0;
391         u_int32_t did;
392         char p0;
393
394         p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
395         if (p0 & 1) p[0]++;
396         p[1] = isdir ? 128 : 0;
397         p += 2;
398         if (cidx != -1) {
399                 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
400                         return 0;
401                 did = dstack[cidx].dir->d_did;
402                 memcpy(p, &did, sizeof(did));
403                 p += sizeof(did);
404         }
405
406         /* Fill offset of returned file name */
407         if (fname != NULL) {
408                 *p++ = 0;
409                 *p++ = (int)(p - *rbuf) + 1;
410                 p[0] = l;
411                 strcpy(p+1, fname);
412                 p += l + 1;
413         }
414
415         if (p0 & 1)
416                 *p++ = 0;
417
418         *rbuf = p;
419         /* *rbuf[0] = (int)(p-*rbuf); */
420         return 1;
421 } /* rslt_add */
422
423 #define VETO_STR \
424         "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
425
426 /* This function performs search. It is called directly from afp_catsearch 
427  * vol - volume we are searching on ...
428  * dir - directory we are starting from ...
429  * c1, c2 - search criteria
430  * rmatches - maximum number of matches we can return
431  * pos - position we've stopped recently
432  * rbuf - output buffer
433  * rbuflen - output buffer length
434 */
435 static int catsearch(struct vol *vol, struct dir *dir,  
436                      int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
437 {
438         int cidx, r, i;
439         char *fname = NULL;
440         struct dirent *entry;
441         struct stat statbuf;
442         int result = AFP_OK;
443         int ccr;
444         char *orig_dir = NULL;
445         int orig_dir_len = 128;
446         char *path = vol->v_path;
447         char *rrbuf = rbuf;
448
449         if (*pos != 0 && *pos != cur_pos) 
450                 return AFPERR_CATCHNG;
451
452         /* FIXME: Category "offspring count ! */
453
454         /* So we are beginning... */
455         /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
456         if (*pos == 0) {
457                 clearstack();
458                 if (dirpos != NULL) {
459                         closedir(dirpos);
460                         dirpos = NULL;
461                 } /* if (dirpos != NULL) */
462                 
463                 if (addstack("", dir, -1) == -1) {
464                         result = AFPERR_MISC;
465                         goto catsearch_end;
466                 }
467                 dstack[0].path = strdup(path);
468                 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
469         }
470
471         /* Save current path */
472         orig_dir = (char*)malloc(orig_dir_len);
473         while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
474                 if (errno != ERANGE) {
475                         result = AFPERR_MISC;
476                         goto catsearch_end;
477                 }
478                 orig_dir_len += 128; 
479                 orig_dir = realloc(orig_dir, orig_dir_len);
480         } /* while() */
481         
482         while ((cidx = reducestack()) != -1) {
483                 if (dirpos == NULL)
484                         dirpos = opendir(dstack[cidx].path);    
485                 if (dirpos == NULL) {
486                         switch (errno) {
487                         case EACCES:
488                                 dstack[cidx].checked = 1;
489                                 continue;
490                         case EMFILE:
491                         case ENFILE:
492                         case ENOENT:
493                                 result = AFPERR_NFILE;
494                                 break;
495                         case ENOMEM:
496                         case ENOTDIR:
497                         default:
498                                 result = AFPERR_MISC;
499                         } /* switch (errno) */
500                         goto catsearch_end;
501                 }
502                 chdir(dstack[cidx].path);
503                 while ((entry=readdir(dirpos)) != NULL) {
504                         (*pos)++;
505                         if (veto_file(VETO_STR, entry->d_name))
506                                 continue;
507                         if (stat(entry->d_name, &statbuf) != 0) {
508                                 switch (errno) {
509                                 case EACCES:
510                                 case ELOOP:
511                                 case ENOENT:
512                                         continue;
513                                 case ENOTDIR:
514                                 case EFAULT:
515                                 case ENOMEM:
516                                 case ENAMETOOLONG:
517                                 default:
518                                         result = AFPERR_MISC;
519                                         goto catsearch_end;
520                                 } /* switch (errno) */
521                         } /* if (stat(entry->d_name, &statbuf) != 0) */
522                         fname = utompath(vol, entry->d_name);
523                         for (i = 0; fname[i] != 0; i++)
524                                 fname[i] = tolower(fname[i]);
525                         if (strlen(fname) > MACFILELEN) 
526                                 continue;
527                         ccr = crit_check(vol, entry->d_name, fname, cidx);
528                         /* bit 0 means that criteria has ben met */
529                         if (ccr & 1) {
530                                 r = rslt_add(vol, &statbuf, 
531                                              (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ? 
532                                                  utompath(vol, entry->d_name) : NULL,   
533                                              (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ? 
534                                                  cidx : -1, 
535                                              S_ISDIR(statbuf.st_mode), &rrbuf); 
536                                 if (r == 0) {
537                                         result = AFPERR_MISC;
538                                         goto catsearch_end;
539                                 } 
540                                 *nrecs += r;
541                                 /* Number of matches limit */
542                                 if (--rmatches == 0) 
543                                         goto catsearch_pause; /* FIXME: timelimit checks ! */
544                                 /* Block size limit */
545                                 if (rrbuf - rbuf >= 448)
546                                         goto catsearch_pause;
547
548                         } 
549                         /* bit 1 means that we have to descend into this directory. */
550                         if (ccr & 2) {
551                                 if (S_ISDIR(statbuf.st_mode))
552                                         if (addstack(entry->d_name, NULL, cidx) == -1) {
553                                                 result = AFPERR_MISC;
554                                                 goto catsearch_end;
555                                         } /* if (addstack... */
556                         }
557                 } /* while ((entry=readdir(dirpos)) != NULL) */
558                 closedir(dirpos);dirpos = NULL;
559                 dstack[cidx].checked = 1;
560         } /* while (current_idx = reducestack()) != -1) */
561
562         /* We have finished traversing our tree. Return EOF here. */
563         result = AFPERR_EOF;
564         goto catsearch_end;
565
566 catsearch_pause:
567         cur_pos = *pos; 
568         save_cidx = cidx;
569
570 catsearch_end: /* Exiting catsearch: error condition */
571         *rsize = rrbuf - rbuf;
572         if (orig_dir != NULL) {
573                 chdir(orig_dir);
574                 free(orig_dir);
575         }
576         return result;
577 } /* catsearch() */
578
579
580 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
581                   char *rbuf, int *rbuflen)
582 {
583     struct vol *vol;
584     u_int16_t   vid;
585     u_int32_t   rmatches, reserved;
586     u_int32_t   catpos[4];
587     u_int32_t   pdid = 0;
588     char        *lname = NULL;
589     struct dir *dir;
590     int ret, rsize, i = 0;
591     u_int32_t nrecs = 0;
592     static int nrr = 1;
593     char *spec1, *spec2, *bspec1, *bspec2;
594
595     memset(&c1, 0, sizeof(c1));
596     memset(&c2, 0, sizeof(c2));
597
598     ibuf += 2;
599     memcpy(&vid, ibuf, sizeof(vid));
600     ibuf += sizeof(vid);
601
602     *rbuflen = 0;
603     if ((vol = getvolbyvid(vid)) == NULL)
604         return AFPERR_PARAM;
605
606     memcpy(&rmatches, ibuf, sizeof(rmatches));
607     rmatches = ntohl(rmatches);
608     ibuf += sizeof(rmatches); 
609
610     /* FIXME: (rl) should we check if reserved == 0 ? */
611     ibuf += sizeof(reserved);
612
613     memcpy(catpos, ibuf, sizeof(catpos));
614     ibuf += sizeof(catpos);
615
616     memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
617     c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
618     ibuf += sizeof(c1.fbitmap);
619
620     memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
621     c1.dbitmap = c2.dbitmap = ntohl(c1.dbitmap);
622     ibuf += sizeof(c1.dbitmap);
623
624     memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
625     c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
626     ibuf += sizeof(c1.rbitmap);
627
628     if (! (c1.fbitmap || c1.dbitmap)) {
629             *rbuflen = 0;
630             return AFPERR_BITMAP;
631     }
632
633     /* Parse file specifications */
634     spec1 = ibuf;
635     spec2 = ibuf + ibuf[0] + 2;
636
637     spec1 += 2; bspec1 = spec1;
638     spec2 += 2; bspec2 = spec2;
639
640     /* File attribute bits... */
641     if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
642             memcpy(&c1.attr, ibuf, sizeof(c1.attr));
643             spec1 += sizeof(c1.attr);
644             c1.attr = ntohs(c1.attr);
645             memcpy(&c2.attr, ibuf, sizeof(c2.attr));
646             spec2 += sizeof(c1.attr);
647             c2.attr = ntohs(c2.attr);
648     }
649
650     /* Parent DID */
651     if (c1.rbitmap & (1 << FILPBIT_PDID)) {
652             memcpy(&c1.pdid, spec1, sizeof(pdid));
653             spec1 += sizeof(c1.pdid);
654             memcpy(&c2.pdid, spec2, sizeof(pdid));
655             spec2 += sizeof(c2.pdid);
656     } /* FIXME: PDID - do we demarshall this argument ? */
657
658     /* Creation date */
659     if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
660             memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
661             spec1 += sizeof(c1.cdate);
662             c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
663             memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
664             spec2 += sizeof(c1.cdate);
665             ibuf += sizeof(c1.cdate);;
666             c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
667     }
668
669     /* Modification date */
670     if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
671             memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
672             c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
673             spec1 += sizeof(c1.mdate);
674             memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
675             c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
676             spec2 += sizeof(c1.mdate);
677     }
678     
679     /* Backup date */
680     if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
681             memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
682             spec1 += sizeof(c1.bdate);
683             c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
684             memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
685             spec2 += sizeof(c2.bdate);
686             c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
687     }
688
689     /* Finder info */
690     if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
691             memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
692             spec1 += sizeof(c1.finfo);
693             memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
694             spec2 += sizeof(c2.finfo);
695     } /* Finder info */
696
697     /* Offspring count - only directories */
698     if (c1.dbitmap != 0 && (c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
699             memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
700             spec1 += sizeof(c1.offcnt);
701             c1.offcnt = ntohs(c1.offcnt);
702             memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
703             spec2 += sizeof(c2.offcnt);
704             c2.offcnt = ntohs(c2.offcnt);
705     } /* Offspring count */
706
707     /* Long name */
708     if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
709         /* Get the long filename */     
710         memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
711         c1.lname[(bspec1 + spec1[1])[0]]= 0;
712         for (i = 0; c1.lname[i] != 0; i++)
713                 c1.lname[i] = tolower(c1.lname[i]);
714         /* FIXME: do we need it ? It's always null ! */
715         memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
716         c2.lname[(bspec2 + spec2[1])[0]]= 0;
717         for (i = 0; c2.lname[i] != 0; i++)
718                 c2.lname[i] = tolower(c2.lname[i]);
719     }
720
721
722     /* Call search */
723     *rbuflen = 24;
724     ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
725     memcpy(rbuf, catpos, sizeof(catpos));
726     rbuf += sizeof(catpos);
727     c1.fbitmap = htons(c1.fbitmap);
728     memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
729     rbuf += sizeof(c1.fbitmap);
730     c1.dbitmap = htons(c1.dbitmap);
731     memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
732     rbuf += sizeof(c2.dbitmap);
733     nrecs = htonl(nrecs);
734     memcpy(rbuf, &nrecs, sizeof(nrecs));
735     rbuf += sizeof(nrecs);
736     *rbuflen += rsize;
737
738     return ret;
739 } /* afp_catsearch */
740
741 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
742 /* (so, all buffer packing/unpacking should be done in stub, everything else 
743    should be done in other functions) */
744
745 #endif
746 /* WITH_CATSEARCH */