]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/catsearch.c
bugfix: directory handling
[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, int isdir)
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 | (isdir)?ADFLAGS_DIR:0, 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                 if (!c1.dbitmap)
283                         return r;
284         }
285         else if (!c1.fbitmap)
286                 return 0;
287                 
288         /* Kind of optimization: 
289          * -- first check things we've already have - filename
290          * -- last check things we get from ad_open()
291          */
292
293         /* Check for filename */
294         if (c1.rbitmap & (1<<DIRPBIT_LNAME)) { 
295                 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
296                         if (strstr(fname, c1.lname) == NULL)
297                                 goto crit_check_ret;
298                 } else
299                         if (strcmp(fname, c1.lname) != 0)
300                                 goto crit_check_ret;
301         } /* if (c1.rbitmap & ... */
302
303
304         /* FIXME */
305         if ((unsigned)c2.mdate > 0x7fffffff)
306                 c2.mdate = 0x7fffffff;
307         if ((unsigned)c2.cdate > 0x7fffffff)
308                 c2.cdate = 0x7fffffff;
309         if ((unsigned)c2.bdate > 0x7fffffff)
310                 c2.bdate = 0x7fffffff;
311
312         /* Check for modification date FIXME: should we look at adouble structure ? */
313         if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) 
314                 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
315                         goto crit_check_ret;
316
317         /* Check for creation date... */
318         if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
319                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
320                         if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
321                                 c_date = AD_DATE_TO_UNIX(c_date);
322                         else c_date = sbuf.st_mtime;
323                 } else c_date = sbuf.st_mtime;
324                 if (c_date < c1.cdate || c_date > c2.cdate)
325                         goto crit_check_ret;
326         }
327
328         /* Check for backup date... */
329         if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
330                 if (adp || (adp == adl_lkup(vol, uname, cidx, r))) {
331                         if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
332                                 b_date = AD_DATE_TO_UNIX(b_date);
333                         else b_date = sbuf.st_mtime;
334                 } else b_date = sbuf.st_mtime;
335                 if (b_date < c1.bdate || b_date > c2.bdate)
336                         goto crit_check_ret;
337         }
338                                 
339         /* Check attributes */
340         if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
341                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
342                         ad_getattr(adp, &attr);
343                         if ((attr & c2.attr) != c1.attr)
344                                 goto crit_check_ret;
345                 } else goto crit_check_ret;
346                 
347
348         /* Check file type ID */
349         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0)
350                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
351                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
352                         if (finfo->f_type != c1.finfo.f_type)
353                                 goto crit_check_ret;
354                 } else goto crit_check_ret;
355
356         /* Check creator ID */
357         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0)
358                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
359                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
360                         if (finfo->creator != c1.finfo.creator)
361                                 goto crit_check_ret;
362                 } else goto crit_check_ret;
363         
364         /* Check finder info attributes */
365         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
366                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
367                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
368                         if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
369                                 goto crit_check_ret;
370                 } else goto crit_check_ret;
371
372         /* Check label */
373         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
374                 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
375                         finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
376                         if ((finfo->label & c2.finfo.label) != c1.finfo.label)
377                                 goto crit_check_ret;
378                 } else goto crit_check_ret;
379         
380         /* FIXME: Attributes check ! */
381         
382         /* All criteria are met. */
383         r |= 1;
384 crit_check_ret:
385         if (adp != NULL)
386                 ad_close(adp, ADFLAGS_HF);
387         return r;
388 }  
389
390
391 /* Adds an item to resultset. */
392 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
393 {
394         char *p = *rbuf;
395         int l = fname != NULL ? strlen(fname) : 0;
396         u_int32_t did;
397         char p0;
398
399         p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
400         if (p0 & 1) p[0]++;
401         p[1] = isdir ? 128 : 0;
402         p += 2;
403         if (cidx != -1) {
404                 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
405                         return 0;
406                 did = dstack[cidx].dir->d_did;
407                 memcpy(p, &did, sizeof(did));
408                 p += sizeof(did);
409         }
410
411         /* Fill offset of returned file name */
412         if (fname != NULL) {
413                 *p++ = 0;
414                 *p++ = (int)(p - *rbuf) - 1;
415                 p[0] = l;
416                 strcpy(p+1, fname);
417                 p += l + 1;
418         }
419
420         if (p0 & 1)
421                 *p++ = 0;
422
423         *rbuf = p;
424         /* *rbuf[0] = (int)(p-*rbuf); */
425         return 1;
426 } /* rslt_add */
427
428 #define VETO_STR \
429         "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
430
431 /* This function performs search. It is called directly from afp_catsearch 
432  * vol - volume we are searching on ...
433  * dir - directory we are starting from ...
434  * c1, c2 - search criteria
435  * rmatches - maximum number of matches we can return
436  * pos - position we've stopped recently
437  * rbuf - output buffer
438  * rbuflen - output buffer length
439 */
440 static int catsearch(struct vol *vol, struct dir *dir,  
441                      int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
442 {
443         int cidx, r, i;
444         char *fname = NULL;
445         struct dirent *entry;
446         struct stat statbuf;
447         int result = AFP_OK;
448         int ccr;
449         char *orig_dir = NULL;
450         int orig_dir_len = 128;
451         char *path = vol->v_path;
452         char *rrbuf = rbuf;
453
454         if (*pos != 0 && *pos != cur_pos) 
455                 return AFPERR_CATCHNG;
456
457         /* FIXME: Category "offspring count ! */
458
459         /* So we are beginning... */
460         /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
461         if (*pos == 0) {
462                 clearstack();
463                 if (dirpos != NULL) {
464                         closedir(dirpos);
465                         dirpos = NULL;
466                 } /* if (dirpos != NULL) */
467                 
468                 if (addstack("", dir, -1) == -1) {
469                         result = AFPERR_MISC;
470                         goto catsearch_end;
471                 }
472                 dstack[0].path = strdup(path);
473                 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
474         }
475
476         /* Save current path */
477         orig_dir = (char*)malloc(orig_dir_len);
478         while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
479                 if (errno != ERANGE) {
480                         result = AFPERR_MISC;
481                         goto catsearch_end;
482                 }
483                 orig_dir_len += 128; 
484                 orig_dir = realloc(orig_dir, orig_dir_len);
485         } /* while() */
486         
487         while ((cidx = reducestack()) != -1) {
488                 if (dirpos == NULL)
489                         dirpos = opendir(dstack[cidx].path);    
490                 if (dirpos == NULL) {
491                         switch (errno) {
492                         case EACCES:
493                                 dstack[cidx].checked = 1;
494                                 continue;
495                         case EMFILE:
496                         case ENFILE:
497                         case ENOENT:
498                                 result = AFPERR_NFILE;
499                                 break;
500                         case ENOMEM:
501                         case ENOTDIR:
502                         default:
503                                 result = AFPERR_MISC;
504                         } /* switch (errno) */
505                         goto catsearch_end;
506                 }
507                 chdir(dstack[cidx].path);
508                 while ((entry=readdir(dirpos)) != NULL) {
509                         (*pos)++;
510                         if (veto_file(VETO_STR, entry->d_name))
511                                 continue;
512                         if (stat(entry->d_name, &statbuf) != 0) {
513                                 switch (errno) {
514                                 case EACCES:
515                                 case ELOOP:
516                                 case ENOENT:
517                                         continue;
518                                 case ENOTDIR:
519                                 case EFAULT:
520                                 case ENOMEM:
521                                 case ENAMETOOLONG:
522                                 default:
523                                         result = AFPERR_MISC;
524                                         goto catsearch_end;
525                                 } /* switch (errno) */
526                         } /* if (stat(entry->d_name, &statbuf) != 0) */
527                         fname = utompath(vol, entry->d_name);
528                         for (i = 0; fname[i] != 0; i++)
529                                 fname[i] = tolower(fname[i]);
530                         if (strlen(fname) > MACFILELEN) 
531                                 continue;
532                         ccr = crit_check(vol, entry->d_name, fname, cidx);
533                         /* bit 0 means that criteria has ben met */
534                         if (ccr & 1) {
535                                 r = rslt_add(vol, &statbuf, 
536                                              (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ? 
537                                                  utompath(vol, entry->d_name) : NULL,   
538                                              (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ? 
539                                                  cidx : -1, 
540                                              S_ISDIR(statbuf.st_mode), &rrbuf); 
541                                 if (r == 0) {
542                                         result = AFPERR_MISC;
543                                         goto catsearch_end;
544                                 } 
545                                 *nrecs += r;
546                                 /* Number of matches limit */
547                                 if (--rmatches == 0) 
548                                         goto catsearch_pause; /* FIXME: timelimit checks ! */
549                                 /* Block size limit */
550                                 if (rrbuf - rbuf >= 448)
551                                         goto catsearch_pause;
552
553                         } 
554                         /* bit 1 means that we have to descend into this directory. */
555                         if (ccr & 2) {
556                                 if (S_ISDIR(statbuf.st_mode))
557                                         if (addstack(entry->d_name, NULL, cidx) == -1) {
558                                                 result = AFPERR_MISC;
559                                                 goto catsearch_end;
560                                         } /* if (addstack... */
561                         }
562                 } /* while ((entry=readdir(dirpos)) != NULL) */
563                 closedir(dirpos);dirpos = NULL;
564                 dstack[cidx].checked = 1;
565         } /* while (current_idx = reducestack()) != -1) */
566
567         /* We have finished traversing our tree. Return EOF here. */
568         result = AFPERR_EOF;
569         goto catsearch_end;
570
571 catsearch_pause:
572         cur_pos = *pos; 
573         save_cidx = cidx;
574
575 catsearch_end: /* Exiting catsearch: error condition */
576         *rsize = rrbuf - rbuf;
577         if (orig_dir != NULL) {
578                 chdir(orig_dir);
579                 free(orig_dir);
580         }
581         return result;
582 } /* catsearch() */
583
584
585 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
586                   char *rbuf, int *rbuflen)
587 {
588     struct vol *vol;
589     u_int16_t   vid;
590     u_int32_t   rmatches, reserved;
591     u_int32_t   catpos[4];
592     u_int32_t   pdid = 0;
593     char        *lname = NULL;
594     struct dir *dir;
595     int ret, rsize, i = 0;
596     u_int32_t nrecs = 0;
597     static int nrr = 1;
598     char *spec1, *spec2, *bspec1, *bspec2;
599
600     memset(&c1, 0, sizeof(c1));
601     memset(&c2, 0, sizeof(c2));
602
603     ibuf += 2;
604     memcpy(&vid, ibuf, sizeof(vid));
605     ibuf += sizeof(vid);
606
607     *rbuflen = 0;
608     if ((vol = getvolbyvid(vid)) == NULL)
609         return AFPERR_PARAM;
610
611     memcpy(&rmatches, ibuf, sizeof(rmatches));
612     rmatches = ntohl(rmatches);
613     ibuf += sizeof(rmatches); 
614
615     /* FIXME: (rl) should we check if reserved == 0 ? */
616     ibuf += sizeof(reserved);
617
618     memcpy(catpos, ibuf, sizeof(catpos));
619     ibuf += sizeof(catpos);
620
621     memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
622     c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
623     ibuf += sizeof(c1.fbitmap);
624
625     memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
626     c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
627     ibuf += sizeof(c1.dbitmap);
628
629     memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
630     c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
631     ibuf += sizeof(c1.rbitmap);
632
633     if (! (c1.fbitmap || c1.dbitmap)) {
634             *rbuflen = 0;
635             return AFPERR_BITMAP;
636     }
637
638     /* Parse file specifications */
639     spec1 = ibuf;
640     spec2 = ibuf + ibuf[0] + 2;
641
642     spec1 += 2; bspec1 = spec1;
643     spec2 += 2; bspec2 = spec2;
644
645     /* File attribute bits... */
646     if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
647             memcpy(&c1.attr, ibuf, sizeof(c1.attr));
648             spec1 += sizeof(c1.attr);
649             c1.attr = ntohs(c1.attr);
650             memcpy(&c2.attr, ibuf, sizeof(c2.attr));
651             spec2 += sizeof(c1.attr);
652             c2.attr = ntohs(c2.attr);
653     }
654
655     /* Parent DID */
656     if (c1.rbitmap & (1 << FILPBIT_PDID)) {
657             memcpy(&c1.pdid, spec1, sizeof(pdid));
658             spec1 += sizeof(c1.pdid);
659             memcpy(&c2.pdid, spec2, sizeof(pdid));
660             spec2 += sizeof(c2.pdid);
661     } /* FIXME: PDID - do we demarshall this argument ? */
662
663     /* Creation date */
664     if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
665             memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
666             spec1 += sizeof(c1.cdate);
667             c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
668             memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
669             spec2 += sizeof(c1.cdate);
670             ibuf += sizeof(c1.cdate);;
671             c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
672     }
673
674     /* Modification date */
675     if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
676             memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
677             c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
678             spec1 += sizeof(c1.mdate);
679             memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
680             c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
681             spec2 += sizeof(c1.mdate);
682     }
683     
684     /* Backup date */
685     if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
686             memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
687             spec1 += sizeof(c1.bdate);
688             c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
689             memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
690             spec2 += sizeof(c2.bdate);
691             c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
692     }
693
694     /* Finder info */
695     if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
696             memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
697             spec1 += sizeof(c1.finfo);
698             memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
699             spec2 += sizeof(c2.finfo);
700     } /* Finder info */
701
702     if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
703         /* Offspring count - only directories */
704         if (c1.fbitmap == 0) {
705             memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
706             spec1 += sizeof(c1.offcnt);
707             c1.offcnt = ntohs(c1.offcnt);
708             memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
709             spec2 += sizeof(c2.offcnt);
710             c2.offcnt = ntohs(c2.offcnt);
711         }
712         else if (c1.dbitmap == 0) {
713                 /* ressource fork length */
714         }
715         else {
716                 /* error */
717         }
718     } /* Offspring count/ressource fork length */
719
720     /* Long name */
721     if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
722         /* Get the long filename */     
723         memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
724         c1.lname[(bspec1 + spec1[1])[0]]= 0;
725         for (i = 0; c1.lname[i] != 0; i++)
726                 c1.lname[i] = tolower(c1.lname[i]);
727         /* FIXME: do we need it ? It's always null ! */
728         memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
729         c2.lname[(bspec2 + spec2[1])[0]]= 0;
730         for (i = 0; c2.lname[i] != 0; i++)
731                 c2.lname[i] = tolower(c2.lname[i]);
732     }
733
734
735     /* Call search */
736     *rbuflen = 24;
737     ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
738     memcpy(rbuf, catpos, sizeof(catpos));
739     rbuf += sizeof(catpos);
740     c1.fbitmap = htons(c1.fbitmap);
741     memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
742     rbuf += sizeof(c1.fbitmap);
743     c1.dbitmap = htons(c1.dbitmap);
744     memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
745     rbuf += sizeof(c1.dbitmap);
746     nrecs = htonl(nrecs);
747     memcpy(rbuf, &nrecs, sizeof(nrecs));
748     rbuf += sizeof(nrecs);
749     *rbuflen += rsize;
750
751     return ret;
752 } /* afp_catsearch */
753
754 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
755 /* (so, all buffer packing/unpacking should be done in stub, everything else 
756    should be done in other functions) */
757
758 #endif
759 /* WITH_CATSEARCH */