2 * Copyright (C) 2002 Rafal Lewczuk (rlewczuk@pronet.pl)
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.
23 #endif /* HAVE_CONFIG_H */
36 #define memcpy(d,s,n) bcopy ((s), (d), (n))
37 #define memmove(d,s,n) bcopy ((s), (d), (n))
38 #endif /* ! HAVE_MEMCPY */
41 #include <sys/types.h>
44 #include <netinet/in.h>
46 #include <netatalk/endian.h>
47 #include <atalk/afp.h>
48 #include <atalk/adouble.h>
50 #include <atalk/cnid.h>
53 #include "directory.h"
61 #include "parse-mtab.h"
69 u_int8_t attrs; /* File attributes (8 bits)*/
70 u_int8_t label; /* Label (8 bits)*/
71 char reserved[22]; /* Unknown (at least for now...) */
75 * 0x04 - has a custom icon
76 * 0x20 - name/icon is locked
90 /* This is our search-criteria structure. */
92 u_int32_t rbitmap; /* Request bitmap - which values should we check ? */
93 u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
94 u_int16_t attr; /* File attributes */
95 time_t cdate; /* Creation date */
96 time_t mdate; /* Last modification date */
97 time_t bdate; /* Last backup date */
98 u_int32_t pdid; /* Parent DID */
99 u_int16_t offcnt; /* Offspring count */
100 struct finderinfo finfo; /* Finder info */
101 char lname[32]; /* Long name */
105 * Directory tree search is recursive by its nature. But AFP specification
106 * requires FPCatSearch to pause after returning n results and be able to
107 * resume the search later. So we have to do recursive search using flat
108 * (iterative) algorithm and remember all directories to look into in an
109 * stack-like structure. The structure below is one item of directory stack.
113 char *lname; /* Long name */
114 struct dir *dir; /* Structure describing this directory */
115 int pidx; /* Parent's dsitem structure index. */
116 int checked; /* Have we checked this directory ? */
117 char *path; /* UNIX path to this directory */
122 static int cur_pos = 0; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
123 static DIR *dirpos = NULL; /* UNIX structure describing currently opened directory. */
124 static int save_cidx = -1; /* Saved index of currently scanned directory. */
126 static struct dsitem *dstack = NULL; /* Directory stack data... */
127 static int dssize = 0; /* Directory stack (allocated) size... */
128 static int dsidx = 0; /* First free item index... */
130 static struct scrit c1, c2; /* search criteria */
132 /* Puts new item onto directory stack. */
133 static int addstack(char *lname, struct dir *dir, int pidx)
137 /* check if we have some space on stack... */
138 if (dsidx >= dssize) {
140 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
145 /* Put new element. Allocate and copy lname and path. */
146 ds = dstack + dsidx++;
147 ds->lname = strdup(lname);
151 ds->path = malloc(strlen(dstack[pidx].path) + strlen(ds->lname) + 2);
152 strcpy(ds->path, dstack[pidx].path);
153 strcat(ds->path, "/");
154 strcat(ds->path, ds->lname);
162 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
163 static int reducestack()
166 if (save_cidx != -1) {
173 if (dstack[dsidx-1].checked) {
175 free(dstack[dsidx].lname);
176 free(dstack[dsidx].path);
177 /* Check if we need to free (or release) dir structures */
182 } /* reducestack() */
184 /* Clears directory stack. */
185 static void clearstack()
190 free(dstack[dsidx].lname);
191 free(dstack[dsidx].path);
192 /* Check if we need to free (or release) dir structures */
196 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
197 static int resolve_dir(struct vol *vol, int cidx)
199 struct dir *dir, *curdir;
202 if (dstack[cidx].dir != NULL)
205 if (dstack[cidx].pidx < 0)
208 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
211 curdir = dstack[dstack[cidx].pidx].dir;
212 dir = curdir->d_child;
214 if (strcmp(dir->d_name, dstack[cidx].lname) == 0)
216 dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
220 if (stat(dstack[cidx].path, &statbuf)==-1) {
221 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
225 if (!dir && ((dir = adddir(vol, curdir, dstack[cidx].lname, strlen(dstack[cidx].lname),
226 dstack[cidx].path, strlen(dstack[cidx].path), &statbuf)) == NULL))
228 dstack[cidx].dir = dir;
233 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
234 static struct adouble *adl_lkup(struct vol *vol, char *upath, int cidx)
236 static struct adouble ad;
238 char *mpath = utompath(vol, upath);
242 //if (dstack[cidx].dir == NULL && !resolve_dir(vol, cidx))
245 //if ((of = of_findname(vol, dstack[cidx].dir, mpath))) {
249 memset(&ad, 0, sizeof(ad));
253 if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
259 #define CATPBIT_PARTIAL 31
260 /* Criteria checker. This function returns a 2-bit value. */
261 /* bit 0 means if checked file meets given criteria. */
262 /* bit 1 means if it is a directory and we should descent into it. */
264 * fname - our fname (translated to UNIX)
265 * cidx - index in directory stack
267 static int crit_check(struct vol *vol, char *uname, char *fname, int cidx) {
271 struct finderinfo *finfo = NULL;
272 struct adouble *adp = NULL;
273 time_t c_date, b_date;
275 if (stat(uname, &sbuf) < 0)
278 if (S_ISDIR(sbuf.st_mode))
281 /* Kind of optimization:
282 * -- first check things we've already have - filename
283 * -- last check things we get from ad_open()
286 /* Check for filename */
287 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
288 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
289 if (strstr(fname, c1.lname) == NULL)
292 if (strcmp(fname, c1.lname) != 0)
294 } /* if (c1.rbitmap & ... */
298 if ((unsigned)c2.mdate > 0x7fffffff)
299 c2.mdate = 0x7fffffff;
300 if ((unsigned)c2.cdate > 0x7fffffff)
301 c2.cdate = 0x7fffffff;
302 if ((unsigned)c2.bdate > 0x7fffffff)
303 c2.bdate = 0x7fffffff;
305 /* Check for modification date FIXME: should we look at adouble structure ? */
306 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
307 if (sbuf.st_mtime < c1.mdate || sbuf.st_mtime > c2.mdate)
310 /* Check for creation date... */
311 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
312 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
313 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
314 c_date = AD_DATE_TO_UNIX(c_date);
315 else c_date = sbuf.st_mtime;
316 } else c_date = sbuf.st_mtime;
317 if (c_date < c1.cdate || c_date > c2.cdate)
321 /* Check for backup date... */
322 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
323 if (adp || (adp == adl_lkup(vol, uname, cidx))) {
324 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
325 b_date = AD_DATE_TO_UNIX(b_date);
326 else b_date = sbuf.st_mtime;
327 } else b_date = sbuf.st_mtime;
328 if (b_date < c1.bdate || b_date > c2.bdate)
332 /* Check attributes */
333 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0)
334 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
335 ad_getattr(adp, &attr);
336 if ((attr & c2.attr) != c1.attr)
338 } 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(vol, uname, cidx))) {
344 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
345 if (finfo->f_type != c1.finfo.f_type)
347 } else goto crit_check_ret;
349 /* Check creator ID */
350 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0)
351 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
352 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
353 if (finfo->creator != c1.finfo.creator)
355 } else goto crit_check_ret;
357 /* Check finder info attributes */
358 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0)
359 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
360 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
361 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
363 } else goto crit_check_ret;
366 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0)
367 if (adp || (adp = adl_lkup(vol, uname, cidx))) {
368 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
369 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
371 } else goto crit_check_ret;
373 /* FIXME: Attributes check ! */
375 /* All criteria are met. */
379 ad_close(adp, ADFLAGS_HF);
384 /* Adds an item to resultset. */
385 static int rslt_add(struct vol *vol, struct stat *statbuf, char *fname, short cidx, int isdir, char **rbuf)
388 int l = fname != NULL ? strlen(fname) : 0;
392 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
394 p[1] = isdir ? 128 : 0;
397 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
399 did = dstack[cidx].dir->d_did;
400 memcpy(p, &did, sizeof(did));
404 /* Fill offset of returned file name */
407 *p++ = (int)(p - *rbuf) + 1;
417 /* *rbuf[0] = (int)(p-*rbuf); */
422 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
424 /* This function performs search. It is called directly from afp_catsearch
425 * vol - volume we are searching on ...
426 * dir - directory we are starting from ...
427 * c1, c2 - search criteria
428 * rmatches - maximum number of matches we can return
429 * pos - position we've stopped recently
430 * rbuf - output buffer
431 * rbuflen - output buffer length
433 static int catsearch(struct vol *vol, struct dir *dir,
434 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
438 struct dirent *entry;
442 char *orig_dir = NULL;
443 int orig_dir_len = 128;
444 char *path = vol->v_path;
447 if (*pos != 0 && *pos != cur_pos)
448 return AFPERR_CATCHNG;
450 /* FIXME: Category "offspring count ! */
452 /* So we are beginning... */
453 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
456 if (dirpos != NULL) {
459 } /* if (dirpos != NULL) */
461 if (addstack("", dir, -1) == -1) {
462 result = AFPERR_MISC;
465 dstack[0].path = strdup(path);
466 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
469 /* Save current path */
470 orig_dir = (char*)malloc(orig_dir_len);
471 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
472 if (errno != ERANGE) {
473 result = AFPERR_MISC;
477 orig_dir = realloc(orig_dir, orig_dir_len);
480 while ((cidx = reducestack()) != -1) {
482 dirpos = opendir(dstack[cidx].path);
483 if (dirpos == NULL) {
486 dstack[cidx].checked = 1;
491 result = AFPERR_NFILE;
496 result = AFPERR_MISC;
497 } /* switch (errno) */
500 chdir(dstack[cidx].path);
501 while ((entry=readdir(dirpos)) != NULL) {
503 if (veto_file(VETO_STR, entry->d_name))
505 if (stat(entry->d_name, &statbuf) != 0) {
516 result = AFPERR_MISC;
518 } /* switch (errno) */
519 } /* if (stat(entry->d_name, &statbuf) != 0) */
520 fname = utompath(vol, entry->d_name);
521 for (i = 0; fname[i] != 0; i++)
522 fname[i] = tolower(fname[i]);
523 if (strlen(fname) > MACFILELEN)
525 ccr = crit_check(vol, entry->d_name, fname, cidx);
526 /* bit 0 means that criteria has ben met */
528 r = rslt_add(vol, &statbuf,
529 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
530 utompath(vol, entry->d_name) : NULL,
531 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
533 S_ISDIR(statbuf.st_mode), &rrbuf);
535 result = AFPERR_MISC;
539 /* Number of matches limit */
541 goto catsearch_pause; /* FIXME: timelimit checks ! */
542 /* Block size limit */
543 if (rrbuf - rbuf >= 448)
544 goto catsearch_pause;
547 /* bit 1 means that we have to descend into this directory. */
549 if (S_ISDIR(statbuf.st_mode))
550 if (addstack(entry->d_name, NULL, cidx) == -1) {
551 result = AFPERR_MISC;
553 } /* if (addstack... */
555 } /* while ((entry=readdir(dirpos)) != NULL) */
556 closedir(dirpos);dirpos = NULL;
557 dstack[cidx].checked = 1;
558 } /* while (current_idx = reducestack()) != -1) */
560 /* We have finished traversing our tree. Return EOF here. */
568 catsearch_end: /* Exiting catsearch: error condition */
569 *rsize = rrbuf - rbuf;
570 if (orig_dir != NULL) {
578 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
579 char *rbuf, int *rbuflen)
583 u_int32_t rmatches, reserved;
588 int ret, rsize, i = 0;
591 char *spec1, *spec2, *bspec1, *bspec2;
593 memset(&c1, 0, sizeof(c1));
594 memset(&c2, 0, sizeof(c2));
597 memcpy(&vid, ibuf, sizeof(vid));
601 if ((vol = getvolbyvid(vid)) == NULL)
604 memcpy(&rmatches, ibuf, sizeof(rmatches));
605 rmatches = ntohl(rmatches);
606 ibuf += sizeof(rmatches);
608 /* FIXME: (rl) should we check if reserved == 0 ? */
609 ibuf += sizeof(reserved);
611 memcpy(catpos, ibuf, sizeof(catpos));
612 ibuf += sizeof(catpos);
614 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
615 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
616 ibuf += sizeof(c1.fbitmap);
618 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
619 c1.dbitmap = c2.dbitmap = ntohl(c1.dbitmap);
620 ibuf += sizeof(c1.dbitmap);
622 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
623 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
624 ibuf += sizeof(c1.rbitmap);
626 if (! (c1.fbitmap || c1.dbitmap)) {
628 return AFPERR_BITMAP;
631 /* Parse file specifications */
633 spec2 = ibuf + ibuf[0] + 2;
635 spec1 += 2; bspec1 = spec1;
636 spec2 += 2; bspec2 = spec2;
638 /* File attribute bits... */
639 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
640 memcpy(&c1.attr, ibuf, sizeof(c1.attr));
641 spec1 += sizeof(c1.attr);
642 c1.attr = ntohs(c1.attr);
643 memcpy(&c2.attr, ibuf, sizeof(c2.attr));
644 spec2 += sizeof(c1.attr);
645 c2.attr = ntohs(c2.attr);
649 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
650 memcpy(&c1.pdid, spec1, sizeof(pdid));
651 spec1 += sizeof(c1.pdid);
652 memcpy(&c2.pdid, spec2, sizeof(pdid));
653 spec2 += sizeof(c2.pdid);
654 } /* FIXME: PDID - do we demarshall this argument ? */
657 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
658 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
659 spec1 += sizeof(c1.cdate);
660 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
661 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
662 spec2 += sizeof(c1.cdate);
663 ibuf += sizeof(c1.cdate);;
664 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
667 /* Modification date */
668 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
669 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
670 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
671 spec1 += sizeof(c1.mdate);
672 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
673 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
674 spec2 += sizeof(c1.mdate);
678 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
679 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
680 spec1 += sizeof(c1.bdate);
681 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
682 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
683 spec2 += sizeof(c2.bdate);
684 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
688 if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
689 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
690 spec1 += sizeof(c1.finfo);
691 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
692 spec2 += sizeof(c2.finfo);
695 /* Offspring count - only directories */
696 if (c1.dbitmap != 0 && (c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
697 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
698 spec1 += sizeof(c1.offcnt);
699 c1.offcnt = ntohs(c1.offcnt);
700 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
701 spec2 += sizeof(c2.offcnt);
702 c2.offcnt = ntohs(c2.offcnt);
703 } /* Offspring count */
706 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
707 /* Get the long filename */
708 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
709 c1.lname[(bspec1 + spec1[1])[0]]= 0;
710 for (i = 0; c1.lname[i] != 0; i++)
711 c1.lname[i] = tolower(c1.lname[i]);
712 /* FIXME: do we need it ? It's always null ! */
713 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
714 c2.lname[(bspec2 + spec2[1])[0]]= 0;
715 for (i = 0; c2.lname[i] != 0; i++)
716 c2.lname[i] = tolower(c2.lname[i]);
722 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
723 memcpy(rbuf, catpos, sizeof(catpos));
724 rbuf += sizeof(catpos);
725 c1.fbitmap = htons(c1.fbitmap);
726 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
727 rbuf += sizeof(c1.fbitmap);
728 c1.dbitmap = htons(c1.dbitmap);
729 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
730 rbuf += sizeof(c2.dbitmap);
731 nrecs = htonl(nrecs);
732 memcpy(rbuf, &nrecs, sizeof(nrecs));
733 rbuf += sizeof(nrecs);
737 } /* afp_catsearch */
739 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
740 /* (so, all buffer packing/unpacking should be done in stub, everything else
741 should be done in other functions) */