X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=lib%2Fopen.cc;h=069c0664de35f0e3ae7364b3085c1c60c512310b;hb=82aa3f4d2762cd56585369de5fa17cb50d77c85d;hp=6e46168a78d20a8418e3b7535c4a464c7813aa87;hpb=793d4305d3563082761a59ac418ce827d00bdc39;p=notmuch diff --git a/lib/open.cc b/lib/open.cc index 6e46168a..069c0664 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -192,6 +192,52 @@ _choose_database_path (void *ctx, return NOTMUCH_STATUS_SUCCESS; } +notmuch_database_t * +_alloc_notmuch () +{ + notmuch_database_t *notmuch; + + notmuch = talloc_zero (NULL, notmuch_database_t); + if (! notmuch) + return NULL; + + notmuch->exception_reported = false; + notmuch->status_string = NULL; + notmuch->writable_xapian_db = NULL; + notmuch->atomic_nesting = 0; + notmuch->view = 1; + return notmuch; +} + +static void +_set_database_path (notmuch_database_t *notmuch, + const char *database_path) +{ + char *path = talloc_strdup (notmuch, database_path); + + strip_trailing (path, '/'); + + _notmuch_config_cache (notmuch, NOTMUCH_CONFIG_DATABASE_PATH, path); +} + +static void +_init_libs () +{ + + static int initialized = 0; + + /* Initialize the GLib type system and threads */ +#if ! GLIB_CHECK_VERSION (2, 35, 1) + g_type_init (); +#endif + + /* Initialize gmime */ + if (! initialized) { + g_mime_init (); + initialized = 1; + } +} + notmuch_status_t notmuch_database_open_with_config (const char *database_path, notmuch_database_mode_t mode, @@ -209,12 +255,21 @@ notmuch_database_open_with_config (const char *database_path, int err; unsigned int version; GKeyFile *key_file = NULL; - static int initialized = 0; - if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path, - &message))) + _init_libs (); + + notmuch = _alloc_notmuch (); + if (! notmuch) { + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } + + if ((status = _choose_database_path (local, config_path, profile, + &key_file, &database_path, &message))) goto DONE; + _set_database_path (notmuch, database_path); + if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) { message = strdup ("Out of memory\n"); status = NOTMUCH_STATUS_OUT_OF_MEMORY; @@ -229,28 +284,6 @@ notmuch_database_open_with_config (const char *database_path, goto DONE; } - /* Initialize the GLib type system and threads */ -#if ! GLIB_CHECK_VERSION (2, 35, 1) - g_type_init (); -#endif - - /* Initialize gmime */ - if (! initialized) { - g_mime_init (); - initialized = 1; - } - - notmuch = talloc_zero (NULL, notmuch_database_t); - notmuch->exception_reported = false; - notmuch->status_string = NULL; - notmuch->path = talloc_strdup (notmuch, database_path); - - strip_trailing (notmuch->path, '/'); - - notmuch->writable_xapian_db = NULL; - notmuch->atomic_nesting = 0; - notmuch->view = 1; - if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) { message = strdup ("Out of memory\n"); status = NOTMUCH_STATUS_OUT_OF_MEMORY; @@ -442,8 +475,10 @@ notmuch_database_create_with_config (const char *database_path, int err; void *local = talloc_new (NULL); - if ((status = _choose_database_path (local, config_path, profile, &key_file, &database_path, - &message))) + _init_libs (); + + if ((status = _choose_database_path (local, config_path, profile, + &key_file, &database_path, &message))) goto DONE; err = stat (database_path, &st);