From 1345882d6a5fc3ea851bfe5510e79861d511c25e Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Tue, 9 Feb 2021 16:08:02 -0500 Subject: [PATCH] glnx_file_copy_at: Add GLNX_FILE_COPY_NOCHOWN In some contexts, we may want to copy a root-owned file but we're not running as root so we can't `fchown` it. (The case I'm interested in is actually a bit more obscure than this: running in a supermin VM as root, and wanting to copy a file we created onto a 9p mount where we don't have perms to `fchown`). Add a `GLNX_FILE_COPY_NOCHOWN` to handle this case. --- glnx-fdio.c | 7 +++++-- glnx-fdio.h | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/glnx-fdio.c b/glnx-fdio.c index d4eeb24c..3fa73b5d 100644 --- a/glnx-fdio.c +++ b/glnx-fdio.c @@ -1000,8 +1000,11 @@ glnx_file_copy_at (int src_dfd, if (glnx_regfile_copy_bytes (src_fd, tmp_dest.fd, (off_t) -1) < 0) return glnx_throw_errno_prefix (error, "regfile copy"); - if (fchown (tmp_dest.fd, src_stbuf->st_uid, src_stbuf->st_gid) != 0) - return glnx_throw_errno_prefix (error, "fchown"); + if (!(copyflags & GLNX_FILE_COPY_NOCHOWN)) + { + if (fchown (tmp_dest.fd, src_stbuf->st_uid, src_stbuf->st_gid) != 0) + return glnx_throw_errno_prefix (error, "fchown"); + } if (!(copyflags & GLNX_FILE_COPY_NOXATTRS)) { diff --git a/glnx-fdio.h b/glnx-fdio.h index 40931bfb..3d1f024c 100644 --- a/glnx-fdio.h +++ b/glnx-fdio.h @@ -189,7 +189,8 @@ glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes); typedef enum { GLNX_FILE_COPY_OVERWRITE = (1 << 0), GLNX_FILE_COPY_NOXATTRS = (1 << 1), - GLNX_FILE_COPY_DATASYNC = (1 << 2) + GLNX_FILE_COPY_DATASYNC = (1 << 2), + GLNX_FILE_COPY_NOCHOWN = (1 << 3) } GLnxFileCopyFlags; gboolean