+/*!
+ * Resolve struct dir for an absolute path
+ *
+ * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
+ * a pointer to struct dir of "subdir".
+ * 1. Remove volue path from absolute path
+ * 2. start path
+ * 3. loop through all elements of the remaining path from 1.
+ * 4. we only allow dirs
+ * 5. search dircache
+ * 6. if not found in the dircache query the CNID database for the DID
+ * 7. and use dirlookup to resolve the DID to a it's struct dir *
+ *
+ * @param vol (r) volume the path is in, must be known
+ * @param path (r) absoule path
+ *
+ * @returns pointer to struct dir or NULL on error
+ */
+struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
+{
+ EC_INIT;
+
+ struct dir *dir = NULL;
+ cnid_t cnid, did;
+ bstring rpath = NULL;
+ bstring statpath = NULL;
+ struct bstrList *l = NULL;
+ struct stat st;
+
+ cnid = htonl(2);
+ dir = vol->v_root;
+
+ EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
+ EC_NULL(statpath = bfromcstr(vol->v_path)); /* 2. */
+
+ l = bsplit(rpath, '/');
+ for (int i = 0; i < l->qty ; i++) { /* 3. */
+ did = cnid;
+ EC_ZERO(bcatcstr(statpath, "/"));
+ EC_ZERO(bconcat(statpath, l->entry[i]));
+ EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
+ "lstat(rpath: %s, elem: %s): %s: %s",
+ cfrombstr(rpath), cfrombstr(l->entry[i]),
+ cfrombstr(statpath), strerror(errno));
+
+ if (!(S_ISDIR(st.st_mode))) /* 4. */
+ EC_FAIL;
+
+ if ((dir = dircache_search_by_name(vol, /* 5. */
+ dir,
+ cfrombstr(l->entry[i]),
+ blength(l->entry[i]))) == NULL) {
+ if ((cnid = cnid_add(vol->v_cdb, /* 6. */
+ &st,
+ did,
+ cfrombstr(l->entry[i]),
+ blength(l->entry[i]),
+ 0)) == CNID_INVALID) {
+ EC_FAIL;
+ }
+
+ if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
+ EC_FAIL;
+ }
+ }
+
+EC_CLEANUP:
+ bdestroy(rpath);
+ bstrListDestroy(l);
+ bdestroy(statpath);
+ if (ret != 0)
+ return NULL;
+
+ return dir;
+}
+