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"
68 u_int8_t attrs; /* File attributes (8 bits)*/
69 u_int8_t label; /* Label (8 bits)*/
70 char reserved[22]; /* Unknown (at least for now...) */
74 * 0x04 - has a custom icon
75 * 0x20 - name/icon is locked
89 /* This is our search-criteria structure. */
91 u_int32_t rbitmap; /* Request bitmap - which values should we check ? */
92 u_int16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
93 u_int16_t attr; /* File attributes */
94 time_t cdate; /* Creation date */
95 time_t mdate; /* Last modification date */
96 time_t bdate; /* Last backup date */
97 u_int32_t pdid; /* Parent DID */
98 u_int16_t offcnt; /* Offspring count */
99 struct finderinfo finfo; /* Finder info */
100 char lname[32]; /* Long name */
104 * Directory tree search is recursive by its nature. But AFP specification
105 * requires FPCatSearch to pause after returning n results and be able to
106 * resume the search later. So we have to do recursive search using flat
107 * (iterative) algorithm and remember all directories to look into in an
108 * stack-like structure. The structure below is one item of directory stack.
112 char *m_name; /* Mac name */
113 char *u_name; /* unix name (== strrchr('/', path)) */
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; /* absolute 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 *uname, char *mname, 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 if (!(ds->m_name = strdup(mname)))
153 l = strlen(dstack[pidx].path);
154 if (!(ds->path = malloc(l + strlen(uname) + 2) ))
156 strcpy(ds->path, dstack[pidx].path);
157 strcat(ds->path, "/");
158 strcat(ds->path, uname);
159 ds->u_name = ds->path +l +1;
167 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
168 static int reducestack()
171 if (save_cidx != -1) {
178 if (dstack[dsidx-1].checked) {
180 free(dstack[dsidx].m_name);
181 free(dstack[dsidx].path);
182 /* Check if we need to free (or release) dir structures */
187 } /* reducestack() */
189 /* Clears directory stack. */
190 static void clearstack()
195 free(dstack[dsidx].m_name);
196 free(dstack[dsidx].path);
197 /* Check if we need to free (or release) dir structures */
201 /* Fills in dir field of dstack[cidx]. Must fill parent dirs' fields if needed... */
202 static int resolve_dir(struct vol *vol, int cidx)
204 struct dir *dir, *cdir;
206 if (dstack[cidx].dir != NULL)
209 if (dstack[cidx].pidx < 0)
212 if (dstack[dstack[cidx].pidx].dir == NULL && resolve_dir(vol, dstack[cidx].pidx) == 0)
215 cdir = dstack[dstack[cidx].pidx].dir;
218 if (strcmp(dir->d_m_name, dstack[cidx].m_name) == 0)
220 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
226 path.u_name = dstack[cidx].path;
227 if (of_stat(&path)==-1) {
228 syslog(LOG_DEBUG, "resolve_dir: stat %s: %s", dstack[cidx].path, strerror(errno));
231 path.m_name = dstack[cidx].m_name;
232 path.u_name = dstack[cidx].u_name;
233 /* adddir works with a filename not absolute pathname */
234 if ((dir = adddir(vol, cdir, &path)) == NULL)
237 dstack[cidx].dir = dir;
242 /* Looks up for an opened adouble structure, opens resource fork of selected file. */
243 static struct adouble *adl_lkup(struct path *path)
245 static struct adouble ad;
248 int isdir = S_ISDIR(path->st.st_mode);
250 if (!isdir && (of = of_findname(path))) {
253 memset(&ad, 0, sizeof(ad));
257 if ( ad_open( path->u_name, ADFLAGS_HF | (isdir)?ADFLAGS_DIR:0, O_RDONLY, 0, adp) < 0 ) {
263 #define CATPBIT_PARTIAL 31
264 /* Criteria checker. This function returns a 2-bit value. */
265 /* bit 0 means if checked file meets given criteria. */
266 /* bit 1 means if it is a directory and we should descent into it. */
268 * fname - our fname (translated to UNIX)
269 * cidx - index in directory stack
271 static int crit_check(struct vol *vol, struct path *path, int cidx) {
274 struct finderinfo *finfo = NULL, finderinfo;
275 struct adouble *adp = NULL;
276 time_t c_date, b_date;
278 if (S_ISDIR(path->st.st_mode)) {
283 else if (!c1.fbitmap)
286 /* Kind of optimization:
287 * -- first check things we've already have - filename
288 * -- last check things we get from ad_open()
289 * FIXME strmcp strstr (icase)
292 /* Check for filename */
293 if (c1.rbitmap & (1<<DIRPBIT_LNAME)) {
294 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
295 if (strcasestr(path->u_name, c1.lname) == NULL)
298 if (strcasecmp(path->u_name, c1.lname) != 0)
300 } /* if (c1.rbitmap & ... */
304 if ((unsigned)c2.mdate > 0x7fffffff)
305 c2.mdate = 0x7fffffff;
306 if ((unsigned)c2.cdate > 0x7fffffff)
307 c2.cdate = 0x7fffffff;
308 if ((unsigned)c2.bdate > 0x7fffffff)
309 c2.bdate = 0x7fffffff;
311 /* Check for modification date FIXME: should we look at adouble structure ? */
312 if ((c1.rbitmap & (1<<DIRPBIT_MDATE)))
313 if (path->st.st_mtime < c1.mdate || path->st.st_mtime > c2.mdate)
316 /* Check for creation date... */
317 if (c1.rbitmap & (1<<DIRPBIT_CDATE)) {
318 if (adp || (adp = adl_lkup(path))) {
319 if (ad_getdate(adp, AD_DATE_CREATE, (u_int32_t*)&c_date) >= 0)
320 c_date = AD_DATE_TO_UNIX(c_date);
321 else c_date = path->st.st_mtime;
322 } else c_date = path->st.st_mtime;
323 if (c_date < c1.cdate || c_date > c2.cdate)
327 /* Check for backup date... */
328 if (c1.rbitmap & (1<<DIRPBIT_BDATE)) {
329 if (adp || (adp == adl_lkup(path))) {
330 if (ad_getdate(adp, AD_DATE_BACKUP, (u_int32_t*)&b_date) >= 0)
331 b_date = AD_DATE_TO_UNIX(b_date);
332 else b_date = path->st.st_mtime;
333 } else b_date = path->st.st_mtime;
334 if (b_date < c1.bdate || b_date > c2.bdate)
338 /* Check attributes */
339 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
340 if (adp || (adp = adl_lkup(path))) {
341 ad_getattr(adp, &attr);
342 if ((attr & c2.attr) != c1.attr)
344 } else goto crit_check_ret;
347 /* Check file type ID */
348 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
350 adp = adl_lkup(path);
351 finfo = get_finderinfo(path->m_name, adp, &finderinfo);
352 if (finfo->f_type != c1.finfo.f_type)
356 /* Check creator ID */
357 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
360 adp = adl_lkup(path);
361 finfo = get_finderinfo(path->m_name, adp, &finderinfo);
363 if (finfo->creator != c1.finfo.creator)
367 /* Check finder info attributes */
368 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
371 if (adp || (adp = adl_lkup(path))) {
372 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
373 attrs = finfo->attrs;
375 else if (*path->u_name == '.') {
376 attrs = htons(FINDERINFO_INVISIBLE);
379 if ((attrs & c2.finfo.attrs) != c1.finfo.attrs)
384 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
385 if (adp || (adp = adl_lkup(path))) {
386 finfo = (struct finderinfo*)ad_entry(adp, ADEID_FINDERI);
387 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
389 } else goto crit_check_ret;
391 /* FIXME: Attributes check ! */
393 /* All criteria are met. */
397 ad_close(adp, ADFLAGS_HF);
402 /* Adds an item to resultset. */
403 static int rslt_add(struct vol *vol, char *fname, short cidx, int isdir, char **rbuf)
406 int l = fname != NULL ? strlen(fname) : 0;
410 p0 = p[0] = cidx != -1 ? l + 7 : l + 5;
412 p[1] = isdir ? 128 : 0;
415 if (dstack[cidx].dir == NULL && resolve_dir(vol, cidx) == 0)
417 did = dstack[cidx].dir->d_did;
418 memcpy(p, &did, sizeof(did));
422 /* Fill offset of returned file name */
425 *p = (int)(p - *rbuf) - 1;
436 /* *rbuf[0] = (int)(p-*rbuf); */
441 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
443 /* This function performs search. It is called directly from afp_catsearch
444 * vol - volume we are searching on ...
445 * dir - directory we are starting from ...
446 * c1, c2 - search criteria
447 * rmatches - maximum number of matches we can return
448 * pos - position we've stopped recently
449 * rbuf - output buffer
450 * rbuflen - output buffer length
452 static int catsearch(struct vol *vol, struct dir *dir,
453 int rmatches, int *pos, char *rbuf, u_int32_t *nrecs, int *rsize)
457 struct dirent *entry;
461 char *orig_dir = NULL;
462 int orig_dir_len = 128;
463 char *vpath = vol->v_path;
466 if (*pos != 0 && *pos != cur_pos)
467 return AFPERR_CATCHNG;
469 /* FIXME: Category "offspring count ! */
471 /* So we are beginning... */
472 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
475 if (dirpos != NULL) {
480 if (addstack("","", dir, -1) == -1) {
481 result = AFPERR_MISC;
484 dstack[0].path = strdup(vpath);
485 /* FIXME: Sometimes DID is given by klient ! (correct this one above !) */
488 /* Save current path */
489 orig_dir = (char*)malloc(orig_dir_len);
490 while (getcwd(orig_dir, orig_dir_len-1)==NULL) {
491 if (errno != ERANGE) {
492 result = AFPERR_MISC;
496 orig_dir = realloc(orig_dir, orig_dir_len);
499 while ((cidx = reducestack()) != -1) {
501 dirpos = opendir(dstack[cidx].path);
502 if (dirpos == NULL) {
505 dstack[cidx].checked = 1;
510 result = AFPERR_NFILE;
515 result = AFPERR_MISC;
516 } /* switch (errno) */
519 chdir(dstack[cidx].path);
520 while ((entry=readdir(dirpos)) != NULL) {
523 if (!(fname = path.m_name = check_dirent(vol, entry->d_name)))
526 path.u_name = entry->d_name;
527 if (of_stat(&path) != 0) {
538 result = AFPERR_MISC;
540 } /* switch (errno) */
541 } /* if (stat(entry->d_name, &path.st) != 0) */
543 for (i = 0; fname[i] != 0; i++)
544 fname[i] = tolower(fname[i]);
546 ccr = crit_check(vol, &path, cidx);
547 /* bit 1 means that we have to descend into this directory. */
548 if ((ccr & 2) && S_ISDIR(path.st.st_mode)) {
549 if (addstack(entry->d_name, fname, NULL, cidx) == -1) {
550 result = AFPERR_MISC;
555 /* bit 0 means that criteria has ben met */
558 (c1.fbitmap&(1<<FILPBIT_LNAME))|(c1.dbitmap&(1<<DIRPBIT_LNAME)) ?
560 (c1.fbitmap&(1<<FILPBIT_PDID))|(c1.dbitmap&(1<<DIRPBIT_PDID)) ?
562 S_ISDIR(path.st.st_mode), &rrbuf);
564 result = AFPERR_MISC;
568 /* Number of matches limit */
570 goto catsearch_pause; /* FIXME: timelimit checks ! */
571 /* Block size limit */
572 if (rrbuf - rbuf >= 448)
573 goto catsearch_pause;
575 } /* while ((entry=readdir(dirpos)) != NULL) */
578 dstack[cidx].checked = 1;
579 } /* while (current_idx = reducestack()) != -1) */
581 /* We have finished traversing our tree. Return EOF here. */
589 catsearch_end: /* Exiting catsearch: error condition */
590 *rsize = rrbuf - rbuf;
591 if (orig_dir != NULL) {
599 int afp_catsearch(AFPObj *obj, char *ibuf, int ibuflen,
600 char *rbuf, int *rbuflen)
604 u_int32_t rmatches, reserved;
609 unsigned char *spec1, *spec2, *bspec1, *bspec2;
611 memset(&c1, 0, sizeof(c1));
612 memset(&c2, 0, sizeof(c2));
615 memcpy(&vid, ibuf, sizeof(vid));
619 if ((vol = getvolbyvid(vid)) == NULL) {
623 memcpy(&rmatches, ibuf, sizeof(rmatches));
624 rmatches = ntohl(rmatches);
625 ibuf += sizeof(rmatches);
627 /* FIXME: (rl) should we check if reserved == 0 ? */
628 ibuf += sizeof(reserved);
630 memcpy(catpos, ibuf, sizeof(catpos));
631 ibuf += sizeof(catpos);
633 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
634 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
635 ibuf += sizeof(c1.fbitmap);
637 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
638 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
639 ibuf += sizeof(c1.dbitmap);
641 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
642 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
643 ibuf += sizeof(c1.rbitmap);
645 if (! (c1.fbitmap || c1.dbitmap)) {
646 return AFPERR_BITMAP;
649 /* Parse file specifications */
651 spec2 = ibuf + ibuf[0] + 2;
653 spec1 += 2; bspec1 = spec1;
654 spec2 += 2; bspec2 = spec2;
656 /* File attribute bits... */
657 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
658 memcpy(&c1.attr, ibuf, sizeof(c1.attr));
659 spec1 += sizeof(c1.attr);
660 c1.attr = ntohs(c1.attr);
661 memcpy(&c2.attr, ibuf, sizeof(c2.attr));
662 spec2 += sizeof(c1.attr);
663 c2.attr = ntohs(c2.attr);
667 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
668 memcpy(&c1.pdid, spec1, sizeof(pdid));
669 spec1 += sizeof(c1.pdid);
670 memcpy(&c2.pdid, spec2, sizeof(pdid));
671 spec2 += sizeof(c2.pdid);
672 } /* FIXME: PDID - do we demarshall this argument ? */
675 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
676 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
677 spec1 += sizeof(c1.cdate);
678 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
679 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
680 spec2 += sizeof(c1.cdate);
681 ibuf += sizeof(c1.cdate);;
682 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
685 /* Modification date */
686 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
687 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
688 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
689 spec1 += sizeof(c1.mdate);
690 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
691 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
692 spec2 += sizeof(c1.mdate);
696 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
697 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
698 spec1 += sizeof(c1.bdate);
699 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
700 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
701 spec2 += sizeof(c2.bdate);
702 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
706 if (c1.rbitmap * (1 << FILPBIT_FINFO)) {
707 memcpy(&c1.finfo, spec1, sizeof(c1.finfo));
708 spec1 += sizeof(c1.finfo);
709 memcpy(&c2.finfo, spec2, sizeof(c2.finfo));
710 spec2 += sizeof(c2.finfo);
713 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
714 /* Offspring count - only directories */
715 if (c1.fbitmap == 0) {
716 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
717 spec1 += sizeof(c1.offcnt);
718 c1.offcnt = ntohs(c1.offcnt);
719 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
720 spec2 += sizeof(c2.offcnt);
721 c2.offcnt = ntohs(c2.offcnt);
723 else if (c1.dbitmap == 0) {
724 /* ressource fork length */
727 return AFPERR_BITMAP; /* error */
729 } /* Offspring count/ressource fork length */
732 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
733 /* Get the long filename */
734 memcpy(c1.lname, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
735 c1.lname[(bspec1 + spec1[1])[0]]= 0;
737 for (i = 0; c1.lname[i] != 0; i++)
738 c1.lname[i] = tolower(c1.lname[i]);
740 /* FIXME: do we need it ? It's always null ! */
741 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
742 c2.lname[(bspec2 + spec2[1])[0]]= 0;
744 for (i = 0; c2.lname[i] != 0; i++)
745 c2.lname[i] = tolower(c2.lname[i]);
752 ret = catsearch(vol, vol->v_dir, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize);
753 memcpy(rbuf, catpos, sizeof(catpos));
754 rbuf += sizeof(catpos);
756 c1.fbitmap = htons(c1.fbitmap);
757 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
758 rbuf += sizeof(c1.fbitmap);
760 c1.dbitmap = htons(c1.dbitmap);
761 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
762 rbuf += sizeof(c1.dbitmap);
764 nrecs = htonl(nrecs);
765 memcpy(rbuf, &nrecs, sizeof(nrecs));
766 rbuf += sizeof(nrecs);
770 } /* afp_catsearch */
772 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
773 /* (so, all buffer packing/unpacking should be done in stub, everything else
774 should be done in other functions) */