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"
63 #include "parse-mtab.h"
71 u_int8_t attrs; /* File attributes (8 bits)*/
72 u_int8_t label; /* Label (8 bits)*/
73 char reserved[22]; /* Unknown (at least for now...) */
77 * 0x04 - has a custom icon
78 * 0x20 - name/icon is locked
92 /* This is our search-criteria structure. */
94 u_int32_t rbitmap; /* Request bitmap - which values should we check ? */
95 u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
96 u_int16_t attr; /* File attributes */
97 time_t cdate; /* Creation date */
98 time_t mdate; /* Last modification date */
99 time_t bdate; /* Last backup date */
100 u_int32_t pdid; /* Parent DID */
101 u_int16_t offcnt; /* Offspring count */
102 struct finderinfo finfo; /* Finder info */
103 char lname[32]; /* Long name */
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.
115 char *lname; /* Long name */
116 struct dir *dir; /* Structure describing this directory */
117 int pidx; /* Parent's dsitem structure index. */
118 int checked; /* Have we checked this directory ? */
119 char *path; /* UNIX path to this directory */
124 static int cur_pos = 0; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
125 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
126 static int save_cidx = -1; /* Saved index of currently scanned directory. */
128 static struct dsitem *dstack = NULL; /* Directory stack data... */
129 static int dssize = 0; /* Directory stack (allocated) size... */
130 static int dsidx = 0; /* First free item index... */
132 static struct scrit c1, c2; /* search criteria */
134 /* Puts new item onto directory stack. */
135 static int addstack(char *lname, struct dir *dir, int pidx)
139 /* check if we have some space on stack... */
140 if (dsidx >= dssize) {
142 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
147 /* Put new element. Allocate and copy lname and path. */
148 ds = dstack + dsidx++;
149 ds->lname = strdup(lname);
153 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
154 strcpy(ds->path, dstack[pidx].path);
155 strcat(ds->path, "/");
156 strcat(ds->path, ds->lname);
164 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
165 static int reducestack()
168 if (save_cidx != -1) {
175 if (dstack[dsidx-1].checked) {
177 free(dstack[dsidx].lname);
178 free(dstack[dsidx].path);
179 /* Check if we need to free (or release) dir structures */
184 } /* reducestack() */
186 /* Clears directory stack. */
187 static void clearstack()
192 free(dstack[dsidx].lname);
193 free(dstack[dsidx].path);
194 /* Check if we need to free (or release) dir structures */
198 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
199 static int resolve_dir(struct vol *vol, int cidx)
201 struct dir *dir, *curdir;
204 if (dstack[cidx].dir != NULL)
207 if (dstack[cidx].pidx < 0)
210 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
213 curdir = dstack[dstack[cidx].pidx].dir;
214 dir = curdir->d_child;
216 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
218 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
222 if (stat(dstack[cidx].path, &statbuf)==-1) {
223 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
227 if (!dir && ((dir = adddir(vol, curdir, dstack[cidx].lname, strlen(dstack[cidx].lname),
228 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
230 dstack[cidx].dir = dir;
235 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
236 static struct adouble *adl_lkup(struct vol *vol, char *upath, int cidx, int isdir)
238 static struct adouble ad;
240 char *mpath = utompath(vol, upath);
244 //if (dstack[cidx].dir == NULL && !resolve_dir(vol, cidx))
247 //if ((of = of_findname(vol, dstack[cidx].dir, mpath))) {
251 memset(&ad, 0, sizeof(ad));
255 if ( ad_open( upath, ADFLAGS_HF | (isdir)?ADFLAGS_DIR:0, O_RDONLY, 0, adp) < 0 ) {
261 #define CATPBIT_PARTIAL 31
262 /* Criteria checker. This function returns a 2-bit value. */
263 /* bit 0 means if checked file meets given criteria. */
264 /* bit 1 means if it is a directory and we should descent into it. */
266 * fname - our fname (translated to UNIX)
267 * cidx - index in directory stack
269 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
273 struct finderinfo *finfo = NULL;
274 struct adouble *adp = NULL;
275 time_t c_date, b_date;
277 if (stat(uname, &sbuf) < 0)
280 if (S_ISDIR(sbuf.st_mode)) {
285 else if (!c1.fbitmap)
288 /* Kind of optimization:
289 * -- first check things we've already have - filename
290 * -- last check things we get from ad_open()
293 /* Check for filename */
294 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
295 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
296 if (strstr(fname, c1.lname) == NULL)
299 if (strcmp(fname, c1.lname) != 0)
301 } /* if (c1.rbitmap & ... */
305 if ((unsigned)c2.mdate > 0x7fffffff)
306 c2.mdate = 0x7fffffff;
307 if ((unsigned)c2.cdate > 0x7fffffff)
308 c2.cdate = 0x7fffffff;
309 if ((unsigned)c2.bdate > 0x7fffffff)
310 c2.bdate = 0x7fffffff;
312 /* Check for modification date FIXME: should we look at adouble structure ? */
313 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
314 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
317 /* Check for creation date... */
318 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
319 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
320 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
321 c_date = AD_DATE_TO_UNIX(c_date);
322 else c_date = sbuf.st_mtime;
323 } else c_date = sbuf.st_mtime;
324 if (c_date < c1.cdate || c_date > c2.cdate)
328 /* Check for backup date... */
329 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
330 if (adp || (adp == adl_lkup(vol, uname, cidx, r))) {
331 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
332 b_date = AD_DATE_TO_UNIX(b_date);
333 else b_date = sbuf.st_mtime;
334 } else b_date = sbuf.st_mtime;
335 if (b_date < c1.bdate || b_date > c2.bdate)
339 /* Check attributes */
340 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
341 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
342 ad_getattr(adp, &attr);
343 if ((attr & c2.attr) != c1.attr)
345 } else goto crit_check_ret;
348 /* Check file type ID */
349 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0)
350 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
351 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
352 if (finfo->f_type != c1.finfo.f_type)
354 } else goto crit_check_ret;
356 /* Check creator ID */
357 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0)
358 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
359 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
360 if (finfo->creator != c1.finfo.creator)
362 } else goto crit_check_ret;
364 /* Check finder info attributes */
365 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
366 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
367 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
368 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
370 } else goto crit_check_ret;
373 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
374 if (adp || (adp = adl_lkup(vol, uname, cidx, r))) {
375 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
376 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
378 } else goto crit_check_ret;
380 /* FIXME: Attributes check ! */
382 /* All criteria are met. */
386 ad_close(adp, ADFLAGS_HF);
391 /* Adds an item to resultset. */
392 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
395 int l = fname != NULL ? strlen(fname) : 0;
399 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
401 p[1] = isdir ? 128 : 0;
404 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
406 did = dstack[cidx].dir->d_did;
407 memcpy(p, &did, sizeof(did));
411 /* Fill offset of returned file name */
414 *p++ = (int)(p - *rbuf) - 1;
424 /* *rbuf[0] = (int)(p-*rbuf); */
429 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
431 /* This function performs search. It is called directly from afp_catsearch
432 * vol - volume we are searching on ...
433 * dir - directory we are starting from ...
434 * c1, c2 - search criteria
435 * rmatches - maximum number of matches we can return
436 * pos - position we've stopped recently
437 * rbuf - output buffer
438 * rbuflen - output buffer length
440 static int catsearch(struct vol *vol, struct dir *dir,
441 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
445 struct dirent *entry;
449 char *orig_dir = NULL;
450 int orig_dir_len = 128;
451 char *path = vol->v_path;
454 if (*pos != 0 && *pos != cur_pos)
455 return AFPERR_CATCHNG;
457 /* FIXME: Category "offspring count ! */
459 /* So we are beginning... */
460 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
463 if (dirpos != NULL) {
466 } /* if (dirpos != NULL) */
468 if (addstack("", dir, -1) == -1) {
469 result = AFPERR_MISC;
472 dstack[0].path = strdup(path);
473 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
476 /* Save current path */
477 orig_dir = (char*)malloc(orig_dir_len);
478 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
479 if (errno != ERANGE) {
480 result = AFPERR_MISC;
484 orig_dir = realloc(orig_dir, orig_dir_len);
487 while ((cidx = reducestack()) != -1) {
489 dirpos = opendir(dstack[cidx].path);
490 if (dirpos == NULL) {
493 dstack[cidx].checked = 1;
498 result = AFPERR_NFILE;
503 result = AFPERR_MISC;
504 } /* switch (errno) */
507 chdir(dstack[cidx].path);
508 while ((entry=readdir(dirpos)) != NULL) {
510 if (veto_file(VETO_STR, entry->d_name))
512 if (stat(entry->d_name, &statbuf) != 0) {
523 result = AFPERR_MISC;
525 } /* switch (errno) */
526 } /* if (stat(entry->d_name, &statbuf) != 0) */
527 fname = utompath(vol, entry->d_name);
528 for (i = 0; fname[i] != 0; i++)
529 fname[i] = tolower(fname[i]);
530 if (strlen(fname) > MACFILELEN)
532 ccr = crit_check(vol, entry->d_name, fname, cidx);
533 /* bit 0 means that criteria has ben met */
535 r = rslt_add(vol, &statbuf,
536 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
537 utompath(vol, entry->d_name) : NULL,
538 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
540 S_ISDIR(statbuf.st_mode), &rrbuf);
542 result = AFPERR_MISC;
546 /* Number of matches limit */
548 goto catsearch_pause; /* FIXME: timelimit checks ! */
549 /* Block size limit */
550 if (rrbuf - rbuf >= 448)
551 goto catsearch_pause;
554 /* bit 1 means that we have to descend into this directory. */
556 if (S_ISDIR(statbuf.st_mode))
557 if (addstack(entry->d_name, NULL, cidx) == -1) {
558 result = AFPERR_MISC;
560 } /* if (addstack... */
562 } /* while ((entry=readdir(dirpos)) != NULL) */
563 closedir(dirpos);dirpos = NULL;
564 dstack[cidx].checked = 1;
565 } /* while (current_idx = reducestack()) != -1) */
567 /* We have finished traversing our tree. Return EOF here. */
575 catsearch_end: /* Exiting catsearch: error condition */
576 *rsize = rrbuf - rbuf;
577 if (orig_dir != NULL) {
585 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
586 char *rbuf, int *rbuflen)
590 u_int32_t rmatches, reserved;
595 int ret, rsize, i = 0;
598 char *spec1, *spec2, *bspec1, *bspec2;
600 memset(&c1, 0, sizeof(c1));
601 memset(&c2, 0, sizeof(c2));
604 memcpy(&vid, ibuf, sizeof(vid));
608 if ((vol = getvolbyvid(vid)) == NULL)
611 memcpy(&rmatches, ibuf, sizeof(rmatches));
612 rmatches = ntohl(rmatches);
613 ibuf += sizeof(rmatches);
615 /* FIXME: (rl) should we check if reserved == 0 ? */
616 ibuf += sizeof(reserved);
618 memcpy(catpos, ibuf, sizeof(catpos));
619 ibuf += sizeof(catpos);
621 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
622 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
623 ibuf += sizeof(c1.fbitmap);
625 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
626 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
627 ibuf += sizeof(c1.dbitmap);
629 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
630 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
631 ibuf += sizeof(c1.rbitmap);
633 if (! (c1.fbitmap || c1.dbitmap)) {
635 return AFPERR_BITMAP;
638 /* Parse file specifications */
640 spec2 = ibuf + ibuf[0] + 2;
642 spec1 += 2; bspec1 = spec1;
643 spec2 += 2; bspec2 = spec2;
645 /* File attribute bits... */
646 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
647 memcpy(&c1.attr, ibuf, sizeof(c1.attr));
648 spec1 += sizeof(c1.attr);
649 c1.attr = ntohs(c1.attr);
650 memcpy(&c2.attr, ibuf, sizeof(c2.attr));
651 spec2 += sizeof(c1.attr);
652 c2.attr = ntohs(c2.attr);
656 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
657 memcpy(&c1.pdid, spec1, sizeof(pdid));
658 spec1 += sizeof(c1.pdid);
659 memcpy(&c2.pdid, spec2, sizeof(pdid));
660 spec2 += sizeof(c2.pdid);
661 } /* FIXME: PDID - do we demarshall this argument ? */
664 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
665 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
666 spec1 += sizeof(c1.cdate);
667 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
668 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
669 spec2 += sizeof(c1.cdate);
670 ibuf += sizeof(c1.cdate);;
671 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
674 /* Modification date */
675 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
676 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
677 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
678 spec1 += sizeof(c1.mdate);
679 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
680 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
681 spec2 += sizeof(c1.mdate);
685 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
686 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
687 spec1 += sizeof(c1.bdate);
688 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
689 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
690 spec2 += sizeof(c2.bdate);
691 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
695 if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
696 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
697 spec1 += sizeof(c1.finfo);
698 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
699 spec2 += sizeof(c2.finfo);
702 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
703 /* Offspring count - only directories */
704 if (c1.fbitmap == 0) {
705 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
706 spec1 += sizeof(c1.offcnt);
707 c1.offcnt = ntohs(c1.offcnt);
708 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
709 spec2 += sizeof(c2.offcnt);
710 c2.offcnt = ntohs(c2.offcnt);
712 else if (c1.dbitmap == 0) {
713 /* ressource fork length */
718 } /* Offspring count/ressource fork length */
721 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
722 /* Get the long filename */
723 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
724 c1.lname[(bspec1 + spec1[1])[0]]= 0;
725 for (i = 0; c1.lname[i] != 0; i++)
726 c1.lname[i] = tolower(c1.lname[i]);
727 /* FIXME: do we need it ? It's always null ! */
728 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
729 c2.lname[(bspec2 + spec2[1])[0]]= 0;
730 for (i = 0; c2.lname[i] != 0; i++)
731 c2.lname[i] = tolower(c2.lname[i]);
737 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
738 memcpy(rbuf, catpos, sizeof(catpos));
739 rbuf += sizeof(catpos);
740 c1.fbitmap = htons(c1.fbitmap);
741 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
742 rbuf += sizeof(c1.fbitmap);
743 c1.dbitmap = htons(c1.dbitmap);
744 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
745 rbuf += sizeof(c1.dbitmap);
746 nrecs = htonl(nrecs);
747 memcpy(rbuf, &nrecs, sizeof(nrecs));
748 rbuf += sizeof(nrecs);
752 } /* afp_catsearch */
754 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
755 /* (so, all buffer packing/unpacking should be done in stub, everything else
756 should be done in other functions) */