diff options
author | Simon McVittie <smcv@collabora.com> | 2021-05-21 17:41:06 +0100 |
---|---|---|
committer | Alexander Larsson <alexander.larsson@gmail.com> | 2021-05-25 11:11:03 +0200 |
commit | 10a4cd7e08cacefe1213b4fe7f1ff10e8074a13c (patch) | |
tree | c1100cddb28431cea9436ba785813978b8b3dd72 | |
parent | 526dae92418616c71517945e810227416f160ce6 (diff) | |
download | flatpak-10a4cd7e08cacefe1213b4fe7f1ff10e8074a13c.tar.gz |
context: Factor out functions to parse an environment block
This makes them easier to test, and easier to use in related tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r-- | common/flatpak-context-private.h | 8 | ||||
-rw-r--r-- | common/flatpak-context.c | 71 |
2 files changed, 55 insertions, 24 deletions
diff --git a/common/flatpak-context-private.h b/common/flatpak-context-private.h index 7a35bb65..6e07eefd 100644 --- a/common/flatpak-context-private.h +++ b/common/flatpak-context-private.h @@ -150,6 +150,14 @@ void flatpak_context_append_bwrap_filesystem (FlatpakContext *context, const char *xdg_dirs_conf, gboolean home_access); +gboolean flatpak_context_parse_env_block (FlatpakContext *context, + const char *data, + gsize length, + GError **error); +gboolean flatpak_context_parse_env_fd (FlatpakContext *context, + int fd, + GError **error); + G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakContext, flatpak_context_free) #endif /* __FLATPAK_CONTEXT_H__ */ diff --git a/common/flatpak-context.c b/common/flatpak-context.c index 83735049..dd230d26 100644 --- a/common/flatpak-context.c +++ b/common/flatpak-context.c @@ -1120,30 +1120,14 @@ option_env_cb (const gchar *option_name, return TRUE; } -static gboolean -option_env_fd_cb (const gchar *option_name, - const gchar *value, - gpointer data, - GError **error) +gboolean +flatpak_context_parse_env_block (FlatpakContext *context, + const char *data, + gsize length, + GError **error) { - FlatpakContext *context = data; - g_autoptr(GBytes) env_block = NULL; - gsize remaining; - const char *p; - guint64 fd; - gchar *endptr; - - fd = g_ascii_strtoull (value, &endptr, 10); - - if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT) - return glnx_throw (error, "Not a valid file descriptor: %s", value); - - env_block = glnx_fd_readall_bytes ((int) fd, NULL, error); - - if (env_block == NULL) - return FALSE; - - p = g_bytes_get_data (env_block, &remaining); + const char *p = data; + gsize remaining = length; /* env_block might not be \0-terminated */ while (remaining > 0) @@ -1175,10 +1159,49 @@ option_env_fd_cb (const gchar *option_name, } } + return TRUE; +} + +gboolean +flatpak_context_parse_env_fd (FlatpakContext *context, + int fd, + GError **error) +{ + g_autoptr(GBytes) env_block = NULL; + const char *data; + gsize len; + + env_block = glnx_fd_readall_bytes (fd, NULL, error); + + if (env_block == NULL) + return FALSE; + + data = g_bytes_get_data (env_block, &len); + return flatpak_context_parse_env_block (context, data, len, error); +} + +static gboolean +option_env_fd_cb (const gchar *option_name, + const gchar *value, + gpointer data, + GError **error) +{ + FlatpakContext *context = data; + guint64 fd; + gchar *endptr; + gboolean ret; + + fd = g_ascii_strtoull (value, &endptr, 10); + + if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT) + return glnx_throw (error, "Not a valid file descriptor: %s", value); + + ret = flatpak_context_parse_env_fd (context, (int) fd, error); + if (fd >= 3) close (fd); - return TRUE; + return ret; } static gboolean |