3 * Copyright (C) 1990, 1993 Regents of The University of Michigan
4 * Copyright (C) 2010 Frank Lahm
5 * All Rights Reserved. See COPYRIGHT
10 * This file contains FPCatSearch implementation. FPCatSearch performs
11 * file/directory search based on specified criteria. It is used by client
12 * to perform fast searches on (propably) big volumes. So, it has to be
15 * This implementation bypasses most of adouble/libatalk stuff as long as
16 * possible and does a standard filesystem search. It calls higher-level
17 * libatalk/afpd functions only when it is really needed, mainly while
18 * returning some non-UNIX information or filtering by non-UNIX criteria.
20 * Initial version written by Rafal Lewczuk <rlewczuk@pronet.pl>
22 * Starting with Netatalk 2.2 searching by name criteria utilizes the
23 * CNID database in conjunction with an enhanced cnid_dbd. This requires
24 * the use of cnidscheme:dbd for the searched volume, the new functionality
25 * is not built into cnidscheme:cdb.
30 #endif /* HAVE_CONFIG_H */
40 #include <netinet/in.h>
42 #include <atalk/afp.h>
43 #include <atalk/adouble.h>
44 #include <atalk/logger.h>
45 #include <atalk/cnid.h>
46 #include <atalk/cnid_dbd_private.h>
47 #include <atalk/util.h>
48 #include <atalk/bstradd.h>
49 #include <atalk/unicode.h>
50 #include <atalk/globals.h>
53 #include "directory.h"
64 uint16_t attrs; /* File attributes (high 8 bits)*/
65 uint16_t label; /* Label (low 8 bits )*/
66 char reserved[22]; /* Unknown (at least for now...) */
69 typedef char packed_finder[ADEDLEN_FINDERI];
72 * 0x04 - has a custom icon
73 * 0x20 - name/icon is locked
87 /* This is our search-criteria structure. */
89 uint32_t rbitmap; /* Request bitmap - which values should we check ? */
90 uint16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
91 uint16_t attr; /* File attributes */
92 time_t cdate; /* Creation date */
93 time_t mdate; /* Last modification date */
94 time_t bdate; /* Last backup date */
95 uint32_t pdid; /* Parent DID */
96 uint16_t offcnt; /* Offspring count */
97 struct finderinfo finfo; /* Finder info */
98 char lname[64]; /* Long name */
99 char utf8name[514]; /* UTF8 or UCS2 name */ /* for convert_charset dest_len parameter +2 */
103 * Directory tree search is recursive by its nature. But AFP specification
104 * requires FPCatSearch to pause after returning n results and be able to
105 * resume the search later. So we have to do recursive search using flat
106 * (iterative) algorithm and remember all directories to look into in an
107 * stack-like structure. The structure below is one item of directory stack.
111 // struct dir *dir; /* Structure describing this directory */
112 // cnid_t did; /* CNID of this directory */
113 int pidx; /* Parent's dsitem structure index. */
114 int checked; /* Have we checked this directory ? */
116 char *path; /* absolute UNIX path to this directory */
121 static int save_cidx = -1; /* Saved index of currently scanned directory. */
123 static struct dsitem *dstack = NULL; /* Directory stack data... */
124 static int dssize = 0; /* Directory stack (allocated) size... */
125 static int dsidx = 0; /* First free item index... */
127 static struct scrit c1, c2; /* search criteria */
129 /* Puts new item onto directory stack. */
130 static int addstack(char *uname, struct dir *dir, int pidx)
135 /* check if we have some space on stack... */
136 if (dsidx >= dssize) {
138 dstack = realloc(dstack, dssize * sizeof(struct dsitem));
143 /* Put new element. Allocate and copy lname and path. */
144 ds = dstack + dsidx++;
145 // ds->did = dir->d_did;
149 l = dstack[pidx].path_len;
150 u = strlen(uname) +1;
151 if (!(ds->path = malloc(l + u + 1) ))
153 memcpy(ds->path, dstack[pidx].path, l);
155 memcpy(&ds->path[l+1], uname, u);
159 ds->path = strdup(uname);
160 ds->path_len = strlen(uname);
165 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
166 static int reducestack(void)
169 if (save_cidx != -1) {
176 if (dstack[dsidx-1].checked) {
178 free(dstack[dsidx].path);
185 /* Clears directory stack. */
186 static void clearstack(void)
191 free(dstack[dsidx].path);
195 /* Looks up for an opened adouble structure, opens resource fork of selected file.
196 * FIXME What about noadouble?
198 static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
200 static struct adouble ad;
208 isdir = S_ISDIR(path->st.st_mode);
210 if (!isdir && (of = of_findname(path))) {
217 if ( ad_metadata( path->u_name, ((isdir) ? ADFLAGS_DIR : 0), adp) < 0 ) {
218 adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
224 /* -------------------- */
225 static struct finderinfo *unpack_buffer(struct finderinfo *finfo, char *buffer)
227 memcpy(&finfo->f_type, buffer +FINDERINFO_FRTYPEOFF, sizeof(finfo->f_type));
228 memcpy(&finfo->creator, buffer +FINDERINFO_FRCREATOFF, sizeof(finfo->creator));
229 memcpy(&finfo->attrs, buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->attrs));
230 memcpy(&finfo->label, buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->label));
231 finfo->attrs &= 0xff00; /* high 8 bits */
232 finfo->label &= 0xff; /* low 8 bits */
237 /* -------------------- */
238 static struct finderinfo *
239 unpack_finderinfo(struct vol *vol, struct path *path, struct adouble **adp, struct finderinfo *finfo, int islnk)
244 *adp = adl_lkup(vol, path, *adp);
245 ptr = get_finderinfo(vol, path->u_name, *adp, &buf,islnk);
246 return unpack_buffer(finfo, ptr);
249 /* -------------------- */
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, struct path *path) {
260 uint16_t attr, flags = CONV_PRECOMPOSE;
261 struct finderinfo *finfo = NULL, finderinfo;
262 struct adouble *adp = NULL;
263 time_t c_date, b_date;
264 uint32_t ac_date, ab_date;
265 static char convbuf[514]; /* for convert_charset dest_len parameter +2 */
268 islnk=S_ISLNK(path->st.st_mode);
270 if (S_ISDIR(path->st.st_mode)) {
278 /* compute the Mac name
279 * first try without id (it's slow to find it)
280 * An other option would be to call get_id in utompath but
281 * we need to pass parent dir
283 if (!(path->m_name = utompath(vol, path->u_name, 0 , utf8_encoding()) )) {
284 /*retry with the right id */
288 adp = adl_lkup(vol, path, adp);
289 id = get_id(vol, adp, &path->st, path->d_dir->d_did, path->u_name, strlen(path->u_name));
294 /* save the id for getfilparm */
296 if (!(path->m_name = utompath(vol, path->u_name, id , utf8_encoding()))) {
302 /* Kind of optimization:
303 * -- first check things we've already have - filename
304 * -- last check things we get from ad_open()
305 * FIXME strmcp strstr (icase)
308 /* Check for filename */
309 if ((c1.rbitmap & (1<<DIRPBIT_LNAME))) {
310 if ( (size_t)(-1) == (len = convert_string(vol->v_maccharset, CH_UCS2, path->m_name, -1, convbuf, 512)) )
313 if ((c1.rbitmap & (1<<CATPBIT_PARTIAL))) {
314 if (strcasestr_w( (ucs2_t*) convbuf, (ucs2_t*) c1.lname) == NULL)
317 if (strcasecmp_w((ucs2_t*) convbuf, (ucs2_t*) c1.lname) != 0)
321 if ((c1.rbitmap & (1<<FILPBIT_PDINFO))) {
322 if ( (size_t)(-1) == (len = convert_charset( CH_UTF8_MAC, CH_UCS2, CH_UTF8, path->m_name, strlen(path->m_name), convbuf, 512, &flags))) {
326 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
327 if (strcasestr_w((ucs2_t *) convbuf, (ucs2_t*)c1.utf8name) == NULL)
330 if (strcasecmp_w((ucs2_t *)convbuf, (ucs2_t*)c1.utf8name) != 0)
336 if ((unsigned)c2.mdate > 0x7fffffff)
337 c2.mdate = 0x7fffffff;
338 if ((unsigned)c2.cdate > 0x7fffffff)
339 c2.cdate = 0x7fffffff;
340 if ((unsigned)c2.bdate > 0x7fffffff)
341 c2.bdate = 0x7fffffff;
343 /* Check for modification date */
344 if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) {
345 if (path->st.st_mtime < c1.mdate || path->st.st_mtime > c2.mdate)
349 /* Check for creation date... */
350 if ((c1.rbitmap & (1<<DIRPBIT_CDATE))) {
351 c_date = path->st.st_mtime;
352 adp = adl_lkup(vol, path, adp);
353 if (adp && ad_getdate(adp, AD_DATE_CREATE, &ac_date) >= 0)
354 c_date = AD_DATE_TO_UNIX(ac_date);
356 if (c_date < c1.cdate || c_date > c2.cdate)
360 /* Check for backup date... */
361 if ((c1.rbitmap & (1<<DIRPBIT_BDATE))) {
362 b_date = path->st.st_mtime;
363 adp = adl_lkup(vol, path, adp);
364 if (adp && ad_getdate(adp, AD_DATE_BACKUP, &ab_date) >= 0)
365 b_date = AD_DATE_TO_UNIX(ab_date);
367 if (b_date < c1.bdate || b_date > c2.bdate)
371 /* Check attributes */
372 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
373 if ((adp = adl_lkup(vol, path, adp))) {
374 ad_getattr(adp, &attr);
375 if ((attr & c2.attr) != c1.attr)
381 /* Check file type ID */
382 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
383 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
384 if (finfo->f_type != c1.finfo.f_type)
388 /* Check creator ID */
389 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
391 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
393 if (finfo->creator != c1.finfo.creator)
397 /* Check finder info attributes */
398 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
400 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
403 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
408 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
410 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
412 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
415 /* FIXME: Attributes check ! */
417 /* All criteria are met. */
421 ad_close(adp, ADFLAGS_HF);
425 /* ------------------------------ */
426 static int rslt_add ( struct vol *vol, struct path *path, char **buf, int ext)
433 int isdir = S_ISDIR(path->st.st_mode);
435 /* Skip resultsize */
437 p += sizeof(resultsize);
442 *p++ = isdir ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE; /* IsDir ? */
449 ret = getdirparams(vol, c1.dbitmap, path, path->d_dir, p , &tbuf );
452 /* FIXME slow if we need the file ID, we already know it, done ? */
453 ret = getfilparams ( vol, c1.fbitmap, path, path->d_dir, p, &tbuf);
459 /* Make sure entry length is even */
466 resultsize = htons(tbuf);
467 memcpy ( *buf, &resultsize, sizeof(resultsize) );
479 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
482 * This function performs a filesystem search
484 * Uses globals c1, c2, the search criteria
486 * @param vol (r) volume we are searching on ...
487 * @param dir (rw) directory we are starting from ...
488 * @param rmatches (r) maximum number of matches we can return
489 * @param pos (r) position we've stopped recently
490 * @param rbuf (w) output buffer
491 * @param nrecs (w) number of matches
492 * @param rsize (w) length of data written to output buffer
493 * @param ext (r) extended search flag
495 #define NUM_ROUNDS 200
496 static int catsearch(struct vol *vol,
505 static uint32_t cur_pos; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
506 static DIR *dirpos; /* UNIX structure describing currently opened directory. */
507 struct dir *currentdir; /* struct dir of current directory */
509 struct dirent *entry;
513 char *vpath = vol->v_path;
516 int num_rounds = NUM_ROUNDS;
520 if (*pos != 0 && *pos != cur_pos) {
521 result = AFPERR_CATCHNG;
525 /* FIXME: Category "offspring count ! */
528 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
531 if (dirpos != NULL) {
536 if (addstack(vpath, dir, -1) == -1) {
537 result = AFPERR_MISC;
540 /* FIXME: Sometimes DID is given by client ! (correct this one above !) */
543 /* Save current path */
544 if ((cwd = open(".", O_RDONLY)) < 0) {
545 result = AFPERR_MISC;
549 /* So we are beginning... */
550 start_time = time(NULL);
552 while ((cidx = reducestack()) != -1) {
553 LOG(log_debug, logtype_afpd, "catsearch: dir: \"%s\"", dstack[cidx].path);
555 error = lchdir(dstack[cidx].path);
557 if (!error && dirpos == NULL)
558 dirpos = opendir(".");
561 dirpos = opendir(dstack[cidx].path);
563 if (error || dirpos == NULL) {
566 dstack[cidx].checked = 1;
571 result = AFPERR_NFILE;
576 result = AFPERR_MISC;
577 } /* switch (errno) */
581 if ((currentdir = dirlookup_bypath(vol, dstack[cidx].path)) == NULL) {
582 result = AFPERR_MISC;
585 LOG(log_debug, logtype_afpd, "catsearch: current struct dir: \"%s\"", cfrombstr(currentdir->d_fullpath));
587 while ((entry = readdir(dirpos)) != NULL) {
590 if (!check_dirent(vol, entry->d_name))
593 LOG(log_debug, logtype_afpd, "catsearch(\"%s\"): dirent: \"%s\"",
594 cfrombstr(currentdir->d_fullpath), entry->d_name);
596 memset(&path, 0, sizeof(path));
597 path.u_name = entry->d_name;
598 if (of_stat(&path) != 0) {
609 result = AFPERR_MISC;
613 if (S_ISDIR(path.st.st_mode)) {
614 /* here we can short cut
615 ie if in the same loop the parent dir wasn't in the cache
616 ALL dirsearch_byname will fail.
618 int unlen = strlen(path.u_name);
619 path.d_dir = dircache_search_by_name(vol,
623 if (path.d_dir == NULL) {
624 /* path.m_name is set by adddir */
625 if ((path.d_dir = dir_add(vol,
629 result = AFPERR_MISC;
633 path.m_name = cfrombstr(path.d_dir->d_m_name);
635 if (addstack(path.u_name, path.d_dir, cidx) == -1) {
636 result = AFPERR_MISC;
640 path.d_dir = currentdir;
643 ccr = crit_check(vol, &path);
645 /* bit 0 means that criteria has been met */
647 r = rslt_add ( vol, &path, &rrbuf, ext);
650 result = AFPERR_MISC;
654 /* Number of matches limit */
656 goto catsearch_pause;
657 /* Block size limit */
658 if (rrbuf - rbuf >= 448)
659 goto catsearch_pause;
661 /* MacOS 9 doesn't like servers executing commands longer than few seconds */
662 if (--num_rounds <= 0) {
663 if (start_time != time(NULL)) {
665 goto catsearch_pause;
667 num_rounds = NUM_ROUNDS;
669 } /* while ((entry=readdir(dirpos)) != NULL) */
672 dstack[cidx].checked = 1;
673 } /* while (current_idx = reducestack()) != -1) */
675 /* We have finished traversing our tree. Return EOF here. */
683 catsearch_end: /* Exiting catsearch: error condition */
684 *rsize = rrbuf - rbuf;
686 if ((fchdir(cwd)) != 0) {
687 LOG(log_debug, logtype_afpd, "error chdiring back: %s", strerror(errno));
695 * This function performs a CNID db search
697 * Uses globals c1, c2, the search criteria
699 * @param vol (r) volume we are searching on ...
700 * @param dir (rw) directory we are starting from ...
701 * @param uname (r) UNIX name of object to search
702 * @param rmatches (r) maximum number of matches we can return
703 * @param pos (r) position we've stopped recently
704 * @param rbuf (w) output buffer
705 * @param nrecs (w) number of matches
706 * @param rsize (w) length of data written to output buffer
707 * @param ext (r) extended search flag
709 static int catsearch_db(struct vol *vol,
719 static char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
720 static uint32_t cur_pos;
721 static int num_matches;
726 char buffer[MAXPATHLEN +2];
727 uint16_t flags = CONV_TOLOWER;
729 LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u, name: %s}",
730 *pos, cur_pos, uname);
732 if (*pos != 0 && *pos != cur_pos) {
733 result = AFPERR_CATCHNG;
737 if (cur_pos == 0 || *pos == 0) {
738 if (convert_charset(vol->v_volcharset,
745 &flags) == (size_t)-1) {
746 LOG(log_error, logtype_afpd, "catsearch_db: conversion error");
747 result = AFPERR_MISC;
751 LOG(log_debug, logtype_afpd, "catsearch_db: %s", buffer);
753 if ((num_matches = cnid_find(vol->v_cdb,
757 sizeof(resbuf))) == -1) {
758 result = AFPERR_MISC;
763 while (cur_pos < num_matches) {
766 char resolvebuf[12 + MAXPATHLEN + 1];
769 /* Next CNID to process from buffer */
770 memcpy(&cnid, resbuf + cur_pos * sizeof(cnid_t), sizeof(cnid_t));
773 if ((name = cnid_resolve(vol->v_cdb, &did, resolvebuf, 12 + MAXPATHLEN + 1)) == NULL)
775 LOG(log_debug, logtype_afpd, "catsearch_db: {pos: %u, name:%s, cnid: %u}",
776 cur_pos, name, ntohl(cnid));
777 if ((dir = dirlookup(vol, did)) == NULL)
779 if (movecwd(vol, dir) < 0 )
782 memset(&path, 0, sizeof(path));
784 path.m_name = utompath(vol, name, cnid, utf8_encoding());
786 if (of_stat(&path) != 0) {
794 result = AFPERR_MISC;
798 /* For files path.d_dir is the parent dir, for dirs its the dir itself */
799 if (S_ISDIR(path.st.st_mode))
800 if ((dir = dirlookup(vol, cnid)) == NULL)
804 LOG(log_maxdebug, logtype_afpd,"catsearch_db: dir: %s, cwd: %s, name: %s",
805 cfrombstr(dir->d_fullpath), getcwdpath(), path.u_name);
807 /* At last we can check the search criteria */
808 ccr = crit_check(vol, &path);
810 LOG(log_debug, logtype_afpd,"catsearch_db: match: %s/%s",
811 getcwdpath(), path.u_name);
812 /* bit 1 means that criteria has been met */
813 r = rslt_add(vol, &path, &rrbuf, ext);
815 result = AFPERR_MISC;
819 /* Number of matches limit */
821 goto catsearch_pause;
822 /* Block size limit */
823 if (rrbuf - rbuf >= 448)
824 goto catsearch_pause;
838 catsearch_end: /* Exiting catsearch: error condition */
839 *rsize = rrbuf - rbuf;
840 LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u}", *pos, cur_pos);
844 /* -------------------------- */
845 static int catsearch_afp(AFPObj *obj _U_, char *ibuf, size_t ibuflen,
846 char *rbuf, size_t *rbuflen, int ext)
851 uint32_t rmatches, reserved;
856 unsigned char *spec1, *spec2, *bspec1, *bspec2;
865 /* min header size */
870 memset(&c1, 0, sizeof(c1));
871 memset(&c2, 0, sizeof(c2));
874 memcpy(&vid, ibuf, sizeof(vid));
877 if ((vol = getvolbyvid(vid)) == NULL) {
881 memcpy(&rmatches, ibuf, sizeof(rmatches));
882 rmatches = ntohl(rmatches);
883 ibuf += sizeof(rmatches);
885 /* FIXME: (rl) should we check if reserved == 0 ? */
886 ibuf += sizeof(reserved);
888 memcpy(catpos, ibuf, sizeof(catpos));
889 ibuf += sizeof(catpos);
891 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
892 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
893 ibuf += sizeof(c1.fbitmap);
895 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
896 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
897 ibuf += sizeof(c1.dbitmap);
899 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
900 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
901 ibuf += sizeof(c1.rbitmap);
903 if (! (c1.fbitmap || c1.dbitmap)) {
904 return AFPERR_BITMAP;
908 memcpy(&spec_len, ibuf, sizeof(spec_len));
909 spec_len = ntohs(spec_len);
912 /* with catsearch only name and parent id are allowed */
913 c1.fbitmap &= (1<<FILPBIT_LNAME) | (1<<FILPBIT_PDID);
914 c1.dbitmap &= (1<<DIRPBIT_LNAME) | (1<<DIRPBIT_PDID);
915 spec_len = *(unsigned char*)ibuf;
918 /* Parse file specifications */
919 spec1 = (unsigned char*)ibuf;
920 spec2 = (unsigned char*)ibuf + spec_len + 2;
927 /* File attribute bits... */
928 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
929 memcpy(&c1.attr, spec1, sizeof(c1.attr));
930 spec1 += sizeof(c1.attr);
931 memcpy(&c2.attr, spec2, sizeof(c2.attr));
932 spec2 += sizeof(c1.attr);
936 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
937 memcpy(&c1.pdid, spec1, sizeof(pdid));
938 spec1 += sizeof(c1.pdid);
939 memcpy(&c2.pdid, spec2, sizeof(pdid));
940 spec2 += sizeof(c2.pdid);
941 } /* FIXME: PDID - do we demarshall this argument ? */
944 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
945 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
946 spec1 += sizeof(c1.cdate);
947 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
948 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
949 spec2 += sizeof(c1.cdate);
950 ibuf += sizeof(c1.cdate);;
951 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
954 /* Modification date */
955 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
956 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
957 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
958 spec1 += sizeof(c1.mdate);
959 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
960 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
961 spec2 += sizeof(c1.mdate);
965 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
966 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
967 spec1 += sizeof(c1.bdate);
968 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
969 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
970 spec2 += sizeof(c2.bdate);
971 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
975 if (c1.rbitmap & (1 << FILPBIT_FINFO)) {
978 memcpy(buf, spec1, sizeof(buf));
979 unpack_buffer(&c1.finfo, buf);
980 spec1 += sizeof(buf);
982 memcpy(buf, spec2, sizeof(buf));
983 unpack_buffer(&c2.finfo, buf);
984 spec2 += sizeof(buf);
987 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
988 /* Offspring count - only directories */
989 if (c1.fbitmap == 0) {
990 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
991 spec1 += sizeof(c1.offcnt);
992 c1.offcnt = ntohs(c1.offcnt);
993 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
994 spec2 += sizeof(c2.offcnt);
995 c2.offcnt = ntohs(c2.offcnt);
997 else if (c1.dbitmap == 0) {
998 /* ressource fork length */
1001 return AFPERR_BITMAP; /* error */
1003 } /* Offspring count/ressource fork length */
1006 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
1007 /* Get the long filename */
1008 memcpy(tmppath, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
1009 tmppath[(bspec1 + spec1[1])[0]]= 0;
1010 len = convert_string ( vol->v_maccharset, CH_UCS2, tmppath, -1, c1.lname, sizeof(c1.lname));
1011 if (len == (size_t)(-1))
1012 return AFPERR_PARAM;
1015 /* FIXME: do we need it ? It's always null ! */
1016 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
1017 c2.lname[(bspec2 + spec2[1])[0]]= 0;
1021 if (c1.rbitmap & (1 << FILPBIT_PDINFO)) {
1024 memcpy(&namelen, spec1, sizeof(namelen));
1025 namelen = ntohs (namelen);
1027 spec1 = bspec1+namelen+4; /* Skip Unicode Hint */
1030 memcpy(&namelen, spec1, sizeof(namelen));
1031 namelen = ntohs (namelen);
1032 if (namelen > UTF8FILELEN_EARLY) /* Safeguard */
1033 namelen = UTF8FILELEN_EARLY;
1035 memcpy (c1.utf8name, spec1+2, namelen);
1036 c1.utf8name[namelen] = 0;
1037 if ((uname = mtoupath(vol, c1.utf8name, 0, utf8_encoding())) == NULL)
1038 return AFPERR_PARAM;
1040 /* convert charset */
1041 flags = CONV_PRECOMPOSE;
1042 len = convert_charset(CH_UTF8_MAC, CH_UCS2, CH_UTF8, c1.utf8name, namelen, c1.utf8name, 512, &flags);
1043 if (len == (size_t)(-1))
1044 return AFPERR_PARAM;
1049 if ((c1.rbitmap & (1 << FILPBIT_PDINFO))
1050 && (strcmp(vol->v_cnidscheme, "dbd") == 0)
1051 && (vol->v_flags & AFPVOL_SEARCHDB))
1052 /* we've got a name and it's a dbd volume, so search CNID database */
1053 ret = catsearch_db(vol, vol->v_root, uname, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
1055 /* perform a slow filesystem tree search */
1056 ret = catsearch(vol, vol->v_root, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
1058 memcpy(rbuf, catpos, sizeof(catpos));
1059 rbuf += sizeof(catpos);
1061 c1.fbitmap = htons(c1.fbitmap);
1062 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
1063 rbuf += sizeof(c1.fbitmap);
1065 c1.dbitmap = htons(c1.dbitmap);
1066 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
1067 rbuf += sizeof(c1.dbitmap);
1069 nrecs = htonl(nrecs);
1070 memcpy(rbuf, &nrecs, sizeof(nrecs));
1071 rbuf += sizeof(nrecs);
1075 } /* catsearch_afp */
1077 /* -------------------------- */
1078 int afp_catsearch (AFPObj *obj, char *ibuf, size_t ibuflen,
1079 char *rbuf, size_t *rbuflen)
1081 return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 0);
1085 int afp_catsearch_ext (AFPObj *obj, char *ibuf, size_t ibuflen,
1086 char *rbuf, size_t *rbuflen)
1088 return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 1);
1091 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
1092 /* (so, all buffer packing/unpacking should be done in stub, everything else
1093 should be done in other functions) */