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 */
38 #define memcpy(d,s,n) bcopy ((s), (d), (n))
39 #define memmove(d,s,n) bcopy ((s), (d), (n))
40 #endif /* ! HAVE_MEMCPY */
43 #include <sys/types.h>
46 #include <netinet/in.h>
48 #include <netatalk/endian.h>
49 #include <atalk/afp.h>
50 #include <atalk/adouble.h>
52 #include <atalk/cnid.h>
55 #include "directory.h"
65 u_int8_t attrs; /* File attributes (8 bits)*/
66 u_int8_t label; /* Label (8 bits)*/
67 char reserved[22]; /* Unknown (at least for now...) */
71 * 0x04 - has a custom icon
72 * 0x20 - name/icon is locked
86 /* This is our search-criteria structure. */
88 u_int32_t rbitmap; /* Request bitmap - which values should we check ? */
89 u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
90 u_int16_t attr; /* File attributes */
91 time_t cdate; /* Creation date */
92 time_t mdate; /* Last modification date */
93 time_t bdate; /* Last backup date */
94 u_int32_t pdid; /* Parent DID */
95 u_int16_t offcnt; /* Offspring count */
96 struct finderinfo finfo; /* Finder info */
97 char lname[32]; /* Long name */
101 * Directory tree search is recursive by its nature. But AFP specification
102 * requires FPCatSearch to pause after returning n results and be able to
103 * resume the search later. So we have to do recursive search using flat
104 * (iterative) algorithm and remember all directories to look into in an
105 * stack-like structure. The structure below is one item of directory stack.
109 char *lname; /* Long name */
110 struct dir *dir; /* Structure describing this directory */
111 int pidx; /* Parent's dsitem structure index. */
112 int checked; /* Have we checked this directory ? */
113 char *path; /* UNIX path to this directory */
118 static int cur_pos = 0; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
119 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
120 static int save_cidx = -1; /* Saved index of currently scanned directory. */
122 static struct dsitem *dstack = NULL; /* Directory stack data... */
123 static int dssize = 0; /* Directory stack (allocated) size... */
124 static int dsidx = 0; /* First free item index... */
126 static struct scrit c1, c2; /* search criteria */
128 /* Puts new item onto directory stack. */
129 static int addstack(char *lname, struct dir *dir, int pidx)
133 /* check if we have some space on stack... */
134 if (dsidx >= dssize) {
136 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
141 /* Put new element. Allocate and copy lname and path. */
142 ds = dstack + dsidx++;
143 ds->lname = strdup(lname);
147 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
148 strcpy(ds->path, dstack[pidx].path);
149 strcat(ds->path, "/");
150 strcat(ds->path, ds->lname);
158 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
159 static int reducestack()
162 if (save_cidx != -1) {
169 if (dstack[dsidx-1].checked) {
171 free(dstack[dsidx].lname);
172 free(dstack[dsidx].path);
173 /* Check if we need to free (or release) dir structures */
178 } /* reducestack() */
180 /* Clears directory stack. */
181 static void clearstack()
186 free(dstack[dsidx].lname);
187 free(dstack[dsidx].path);
188 /* Check if we need to free (or release) dir structures */
192 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
193 static int resolve_dir(struct vol *vol, int cidx)
195 struct dir *dir, *curdir;
198 if (dstack[cidx].dir != NULL)
201 if (dstack[cidx].pidx < 0)
204 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
207 curdir = dstack[dstack[cidx].pidx].dir;
208 dir = curdir->d_child;
210 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
212 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
216 if (stat(dstack[cidx].path, &statbuf)==-1) {
217 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
221 if (!dir && ((dir = adddir(vol, curdir, dstack[cidx].lname, strlen(dstack[cidx].lname),
222 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
224 dstack[cidx].dir = dir;
229 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
230 static struct adouble *adl_lkup(char *upath, struct stat *sb)
232 static struct adouble ad;
235 int isdir = S_ISDIR(sb->st_mode);
237 if (!isdir && (of = of_findname(upath, sb ))) {
240 memset(&ad, 0, sizeof(ad));
244 if ( ad_open( upath, ADFLAGS_HF | (isdir)?ADFLAGS_DIR:0, O_RDONLY, 0, adp) < 0 ) {
250 #define CATPBIT_PARTIAL 31
251 /* Criteria checker. This function returns a 2-bit value. */
252 /* bit 0 means if checked file meets given criteria. */
253 /* bit 1 means if it is a directory and we should descent into it. */
255 * fname - our fname (translated to UNIX)
256 * cidx - index in directory stack
258 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
262 struct finderinfo *finfo = NULL;
263 struct adouble *adp = NULL;
264 time_t c_date, b_date;
266 if (stat(uname, &sbuf) < 0)
269 if (S_ISDIR(sbuf.st_mode)) {
274 else if (!c1.fbitmap)
277 /* Kind of optimization:
278 * -- first check things we've already have - filename
279 * -- last check things we get from ad_open()
282 /* Check for filename */
283 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
284 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
285 if (strstr(fname, c1.lname) == NULL)
288 if (strcmp(fname, c1.lname) != 0)
290 } /* if (c1.rbitmap & ... */
294 if ((unsigned)c2.mdate > 0x7fffffff)
295 c2.mdate = 0x7fffffff;
296 if ((unsigned)c2.cdate > 0x7fffffff)
297 c2.cdate = 0x7fffffff;
298 if ((unsigned)c2.bdate > 0x7fffffff)
299 c2.bdate = 0x7fffffff;
301 /* Check for modification date FIXME: should we look at adouble structure ? */
302 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
303 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
306 /* Check for creation date... */
307 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
308 if (adp || (adp = adl_lkup(uname, &sbuf))) {
309 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
310 c_date = AD_DATE_TO_UNIX(c_date);
311 else c_date = sbuf.st_mtime;
312 } else c_date = sbuf.st_mtime;
313 if (c_date < c1.cdate || c_date > c2.cdate)
317 /* Check for backup date... */
318 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
319 if (adp || (adp == adl_lkup(uname, &sbuf))) {
320 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
321 b_date = AD_DATE_TO_UNIX(b_date);
322 else b_date = sbuf.st_mtime;
323 } else b_date = sbuf.st_mtime;
324 if (b_date < c1.bdate || b_date > c2.bdate)
328 /* Check attributes */
329 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
330 if (adp || (adp = adl_lkup(uname, &sbuf))) {
331 ad_getattr(adp, &attr);
332 if ((attr & c2.attr) != c1.attr)
334 } else goto crit_check_ret;
337 /* Check file type ID */
338 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0)
339 if (adp || (adp = adl_lkup(uname, &sbuf))) {
340 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
341 if (finfo->f_type != c1.finfo.f_type)
343 } else goto crit_check_ret;
345 /* Check creator ID */
346 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0)
347 if (adp || (adp = adl_lkup(uname, &sbuf))) {
348 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
349 if (finfo->creator != c1.finfo.creator)
351 } else goto crit_check_ret;
353 /* Check finder info attributes */
354 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
355 if (adp || (adp = adl_lkup(uname, &sbuf))) {
356 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
357 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
359 } else goto crit_check_ret;
362 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
363 if (adp || (adp = adl_lkup(uname, &sbuf))) {
364 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
365 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
367 } else goto crit_check_ret;
369 /* FIXME: Attributes check ! */
371 /* All criteria are met. */
375 ad_close(adp, ADFLAGS_HF);
380 /* Adds an item to resultset. */
381 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
384 int l = fname != NULL ? strlen(fname) : 0;
388 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
390 p[1] = isdir ? 128 : 0;
393 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
395 did = dstack[cidx].dir->d_did;
396 memcpy(p, &did, sizeof(did));
400 /* Fill offset of returned file name */
403 *p++ = (int)(p - *rbuf) - 1;
413 /* *rbuf[0] = (int)(p-*rbuf); */
418 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
420 /* This function performs search. It is called directly from afp_catsearch
421 * vol - volume we are searching on ...
422 * dir - directory we are starting from ...
423 * c1, c2 - search criteria
424 * rmatches - maximum number of matches we can return
425 * pos - position we've stopped recently
426 * rbuf - output buffer
427 * rbuflen - output buffer length
429 static int catsearch(struct vol *vol, struct dir *dir,
430 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
434 struct dirent *entry;
438 char *orig_dir = NULL;
439 int orig_dir_len = 128;
440 char *path = vol->v_path;
443 if (*pos != 0 && *pos != cur_pos)
444 return AFPERR_CATCHNG;
446 /* FIXME: Category "offspring count ! */
448 /* So we are beginning... */
449 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
452 if (dirpos != NULL) {
455 } /* if (dirpos != NULL) */
457 if (addstack("", dir, -1) == -1) {
458 result = AFPERR_MISC;
461 dstack[0].path = strdup(path);
462 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
465 /* Save current path */
466 orig_dir = (char*)malloc(orig_dir_len);
467 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
468 if (errno != ERANGE) {
469 result = AFPERR_MISC;
473 orig_dir = realloc(orig_dir, orig_dir_len);
476 while ((cidx = reducestack()) != -1) {
478 dirpos = opendir(dstack[cidx].path);
479 if (dirpos == NULL) {
482 dstack[cidx].checked = 1;
487 result = AFPERR_NFILE;
492 result = AFPERR_MISC;
493 } /* switch (errno) */
496 chdir(dstack[cidx].path);
497 while ((entry=readdir(dirpos)) != NULL) {
499 if (veto_file(VETO_STR, entry->d_name))
501 if (stat(entry->d_name, &statbuf) != 0) {
512 result = AFPERR_MISC;
514 } /* switch (errno) */
515 } /* if (stat(entry->d_name, &statbuf) != 0) */
516 fname = utompath(vol, entry->d_name);
517 for (i = 0; fname[i] != 0; i++)
518 fname[i] = tolower(fname[i]);
519 if (strlen(fname) > MACFILELEN)
521 ccr = crit_check(vol, entry->d_name, fname, cidx);
522 /* bit 0 means that criteria has ben met */
524 r = rslt_add(vol, &statbuf,
525 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
526 utompath(vol, entry->d_name) : NULL,
527 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
529 S_ISDIR(statbuf.st_mode), &rrbuf);
531 result = AFPERR_MISC;
535 /* Number of matches limit */
537 goto catsearch_pause; /* FIXME: timelimit checks ! */
538 /* Block size limit */
539 if (rrbuf - rbuf >= 448)
540 goto catsearch_pause;
543 /* bit 1 means that we have to descend into this directory. */
545 if (S_ISDIR(statbuf.st_mode))
546 if (addstack(entry->d_name, NULL, cidx) == -1) {
547 result = AFPERR_MISC;
549 } /* if (addstack... */
551 } /* while ((entry=readdir(dirpos)) != NULL) */
552 closedir(dirpos);dirpos = NULL;
553 dstack[cidx].checked = 1;
554 } /* while (current_idx = reducestack()) != -1) */
556 /* We have finished traversing our tree. Return EOF here. */
564 catsearch_end: /* Exiting catsearch: error condition */
565 *rsize = rrbuf - rbuf;
566 if (orig_dir != NULL) {
574 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
575 char *rbuf, int *rbuflen)
579 u_int32_t rmatches, reserved;
584 int ret, rsize, i = 0;
587 char *spec1, *spec2, *bspec1, *bspec2;
589 memset(&c1, 0, sizeof(c1));
590 memset(&c2, 0, sizeof(c2));
593 memcpy(&vid, ibuf, sizeof(vid));
597 if ((vol = getvolbyvid(vid)) == NULL)
600 memcpy(&rmatches, ibuf, sizeof(rmatches));
601 rmatches = ntohl(rmatches);
602 ibuf += sizeof(rmatches);
604 /* FIXME: (rl) should we check if reserved == 0 ? */
605 ibuf += sizeof(reserved);
607 memcpy(catpos, ibuf, sizeof(catpos));
608 ibuf += sizeof(catpos);
610 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
611 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
612 ibuf += sizeof(c1.fbitmap);
614 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
615 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
616 ibuf += sizeof(c1.dbitmap);
618 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
619 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
620 ibuf += sizeof(c1.rbitmap);
622 if (! (c1.fbitmap || c1.dbitmap)) {
624 return AFPERR_BITMAP;
627 /* Parse file specifications */
629 spec2 = ibuf + ibuf[0] + 2;
631 spec1 += 2; bspec1 = spec1;
632 spec2 += 2; bspec2 = spec2;
634 /* File attribute bits... */
635 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
636 memcpy(&c1.attr, ibuf, sizeof(c1.attr));
637 spec1 += sizeof(c1.attr);
638 c1.attr = ntohs(c1.attr);
639 memcpy(&c2.attr, ibuf, sizeof(c2.attr));
640 spec2 += sizeof(c1.attr);
641 c2.attr = ntohs(c2.attr);
645 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
646 memcpy(&c1.pdid, spec1, sizeof(pdid));
647 spec1 += sizeof(c1.pdid);
648 memcpy(&c2.pdid, spec2, sizeof(pdid));
649 spec2 += sizeof(c2.pdid);
650 } /* FIXME: PDID - do we demarshall this argument ? */
653 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
654 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
655 spec1 += sizeof(c1.cdate);
656 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
657 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
658 spec2 += sizeof(c1.cdate);
659 ibuf += sizeof(c1.cdate);;
660 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
663 /* Modification date */
664 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
665 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
666 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
667 spec1 += sizeof(c1.mdate);
668 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
669 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
670 spec2 += sizeof(c1.mdate);
674 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
675 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
676 spec1 += sizeof(c1.bdate);
677 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
678 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
679 spec2 += sizeof(c2.bdate);
680 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
684 if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
685 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
686 spec1 += sizeof(c1.finfo);
687 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
688 spec2 += sizeof(c2.finfo);
691 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
692 /* Offspring count - only directories */
693 if (c1.fbitmap == 0) {
694 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
695 spec1 += sizeof(c1.offcnt);
696 c1.offcnt = ntohs(c1.offcnt);
697 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
698 spec2 += sizeof(c2.offcnt);
699 c2.offcnt = ntohs(c2.offcnt);
701 else if (c1.dbitmap == 0) {
702 /* ressource fork length */
707 } /* Offspring count/ressource fork length */
710 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
711 /* Get the long filename */
712 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
713 c1.lname[(bspec1 + spec1[1])[0]]= 0;
714 for (i = 0; c1.lname[i] != 0; i++)
715 c1.lname[i] = tolower(c1.lname[i]);
716 /* FIXME: do we need it ? It's always null ! */
717 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
718 c2.lname[(bspec2 + spec2[1])[0]]= 0;
719 for (i = 0; c2.lname[i] != 0; i++)
720 c2.lname[i] = tolower(c2.lname[i]);
726 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
727 memcpy(rbuf, catpos, sizeof(catpos));
728 rbuf += sizeof(catpos);
729 c1.fbitmap = htons(c1.fbitmap);
730 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
731 rbuf += sizeof(c1.fbitmap);
732 c1.dbitmap = htons(c1.dbitmap);
733 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
734 rbuf += sizeof(c1.dbitmap);
735 nrecs = htonl(nrecs);
736 memcpy(rbuf, &nrecs, sizeof(nrecs));
737 rbuf += sizeof(nrecs);
741 } /* afp_catsearch */
743 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
744 /* (so, all buffer packing/unpacking should be done in stub, everything else
745 should be done in other functions) */