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 authored and Atomic Bot committed 2016-06-09 14:39:09 +00:00
1 parent 5a996c04de
commit 3a03a35071
5 files changed
+25 -31

No files matched your search

+13 -4
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);
+6 -1
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);