+ return path;
+}
+
+/* Given a 'path' (relative to the database path) return the document
+ * ID of the directory document corresponding to the parent directory
+ * of 'path' in 'parent_id'.
+ *
+ * The original 'path' can represent either a regular file or a
+ * directory, (in either case, the document ID of the parent will be
+ * returned). Trailing slashes on 'path' will be ignored, and any
+ * cases of multiple '/' characters appearing in series will be
+ * treated as a single '/'.
+ *
+ * If no directory document exists in the database for the parent, (or
+ * for any of its parents up to the top-level database path), then
+ * directory documents will be created for these (each with an mtime
+ * of 0).
+ *
+ * Return value:
+ *
+ * NOTMUCH_STATUS_SUCCESS: Valid value available in parent_id.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
+ * occurred and parent_id will be set to (unsigned) -1.
+ */
+notmuch_status_t
+_notmuch_database_find_parent_id (notmuch_database_t *notmuch,
+ const char *path,
+ unsigned int *parent_id)
+{
+ const char *slash, *parent_db_path;
+ char *parent_path;
+ notmuch_private_status_t private_status;
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+
+ if (path == NULL || *path == '\0') {
+ *parent_id = 0;
+ return NOTMUCH_STATUS_SUCCESS;
+ }
+
+ /* Find the last slash (not counting a trailing slash), if any. */
+
+ slash = path + strlen (path) - 1;
+
+ /* First, skip trailing slashes. */
+ while (slash != path) {
+ if (*slash != '/')
+ break;
+
+ --slash;
+ }
+
+ /* Then, find a slash. */
+ while (slash != path) {
+ if (*slash == '/')
+ break;
+
+ --slash;
+ }
+
+ /* Finally, skip multiple slashes. */
+ while (slash != path) {
+ if (*slash != '/')
+ break;
+
+ --slash;
+ }
+
+ if (slash == path)
+ parent_path = talloc_strdup (notmuch, "");
+ else
+ parent_path = talloc_strndup (notmuch, path, slash - path + 1);
+
+ parent_db_path = directory_db_path (parent_path);
+
+ private_status = find_unique_doc_id (notmuch, "directory",
+ parent_db_path, parent_id);
+ if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
+ status = notmuch_database_set_directory_mtime (notmuch,
+ parent_path, 0);
+ if (status)
+ return status;
+ private_status = find_unique_doc_id (notmuch, "directory",
+ parent_db_path, parent_id);
+ status = COERCE_STATUS (private_status, "_find_parent_id");
+ }
+
+ if (parent_db_path != parent_path)
+ free ((char *) parent_db_path);
+
+ talloc_free (parent_path);
+
+ if (status)
+ *parent_id = -1;
+
+ return status;
+}
+
+/* Given a legal 'path' for the database, return the relative path.
+ *
+ * The return value will be a pointer to the originl path contents,
+ * and will be either the original string (if 'path' was relative) or
+ * a portion of the string (if path was absolute and begins with the
+ * database path).
+ */
+const char *
+_notmuch_database_relative_path (notmuch_database_t *notmuch,
+ const char *path)
+{
+ const char *db_path, *relative;
+ unsigned int db_path_len;
+
+ db_path = notmuch_database_get_path (notmuch);
+ db_path_len = strlen (db_path);
+
+ relative = path;
+
+ if (*relative == '/') {
+ while (*relative == '/' && *(relative+1) == '/')
+ relative++;
+
+ if (strncmp (relative, db_path, db_path_len) == 0)
+ {
+ relative += db_path_len;
+ while (*relative == '/')
+ relative++;
+ }
+ }
+
+ return relative;