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