libpriv: Introduce an rpmostree_mkdtemp()

- Can also give you a file descriptor
 - Takes a constant string as input, returning a mutated string as a
   separate variable which means that one can check whether the variable
   is `NULL` to know whether or not one needs to `rm -rf` it on error
   paths.
This commit is contained in:
Colin Walters 2015-05-22 14:19:34 -04:00
parent bfe801b77e
commit b8ac0f7caf
4 changed files with 55 additions and 15 deletions

View File

@ -648,7 +648,11 @@ rpmostree_compose_builtin_tree (int argc,
}
else
{
gs_free char *tmpd = g_mkdtemp (g_strdup ("/var/tmp/rpm-ostree.XXXXXX"));
gs_free char *tmpd = NULL;
if (!rpmostree_mkdtemp ("/var/tmp/rpm-ostree.XXXXXX", &tmpd, NULL, error))
goto out;
self->workdir = g_file_new_for_path (tmpd);
workdir_is_tmp = TRUE;

View File

@ -710,24 +710,16 @@ rpmostree_checkout_only_rpmdb_tempdir (OstreeRepo *repo,
{
gboolean ret = FALSE;
g_autofree char *commit = NULL;
g_autofree char *tempdir = g_strdup ("/tmp/rpmostree-dbquery-XXXXXXXX");
gboolean created_tmpdir = FALSE;
g_autofree char *tempdir = NULL;
OstreeRepoCheckoutOptions checkout_options = { 0, };
glnx_fd_close int tempdir_dfd = -1;
g_return_val_if_fail (out_tempdir != NULL, FALSE);
if (!ostree_repo_resolve_rev (repo, ref, FALSE, &commit, error))
if (!rpmostree_mkdtemp ("/tmp/rpmostree-dbquery-XXXXXX", &tempdir, &tempdir_dfd, error))
goto out;
if (mkdtemp (tempdir) == NULL)
{
glnx_set_error_from_errno (error);
goto out;
}
created_tmpdir = TRUE;
if (!glnx_opendirat (AT_FDCWD, tempdir, FALSE, &tempdir_dfd, error))
if (!ostree_repo_resolve_rev (repo, ref, FALSE, &commit, error))
goto out;
/* Create intermediate dirs */
@ -753,8 +745,7 @@ rpmostree_checkout_only_rpmdb_tempdir (OstreeRepo *repo,
goto out;
}
*out_tempdir = tempdir;
tempdir = NULL;
*out_tempdir = g_steal_pointer (&tempdir);
if (out_tempdir_dfd)
{
*out_tempdir_dfd = tempdir_dfd;
@ -762,7 +753,7 @@ rpmostree_checkout_only_rpmdb_tempdir (OstreeRepo *repo,
}
ret = TRUE;
out:
if (created_tmpdir && tempdir)
if (tempdir)
(void) glnx_shutil_rm_rf_at (AT_FDCWD, tempdir, NULL, NULL);
return ret;
}

View File

@ -28,6 +28,7 @@
#include "rpmostree-util.h"
#include "libgsystem.h"
#include "libglnx.h"
void
_rpmostree_set_error_from_errno (GError **error,
@ -65,6 +66,45 @@ _rpmostree_perror_fatal (const char *message)
exit (1);
}
gboolean
rpmostree_mkdtemp (const char *template,
char **out_tmpdir,
int *out_tmpdir_dfd, /* allow-none */
GError **error)
{
gboolean ret = FALSE;
g_autofree char *tmpdir = g_strdup (template);
gboolean created_tmpdir = FALSE;
glnx_fd_close int ret_tmpdir_dfd = -1;
if (mkdtemp (tmpdir) == NULL)
{
glnx_set_error_from_errno (error);
goto out;
}
created_tmpdir = TRUE;
if (out_tmpdir_dfd)
{
if (!glnx_opendirat (AT_FDCWD, tmpdir, FALSE, &ret_tmpdir_dfd, error))
goto out;
}
ret = TRUE;
*out_tmpdir = g_steal_pointer (&tmpdir);
if (out_tmpdir_dfd)
{
*out_tmpdir_dfd = ret_tmpdir_dfd;
ret_tmpdir_dfd = -1;
}
out:
if (created_tmpdir && tmpdir)
{
(void) glnx_shutil_rm_rf_at (AT_FDCWD, tmpdir, NULL, NULL);
}
return ret;
}
gboolean
_rpmostree_util_enumerate_directory_allow_noent (GFile *dirpath,
const char *queryargs,

View File

@ -37,6 +37,11 @@ _rpmostree_set_prefix_error_from_errno (GError **error,
void _rpmostree_perror_fatal (const char *message) __attribute__ ((noreturn));
gboolean rpmostree_mkdtemp (const char *template,
char **out_tmpdir,
int *out_tmpdir_dfd, /* allow-none */
GError **error);
gboolean
_rpmostree_util_enumerate_directory_allow_noent (GFile *dirpath,
const char *queryargs,