]> git.notmuchmail.org Git - notmuch/blobdiff - lib/open.cc
lib/open: return non-SUCCESS on missing database path
[notmuch] / lib / open.cc
index 4e5d25451aa7a6bb31b190556fe1de9cdcc2043f..bc45055523588d7ff6bf58b381b9b06274704659 100644 (file)
@@ -190,9 +190,10 @@ _choose_database_path (notmuch_database_t *notmuch,
                       const char *profile,
                       GKeyFile *key_file,
                       const char **database_path,
-                      bool *split,
                       char **message)
 {
+    notmuch_status_t status;
+
     if (! *database_path) {
        *database_path = getenv ("NOTMUCH_DATABASE");
     }
@@ -208,14 +209,12 @@ _choose_database_path (notmuch_database_t *notmuch,
        }
     }
     if (! *database_path) {
-       notmuch_status_t status;
-
        *database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
        status = _db_dir_exists (*database_path, message);
        if (status) {
            *database_path = NULL;
        } else {
-           *split = true;
+           notmuch->params |= NOTMUCH_PARAM_SPLIT;
        }
     }
 
@@ -224,8 +223,6 @@ _choose_database_path (notmuch_database_t *notmuch,
     }
 
     if (! *database_path) {
-       notmuch_status_t status;
-
        *database_path = talloc_asprintf (notmuch, "%s/mail", getenv ("HOME"));
        status = _db_dir_exists (*database_path, message);
        if (status) {
@@ -242,9 +239,30 @@ _choose_database_path (notmuch_database_t *notmuch,
        *message = strdup ("Error: Database path must be absolute.\n");
        return NOTMUCH_STATUS_PATH_ERROR;
     }
+
+    status = _db_dir_exists (*database_path, message);
+    if (status) {
+       IGNORE_RESULT (asprintf (message,
+                                "Error: database path '%s' does not exist or is not a directory.\n",
+                                *database_path));
+       return NOTMUCH_STATUS_NO_DATABASE;
+    }
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
+static notmuch_status_t
+_mkdir (const char *path, char **message)
+{
+    if (g_mkdir_with_parents (path, 0755)) {
+       IGNORE_RESULT (asprintf (message, "Error: Cannot create directory %s: %s.\n",
+                                path, strerror (errno)));
+       return NOTMUCH_STATUS_FILE_ERROR;
+    }
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+
 static notmuch_database_t *
 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
 {
@@ -513,7 +531,6 @@ notmuch_database_open_with_config (const char *database_path,
     notmuch_database_t *notmuch = NULL;
     char *message = NULL;
     GKeyFile *key_file = NULL;
-    bool split = false;
 
     _notmuch_init ();
 
@@ -530,7 +547,7 @@ notmuch_database_open_with_config (const char *database_path,
     }
 
     if ((status = _choose_database_path (notmuch, profile, key_file,
-                                        &database_path, &split,
+                                        &database_path,
                                         &message)))
        goto DONE;
 
@@ -609,8 +626,6 @@ notmuch_database_create_with_config (const char *database_path,
     const char *notmuch_path = NULL;
     char *message = NULL;
     GKeyFile *key_file = NULL;
-    int err;
-    bool split = false;
 
     _notmuch_init ();
 
@@ -627,27 +642,24 @@ notmuch_database_create_with_config (const char *database_path,
     }
 
     if ((status = _choose_database_path (notmuch, profile, key_file,
-                                        &database_path, &split, &message)))
-       goto DONE;
-
-    status = _db_dir_exists (database_path, &message);
-    if (status)
+                                        &database_path, &message)))
        goto DONE;
 
     _set_database_path (notmuch, database_path);
 
-    if (key_file && ! split) {
+    if (key_file && ! (notmuch->params & NOTMUCH_PARAM_SPLIT)) {
        char *mail_root = notmuch_canonicalize_file_name (
            g_key_file_get_string (key_file, "database", "mail_root", NULL));
        char *db_path = notmuch_canonicalize_file_name (database_path);
 
-       split = (mail_root && (0 != strcmp (mail_root, db_path)));
+       if (mail_root && (0 != strcmp (mail_root, db_path)))
+           notmuch->params |= NOTMUCH_PARAM_SPLIT;
 
        free (mail_root);
        free (db_path);
     }
 
-    if (split) {
+    if (notmuch->params & NOTMUCH_PARAM_SPLIT) {
        notmuch_path = database_path;
     } else {
        if (! (notmuch_path = talloc_asprintf (notmuch, "%s/%s", database_path, ".notmuch"))) {
@@ -655,15 +667,9 @@ notmuch_database_create_with_config (const char *database_path,
            goto DONE;
        }
 
-       err = mkdir (notmuch_path, 0755);
-       if (err) {
-           if (errno != EEXIST) {
-               IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
-                                        notmuch_path, strerror (errno)));
-               status = NOTMUCH_STATUS_FILE_ERROR;
-               goto DONE;
-           }
-       }
+       status = _mkdir (notmuch_path, &message);
+       if (status)
+           goto DONE;
     }
 
     if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
@@ -805,7 +811,6 @@ notmuch_database_load_config (const char *database_path,
     notmuch_database_t *notmuch = NULL;
     char *message = NULL;
     GKeyFile *key_file = NULL;
-    bool split = false;
 
     _notmuch_init ();
 
@@ -828,7 +833,7 @@ notmuch_database_load_config (const char *database_path,
     }
 
     status = _choose_database_path (notmuch, profile, key_file,
-                                   &database_path, &split, &message);
+                                   &database_path, &message);
     switch (status) {
     case NOTMUCH_STATUS_NO_DATABASE:
     case NOTMUCH_STATUS_SUCCESS: