From 303320163f2365db9fea67f64dcc1578e034116e Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 6 Sep 2017 11:37:02 -0400 Subject: [PATCH] tree-wide: Use helpers for unlinkat() We have `ot_ensure_unlinked_at()` for the "ignore ENOENT" case, and `glnx_unlinkat()` otherwise. Port all in-tree callers to one or the other as appropriate. Just noticed an unprefixed error in the refs case and decided to do a tree-wide check. Closes: #1142 Approved by: jlebon --- src/libostree/ostree-repo-checkout.c | 7 ++----- src/libostree/ostree-repo-prune.c | 12 +++--------- src/libostree/ostree-repo-pull.c | 7 ++----- src/libostree/ostree-repo-refs.c | 7 ++----- src/libostree/ostree-repo.c | 18 ++++++------------ src/libotutil/ot-fs-utils.c | 1 + 6 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/libostree/ostree-repo-checkout.c b/src/libostree/ostree-repo-checkout.c index 4fbbee7d..dbdbf058 100644 --- a/src/libostree/ostree-repo-checkout.c +++ b/src/libostree/ostree-repo-checkout.c @@ -1121,11 +1121,8 @@ ostree_repo_checkout_gc (OstreeRepo *self, if (stbuf.st_nlink == 1) { - if (unlinkat (dfd_iter.fd, dent->d_name, 0) != 0) - { - glnx_set_error_from_errno (error); - return FALSE; - } + if (!glnx_unlinkat (dfd_iter.fd, dent->d_name, 0, error)) + return FALSE; } } } diff --git a/src/libostree/ostree-repo-prune.c b/src/libostree/ostree-repo-prune.c index 2b596ecb..6ea899bc 100644 --- a/src/libostree/ostree-repo-prune.c +++ b/src/libostree/ostree-repo-prune.c @@ -43,13 +43,7 @@ prune_commitpartial_file (OstreeRepo *repo, GError **error) { g_autofree char *path = _ostree_get_commitpartial_path (checksum); - if (unlinkat (repo->repo_dir_fd, path, 0) != 0) - { - if (errno != ENOENT) - return glnx_throw_errno_prefix (error, "unlinkat"); - } - - return TRUE; + return ot_ensure_unlinked_at (repo->repo_dir_fd, path, error); } static gboolean @@ -147,8 +141,8 @@ _ostree_repo_prune_tmp (OstreeRepo *self, if (has_sig_suffix) dent->d_name[len - 4] = '.'; - if (unlinkat (dfd_iter.fd, dent->d_name, 0) < 0) - return glnx_throw_errno_prefix (error, "unlinkat"); + if (!glnx_unlinkat (dfd_iter.fd, dent->d_name, 0, error)) + return FALSE; } } diff --git a/src/libostree/ostree-repo-pull.c b/src/libostree/ostree-repo-pull.c index 98fb96b4..d233b384 100644 --- a/src/libostree/ostree-repo-pull.c +++ b/src/libostree/ostree-repo-pull.c @@ -1357,11 +1357,8 @@ static_deltapart_fetch_on_complete (GObject *object, goto out; /* From here on, if we fail to apply the delta, we'll re-fetch it */ - if (unlinkat (_ostree_fetcher_get_dfd (fetcher), temp_path, 0) < 0) - { - glnx_set_error_from_errno (error); - goto out; - } + if (!glnx_unlinkat (_ostree_fetcher_get_dfd (fetcher), temp_path, 0, error)) + goto out; in = g_unix_input_stream_new (fd, FALSE); diff --git a/src/libostree/ostree-repo-refs.c b/src/libostree/ostree-repo-refs.c index f8af3c43..b9b00896 100644 --- a/src/libostree/ostree-repo-refs.c +++ b/src/libostree/ostree-repo-refs.c @@ -1007,11 +1007,8 @@ _ostree_repo_write_ref (OstreeRepo *self, { if (dfd >= 0) { - if (unlinkat (dfd, ref->ref_name, 0) != 0) - { - if (errno != ENOENT) - return glnx_throw_errno (error); - } + if (!ot_ensure_unlinked_at (dfd, ref->ref_name, error)) + return FALSE; } } else if (rev != NULL) diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index 2226e8a5..e7807d11 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -3256,15 +3256,12 @@ ostree_repo_delete_object (OstreeRepo *self, _ostree_loose_path (meta_loose, sha256, OSTREE_OBJECT_TYPE_COMMIT_META, self->mode); - if (TEMP_FAILURE_RETRY (unlinkat (self->objects_dir_fd, meta_loose, 0)) < 0) - { - if (G_UNLIKELY (errno != ENOENT)) - return glnx_throw_errno_prefix (error, "unlinkat(%s)", meta_loose); - } + if (!ot_ensure_unlinked_at (self->objects_dir_fd, meta_loose, error)) + return FALSE; } - if (TEMP_FAILURE_RETRY (unlinkat (self->objects_dir_fd, loose_path, 0)) < 0) - return glnx_throw_errno_prefix (error, "Deleting object %s.%s", sha256, ostree_object_type_to_string (objtype)); + if (!glnx_unlinkat (self->objects_dir_fd, loose_path, 0, error)) + return glnx_prefix_error (error, "Deleting object %s.%s", sha256, ostree_object_type_to_string (objtype)); /* If the repository is configured to use tombstone commits, create one when deleting a commit. */ if (objtype == OSTREE_OBJECT_TYPE_COMMIT) @@ -5036,11 +5033,8 @@ ostree_repo_regenerate_summary (OstreeRepo *self, error)) return FALSE; - if (unlinkat (self->repo_dir_fd, "summary.sig", 0) < 0) - { - if (errno != ENOENT) - return glnx_throw_errno_prefix (error, "unlinkat"); - } + if (!ot_ensure_unlinked_at (self->repo_dir_fd, "summary.sig", error)) + return FALSE; return TRUE; } diff --git a/src/libotutil/ot-fs-utils.c b/src/libotutil/ot-fs-utils.c index 2f0ae19c..9100b85b 100644 --- a/src/libotutil/ot-fs-utils.c +++ b/src/libotutil/ot-fs-utils.c @@ -90,6 +90,7 @@ ot_openat_read_stream (int dfd, return TRUE; } +/* Like unlinkat() but ignore ENOENT */ gboolean ot_ensure_unlinked_at (int dfd, const char *path,