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 */
32 #include <sys/types.h>
43 #include <netinet/in.h>
45 #include <atalk/afp.h>
46 #include <atalk/adouble.h>
47 #include <atalk/logger.h>
48 #include <atalk/cnid.h>
49 #include <atalk/cnid_dbd_private.h>
50 #include <atalk/util.h>
51 #include <atalk/bstradd.h>
52 #include <atalk/unicode.h>
53 #include <atalk/globals.h>
54 #include <atalk/netatalk_conf.h>
57 #include "directory.h"
68 uint16_t attrs; /* File attributes (high 8 bits)*/
69 uint16_t label; /* Label (low 8 bits )*/
70 char reserved[22]; /* Unknown (at least for now...) */
73 typedef char packed_finder[ADEDLEN_FINDERI];
76 * 0x04 - has a custom icon
77 * 0x20 - name/icon is locked
91 /* This is our search-criteria structure. */
93 uint32_t rbitmap; /* Request bitmap - which values should we check ? */
94 uint16_t fbitmap, dbitmap; /* file & directory bitmap - which values should we return ? */
95 uint16_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 uint32_t pdid; /* Parent DID */
100 uint16_t offcnt; /* Offspring count */
101 struct finderinfo finfo; /* Finder info */
102 char lname[64]; /* Long name */
103 char utf8name[514]; /* UTF8 or UCS2 name */ /* for convert_charset dest_len parameter +2 */
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 cnid_t ds_did; /* CNID of this directory */
116 int ds_checked; /* Have we checked this directory ? */
121 static int save_cidx = -1; /* Saved index of currently scanned directory. */
122 static struct dsitem *dstack = NULL; /* Directory stack data... */
123 static int dssize = 0; /* Directory stack (allocated) size... */
124 static int dsidx = 0; /* First free item index... */
125 static struct scrit c1, c2; /* search criteria */
127 /* Clears directory stack. */
128 static void clearstack(void)
136 /* Puts new item onto directory stack. */
137 static int addstack(char *uname, struct dir *dir, int pidx)
140 struct dsitem *tmpds = NULL;
142 /* check if we have some space on stack... */
143 if (dsidx >= dssize) {
145 tmpds = realloc(dstack, dssize * sizeof(struct dsitem));
154 /* Put new element. Allocate and copy lname and path. */
155 ds = dstack + dsidx++;
156 ds->ds_did = dir->d_did;
161 /* Removes checked items from top of directory stack. Returns index of the first unchecked elements or -1. */
162 static int reducestack(void)
165 if (save_cidx != -1) {
172 if (dstack[dsidx-1].ds_checked) {
174 // free(dstack[dsidx].path);
181 /* Looks up for an opened adouble structure, opens resource fork of selected file.
182 * FIXME What about noadouble?
184 static struct adouble *adl_lkup(struct vol *vol, struct path *path, struct adouble *adp)
186 static struct adouble ad;
194 isdir = S_ISDIR(path->st.st_mode);
196 if (!isdir && (of = of_findname(vol, path))) {
203 if ( ad_metadata( path->u_name, ((isdir) ? ADFLAGS_DIR : 0), adp) < 0 ) {
204 adp = NULL; /* FIXME without resource fork adl_lkup will be call again */
210 /* -------------------- */
211 static struct finderinfo *unpack_buffer(struct finderinfo *finfo, char *buffer)
213 memcpy(&finfo->f_type, buffer +FINDERINFO_FRTYPEOFF, sizeof(finfo->f_type));
214 memcpy(&finfo->creator, buffer +FINDERINFO_FRCREATOFF, sizeof(finfo->creator));
215 memcpy(&finfo->attrs, buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->attrs));
216 memcpy(&finfo->label, buffer +FINDERINFO_FRFLAGOFF, sizeof(finfo->label));
217 finfo->attrs &= 0xff00; /* high 8 bits */
218 finfo->label &= 0xff; /* low 8 bits */
223 /* -------------------- */
224 static struct finderinfo *
225 unpack_finderinfo(struct vol *vol, struct path *path, struct adouble **adp, struct finderinfo *finfo, int islnk)
230 *adp = adl_lkup(vol, path, *adp);
231 ptr = get_finderinfo(vol, path->u_name, *adp, &buf,islnk);
232 return unpack_buffer(finfo, ptr);
235 /* -------------------- */
236 #define CATPBIT_PARTIAL 31
237 /* Criteria checker. This function returns a 2-bit value. */
238 /* bit 0 means if checked file meets given criteria. */
239 /* bit 1 means if it is a directory and we should descent into it. */
241 * fname - our fname (translated to UNIX)
242 * cidx - index in directory stack
244 static int crit_check(struct vol *vol, struct path *path) {
246 uint16_t attr, flags = CONV_PRECOMPOSE;
247 struct finderinfo *finfo = NULL, finderinfo;
248 struct adouble *adp = NULL;
249 time_t c_date, b_date;
250 uint32_t ac_date, ab_date;
251 static char convbuf[514]; /* for convert_charset dest_len parameter +2 */
254 islnk=S_ISLNK(path->st.st_mode);
256 if (S_ISDIR(path->st.st_mode)) {
264 /* compute the Mac name
265 * first try without id (it's slow to find it)
266 * An other option would be to call get_id in utompath but
267 * we need to pass parent dir
269 if (!(path->m_name = utompath(vol, path->u_name, 0 , utf8_encoding(vol->v_obj)) )) {
270 /*retry with the right id */
274 adp = adl_lkup(vol, path, adp);
275 id = get_id(vol, adp, &path->st, path->d_dir->d_did, path->u_name, strlen(path->u_name));
280 /* save the id for getfilparm */
282 if (!(path->m_name = utompath(vol, path->u_name, id , utf8_encoding(vol->v_obj)))) {
288 /* Kind of optimization:
289 * -- first check things we've already have - filename
290 * -- last check things we get from ad_open()
291 * FIXME strmcp strstr (icase)
294 /* Check for filename */
295 if ((c1.rbitmap & (1<<DIRPBIT_LNAME))) {
296 if ( (size_t)(-1) == (len = convert_string(vol->v_maccharset, CH_UCS2, path->m_name, -1, convbuf, 512)) )
299 if ((c1.rbitmap & (1<<CATPBIT_PARTIAL))) {
300 if (strcasestr_w( (ucs2_t*) convbuf, (ucs2_t*) c1.lname) == NULL)
303 if (strcasecmp_w((ucs2_t*) convbuf, (ucs2_t*) c1.lname) != 0)
307 if ((c1.rbitmap & (1<<FILPBIT_PDINFO))) {
308 if ( (size_t)(-1) == (len = convert_charset( CH_UTF8_MAC, CH_UCS2, CH_UTF8, path->m_name, strlen(path->m_name), convbuf, 512, &flags))) {
312 if (c1.rbitmap & (1<<CATPBIT_PARTIAL)) {
313 if (strcasestr_w((ucs2_t *) convbuf, (ucs2_t*)c1.utf8name) == NULL)
316 if (strcasecmp_w((ucs2_t *)convbuf, (ucs2_t*)c1.utf8name) != 0)
322 if ((unsigned)c2.mdate > 0x7fffffff)
323 c2.mdate = 0x7fffffff;
324 if ((unsigned)c2.cdate > 0x7fffffff)
325 c2.cdate = 0x7fffffff;
326 if ((unsigned)c2.bdate > 0x7fffffff)
327 c2.bdate = 0x7fffffff;
329 /* Check for modification date */
330 if ((c1.rbitmap & (1<<DIRPBIT_MDATE))) {
331 if (path->st.st_mtime < c1.mdate || path->st.st_mtime > c2.mdate)
335 /* Check for creation date... */
336 if ((c1.rbitmap & (1<<DIRPBIT_CDATE))) {
337 c_date = path->st.st_mtime;
338 adp = adl_lkup(vol, path, adp);
339 if (adp && ad_getdate(adp, AD_DATE_CREATE, &ac_date) >= 0)
340 c_date = AD_DATE_TO_UNIX(ac_date);
342 if (c_date < c1.cdate || c_date > c2.cdate)
346 /* Check for backup date... */
347 if ((c1.rbitmap & (1<<DIRPBIT_BDATE))) {
348 b_date = path->st.st_mtime;
349 adp = adl_lkup(vol, path, adp);
350 if (adp && ad_getdate(adp, AD_DATE_BACKUP, &ab_date) >= 0)
351 b_date = AD_DATE_TO_UNIX(ab_date);
353 if (b_date < c1.bdate || b_date > c2.bdate)
357 /* Check attributes */
358 if ((c1.rbitmap & (1<<DIRPBIT_ATTR)) && c2.attr != 0) {
359 if ((adp = adl_lkup(vol, path, adp))) {
360 ad_getattr(adp, &attr);
361 if ((attr & c2.attr) != c1.attr)
367 /* Check file type ID */
368 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.f_type != 0) {
369 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
370 if (finfo->f_type != c1.finfo.f_type)
374 /* Check creator ID */
375 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.creator != 0) {
377 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
379 if (finfo->creator != c1.finfo.creator)
383 /* Check finder info attributes */
384 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.attrs != 0) {
386 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
389 if ((finfo->attrs & c2.finfo.attrs) != c1.finfo.attrs)
394 if ((c1.rbitmap & (1<<DIRPBIT_FINFO)) && c2.finfo.label != 0) {
396 finfo = unpack_finderinfo(vol, path, &adp, &finderinfo,islnk);
398 if ((finfo->label & c2.finfo.label) != c1.finfo.label)
401 /* FIXME: Attributes check ! */
403 /* All criteria are met. */
407 ad_close(adp, ADFLAGS_HF);
411 /* ------------------------------ */
412 static int rslt_add (const AFPObj *obj, struct vol *vol, struct path *path, char **buf, int ext)
419 int isdir = S_ISDIR(path->st.st_mode);
421 /* Skip resultsize */
423 p += sizeof(resultsize);
428 *p++ = isdir ? FILDIRBIT_ISDIR : FILDIRBIT_ISFILE; /* IsDir ? */
435 ret = getdirparams(obj, vol, c1.dbitmap, path, path->d_dir, p , &tbuf );
438 /* FIXME slow if we need the file ID, we already know it, done ? */
439 ret = getfilparams (obj, vol, c1.fbitmap, path, path->d_dir, p, &tbuf, 0);
445 /* Make sure entry length is even */
452 resultsize = htons(tbuf);
453 memcpy ( *buf, &resultsize, sizeof(resultsize) );
465 "./../.AppleDouble/.AppleDB/Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/.AppleDesktop/.Parent/"
468 * This function performs a filesystem search
470 * Uses globals c1, c2, the search criteria
472 * @param vol (r) volume we are searching on ...
473 * @param dir (rw) directory we are starting from ...
474 * @param rmatches (r) maximum number of matches we can return
475 * @param pos (r) position we've stopped recently
476 * @param rbuf (w) output buffer
477 * @param nrecs (w) number of matches
478 * @param rsize (w) length of data written to output buffer
479 * @param ext (r) extended search flag
481 #define NUM_ROUNDS 200
482 static int catsearch(const AFPObj *obj,
492 static uint32_t cur_pos; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */
493 static DIR *dirpos; /* UNIX structure describing currently opened directory. */
494 struct dir *currentdir; /* struct dir of current directory */
496 struct dirent *entry;
500 char *vpath = vol->v_path;
503 int num_rounds = NUM_ROUNDS;
508 if (*pos != 0 && *pos != cur_pos) {
509 result = AFPERR_CATCHNG;
513 /* FIXME: Category "offspring count ! */
516 /* We need to initialize all mandatory structures/variables and change working directory appropriate... */
519 if (dirpos != NULL) {
524 if (addstack(vpath, dir, -1) == -1) {
525 result = AFPERR_MISC;
528 /* FIXME: Sometimes DID is given by client ! (correct this one above !) */
531 /* Save current path */
532 if ((cwd = open(".", O_RDONLY)) < 0) {
533 result = AFPERR_MISC;
537 /* So we are beginning... */
538 start_time = time(NULL);
540 while ((cidx = reducestack()) != -1) {
541 if ((currentdir = dirlookup(vol, dstack[cidx].ds_did)) == NULL) {
542 result = AFPERR_MISC;
545 LOG(log_debug, logtype_afpd, "catsearch: current struct dir: \"%s\"", cfrombstr(currentdir->d_fullpath));
547 error = movecwd(vol, currentdir);
549 if (!error && dirpos == NULL)
550 dirpos = opendir(".");
553 dirpos = opendir(cfrombstr(currentdir->d_fullpath));
555 if (error || dirpos == NULL) {
558 dstack[cidx].ds_checked = 1;
563 result = AFPERR_NFILE;
568 result = AFPERR_MISC;
569 } /* switch (errno) */
574 while ((entry = readdir(dirpos)) != NULL) {
577 if (!check_dirent(vol, entry->d_name))
580 LOG(log_debug, logtype_afpd, "catsearch(\"%s\"): dirent: \"%s\"",
581 cfrombstr(currentdir->d_fullpath), entry->d_name);
583 memset(&path, 0, sizeof(path));
584 path.u_name = entry->d_name;
585 if (of_stat(vol, &path) != 0) {
596 result = AFPERR_MISC;
600 switch (S_IFMT & path.st.st_mode) {
602 /* here we can short cut
603 ie if in the same loop the parent dir wasn't in the cache
604 ALL dirsearch_byname will fail.
606 unlen = strlen(path.u_name);
607 path.d_dir = dircache_search_by_name(vol,
611 if (path.d_dir == NULL) {
612 /* path.m_name is set by adddir */
613 if ((path.d_dir = dir_add(vol,
617 result = AFPERR_MISC;
621 path.m_name = cfrombstr(path.d_dir->d_m_name);
623 if (addstack(path.u_name, path.d_dir, cidx) == -1) {
624 result = AFPERR_MISC;
629 path.d_dir = currentdir;
635 ccr = crit_check(vol, &path);
637 /* bit 0 means that criteria has been met */
639 r = rslt_add (obj, vol, &path, &rrbuf, ext);
642 result = AFPERR_MISC;
646 /* Number of matches limit */
648 goto catsearch_pause;
649 /* Block size limit */
650 if (rrbuf - rbuf >= 448)
651 goto catsearch_pause;
653 /* MacOS 9 doesn't like servers executing commands longer than few seconds */
654 if (--num_rounds <= 0) {
655 if (start_time != time(NULL)) {
657 goto catsearch_pause;
659 num_rounds = NUM_ROUNDS;
661 } /* while ((entry=readdir(dirpos)) != NULL) */
664 dstack[cidx].ds_checked = 1;
665 } /* while (current_idx = reducestack()) != -1) */
667 /* We have finished traversing our tree. Return EOF here. */
675 catsearch_end: /* Exiting catsearch: error condition */
676 *rsize = rrbuf - rbuf;
678 if ((fchdir(cwd)) != 0) {
679 LOG(log_debug, logtype_afpd, "error chdiring back: %s", strerror(errno));
687 * This function performs a CNID db search
689 * Uses globals c1, c2, the search criteria
691 * @param vol (r) volume we are searching on ...
692 * @param dir (rw) directory we are starting from ...
693 * @param uname (r) UNIX name of object to search
694 * @param rmatches (r) maximum number of matches we can return
695 * @param pos (r) position we've stopped recently
696 * @param rbuf (w) output buffer
697 * @param nrecs (w) number of matches
698 * @param rsize (w) length of data written to output buffer
699 * @param ext (r) extended search flag
701 static int catsearch_db(const AFPObj *obj,
712 static char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
713 static uint32_t cur_pos;
714 static int num_matches;
719 char buffer[MAXPATHLEN +2];
720 uint16_t flags = CONV_TOLOWER;
722 LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u, name: %s}",
723 *pos, cur_pos, uname);
725 if (*pos != 0 && *pos != cur_pos) {
726 result = AFPERR_CATCHNG;
730 if (cur_pos == 0 || *pos == 0) {
731 if (convert_charset(vol->v_volcharset,
738 &flags) == (size_t)-1) {
739 LOG(log_error, logtype_afpd, "catsearch_db: conversion error");
740 result = AFPERR_MISC;
744 LOG(log_debug, logtype_afpd, "catsearch_db: %s", buffer);
746 if ((num_matches = cnid_find(vol->v_cdb,
750 sizeof(resbuf))) == -1) {
751 result = AFPERR_MISC;
756 while (cur_pos < num_matches) {
759 char resolvebuf[12 + MAXPATHLEN + 1];
762 /* Next CNID to process from buffer */
763 memcpy(&cnid, resbuf + cur_pos * sizeof(cnid_t), sizeof(cnid_t));
766 if ((name = cnid_resolve(vol->v_cdb, &did, resolvebuf, 12 + MAXPATHLEN + 1)) == NULL)
768 LOG(log_debug, logtype_afpd, "catsearch_db: {pos: %u, name:%s, cnid: %u}",
769 cur_pos, name, ntohl(cnid));
770 if ((dir = dirlookup(vol, did)) == NULL)
772 if (movecwd(vol, dir) < 0 )
775 memset(&path, 0, sizeof(path));
777 path.m_name = utompath(vol, name, cnid, utf8_encoding(vol->v_obj));
779 if (of_stat(vol, &path) != 0) {
787 result = AFPERR_MISC;
791 /* For files path.d_dir is the parent dir, for dirs its the dir itself */
792 if (S_ISDIR(path.st.st_mode))
793 if ((dir = dirlookup(vol, cnid)) == NULL)
797 LOG(log_maxdebug, logtype_afpd,"catsearch_db: dir: %s, cwd: %s, name: %s",
798 cfrombstr(dir->d_fullpath), getcwdpath(), path.u_name);
800 /* At last we can check the search criteria */
801 ccr = crit_check(vol, &path);
803 LOG(log_debug, logtype_afpd,"catsearch_db: match: %s/%s",
804 getcwdpath(), path.u_name);
805 /* bit 1 means that criteria has been met */
806 r = rslt_add(obj, vol, &path, &rrbuf, ext);
808 result = AFPERR_MISC;
812 /* Number of matches limit */
814 goto catsearch_pause;
815 /* Block size limit */
816 if (rrbuf - rbuf >= 448)
817 goto catsearch_pause;
831 catsearch_end: /* Exiting catsearch: error condition */
832 *rsize = rrbuf - rbuf;
833 LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u}", *pos, cur_pos);
837 /* -------------------------- */
838 static int catsearch_afp(AFPObj *obj _U_, char *ibuf, size_t ibuflen,
839 char *rbuf, size_t *rbuflen, int ext)
844 uint32_t rmatches, reserved;
849 unsigned char *spec1, *spec2, *bspec1, *bspec2;
858 /* min header size */
863 memset(&c1, 0, sizeof(c1));
864 memset(&c2, 0, sizeof(c2));
867 memcpy(&vid, ibuf, sizeof(vid));
870 if ((vol = getvolbyvid(vid)) == NULL) {
874 memcpy(&rmatches, ibuf, sizeof(rmatches));
875 rmatches = ntohl(rmatches);
876 ibuf += sizeof(rmatches);
878 /* FIXME: (rl) should we check if reserved == 0 ? */
879 ibuf += sizeof(reserved);
881 memcpy(catpos, ibuf, sizeof(catpos));
882 ibuf += sizeof(catpos);
884 memcpy(&c1.fbitmap, ibuf, sizeof(c1.fbitmap));
885 c1.fbitmap = c2.fbitmap = ntohs(c1.fbitmap);
886 ibuf += sizeof(c1.fbitmap);
888 memcpy(&c1.dbitmap, ibuf, sizeof(c1.dbitmap));
889 c1.dbitmap = c2.dbitmap = ntohs(c1.dbitmap);
890 ibuf += sizeof(c1.dbitmap);
892 memcpy(&c1.rbitmap, ibuf, sizeof(c1.rbitmap));
893 c1.rbitmap = c2.rbitmap = ntohl(c1.rbitmap);
894 ibuf += sizeof(c1.rbitmap);
896 if (! (c1.fbitmap || c1.dbitmap)) {
897 return AFPERR_BITMAP;
901 memcpy(&spec_len, ibuf, sizeof(spec_len));
902 spec_len = ntohs(spec_len);
905 /* with catsearch only name and parent id are allowed */
906 c1.fbitmap &= (1<<FILPBIT_LNAME) | (1<<FILPBIT_PDID);
907 c1.dbitmap &= (1<<DIRPBIT_LNAME) | (1<<DIRPBIT_PDID);
908 spec_len = *(unsigned char*)ibuf;
911 /* Parse file specifications */
912 spec1 = (unsigned char*)ibuf;
913 spec2 = (unsigned char*)ibuf + spec_len + 2;
920 /* File attribute bits... */
921 if (c1.rbitmap & (1 << FILPBIT_ATTR)) {
922 memcpy(&c1.attr, spec1, sizeof(c1.attr));
923 spec1 += sizeof(c1.attr);
924 memcpy(&c2.attr, spec2, sizeof(c2.attr));
925 spec2 += sizeof(c1.attr);
929 if (c1.rbitmap & (1 << FILPBIT_PDID)) {
930 memcpy(&c1.pdid, spec1, sizeof(pdid));
931 spec1 += sizeof(c1.pdid);
932 memcpy(&c2.pdid, spec2, sizeof(pdid));
933 spec2 += sizeof(c2.pdid);
934 } /* FIXME: PDID - do we demarshall this argument ? */
937 if (c1.rbitmap & (1 << FILPBIT_CDATE)) {
938 memcpy(&c1.cdate, spec1, sizeof(c1.cdate));
939 spec1 += sizeof(c1.cdate);
940 c1.cdate = AD_DATE_TO_UNIX(c1.cdate);
941 memcpy(&c2.cdate, spec2, sizeof(c2.cdate));
942 spec2 += sizeof(c1.cdate);
943 ibuf += sizeof(c1.cdate);;
944 c2.cdate = AD_DATE_TO_UNIX(c2.cdate);
947 /* Modification date */
948 if (c1.rbitmap & (1 << FILPBIT_MDATE)) {
949 memcpy(&c1.mdate, spec1, sizeof(c1.mdate));
950 c1.mdate = AD_DATE_TO_UNIX(c1.mdate);
951 spec1 += sizeof(c1.mdate);
952 memcpy(&c2.mdate, spec2, sizeof(c2.mdate));
953 c2.mdate = AD_DATE_TO_UNIX(c2.mdate);
954 spec2 += sizeof(c1.mdate);
958 if (c1.rbitmap & (1 << FILPBIT_BDATE)) {
959 memcpy(&c1.bdate, spec1, sizeof(c1.bdate));
960 spec1 += sizeof(c1.bdate);
961 c1.bdate = AD_DATE_TO_UNIX(c1.bdate);
962 memcpy(&c2.bdate, spec2, sizeof(c2.bdate));
963 spec2 += sizeof(c2.bdate);
964 c1.bdate = AD_DATE_TO_UNIX(c2.bdate);
968 if (c1.rbitmap & (1 << FILPBIT_FINFO)) {
971 memcpy(buf, spec1, sizeof(buf));
972 unpack_buffer(&c1.finfo, buf);
973 spec1 += sizeof(buf);
975 memcpy(buf, spec2, sizeof(buf));
976 unpack_buffer(&c2.finfo, buf);
977 spec2 += sizeof(buf);
980 if ((c1.rbitmap & (1 << DIRPBIT_OFFCNT)) != 0) {
981 /* Offspring count - only directories */
982 if (c1.fbitmap == 0) {
983 memcpy(&c1.offcnt, spec1, sizeof(c1.offcnt));
984 spec1 += sizeof(c1.offcnt);
985 c1.offcnt = ntohs(c1.offcnt);
986 memcpy(&c2.offcnt, spec2, sizeof(c2.offcnt));
987 spec2 += sizeof(c2.offcnt);
988 c2.offcnt = ntohs(c2.offcnt);
990 else if (c1.dbitmap == 0) {
991 /* ressource fork length */
994 return AFPERR_BITMAP; /* error */
996 } /* Offspring count/ressource fork length */
999 if (c1.rbitmap & (1 << FILPBIT_LNAME)) {
1000 /* Get the long filename */
1001 memcpy(tmppath, bspec1 + spec1[1] + 1, (bspec1 + spec1[1])[0]);
1002 tmppath[(bspec1 + spec1[1])[0]]= 0;
1003 len = convert_string ( vol->v_maccharset, CH_UCS2, tmppath, -1, c1.lname, sizeof(c1.lname));
1004 if (len == (size_t)(-1))
1005 return AFPERR_PARAM;
1008 /* FIXME: do we need it ? It's always null ! */
1009 memcpy(c2.lname, bspec2 + spec2[1] + 1, (bspec2 + spec2[1])[0]);
1010 c2.lname[(bspec2 + spec2[1])[0]]= 0;
1014 if (c1.rbitmap & (1 << FILPBIT_PDINFO)) {
1017 memcpy(&namelen, spec1, sizeof(namelen));
1018 namelen = ntohs (namelen);
1020 spec1 = bspec1+namelen+4; /* Skip Unicode Hint */
1023 memcpy(&namelen, spec1, sizeof(namelen));
1024 namelen = ntohs (namelen);
1025 if (namelen > UTF8FILELEN_EARLY) /* Safeguard */
1026 namelen = UTF8FILELEN_EARLY;
1028 memcpy (c1.utf8name, spec1+2, namelen);
1029 c1.utf8name[namelen] = 0;
1030 if ((uname = mtoupath(vol, c1.utf8name, 0, utf8_encoding(obj))) == NULL)
1031 return AFPERR_PARAM;
1033 /* convert charset */
1034 flags = CONV_PRECOMPOSE;
1035 len = convert_charset(CH_UTF8_MAC, CH_UCS2, CH_UTF8, c1.utf8name, namelen, c1.utf8name, 512, &flags);
1036 if (len == (size_t)(-1))
1037 return AFPERR_PARAM;
1042 if ((c1.rbitmap & (1 << FILPBIT_PDINFO))
1043 && !(c1.rbitmap & (1<<CATPBIT_PARTIAL))
1044 && (strcmp(vol->v_cnidscheme, "dbd") == 0)
1045 && (vol->v_flags & AFPVOL_SEARCHDB))
1046 /* we've got a name and it's a dbd volume, so search CNID database */
1047 ret = catsearch_db(obj, vol, vol->v_root, uname, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
1049 /* perform a slow filesystem tree search */
1050 ret = catsearch(obj, vol, vol->v_root, rmatches, &catpos[0], rbuf+24, &nrecs, &rsize, ext);
1052 memcpy(rbuf, catpos, sizeof(catpos));
1053 rbuf += sizeof(catpos);
1055 c1.fbitmap = htons(c1.fbitmap);
1056 memcpy(rbuf, &c1.fbitmap, sizeof(c1.fbitmap));
1057 rbuf += sizeof(c1.fbitmap);
1059 c1.dbitmap = htons(c1.dbitmap);
1060 memcpy(rbuf, &c1.dbitmap, sizeof(c1.dbitmap));
1061 rbuf += sizeof(c1.dbitmap);
1063 nrecs = htonl(nrecs);
1064 memcpy(rbuf, &nrecs, sizeof(nrecs));
1065 rbuf += sizeof(nrecs);
1069 } /* catsearch_afp */
1071 /* -------------------------- */
1072 int afp_catsearch (AFPObj *obj, char *ibuf, size_t ibuflen,
1073 char *rbuf, size_t *rbuflen)
1075 return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 0);
1079 int afp_catsearch_ext (AFPObj *obj, char *ibuf, size_t ibuflen,
1080 char *rbuf, size_t *rbuflen)
1082 return catsearch_afp( obj, ibuf, ibuflen, rbuf, rbuflen, 1);
1085 /* FIXME: we need a clean separation between afp stubs and 'real' implementation */
1086 /* (so, all buffer packing/unpacking should be done in stub, everything else
1087 should be done in other functions) */