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
This commit is contained in:
Colin Walters 2016-06-03 10:40:56 -04:00 committed by Atomic Bot
parent 5a996c04de
commit 3a03a35071
5 changed files with 25 additions and 31 deletions

View File

@ -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;

View File

@ -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));

View File

@ -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++)
{

View File

@ -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);

View File

@ -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);