]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/catsearch.c
Symlink patch from Anton Starikov
[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 <errno.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <time.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/file.h>
44 #include <netinet/in.h>
45
46 #include <atalk/afp.h>
47 #include <atalk/adouble.h>
48 #include <atalk/logger.h>
49 #ifdef CNID_DB
50 #include <atalk/cnid.h>
51 #endif /* CNID_DB */
52 #include "desktop.h"
53 #include "directory.h"
54 #include "file.h"
55 #include "volume.h"
56 #include "globals.h"
57 #include "filedir.h"
58 #include "fork.h"
59
60
61 struct finderinfo {
62         u_int32_t f_type;
63         u_int32_t creator;
64         u_int16_t attrs;    /* File attributes (high 8 bits)*/
65         u_int16_t label;    /* Label (low 8 bits )*/
66         char reserved[22]; /* Unknown (at least for now...) */
67 };
68
69 typedef char packed_finder[ADEDLEN_FINDERI];
70
71 /* Known attributes:
72  * 0x04 - has a custom icon
73  * 0x20 - name/icon is locked
74  * 0x40 - is invisible
75  * 0x80 - is alias
76  *
77  * Known labels:
78  * 0x02 - project 2
79  * 0x04 - project 1
80  * 0x06 - personal
81  * 0x08 - cool
82  * 0x0a - in progress
83  * 0x0c - hot
84  * 0x0e - essential
85  */
86
87 /* This is our search-criteria structure. */
88 struct scrit {
89         u_int32_t rbitmap;          /* Request bitmap - which values should we check ? */
90         u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
91         u_int16_t attr;             /* File attributes */
92         time_t cdate;               /* Creation date */
93         time_t mdate;               /* Last modification date */
94         time_t bdate;               /* Last backup date */
95         u_int32_t pdid;             /* Parent DID */
96     u_int16_t offcnt;           /* Offspring count */
97         struct finderinfo finfo;    /* Finder info */
98         char lname[64];             /* Long name */ 
99         char utf8name[514];         /* UTF8 or UCS2 name */ /* for convert_charset dest_len parameter +2 */
100 };
101
102 /*
103  * Directory tree search is recursive by its nature. But AFP specification
104  * requires FPCatSearch to pause after returning n results and be able to
105  * resume the search later. So we have to do recursive search using flat
106  * (iterative) algorithm and remember all directories to look into in an
107  * stack-like structure. The structure below is one item of directory stack.
108  *
109  */
110 struct dsitem {
111         struct dir *dir; /* Structure describing this directory */
112         int pidx;        /* Parent's dsitem structure index. */
113         int checked;     /* Have we checked this directory ? */
114         int path_len;
115         char *path;      /* absolute UNIX path to this directory */
116 };
117  
118
119 #define DS_BSIZE 128
120 static u_int32_t cur_pos = 0;    /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
121 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
122 static int save_cidx = -1; /* Saved index of currently scanned directory. */
123
124 static struct dsitem *dstack = NULL; /* Directory stack data... */
125 static int dssize = 0;               /* Directory stack (allocated) size... */
126 static int dsidx = 0;                /* First free item index... */
127
128 static struct scrit c1, c2;          /* search criteria */
129
130 /* Puts new item onto directory stack. */
131 static int addstack(char *uname, struct dir *dir, int pidx)
132 {
133         struct dsitem *ds;
134         size_t         l, u;
135
136         /* check if we have some space on stack... */
137         if (dsidx >= dssize) {
138                 dssize += DS_BSIZE;
139                 dstack = realloc(dstack, dssize * sizeof(struct dsitem));       
140                 if (dstack == NULL)
141                         return -1;
142         }
143
144         /* Put new element. Allocate and copy lname and path. */
145         ds = dstack + dsidx++;
146         ds->dir = dir;
147         ds->pidx = pidx;
148         ds->checked = 0;
149         if (pidx >= 0) {
150             l = dstack[pidx].path_len;
151             u = strlen(uname) +1;
152             if (!(ds->path = malloc(l + u + 1) ))
153                         return -1;
154                 memcpy(ds->path, dstack[pidx].path, l);
155                 ds->path[l] = '/';
156                 memcpy(&ds->path[l+1], uname, u);
157                 ds->path_len = l +u;
158         }
159         else {
160             ds->path = strdup(uname);
161                 ds->path_len = strlen(uname);
162         }
163         return 0;
164 }
165
166 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
167 static int reducestack(void)
168 {
169         int r;
170         if (save_cidx != -1) {
171                 r = save_cidx;
172                 save_cidx = -1;
173                 return r;
174         }
175
176         while (dsidx > 0) {
177                 if (dstack[dsidx-1].checked) {
178                         dsidx--;
179                         free(dstack[dsidx].path);
180                 } else
181                         return dsidx - 1;
182         } 
183         return -1;
184
185
186 /* Clears directory stack. */
187 static void clearstack(void) 
188 {
189         save_cidx = -1;
190         while (dsidx > 0) {
191                 dsidx--;
192                 free(dstack[dsidx].path);
193         }
194
195
196 /* Looks up for an opened adouble structure, opens resource fork of selected file. 
197  * FIXME What about noadouble?
198 */
199 static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
200 {
201         static struct adouble ad;
202         
203         struct ofork *of;
204         int isdir;
205         
206         if (adp)
207             return adp;
208             
209         isdir  = S_ISDIR(path->st.st_mode);
210
211         if (!isdir && (of = of_findname(path))) {
212                 adp = of->of_ad;
213         } else {
214                 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
215                 adp = &ad;
216         } 
217
218     if ( ad_metadata( path->u_name, vol_noadouble(vol) | ((isdir)?ADFLAGS_DIR:0), adp) < 0 ) {
219         adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
220     }
221     
222         return adp;     
223 }
224
225 /* -------------------- */
226 static struct finderinfo *unpack_buffer(struct finderinfo *finfo, char *buffer)
227 {
228         memcpy(&finfo->f_type,  buffer +FINDERINFO_FRTYPEOFF, sizeof(finfo->f_type));
229         memcpy(&finfo->creator, buffer +FINDERINFO_FRCREATOFF, sizeof(finfo->creator));
230         memcpy(&finfo->attrs,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->attrs));
231         memcpy(&finfo->label,   buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->label));
232         finfo->attrs &= 0xff00; /* high 8 bits */
233         finfo->label &= 0xff;   /* low 8 bits */
234
235         return finfo;
236 }
237
238 /* -------------------- */
239 static struct finderinfo *
240 unpack_finderinfo(struct vol *vol, struct path *path, struct adouble **adp, struct finderinfo *finfo, int islnk)
241 {
242         packed_finder  buf;
243         void           *ptr;
244         
245     *adp = adl_lkup(vol, path, *adp);
246         ptr = get_finderinfo(vol, path->u_name, *adp, &buf,islnk);
247         return unpack_buffer(finfo, ptr);
248 }
249
250 /* -------------------- */
251 #define CATPBIT_PARTIAL 31
252 /* Criteria checker. This function returns a 2-bit value. */
253 /* bit 0 means if checked file meets given criteria. */
254 /* bit 1 means if it is a directory and we should descent into it. */
255 /* uname - UNIX name 
256  * fname - our fname (translated to UNIX)
257  * cidx - index in directory stack
258  */
259 static int crit_check(struct vol *vol, struct path *path) {
260         int result = 0;
261         u_int16_t attr, flags = CONV_PRECOMPOSE;
262         struct finderinfo *finfo = NULL, finderinfo;
263         struct adouble *adp = NULL;
264         time_t c_date, b_date;
265         u_int32_t ac_date, ab_date;
266         static char convbuf[514]; /* for convert_charset dest_len parameter +2 */
267         size_t len;
268     int islnk;
269     islnk=S_ISLNK(path->st.st_mode);
270
271         if (S_ISDIR(path->st.st_mode)) {
272                 if (!c1.dbitmap)
273                         return 0;
274         }
275         else {
276                 if (!c1.fbitmap)
277                         return 0;
278
279                 /* compute the Mac name 
280                  * first try without id (it's slow to find it)
281                  * An other option would be to call get_id in utompath but 
282                  * we need to pass parent dir
283                 */
284         if (!(path->m_name = utompath(vol, path->u_name, 0 , utf8_encoding()) )) {
285                 /*retry with the right id */
286        
287                 cnid_t id;
288                 
289                 adp = adl_lkup(vol, path, adp);
290                 id = get_id(vol, adp, &path->st, path->d_dir->d_did, path->u_name, strlen(path->u_name));
291                 if (!id) {
292                         /* FIXME */
293                         return 0;
294                 }
295                 /* save the id for getfilparm */
296                 path->id = id;
297                 if (!(path->m_name = utompath(vol, path->u_name, id , utf8_encoding()))) {
298                         return 0;
299                 }
300         }
301         }
302                 
303         /* Kind of optimization: 
304          * -- first check things we've already have - filename
305          * -- last check things we get from ad_open()
306          * FIXME strmcp strstr (icase)
307          */
308
309         /* Check for filename */
310         if ((c1.rbitmap & (1<<DIRPBIT_LNAME))) { 
311                 if ( (size_t)(-1) == (len = convert_string(vol->v_maccharset, CH_UCS2, path->m_name, -1, convbuf, 512)) )
312                         goto crit_check_ret;
313
314                 if ((c1.rbitmap & (1<<CATPBIT_PARTIAL))) {
315                         if (strcasestr_w( (ucs2_t*) convbuf, (ucs2_t*) c1.lname) == NULL)
316                                 goto crit_check_ret;
317                 } else
318                         if (strcasecmp_w((ucs2_t*) convbuf, (ucs2_t*) c1.lname) != 0)
319                                 goto crit_check_ret;
320         } 
321         
322         if ((c1.rbitmap & (1<<FILPBIT_PDINFO))) { 
323                 if ( (size_t)(-1) == (len = convert_charset( CH_UTF8_MAC, CH_UCS2, CH_UTF8, path->m_name, strlen(path->m_name), convbuf, 512, &flags))) {
324                         goto crit_check_ret;
325                 }
326
327                 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
328                         if (strcasestr_w((ucs2_t *) convbuf, (ucs2_t*)c1.utf8name) == NULL)
329                                 goto crit_check_ret;
330                 } else
331                         if (strcasecmp_w((ucs2_t *)convbuf, (ucs2_t*)c1.utf8name) != 0)
332                                 goto crit_check_ret;
333         } 
334
335
336         /* FIXME */
337         if ((unsigned)c2.mdate > 0x7fffffff)
338                 c2.mdate = 0x7fffffff;
339         if ((unsigned)c2.cdate > 0x7fffffff)
340                 c2.cdate = 0x7fffffff;
341         if ((unsigned)c2.bdate > 0x7fffffff)
342                 c2.bdate = 0x7fffffff;
343
344         /* Check for modification date */
345         if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) {
346                 if (path->st.st_mtime < c1.mdate || path->st.st_mtime > c2.mdate)
347                         goto crit_check_ret;
348         }
349         
350         /* Check for creation date... */
351         if ((c1.rbitmap & (1<<DIRPBIT_CDATE))) {
352                 c_date = path->st.st_mtime;
353                 adp = adl_lkup(vol, path, adp);
354                 if (adp && ad_getdate(adp, AD_DATE_CREATE, &ac_date) >= 0)
355                     c_date = AD_DATE_TO_UNIX(ac_date);
356
357                 if (c_date < c1.cdate || c_date > c2.cdate)
358                         goto crit_check_ret;
359         }
360
361         /* Check for backup date... */
362         if ((c1.rbitmap & (1<<DIRPBIT_BDATE))) {
363                 b_date = path->st.st_mtime;
364                 adp = adl_lkup(vol, path, adp);
365                 if (adp && ad_getdate(adp, AD_DATE_BACKUP, &ab_date) >= 0)
366                         b_date = AD_DATE_TO_UNIX(ab_date);
367
368                 if (b_date < c1.bdate || b_date > c2.bdate)
369                         goto crit_check_ret;
370         }
371                                 
372         /* Check attributes */
373         if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
374                 if ((adp = adl_lkup(vol, path, adp))) {
375                         ad_getattr(adp, &attr);
376                         if ((attr & c2.attr) != c1.attr)
377                                 goto crit_check_ret;
378                 } else 
379                         goto crit_check_ret;
380         }               
381
382         /* Check file type ID */
383         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
384             finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
385                 if (finfo->f_type != c1.finfo.f_type)
386                         goto crit_check_ret;
387         }
388         
389         /* Check creator ID */
390         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
391                 if (!finfo) {
392                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
393                 }
394                 if (finfo->creator != c1.finfo.creator)
395                         goto crit_check_ret;
396         }
397                 
398         /* Check finder info attributes */
399         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
400                 if (!finfo) {
401                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
402                 }
403
404                 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
405                         goto crit_check_ret;
406         }
407         
408         /* Check label */
409         if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
410                 if (!finfo) {
411                         finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
412                 }
413                 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
414                         goto crit_check_ret;
415         }       
416         /* FIXME: Attributes check ! */
417         
418         /* All criteria are met. */
419         result |= 1;
420 crit_check_ret:
421         if (adp != NULL)
422                 ad_close_metadata(adp);
423         return result;
424 }  
425
426 /* ------------------------------ */
427 static int rslt_add ( struct vol *vol, struct path *path, char **buf, int ext)
428 {
429
430         char            *p = *buf;
431         int             ret;
432         size_t          tbuf =0;
433         u_int16_t       resultsize;
434         int             isdir = S_ISDIR(path->st.st_mode); 
435
436         /* Skip resultsize */
437         if (ext) {
438                 p += sizeof(resultsize); 
439         }
440         else {
441                 p++;
442         }
443         *p++ = isdir ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE;    /* IsDir ? */
444
445         if (ext) {
446                 *p++ = 0;                  /* Pad */
447         }
448         
449         if ( isdir ) {
450         ret = getdirparams(vol, c1.dbitmap, path, path->d_dir, p , &tbuf ); 
451         }
452         else {
453             /* FIXME slow if we need the file ID, we already know it, done ? */
454                 ret = getfilparams ( vol, c1.fbitmap, path, path->d_dir, p, &tbuf);
455         }
456
457         if ( ret != AFP_OK )
458                 return 0;
459
460         /* Make sure entry length is even */
461         if ((tbuf & 1)) {
462            *p++ = 0;
463            tbuf++;
464         }
465
466         if (ext) {
467                 resultsize = htons(tbuf);
468                 memcpy ( *buf, &resultsize, sizeof(resultsize) );
469                 *buf += tbuf + 4;
470         }
471         else {
472                 **buf = tbuf;
473                 *buf += tbuf + 2;
474         }
475
476         return 1;
477
478         
479 #define VETO_STR \
480         "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
481
482 /* This function performs search. It is called directly from afp_catsearch 
483  * vol - volume we are searching on ...
484  * dir - directory we are starting from ...
485  * c1, c2 - search criteria
486  * rmatches - maximum number of matches we can return
487  * pos - position we've stopped recently
488  * rbuf - output buffer
489  * rbuflen - output buffer length
490  */
491 #define NUM_ROUNDS 200
492 static int catsearch(struct vol *vol, struct dir *dir,  
493                      int rmatches, u_int32_t *pos, char *rbuf, u_int32_t *nrecs, int *rsize, int ext)
494 {
495         int cidx, r;
496         struct dirent *entry;
497         int result = AFP_OK;
498         int ccr;
499     struct path path;
500         char *orig_dir = NULL;
501         int orig_dir_len = 128;
502         char *vpath = vol->v_path;
503         char *rrbuf = rbuf;
504     time_t start_time;
505     int num_rounds = NUM_ROUNDS;
506     int cached;
507         
508         if (*pos != 0 && *pos != cur_pos) {
509                 result = AFPERR_CATCHNG;
510                 goto catsearch_end;
511         }
512
513         /* FIXME: Category "offspring count ! */
514
515         /* So we are beginning... */
516     start_time = time(NULL);
517
518         /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
519         if (*pos == 0) {
520                 clearstack();
521                 if (dirpos != NULL) {
522                         closedir(dirpos);
523                         dirpos = NULL;
524                 } 
525                 
526                 if (addstack(vpath, dir, -1) == -1) {
527                         result = AFPERR_MISC;
528                         goto catsearch_end;
529                 }
530                 /* FIXME: Sometimes DID is given by client ! (correct this one above !) */
531         }
532
533         /* Save current path */
534         orig_dir = (char*)malloc(orig_dir_len);
535         while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
536                 if (errno != ERANGE) {
537                         result = AFPERR_MISC;
538                         goto catsearch_end;
539                 }
540                 orig_dir_len += 128; 
541                 orig_dir = realloc(orig_dir, orig_dir_len);
542         } /* while() */
543         
544         while ((cidx = reducestack()) != -1) {
545                 cached = 1;
546                 if (dirpos == NULL) {
547                         dirpos = opendir(dstack[cidx].path);
548                         cached = (dstack[cidx].dir->d_child != NULL);
549                 }
550                 if (dirpos == NULL) {
551                         switch (errno) {
552                         case EACCES:
553                                 dstack[cidx].checked = 1;
554                                 continue;
555                         case EMFILE:
556                         case ENFILE:
557                         case ENOENT:
558                                 result = AFPERR_NFILE;
559                                 break;
560                         case ENOMEM:
561                         case ENOTDIR:
562                         default:
563                                 result = AFPERR_MISC;
564                         } /* switch (errno) */
565                         goto catsearch_end;
566                 }
567                 /* FIXME error in chdir, what do we do? */
568                 chdir(dstack[cidx].path);
569                 
570
571                 while ((entry=readdir(dirpos)) != NULL) {
572                         (*pos)++;
573
574                         if (!check_dirent(vol, entry->d_name))
575                            continue;
576
577                         memset(&path, 0, sizeof(path));
578                         path.u_name = entry->d_name;
579                         if (of_stat(&path) != 0) {
580                                 switch (errno) {
581                                 case EACCES:
582                                 case ELOOP:
583                                 case ENOENT:
584                                         continue;
585                                 case ENOTDIR:
586                                 case EFAULT:
587                                 case ENOMEM:
588                                 case ENAMETOOLONG:
589                                 default:
590                                         result = AFPERR_MISC;
591                                         goto catsearch_end;
592                                 } 
593                         }
594                         if (S_ISDIR(path.st.st_mode)) {
595                                 /* here we can short cut 
596                                    ie if in the same loop the parent dir wasn't in the cache
597                                    ALL dirsearch_byname will fail.
598                                 */
599                                 if (cached)
600                         path.d_dir = dirsearch_byname(vol, dstack[cidx].dir, path.u_name);
601                 else
602                         path.d_dir = NULL;
603                 if (!path.d_dir) {
604                         /* path.m_name is set by adddir */
605                     if (NULL == (path.d_dir = adddir( vol, dstack[cidx].dir, &path) ) ) {
606                                                 result = AFPERR_MISC;
607                                                 goto catsearch_end;
608                                         }
609                 }
610                 path.m_name = path.d_dir->d_m_name; 
611                         
612                                 if (addstack(path.u_name, path.d_dir, cidx) == -1) {
613                                         result = AFPERR_MISC;
614                                         goto catsearch_end;
615                                 } 
616             }
617             else {
618                 /* yes it sucks for directory d_dir is the directory, for file it's the parent directory*/
619                 path.d_dir = dstack[cidx].dir;
620             }
621                         ccr = crit_check(vol, &path);
622
623                         /* bit 0 means that criteria has been met */
624                         if ((ccr & 1)) {
625                                 r = rslt_add ( vol, &path, &rrbuf, ext);
626                                 
627                                 if (r == 0) {
628                                         result = AFPERR_MISC;
629                                         goto catsearch_end;
630                                 } 
631                                 *nrecs += r;
632                                 /* Number of matches limit */
633                                 if (--rmatches == 0) 
634                                         goto catsearch_pause;
635                                 /* Block size limit */
636                                 if (rrbuf - rbuf >= 448)
637                                         goto catsearch_pause;
638                         }
639                         /* MacOS 9 doesn't like servers executing commands longer than few seconds */
640                         if (--num_rounds <= 0) {
641                             if (start_time != time(NULL)) {
642                                         result=AFP_OK;
643                                         goto catsearch_pause;
644                             }
645                             num_rounds = NUM_ROUNDS;
646                         }
647                 } /* while ((entry=readdir(dirpos)) != NULL) */
648                 closedir(dirpos);
649                 dirpos = NULL;
650                 dstack[cidx].checked = 1;
651         } /* while (current_idx = reducestack()) != -1) */
652
653         /* We have finished traversing our tree. Return EOF here. */
654         result = AFPERR_EOF;
655         goto catsearch_end;
656
657 catsearch_pause:
658         cur_pos = *pos; 
659         save_cidx = cidx;
660
661 catsearch_end: /* Exiting catsearch: error condition */
662         *rsize = rrbuf - rbuf;
663         if (orig_dir != NULL) {
664                 chdir(orig_dir);
665                 free(orig_dir);
666         }
667         return result;
668 } /* catsearch() */
669
670 /* -------------------------- */
671 static int catsearch_afp(AFPObj *obj _U_, char *ibuf, size_t ibuflen,
672                   char *rbuf, size_t *rbuflen, int ext)
673 {
674     struct vol *vol;
675     u_int16_t   vid;
676     u_int16_t   spec_len;
677     u_int32_t   rmatches, reserved;
678     u_int32_t   catpos[4];
679     u_int32_t   pdid = 0;
680     int ret, rsize;
681     u_int32_t nrecs = 0;
682     unsigned char *spec1, *spec2, *bspec1, *bspec2;
683     size_t      len;
684     u_int16_t   namelen;
685     u_int16_t   flags;
686     char        tmppath[256];
687
688     *rbuflen = 0;
689
690     /* min header size */
691     if (ibuflen < 32) {
692         return AFPERR_PARAM;
693     }
694
695     memset(&c1, 0, sizeof(c1));
696     memset(&c2, 0, sizeof(c2));
697
698     ibuf += 2;
699     memcpy(&vid, ibuf, sizeof(vid));
700     ibuf += sizeof(vid);
701
702     if ((vol = getvolbyvid(vid)) == NULL) {
703         return AFPERR_PARAM;
704     }
705     
706     memcpy(&rmatches, ibuf, sizeof(rmatches));
707     rmatches = ntohl(rmatches);
708     ibuf += sizeof(rmatches); 
709
710     /* FIXME: (rl) should we check if reserved == 0 ? */
711     ibuf += sizeof(reserved);
712
713     memcpy(catpos, ibuf, sizeof(catpos));
714     ibuf += sizeof(catpos);
715
716     memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
717     c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
718     ibuf += sizeof(c1.fbitmap);
719
720     memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
721     c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
722     ibuf += sizeof(c1.dbitmap);
723
724     memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
725     c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
726     ibuf += sizeof(c1.rbitmap);
727
728     if (! (c1.fbitmap || c1.dbitmap)) {
729             return AFPERR_BITMAP;
730     }
731
732     if ( ext) {
733         memcpy(&spec_len, ibuf, sizeof(spec_len));
734         spec_len = ntohs(spec_len);
735     }
736     else {
737         /* with catsearch only name and parent id are allowed */
738         c1.fbitmap &= (1<<FILPBIT_LNAME) | (1<<FILPBIT_PDID);
739         c1.dbitmap &= (1<<DIRPBIT_LNAME) | (1<<DIRPBIT_PDID);
740         spec_len = *(unsigned char*)ibuf;
741     }
742
743     /* Parse file specifications */
744     spec1 = (unsigned char*)ibuf;
745     spec2 = (unsigned char*)ibuf + spec_len + 2;
746
747     spec1 += 2; 
748     spec2 += 2; 
749
750     bspec1 = spec1;
751     bspec2 = spec2;
752     /* File attribute bits... */
753     if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
754             memcpy(&c1.attr, spec1, sizeof(c1.attr));
755             spec1 += sizeof(c1.attr);
756             memcpy(&c2.attr, spec2, sizeof(c2.attr));
757             spec2 += sizeof(c1.attr);
758     }
759
760     /* Parent DID */
761     if (c1.rbitmap & (1 << FILPBIT_PDID)) {
762             memcpy(&c1.pdid, spec1, sizeof(pdid));
763             spec1 += sizeof(c1.pdid);
764             memcpy(&c2.pdid, spec2, sizeof(pdid));
765             spec2 += sizeof(c2.pdid);
766     } /* FIXME: PDID - do we demarshall this argument ? */
767
768     /* Creation date */
769     if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
770             memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
771             spec1 += sizeof(c1.cdate);
772             c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
773             memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
774             spec2 += sizeof(c1.cdate);
775             ibuf += sizeof(c1.cdate);;
776             c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
777     }
778
779     /* Modification date */
780     if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
781             memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
782             c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
783             spec1 += sizeof(c1.mdate);
784             memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
785             c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
786             spec2 += sizeof(c1.mdate);
787     }
788     
789     /* Backup date */
790     if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
791             memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
792             spec1 += sizeof(c1.bdate);
793             c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
794             memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
795             spec2 += sizeof(c2.bdate);
796             c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
797     }
798
799     /* Finder info */
800     if (c1.rbitmap & (1 << FILPBIT_FINFO)) {
801         packed_finder buf;
802         
803             memcpy(buf, spec1, sizeof(buf));
804             unpack_buffer(&c1.finfo, buf);      
805             spec1 += sizeof(buf);
806
807             memcpy(buf, spec2, sizeof(buf));
808             unpack_buffer(&c2.finfo, buf);
809             spec2 += sizeof(buf);
810     } /* Finder info */
811
812     if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
813         /* Offspring count - only directories */
814                 if (c1.fbitmap == 0) {
815                 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
816                 spec1 += sizeof(c1.offcnt);
817                 c1.offcnt = ntohs(c1.offcnt);
818                 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
819                 spec2 += sizeof(c2.offcnt);
820                 c2.offcnt = ntohs(c2.offcnt);
821                 }
822                 else if (c1.dbitmap == 0) {
823                         /* ressource fork length */
824                 }
825                 else {
826                 return AFPERR_BITMAP;  /* error */
827                 }
828     } /* Offspring count/ressource fork length */
829
830     /* Long name */
831     if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
832         /* Get the long filename */     
833                 memcpy(tmppath, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
834                 tmppath[(bspec1 + spec1[1])[0]]= 0;
835                 len = convert_string ( vol->v_maccharset, CH_UCS2, tmppath, -1, c1.lname, sizeof(c1.lname));
836         if (len == (size_t)(-1))
837             return AFPERR_PARAM;
838
839 #if 0   
840                 /* FIXME: do we need it ? It's always null ! */
841                 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
842                 c2.lname[(bspec2 + spec2[1])[0]]= 0;
843 #endif
844     }
845         /* UTF8 Name */
846     if (c1.rbitmap & (1 << FILPBIT_PDINFO)) {
847
848                 /* offset */
849                 memcpy(&namelen, spec1, sizeof(namelen));
850                 namelen = ntohs (namelen);
851
852                 spec1 = bspec1+namelen+4; /* Skip Unicode Hint */
853
854                 /* length */
855                 memcpy(&namelen, spec1, sizeof(namelen));
856                 namelen = ntohs (namelen);
857                 if (namelen > 255)  /* Safeguard */
858                         namelen = 255;
859
860                 memcpy (c1.utf8name, spec1+2, namelen);
861                 c1.utf8name[(namelen+1)] =0;
862
863                 /* convert charset */
864                 flags = CONV_PRECOMPOSE;
865                 len = convert_charset(CH_UTF8_MAC, CH_UCS2, CH_UTF8, c1.utf8name, namelen, c1.utf8name, 512, &flags);
866         if (len == (size_t)(-1))
867             return AFPERR_PARAM;
868     }
869     
870     /* Call search */
871     *rbuflen = 24;
872     ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
873     memcpy(rbuf, catpos, sizeof(catpos));
874     rbuf += sizeof(catpos);
875
876     c1.fbitmap = htons(c1.fbitmap);
877     memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
878     rbuf += sizeof(c1.fbitmap);
879
880     c1.dbitmap = htons(c1.dbitmap);
881     memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
882     rbuf += sizeof(c1.dbitmap);
883
884     nrecs = htonl(nrecs);
885     memcpy(rbuf, &nrecs, sizeof(nrecs));
886     rbuf += sizeof(nrecs);
887     *rbuflen += rsize;
888
889     return ret;
890 } /* catsearch_afp */
891
892 /* -------------------------- */
893 int afp_catsearch (AFPObj *obj, char *ibuf, size_t ibuflen,
894                   char *rbuf, size_t *rbuflen)
895 {
896         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 0);
897 }
898
899
900 int afp_catsearch_ext (AFPObj *obj, char *ibuf, size_t ibuflen,
901                   char *rbuf, size_t *rbuflen)
902 {
903         return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 1);
904 }
905
906 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
907 /* (so, all buffer packing/unpacking should be done in stub, everything else 
908    should be done in other functions) */