From 02202b9e767e59074b86f65a345f736e0aae1cc4 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 7 Feb 2017 16:24:04 +0100 Subject: [PATCH] utils: Add flatpak_copy_bytes This copies data from one fd to another. --- common/flatpak-utils.c | 38 ++++++++++++++++++++++++++++++++++++++ common/flatpak-utils.h | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/common/flatpak-utils.c b/common/flatpak-utils.c index 3867073bf..c180340b1 100644 --- a/common/flatpak-utils.c +++ b/common/flatpak-utils.c @@ -1984,6 +1984,44 @@ flatpak_openat_noatime (int dfd, } } +#define COPY_BUFFER_SIZE (16*1024) +gboolean +flatpak_copy_bytes (int fdf, + int fdt, + GError **error) +{ + char buf[COPY_BUFFER_SIZE]; + int r; + + g_return_val_if_fail (fdf >= 0, FALSE); + g_return_val_if_fail (fdt >= 0, FALSE); + + for (;;) + { + size_t m = COPY_BUFFER_SIZE; + ssize_t n; + + n = read (fdf, buf, m); + if (n < 0) + { + glnx_set_error_from_errno (error); + return FALSE; + } + if (n == 0) /* EOF */ + break; + + r = glnx_loop_write (fdt, buf, (size_t) n); + if (r < 0) + { + errno = r; + glnx_set_error_from_errno (error); + return FALSE; + } + } + + return TRUE; +} + gboolean flatpak_cp_a (GFile *src, GFile *dest, diff --git a/common/flatpak-utils.h b/common/flatpak-utils.h index c63f66561..2b0a81962 100644 --- a/common/flatpak-utils.h +++ b/common/flatpak-utils.h @@ -362,6 +362,10 @@ gboolean flatpak_openat_noatime (int dfd, GCancellable *cancellable, GError **error); +gboolean flatpak_copy_bytes (int fdf, + int fdt, + GError **error); + typedef enum { FLATPAK_CP_FLAGS_NONE = 0, FLATPAK_CP_FLAGS_MERGE = 1<<0,