[libglnx: 1/2] glnx_file_copy_at: Add GLNX_FILE_COPY_NOCHOWN




commit 1345882d6a5fc3ea851bfe5510e79861d511c25e
Author: Jonathan Lebon <jonathan jlebon com>
Date:   Tue Feb 9 16:08:02 2021 -0500

    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 d4eeb24..3fa73b5 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 40931bf..3d1f024 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]