From 3a03a35071f9ac56de2345f5f496c662e04d8029 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 3 Jun 2016 10:40:56 -0400 Subject: [PATCH] lib: Add `_ALLOW_NOENT` flag to internal variant mapping API We have a lot of "allow_noent" type wrapper functions since a common pattern is to allow files to not exist, but still throw cleanly on other issues. This is another instance of that, and cleans up duplicated error handling code. Part of this is prep for moving away from `GFile` consumers. Closes: #319 Approved by: jlebon --- src/libostree/ostree-repo-commit.c | 15 +++------------ src/libostree/ostree-repo-static-delta-core.c | 2 +- src/libostree/ostree-repo.c | 15 ++------------- src/libotutil/ot-variant-utils.c | 17 +++++++++++++---- src/libotutil/ot-variant-utils.h | 7 ++++++- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c index b8f4824a..6cb46ca9 100644 --- a/src/libostree/ostree-repo-commit.c +++ b/src/libostree/ostree-repo-commit.c @@ -2097,22 +2097,13 @@ ostree_repo_read_commit_detached_metadata (OstreeRepo *self, g_autoptr(GFile) metadata_path = _ostree_repo_get_commit_metadata_loose_path (self, checksum); g_autoptr(GVariant) ret_metadata = NULL; - GError *temp_error = NULL; if (!ot_util_variant_map_at (AT_FDCWD, gs_file_get_path_cached (metadata_path), G_VARIANT_TYPE ("a{sv}"), - TRUE, &ret_metadata, &temp_error)) + OT_VARIANT_MAP_ALLOW_NOENT | OT_VARIANT_MAP_TRUSTED, &ret_metadata, error)) { - if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) - { - g_clear_error (&temp_error); - } - else - { - g_prefix_error (error, "Unable to read existing detached metadata: "); - g_propagate_error (error, temp_error); - goto out; - } + g_prefix_error (error, "Unable to read existing detached metadata: "); + goto out; } ret = TRUE; diff --git a/src/libostree/ostree-repo-static-delta-core.c b/src/libostree/ostree-repo-static-delta-core.c index 2b1338b5..de9e6c7a 100644 --- a/src/libostree/ostree-repo-static-delta-core.c +++ b/src/libostree/ostree-repo-static-delta-core.c @@ -842,7 +842,7 @@ _ostree_repo_static_delta_dump (OstreeRepo *self, if (!ot_util_variant_map_at (self->repo_dir_fd, superblock_path, (GVariantType*)OSTREE_STATIC_DELTA_SUPERBLOCK_FORMAT, - TRUE, &delta_superblock, error)) + OT_VARIANT_MAP_TRUSTED, &delta_superblock, error)) goto out; g_print ("%s\n", g_variant_print (delta_superblock, 1)); diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index 38dac761..2a26ffed 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -4620,7 +4620,6 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo *self, g_autoptr(GBytes) summary_data = NULL; g_autoptr(GFile) summary_file = NULL; g_autoptr(GFile) signature_path = NULL; - GError *temp_error = NULL; g_autoptr(GVariant) existing_signatures = NULL; g_autoptr(GVariant) new_metadata = NULL; g_autoptr(GVariant) normalized = NULL; @@ -4634,18 +4633,8 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo *self, if (!ot_util_variant_map_at (AT_FDCWD, gs_file_get_path_cached (signature_path), G_VARIANT_TYPE (OSTREE_SUMMARY_SIG_GVARIANT_STRING), - TRUE, &existing_signatures, &temp_error)) - { - if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) - { - g_clear_error (&temp_error); - } - else - { - g_propagate_error (error, temp_error); - goto out; - } - } + OT_VARIANT_MAP_ALLOW_NOENT, &existing_signatures, error)) + goto out; for (i = 0; key_id[i]; i++) { diff --git a/src/libotutil/ot-variant-utils.c b/src/libotutil/ot-variant-utils.c index 26054459..9d4a62d2 100644 --- a/src/libotutil/ot-variant-utils.c +++ b/src/libotutil/ot-variant-utils.c @@ -121,18 +121,27 @@ gboolean ot_util_variant_map_at (int dfd, const char *path, const GVariantType *type, - gboolean trusted, + OtVariantMapFlags flags, GVariant **out_variant, GError **error) { glnx_fd_close int fd = -1; + const gboolean trusted = (flags & OT_VARIANT_MAP_TRUSTED) > 0; fd = openat (dfd, path, O_RDONLY | O_CLOEXEC); if (fd < 0) { - glnx_set_error_from_errno (error); - g_prefix_error (error, "Opening %s: ", path); - return FALSE; + if (errno == ENOENT && (flags & OT_VARIANT_MAP_ALLOW_NOENT) > 0) + { + *out_variant = NULL; + return TRUE; + } + else + { + glnx_set_error_from_errno (error); + g_prefix_error (error, "Opening %s: ", path); + return FALSE; + } } return ot_util_variant_map_fd (fd, 0, type, trusted, out_variant, error); diff --git a/src/libotutil/ot-variant-utils.h b/src/libotutil/ot-variant-utils.h index f185b4fd..8a33cf60 100644 --- a/src/libotutil/ot-variant-utils.h +++ b/src/libotutil/ot-variant-utils.h @@ -42,10 +42,15 @@ gboolean ot_util_variant_save (GFile *dest, GCancellable *cancellable, GError **error); +typedef enum { + OT_VARIANT_MAP_TRUSTED = (1 << 0), + OT_VARIANT_MAP_ALLOW_NOENT = (1 << 1) +} OtVariantMapFlags; + gboolean ot_util_variant_map_at (int dfd, const char *path, const GVariantType *type, - gboolean trusted, + OtVariantMapFlags flags, GVariant **out_variant, GError **error);