3 * Copyright (C) 1990, 1993 Regents of The University of Michigan
4 * All Rights Reserved. See COPYRIGHT
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
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.
19 * Initial version written by Rafal Lewczuk <rlewczuk@pronet.pl>
25 #endif /* HAVE_CONFIG_H */
39 #define memcpy(d,s,n) bcopy ((s), (d), (n))
40 #define memmove(d,s,n) bcopy ((s), (d), (n))
41 #endif /* ! HAVE_MEMCPY */
44 #include <sys/types.h>
47 #include <netinet/in.h>
49 #include <netatalk/endian.h>
50 #include <atalk/afp.h>
51 #include <atalk/adouble.h>
53 #include <atalk/cnid.h>
56 #include "directory.h"
66 u_int8_t attrs; /* File attributes (8 bits)*/
67 u_int8_t label; /* Label (8 bits)*/
68 char reserved[22]; /* Unknown (at least for now...) */
72 * 0x04 - has a custom icon
73 * 0x20 - name/icon is locked
87 /* This is our search-criteria structure. */
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[32]; /* Long name */
102 * Directory tree search is recursive by its nature. But AFP specification
103 * requires FPCatSearch to pause after returning n results and be able to
104 * resume the search later. So we have to do recursive search using flat
105 * (iterative) algorithm and remember all directories to look into in an
106 * stack-like structure. The structure below is one item of directory stack.
110 char *lname; /* Long name */
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 char *path; /* UNIX path to this directory */
119 static int cur_pos = 0; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
120 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
121 static int save_cidx = -1; /* Saved index of currently scanned directory. */
123 static struct dsitem *dstack = NULL; /* Directory stack data... */
124 static int dssize = 0; /* Directory stack (allocated) size... */
125 static int dsidx = 0; /* First free item index... */
127 static struct scrit c1, c2; /* search criteria */
129 /* Puts new item onto directory stack. */
130 static int addstack(char *lname, struct dir *dir, int pidx)
134 /* check if we have some space on stack... */
135 if (dsidx >= dssize) {
137 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
142 /* Put new element. Allocate and copy lname and path. */
143 ds = dstack + dsidx++;
144 ds->lname = strdup(lname);
148 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
149 strcpy(ds->path, dstack[pidx].path);
150 strcat(ds->path, "/");
151 strcat(ds->path, ds->lname);
159 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
160 static int reducestack()
163 if (save_cidx != -1) {
170 if (dstack[dsidx-1].checked) {
172 free(dstack[dsidx].lname);
173 free(dstack[dsidx].path);
174 /* Check if we need to free (or release) dir structures */
179 } /* reducestack() */
181 /* Clears directory stack. */
182 static void clearstack()
187 free(dstack[dsidx].lname);
188 free(dstack[dsidx].path);
189 /* Check if we need to free (or release) dir structures */
193 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
194 static int resolve_dir(struct vol *vol, int cidx)
196 struct dir *dir, *curdir;
200 if (dstack[cidx].dir != NULL)
203 if (dstack[cidx].pidx < 0)
206 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
209 curdir = dstack[dstack[cidx].pidx].dir;
210 dir = curdir->d_child;
212 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
214 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
218 if (stat(dstack[cidx].path, &statbuf)==-1) {
219 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
223 mname = utompath(vol, dstack[cidx].lname);
224 if (!dir && ((dir = adddir(vol, curdir, mname, strlen(mname),
225 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
227 dstack[cidx].dir = dir;
232 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
233 static struct adouble *adl_lkup(char *upath, struct stat *sb)
235 static struct adouble ad;
238 int isdir = S_ISDIR(sb->st_mode);
240 if (!isdir && (of = of_findname(upath, sb ))) {
243 memset(&ad, 0, sizeof(ad));
247 if ( ad_open( upath, ADFLAGS_HF | (isdir)?ADFLAGS_DIR:0, O_RDONLY, 0, adp) < 0 ) {
253 #define CATPBIT_PARTIAL 31
254 /* Criteria checker. This function returns a 2-bit value. */
255 /* bit 0 means if checked file meets given criteria. */
256 /* bit 1 means if it is a directory and we should descent into it. */
258 * fname - our fname (translated to UNIX)
259 * cidx - index in directory stack
261 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
265 struct finderinfo *finfo = NULL;
266 struct adouble *adp = NULL;
267 time_t c_date, b_date;
269 if (stat(uname, &sbuf) < 0)
272 if (S_ISDIR(sbuf.st_mode)) {
277 else if (!c1.fbitmap)
280 /* Kind of optimization:
281 * -- first check things we've already have - filename
282 * -- last check things we get from ad_open()
285 /* Check for filename */
286 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
287 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
288 if (strstr(fname, c1.lname) == NULL)
291 if (strcmp(fname, c1.lname) != 0)
293 } /* if (c1.rbitmap & ... */
297 if ((unsigned)c2.mdate > 0x7fffffff)
298 c2.mdate = 0x7fffffff;
299 if ((unsigned)c2.cdate > 0x7fffffff)
300 c2.cdate = 0x7fffffff;
301 if ((unsigned)c2.bdate > 0x7fffffff)
302 c2.bdate = 0x7fffffff;
304 /* Check for modification date FIXME: should we look at adouble structure ? */
305 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
306 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
309 /* Check for creation date... */
310 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
311 if (adp || (adp = adl_lkup(uname, &sbuf))) {
312 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
313 c_date = AD_DATE_TO_UNIX(c_date);
314 else c_date = sbuf.st_mtime;
315 } else c_date = sbuf.st_mtime;
316 if (c_date < c1.cdate || c_date > c2.cdate)
320 /* Check for backup date... */
321 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
322 if (adp || (adp == adl_lkup(uname, &sbuf))) {
323 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
324 b_date = AD_DATE_TO_UNIX(b_date);
325 else b_date = sbuf.st_mtime;
326 } else b_date = sbuf.st_mtime;
327 if (b_date < c1.bdate || b_date > c2.bdate)
331 /* Check attributes */
332 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
333 if (adp || (adp = adl_lkup(uname, &sbuf))) {
334 ad_getattr(adp, &attr);
335 if ((attr & c2.attr) != c1.attr)
337 } else goto crit_check_ret;
341 /* Check file type ID */
342 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
343 if (adp || (adp = adl_lkup(uname, &sbuf))) {
344 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
345 if (finfo->f_type != c1.finfo.f_type)
347 } else goto crit_check_ret;
350 /* Check creator ID */
351 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
352 if (adp || (adp = adl_lkup(uname, &sbuf))) {
353 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
354 if (finfo->creator != c1.finfo.creator)
356 } else goto crit_check_ret;
359 /* Check finder info attributes */
360 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
361 if (adp || (adp = adl_lkup(uname, &sbuf))) {
362 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
363 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
365 } else goto crit_check_ret;
369 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
370 if (adp || (adp = adl_lkup(uname, &sbuf))) {
371 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
372 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
374 } else goto crit_check_ret;
377 /* FIXME: Attributes check ! */
379 /* All criteria are met. */
383 ad_close(adp, ADFLAGS_HF);
388 /* Adds an item to resultset. */
389 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
392 char *savefname = (fname != NULL) ? strdup (fname) : NULL;
393 int l = savefname != NULL ? strlen(savefname) : 0;
397 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
399 p[1] = isdir ? 128 : 0;
402 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
404 did = dstack[cidx].dir->d_did;
405 memcpy(p, &did, sizeof(did));
409 /* Fill offset of returned file name */
410 if (savefname != NULL) {
413 *p = (int)(p - *rbuf) - 1;
416 strcpy(p+1, savefname);
424 /* *rbuf[0] = (int)(p-*rbuf); */
425 if (savefname != NULL)
431 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
433 /* This function performs search. It is called directly from afp_catsearch
434 * vol - volume we are searching on ...
435 * dir - directory we are starting from ...
436 * c1, c2 - search criteria
437 * rmatches - maximum number of matches we can return
438 * pos - position we've stopped recently
439 * rbuf - output buffer
440 * rbuflen - output buffer length
442 static int catsearch(struct vol *vol, struct dir *dir,
443 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
447 struct dirent *entry;
451 char *orig_dir = NULL;
452 int orig_dir_len = 128;
453 char *path = vol->v_path;
456 if (*pos != 0 && *pos != cur_pos)
457 return AFPERR_CATCHNG;
459 /* FIXME: Category "offspring count ! */
461 /* So we are beginning... */
462 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
465 if (dirpos != NULL) {
468 } /* if (dirpos != NULL) */
470 if (addstack("", dir, -1) == -1) {
471 result = AFPERR_MISC;
474 dstack[0].path = strdup(path);
475 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
478 /* Save current path */
479 orig_dir = (char*)malloc(orig_dir_len);
480 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
481 if (errno != ERANGE) {
482 result = AFPERR_MISC;
486 orig_dir = realloc(orig_dir, orig_dir_len);
489 while ((cidx = reducestack()) != -1) {
491 dirpos = opendir(dstack[cidx].path);
492 if (dirpos == NULL) {
495 dstack[cidx].checked = 1;
500 result = AFPERR_NFILE;
505 result = AFPERR_MISC;
506 } /* switch (errno) */
509 chdir(dstack[cidx].path);
510 while ((entry=readdir(dirpos)) != NULL) {
512 if (veto_file(VETO_STR, entry->d_name))
514 if (stat(entry->d_name, &statbuf) != 0) {
525 result = AFPERR_MISC;
527 } /* switch (errno) */
528 } /* if (stat(entry->d_name, &statbuf) != 0) */
529 fname = utompath(vol, entry->d_name);
530 for (i = 0; fname[i] != 0; i++)
531 fname[i] = tolower(fname[i]);
532 if (strlen(fname) > MACFILELEN)
534 ccr = crit_check(vol, entry->d_name, fname, cidx);
535 /* bit 0 means that criteria has ben met */
537 r = rslt_add(vol, &statbuf,
538 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
539 utompath(vol, entry->d_name) : NULL,
540 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
542 S_ISDIR(statbuf.st_mode), &rrbuf);
544 result = AFPERR_MISC;
548 /* Number of matches limit */
550 goto catsearch_pause; /* FIXME: timelimit checks ! */
551 /* Block size limit */
552 if (rrbuf - rbuf >= 448)
553 goto catsearch_pause;
556 /* bit 1 means that we have to descend into this directory. */
558 if (S_ISDIR(statbuf.st_mode))
559 if (addstack(entry->d_name, NULL, cidx) == -1) {
560 result = AFPERR_MISC;
562 } /* if (addstack... */
564 } /* while ((entry=readdir(dirpos)) != NULL) */
565 closedir(dirpos);dirpos = NULL;
566 dstack[cidx].checked = 1;
567 } /* while (current_idx = reducestack()) != -1) */
569 /* We have finished traversing our tree. Return EOF here. */
577 catsearch_end: /* Exiting catsearch: error condition */
578 *rsize = rrbuf - rbuf;
579 if (orig_dir != NULL) {
587 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
588 char *rbuf, int *rbuflen)
592 u_int32_t rmatches, reserved;
595 int ret, rsize, i = 0;
597 char *spec1, *spec2, *bspec1, *bspec2;
599 memset(&c1, 0, sizeof(c1));
600 memset(&c2, 0, sizeof(c2));
603 memcpy(&vid, ibuf, sizeof(vid));
607 if ((vol = getvolbyvid(vid)) == NULL)
610 memcpy(&rmatches, ibuf, sizeof(rmatches));
611 rmatches = ntohl(rmatches);
612 ibuf += sizeof(rmatches);
614 /* FIXME: (rl) should we check if reserved == 0 ? */
615 ibuf += sizeof(reserved);
617 memcpy(catpos, ibuf, sizeof(catpos));
618 ibuf += sizeof(catpos);
620 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
621 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
622 ibuf += sizeof(c1.fbitmap);
624 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
625 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
626 ibuf += sizeof(c1.dbitmap);
628 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
629 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
630 ibuf += sizeof(c1.rbitmap);
632 if (! (c1.fbitmap || c1.dbitmap)) {
634 return AFPERR_BITMAP;
637 /* Parse file specifications */
639 spec2 = ibuf + ibuf[0] + 2;
641 spec1 += 2; bspec1 = spec1;
642 spec2 += 2; bspec2 = spec2;
644 /* File attribute bits... */
645 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
646 memcpy(&c1.attr, spec1, sizeof(c1.attr));
647 spec1 += sizeof(c1.attr);
648 c1.attr = ntohs(c1.attr);
649 memcpy(&c2.attr, spec2, sizeof(c2.attr));
650 spec2 += sizeof(c1.attr);
651 c2.attr = ntohs(c2.attr);
655 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
656 memcpy(&c1.pdid, spec1, sizeof(pdid));
657 spec1 += sizeof(c1.pdid);
658 memcpy(&c2.pdid, spec2, sizeof(pdid));
659 spec2 += sizeof(c2.pdid);
660 } /* FIXME: PDID - do we demarshall this argument ? */
663 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
664 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
665 spec1 += sizeof(c1.cdate);
666 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
667 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
668 spec2 += sizeof(c1.cdate);
669 ibuf += sizeof(c1.cdate);;
670 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
673 /* Modification date */
674 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
675 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
676 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
677 spec1 += sizeof(c1.mdate);
678 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
679 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
680 spec2 += sizeof(c1.mdate);
684 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
685 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
686 spec1 += sizeof(c1.bdate);
687 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
688 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
689 spec2 += sizeof(c2.bdate);
690 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
694 if (c1.rbitmap & (1 << FILPBIT_FINFO)) {
695 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
696 spec1 += sizeof(c1.finfo);
697 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
698 spec2 += sizeof(c2.finfo);
701 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
702 /* Offspring count - only directories */
703 if (c1.fbitmap == 0) {
704 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
705 spec1 += sizeof(c1.offcnt);
706 c1.offcnt = ntohs(c1.offcnt);
707 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
708 spec2 += sizeof(c2.offcnt);
709 c2.offcnt = ntohs(c2.offcnt);
711 else if (c1.dbitmap == 0) {
712 /* ressource fork length */
717 } /* Offspring count/ressource fork length */
720 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
721 /* Get the long filename */
722 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
723 c1.lname[(int) (bspec1 + spec1[1])[0]]= 0;
724 for (i = 0; c1.lname[i] != 0; i++)
725 c1.lname[i] = tolower(c1.lname[i]);
726 /* FIXME: do we need it ? It's always null ! */
727 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
728 c2.lname[(int) (bspec2 + spec2[1])[0]]= 0;
729 for (i = 0; c2.lname[i] != 0; i++)
730 c2.lname[i] = tolower(c2.lname[i]);
736 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
737 memcpy(rbuf, catpos, sizeof(catpos));
738 rbuf += sizeof(catpos);
739 c1.fbitmap = htons(c1.fbitmap);
740 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
741 rbuf += sizeof(c1.fbitmap);
742 c1.dbitmap = htons(c1.dbitmap);
743 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
744 rbuf += sizeof(c1.dbitmap);
745 nrecs = htonl(nrecs);
746 memcpy(rbuf, &nrecs, sizeof(nrecs));
747 rbuf += sizeof(nrecs);
751 } /* afp_catsearch */
753 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
754 /* (so, all buffer packing/unpacking should be done in stub, everything else
755 should be done in other functions) */