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