--- This will be used for downloading sync directories into a temporary location so they can be validated before replacing exisiting sync dbs. lib/libalpm/trans.c | 11 +---------- lib/libalpm/util.c | 28 ++++++++++++++++++++++++++++ lib/libalpm/util.h | 1 + 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index 14f7d5e3..e4c8e404 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -353,16 +353,7 @@ int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath, strcpy(arg1, "-c"); /* create a directory in $root/tmp/ for copying/extracting the scriptlet */ - len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1; - MALLOC(tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); - snprintf(tmpdir, len, "%stmp/", handle->root); - if(access(tmpdir, F_OK) != 0) { - _alpm_makepath_mode(tmpdir, 01777); - } - snprintf(tmpdir, len, "%stmp/alpm_XXXXXX", handle->root); - if(mkdtemp(tmpdir) == NULL) { - _alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n")); - free(tmpdir); + if((len = _alpm_mkdtemp(handle, &tmpdir)) == 0) { return 1; } diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index d12a4403..0ec9f650 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -142,6 +142,34 @@ done: return ret; } + +/** Creates a temporary directory of form /$root/tmp/alpm_XXXXXX. + * @param handle the context handle + * @param tmpdir pointer to storage of created directory path + * @return path length on success, 0 on error + */ +int _alpm_mkdtemp(alpm_handle_t *handle, char **tmpdir) +{ + size_t len; + + ASSERT(tmpdir, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, 0)); + + len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1; + MALLOC(*tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, 0)); + snprintf(*tmpdir, len, "%stmp/", handle->root); + if(access(*tmpdir, F_OK) != 0) { + _alpm_makepath_mode(*tmpdir, 01777); + } + snprintf(*tmpdir, len, "%stmp/alpm_XXXXXX", handle->root); + if(mkdtemp(*tmpdir) == NULL) { + _alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n")); + free(tmpdir); + return 0; + } + + return len; +} + /** Copies a file. * @param src file path to copy from * @param dest file path to copy to diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 71dadc2c..12c7fbc7 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -111,6 +111,7 @@ struct archive_read_buffer { int _alpm_makepath(const char *path); int _alpm_makepath_mode(const char *path, mode_t mode); +int _alpm_mkdtemp(alpm_handle_t *handle, char **tmpdir); int _alpm_copyfile(const char *src, const char *dest); size_t _alpm_strip_newline(char *str, size_t len); -- 2.24.1