lib/open: refactor call to mkdir into function

This makes the error handling available for re-use. Using
g_mkdir_with_parents also handles the case of a pre-existing
directory. This introduces new functionality, namely creating the
parent directories, which will be useful for creating directories like
'.local/share/notmuch/default'.
This commit is contained in:
David Bremner 2022-07-28 06:41:02 -04:00
parent 44c9338061
commit 25e2790e30

View file

@ -244,6 +244,18 @@ _choose_database_path (notmuch_database_t *notmuch,
return NOTMUCH_STATUS_SUCCESS; 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 * static notmuch_database_t *
_alloc_notmuch (const char *database_path, const char *config_path, const char *profile) _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
{ {
@ -607,7 +619,6 @@ notmuch_database_create_with_config (const char *database_path,
const char *notmuch_path = NULL; const char *notmuch_path = NULL;
char *message = NULL; char *message = NULL;
GKeyFile *key_file = NULL; GKeyFile *key_file = NULL;
int err;
_notmuch_init (); _notmuch_init ();
@ -653,15 +664,9 @@ notmuch_database_create_with_config (const char *database_path,
goto DONE; goto DONE;
} }
err = mkdir (notmuch_path, 0755); status = _mkdir (notmuch_path, &message);
if (err) { if (status)
if (errno != EEXIST) { goto DONE;
IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
notmuch_path, strerror (errno)));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
}
} }
if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) { if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {