From b8ac0f7caf51e4a9a563ceb63f1acee5fa098049 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 22 May 2015 14:19:34 -0400 Subject: [PATCH] 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. --- src/app/rpmostree-compose-builtin-tree.c | 6 +++- src/libpriv/rpmostree-rpm-util.c | 19 +++-------- src/libpriv/rpmostree-util.c | 40 ++++++++++++++++++++++++ src/libpriv/rpmostree-util.h | 5 +++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/app/rpmostree-compose-builtin-tree.c b/src/app/rpmostree-compose-builtin-tree.c index 54406a86..282bccb7 100644 --- a/src/app/rpmostree-compose-builtin-tree.c +++ b/src/app/rpmostree-compose-builtin-tree.c @@ -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; diff --git a/src/libpriv/rpmostree-rpm-util.c b/src/libpriv/rpmostree-rpm-util.c index 7c656935..b2f724d9 100644 --- a/src/libpriv/rpmostree-rpm-util.c +++ b/src/libpriv/rpmostree-rpm-util.c @@ -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; } diff --git a/src/libpriv/rpmostree-util.c b/src/libpriv/rpmostree-util.c index e8e1ce96..50fb81b1 100644 --- a/src/libpriv/rpmostree-util.c +++ b/src/libpriv/rpmostree-util.c @@ -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, diff --git a/src/libpriv/rpmostree-util.h b/src/libpriv/rpmostree-util.h index 2aa80bce..bb55e9a2 100644 --- a/src/libpriv/rpmostree-util.h +++ b/src/libpriv/rpmostree-util.h @@ -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,