2 * Copyright (C) 1990, 1993 Regents of The University of Michigan
3 * All Rights Reserved. See COPYRIGHT
8 * This file contains FPCatSearch implementation. FPCatSearch performs
9 * file/directory search based on specified criteria. It is used by client
10 * to perform fast searches on (propably) big volumes. So, it has to be
13 * This implementation bypasses most of adouble/libatalk stuff as long as
14 * possible and does a standard filesystem search. It calls higher-level
15 * libatalk/afpd functions only when it is really needed, mainly while
16 * returning some non-UNIX information or filtering by non-UNIX criteria.
18 * Initial version written by Rafal Lewczuk <rlewczuk@pronet.pl>
24 #endif /* HAVE_CONFIG_H */
37 #define memcpy(d,s,n) bcopy ((s), (d), (n))
38 #define memmove(d,s,n) bcopy ((s), (d), (n))
39 #endif /* ! HAVE_MEMCPY */
42 #include <sys/types.h>
45 #include <netinet/in.h>
47 #include <netatalk/endian.h>
48 #include <atalk/afp.h>
49 #include <atalk/adouble.h>
51 #include <atalk/cnid.h>
54 #include "directory.h"
62 #include "parse-mtab.h"
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...) */
76 * 0x04 - has a custom icon
77 * 0x20 - name/icon is locked
91 /* This is our search-criteria structure. */
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[32]; /* Long name */
106 * Directory tree search is recursive by its nature. But AFP specification
107 * requires FPCatSearch to pause after returning n results and be able to
108 * resume the search later. So we have to do recursive search using flat
109 * (iterative) algorithm and remember all directories to look into in an
110 * stack-like structure. The structure below is one item of directory stack.
114 char *lname; /* Long name */
115 struct dir *dir; /* Structure describing this directory */
116 int pidx; /* Parent's dsitem structure index. */
117 int checked; /* Have we checked this directory ? */
118 char *path; /* UNIX path to this directory */
123 static int cur_pos = 0; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
124 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
125 static int save_cidx = -1; /* Saved index of currently scanned directory. */
127 static struct dsitem *dstack = NULL; /* Directory stack data... */
128 static int dssize = 0; /* Directory stack (allocated) size... */
129 static int dsidx = 0; /* First free item index... */
131 static struct scrit c1, c2; /* search criteria */
133 /* Puts new item onto directory stack. */
134 static int addstack(char *lname, struct dir *dir, int pidx)
138 /* check if we have some space on stack... */
139 if (dsidx >= dssize) {
141 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
146 /* Put new element. Allocate and copy lname and path. */
147 ds = dstack + dsidx++;
148 ds->lname = strdup(lname);
152 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
153 strcpy(ds->path, dstack[pidx].path);
154 strcat(ds->path, "/");
155 strcat(ds->path, ds->lname);
163 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
164 static int reducestack()
167 if (save_cidx != -1) {
174 if (dstack[dsidx-1].checked) {
176 free(dstack[dsidx].lname);
177 free(dstack[dsidx].path);
178 /* Check if we need to free (or release) dir structures */
183 } /* reducestack() */
185 /* Clears directory stack. */
186 static void clearstack()
191 free(dstack[dsidx].lname);
192 free(dstack[dsidx].path);
193 /* Check if we need to free (or release) dir structures */
197 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
198 static int resolve_dir(struct vol *vol, int cidx)
200 struct dir *dir, *curdir;
203 if (dstack[cidx].dir != NULL)
206 if (dstack[cidx].pidx < 0)
209 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
212 curdir = dstack[dstack[cidx].pidx].dir;
213 dir = curdir->d_child;
215 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
217 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
221 if (stat(dstack[cidx].path, &statbuf)==-1) {
222 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
226 if (!dir && ((dir = adddir(vol, curdir, dstack[cidx].lname, strlen(dstack[cidx].lname),
227 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
229 dstack[cidx].dir = dir;
234 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
235 static struct adouble *adl_lkup(struct vol *vol, char *upath, int cidx)
237 static struct adouble ad;
239 char *mpath = utompath(vol, upath);
243 //if (dstack[cidx].dir == NULL && !resolve_dir(vol, cidx))
246 //if ((of = of_findname(vol, dstack[cidx].dir, mpath))) {
250 memset(&ad, 0, sizeof(ad));
254 if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
260 #define CATPBIT_PARTIAL 31
261 /* Criteria checker. This function returns a 2-bit value. */
262 /* bit 0 means if checked file meets given criteria. */
263 /* bit 1 means if it is a directory and we should descent into it. */
265 * fname - our fname (translated to UNIX)
266 * cidx - index in directory stack
268 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
272 struct finderinfo *finfo = NULL;
273 struct adouble *adp = NULL;
274 time_t c_date, b_date;
276 if (stat(uname, &sbuf) < 0)
279 if (S_ISDIR(sbuf.st_mode))
282 /* Kind of optimization:
283 * -- first check things we've already have - filename
284 * -- last check things we get from ad_open()
287 /* Check for filename */
288 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
289 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
290 if (strstr(fname, c1.lname) == NULL)
293 if (strcmp(fname, c1.lname) != 0)
295 } /* if (c1.rbitmap & ... */
299 if ((unsigned)c2.mdate > 0x7fffffff)
300 c2.mdate = 0x7fffffff;
301 if ((unsigned)c2.cdate > 0x7fffffff)
302 c2.cdate = 0x7fffffff;
303 if ((unsigned)c2.bdate > 0x7fffffff)
304 c2.bdate = 0x7fffffff;
306 /* Check for modification date FIXME: should we look at adouble structure ? */
307 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
308 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
311 /* Check for creation date... */
312 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
313 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
314 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
315 c_date = AD_DATE_TO_UNIX(c_date);
316 else c_date = sbuf.st_mtime;
317 } else c_date = sbuf.st_mtime;
318 if (c_date < c1.cdate || c_date > c2.cdate)
322 /* Check for backup date... */
323 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
324 if (adp || (adp == adl_lkup(vol, uname, cidx))) {
325 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
326 b_date = AD_DATE_TO_UNIX(b_date);
327 else b_date = sbuf.st_mtime;
328 } else b_date = sbuf.st_mtime;
329 if (b_date < c1.bdate || b_date > c2.bdate)
333 /* Check attributes */
334 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
335 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
336 ad_getattr(adp, &attr);
337 if ((attr & c2.attr) != c1.attr)
339 } else goto crit_check_ret;
342 /* Check file type ID */
343 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0)
344 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
345 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
346 if (finfo->f_type != c1.finfo.f_type)
348 } 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(vol, uname, cidx))) {
353 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
354 if (finfo->creator != c1.finfo.creator)
356 } else goto crit_check_ret;
358 /* Check finder info attributes */
359 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
360 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
361 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
362 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
364 } else goto crit_check_ret;
367 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
368 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
369 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
370 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
372 } else goto crit_check_ret;
374 /* FIXME: Attributes check ! */
376 /* All criteria are met. */
380 ad_close(adp, ADFLAGS_HF);
385 /* Adds an item to resultset. */
386 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
389 int l = fname != NULL ? strlen(fname) : 0;
393 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
395 p[1] = isdir ? 128 : 0;
398 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
400 did = dstack[cidx].dir->d_did;
401 memcpy(p, &did, sizeof(did));
405 /* Fill offset of returned file name */
408 *p++ = (int)(p - *rbuf) + 1;
418 /* *rbuf[0] = (int)(p-*rbuf); */
423 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
425 /* This function performs search. It is called directly from afp_catsearch
426 * vol - volume we are searching on ...
427 * dir - directory we are starting from ...
428 * c1, c2 - search criteria
429 * rmatches - maximum number of matches we can return
430 * pos - position we've stopped recently
431 * rbuf - output buffer
432 * rbuflen - output buffer length
434 static int catsearch(struct vol *vol, struct dir *dir,
435 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
439 struct dirent *entry;
443 char *orig_dir = NULL;
444 int orig_dir_len = 128;
445 char *path = vol->v_path;
448 if (*pos != 0 && *pos != cur_pos)
449 return AFPERR_CATCHNG;
451 /* FIXME: Category "offspring count ! */
453 /* So we are beginning... */
454 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
457 if (dirpos != NULL) {
460 } /* if (dirpos != NULL) */
462 if (addstack("", dir, -1) == -1) {
463 result = AFPERR_MISC;
466 dstack[0].path = strdup(path);
467 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
470 /* Save current path */
471 orig_dir = (char*)malloc(orig_dir_len);
472 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
473 if (errno != ERANGE) {
474 result = AFPERR_MISC;
478 orig_dir = realloc(orig_dir, orig_dir_len);
481 while ((cidx = reducestack()) != -1) {
483 dirpos = opendir(dstack[cidx].path);
484 if (dirpos == NULL) {
487 dstack[cidx].checked = 1;
492 result = AFPERR_NFILE;
497 result = AFPERR_MISC;
498 } /* switch (errno) */
501 chdir(dstack[cidx].path);
502 while ((entry=readdir(dirpos)) != NULL) {
504 if (veto_file(VETO_STR, entry->d_name))
506 if (stat(entry->d_name, &statbuf) != 0) {
517 result = AFPERR_MISC;
519 } /* switch (errno) */
520 } /* if (stat(entry->d_name, &statbuf) != 0) */
521 fname = utompath(vol, entry->d_name);
522 for (i = 0; fname[i] != 0; i++)
523 fname[i] = tolower(fname[i]);
524 if (strlen(fname) > MACFILELEN)
526 ccr = crit_check(vol, entry->d_name, fname, cidx);
527 /* bit 0 means that criteria has ben met */
529 r = rslt_add(vol, &statbuf,
530 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
531 utompath(vol, entry->d_name) : NULL,
532 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
534 S_ISDIR(statbuf.st_mode), &rrbuf);
536 result = AFPERR_MISC;
540 /* Number of matches limit */
542 goto catsearch_pause; /* FIXME: timelimit checks ! */
543 /* Block size limit */
544 if (rrbuf - rbuf >= 448)
545 goto catsearch_pause;
548 /* bit 1 means that we have to descend into this directory. */
550 if (S_ISDIR(statbuf.st_mode))
551 if (addstack(entry->d_name, NULL, cidx) == -1) {
552 result = AFPERR_MISC;
554 } /* if (addstack... */
556 } /* while ((entry=readdir(dirpos)) != NULL) */
557 closedir(dirpos);dirpos = NULL;
558 dstack[cidx].checked = 1;
559 } /* while (current_idx = reducestack()) != -1) */
561 /* We have finished traversing our tree. Return EOF here. */
569 catsearch_end: /* Exiting catsearch: error condition */
570 *rsize = rrbuf - rbuf;
571 if (orig_dir != NULL) {
579 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
580 char *rbuf, int *rbuflen)
584 u_int32_t rmatches, reserved;
589 int ret, rsize, i = 0;
592 char *spec1, *spec2, *bspec1, *bspec2;
594 memset(&c1, 0, sizeof(c1));
595 memset(&c2, 0, sizeof(c2));
598 memcpy(&vid, ibuf, sizeof(vid));
602 if ((vol = getvolbyvid(vid)) == NULL)
605 memcpy(&rmatches, ibuf, sizeof(rmatches));
606 rmatches = ntohl(rmatches);
607 ibuf += sizeof(rmatches);
609 /* FIXME: (rl) should we check if reserved == 0 ? */
610 ibuf += sizeof(reserved);
612 memcpy(catpos, ibuf, sizeof(catpos));
613 ibuf += sizeof(catpos);
615 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
616 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
617 ibuf += sizeof(c1.fbitmap);
619 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
620 c1.dbitmap = c2.dbitmap = ntohl(c1.dbitmap);
621 ibuf += sizeof(c1.dbitmap);
623 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
624 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
625 ibuf += sizeof(c1.rbitmap);
627 if (! (c1.fbitmap || c1.dbitmap)) {
629 return AFPERR_BITMAP;
632 /* Parse file specifications */
634 spec2 = ibuf + ibuf[0] + 2;
636 spec1 += 2; bspec1 = spec1;
637 spec2 += 2; bspec2 = spec2;
639 /* File attribute bits... */
640 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
641 memcpy(&c1.attr, ibuf, sizeof(c1.attr));
642 spec1 += sizeof(c1.attr);
643 c1.attr = ntohs(c1.attr);
644 memcpy(&c2.attr, ibuf, sizeof(c2.attr));
645 spec2 += sizeof(c1.attr);
646 c2.attr = ntohs(c2.attr);
650 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
651 memcpy(&c1.pdid, spec1, sizeof(pdid));
652 spec1 += sizeof(c1.pdid);
653 memcpy(&c2.pdid, spec2, sizeof(pdid));
654 spec2 += sizeof(c2.pdid);
655 } /* FIXME: PDID - do we demarshall this argument ? */
658 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
659 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
660 spec1 += sizeof(c1.cdate);
661 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
662 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
663 spec2 += sizeof(c1.cdate);
664 ibuf += sizeof(c1.cdate);;
665 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
668 /* Modification date */
669 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
670 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
671 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
672 spec1 += sizeof(c1.mdate);
673 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
674 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
675 spec2 += sizeof(c1.mdate);
679 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
680 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
681 spec1 += sizeof(c1.bdate);
682 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
683 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
684 spec2 += sizeof(c2.bdate);
685 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
689 if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
690 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
691 spec1 += sizeof(c1.finfo);
692 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
693 spec2 += sizeof(c2.finfo);
696 /* Offspring count - only directories */
697 if (c1.dbitmap != 0 && (c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
698 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
699 spec1 += sizeof(c1.offcnt);
700 c1.offcnt = ntohs(c1.offcnt);
701 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
702 spec2 += sizeof(c2.offcnt);
703 c2.offcnt = ntohs(c2.offcnt);
704 } /* Offspring count */
707 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
708 /* Get the long filename */
709 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
710 c1.lname[(bspec1 + spec1[1])[0]]= 0;
711 for (i = 0; c1.lname[i] != 0; i++)
712 c1.lname[i] = tolower(c1.lname[i]);
713 /* FIXME: do we need it ? It's always null ! */
714 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
715 c2.lname[(bspec2 + spec2[1])[0]]= 0;
716 for (i = 0; c2.lname[i] != 0; i++)
717 c2.lname[i] = tolower(c2.lname[i]);
723 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
724 memcpy(rbuf, catpos, sizeof(catpos));
725 rbuf += sizeof(catpos);
726 c1.fbitmap = htons(c1.fbitmap);
727 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
728 rbuf += sizeof(c1.fbitmap);
729 c1.dbitmap = htons(c1.dbitmap);
730 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
731 rbuf += sizeof(c2.dbitmap);
732 nrecs = htonl(nrecs);
733 memcpy(rbuf, &nrecs, sizeof(nrecs));
734 rbuf += sizeof(nrecs);
738 } /* afp_catsearch */
740 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
741 /* (so, all buffer packing/unpacking should be done in stub, everything else
742 should be done in other functions) */