+/*!
+ * This function performs a CNID db search
+ *
+ * Uses globals c1, c2, the search criteria
+ *
+ * @param vol (r) volume we are searching on ...
+ * @param dir (rw) directory we are starting from ...
+ * @param uname (r) UNIX name of object to search
+ * @param rmatches (r) maximum number of matches we can return
+ * @param pos (r) position we've stopped recently
+ * @param rbuf (w) output buffer
+ * @param nrecs (w) number of matches
+ * @param rsize (w) length of data written to output buffer
+ * @param ext (r) extended search flag
+ */
+static int catsearch_db(const AFPObj *obj,
+ struct vol *vol,
+ struct dir *dir,
+ const char *uname,
+ int rmatches,
+ uint32_t *pos,
+ char *rbuf,
+ uint32_t *nrecs,
+ int *rsize,
+ int ext)
+{
+ static char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
+ static uint32_t cur_pos;
+ static int num_matches;
+ int ccr ,r;
+ int result = AFP_OK;
+ struct path path;
+ char *rrbuf = rbuf;
+ char buffer[MAXPATHLEN +2];
+ uint16_t flags = CONV_TOLOWER;
+
+ LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u, name: %s}",
+ *pos, cur_pos, uname);
+
+ if (*pos != 0 && *pos != cur_pos) {
+ result = AFPERR_CATCHNG;
+ goto catsearch_end;
+ }
+
+ if (cur_pos == 0 || *pos == 0) {
+ if (convert_charset(vol->v_volcharset,
+ vol->v_volcharset,
+ vol->v_maccharset,
+ uname,
+ strlen(uname),
+ buffer,
+ MAXPATHLEN,
+ &flags) == (size_t)-1) {
+ LOG(log_error, logtype_afpd, "catsearch_db: conversion error");
+ result = AFPERR_MISC;
+ goto catsearch_end;
+ }
+
+ LOG(log_debug, logtype_afpd, "catsearch_db: %s", buffer);
+
+ if ((num_matches = cnid_find(vol->v_cdb,
+ buffer,
+ strlen(uname),
+ resbuf,
+ sizeof(resbuf))) == -1) {
+ result = AFPERR_MISC;
+ goto catsearch_end;
+ }
+ }
+
+ while (cur_pos < num_matches) {
+ char *name;
+ cnid_t cnid, did;
+ char resolvebuf[12 + MAXPATHLEN + 1];
+ struct dir *dir;
+
+ /* Next CNID to process from buffer */
+ memcpy(&cnid, resbuf + cur_pos * sizeof(cnid_t), sizeof(cnid_t));
+ did = cnid;
+
+ if ((name = cnid_resolve(vol->v_cdb, &did, resolvebuf, 12 + MAXPATHLEN + 1)) == NULL)
+ goto next;
+ LOG(log_debug, logtype_afpd, "catsearch_db: {pos: %u, name:%s, cnid: %u}",
+ cur_pos, name, ntohl(cnid));
+ if ((dir = dirlookup(vol, did)) == NULL)
+ goto next;
+ if (movecwd(vol, dir) < 0 )
+ goto next;
+
+ memset(&path, 0, sizeof(path));
+ path.u_name = name;
+ path.m_name = utompath(vol, name, cnid, utf8_encoding(vol->v_obj));
+
+ if (of_stat(&path) != 0) {
+ switch (errno) {
+ case EACCES:
+ case ELOOP:
+ goto next;
+ case ENOENT:
+
+ default:
+ result = AFPERR_MISC;
+ goto catsearch_end;
+ }
+ }
+ /* For files path.d_dir is the parent dir, for dirs its the dir itself */
+ if (S_ISDIR(path.st.st_mode))
+ if ((dir = dirlookup(vol, cnid)) == NULL)
+ goto next;
+ path.d_dir = dir;
+
+ LOG(log_maxdebug, logtype_afpd,"catsearch_db: dir: %s, cwd: %s, name: %s",
+ cfrombstr(dir->d_fullpath), getcwdpath(), path.u_name);
+
+ /* At last we can check the search criteria */
+ ccr = crit_check(vol, &path);
+ if ((ccr & 1)) {
+ LOG(log_debug, logtype_afpd,"catsearch_db: match: %s/%s",
+ getcwdpath(), path.u_name);
+ /* bit 1 means that criteria has been met */
+ r = rslt_add(obj, vol, &path, &rrbuf, ext);
+ if (r == 0) {
+ result = AFPERR_MISC;
+ goto catsearch_end;
+ }
+ *nrecs += r;
+ /* Number of matches limit */
+ if (--rmatches == 0)
+ goto catsearch_pause;
+ /* Block size limit */
+ if (rrbuf - rbuf >= 448)
+ goto catsearch_pause;
+ }
+ next:
+ cur_pos++;
+ } /* while */
+
+ /* finished */
+ result = AFPERR_EOF;
+ cur_pos = 0;
+ goto catsearch_end;
+
+catsearch_pause:
+ *pos = cur_pos;
+
+catsearch_end: /* Exiting catsearch: error condition */
+ *rsize = rrbuf - rbuf;
+ LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u}", *pos, cur_pos);
+ return result;
+}
+