[libglnx] fdio: Also add a replace variant that takes mode/uid/gid



commit 376219a9c276237f21c2c5aa1f0b7875a89586b6
Author: Colin Walters <walters verbum org>
Date:   Wed Apr 8 21:31:43 2015 -0400

    fdio: Also add a replace variant that takes mode/uid/gid
    
    This will be used for OSTree too.

 glnx-fdio.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 glnx-fdio.h |   13 ++++++++++++-
 2 files changed, 62 insertions(+), 9 deletions(-)
---
diff --git a/glnx-fdio.c b/glnx-fdio.c
index 42fc10a..b45b927 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -587,7 +587,7 @@ glnx_file_copy_at (int                   src_dfd,
 }
 
 /**
- * glnx_file_replace_contents_utf8_at:
+ * glnx_file_replace_contents_at:
  * @dfd: Directory fd
  * @subpath: Subpath
  * @buf: (array len=len) (element-type guint8): File contents
@@ -609,11 +609,41 @@ glnx_file_replace_contents_at (int                   dfd,
                                const char           *subpath,
                                const guint8         *buf,
                                gsize                 len,
-                               int                   mode,
                                GLnxFileReplaceFlags  flags,
                                GCancellable         *cancellable,
                                GError              **error)
 {
+  return glnx_file_replace_contents_with_perms_at (dfd, subpath, buf, len,
+                                                   (mode_t) -1, (uid_t) -1, (gid_t) -1,
+                                                   flags, cancellable, error);
+                                                   
+}
+
+/**
+ * glnx_file_replace_contents_with_perms_at:
+ * @dfd: Directory fd
+ * @subpath: Subpath
+ * @buf: (array len=len) (element-type guint8): File contents
+ * @len: Length (if `-1`, assume @buf is `NUL` terminated)
+ * @flags: Flags
+ * @cancellable: Cancellable
+ * @error: Error
+ *
+ * Like glnx_file_replace_contents_at(), but also supports
+ * setting mode, and uid/gid.
+ */ 
+gboolean
+glnx_file_replace_contents_with_perms_at (int                   dfd,
+                                          const char           *subpath,
+                                          const guint8         *buf,
+                                          gsize                 len,
+                                          mode_t                mode,
+                                          uid_t                 uid,
+                                          gid_t                 gid,
+                                          GLnxFileReplaceFlags  flags,
+                                          GCancellable         *cancellable,
+                                          GError              **error)
+{
   gboolean ret = FALSE;
   /* We use the /proc/self trick as there's no mkostemp_at() yet */
   g_autofree char *tmppath = g_strdup_printf ("/proc/self/fd/%d/.tmpXXXXXX", dfd);
@@ -625,12 +655,6 @@ glnx_file_replace_contents_at (int                   dfd,
       goto out;
     }
 
-  if (fchmod (fd, mode) != 0)
-    {
-      glnx_set_error_from_errno (error);
-      goto out;
-    }
-
   if (len == -1)
     len = strlen ((char*)buf);
 
@@ -673,6 +697,24 @@ glnx_file_replace_contents_at (int                   dfd,
         }
     }
 
+  if (uid != (uid_t) -1)
+    {
+      if (fchown (fd, uid, gid) != 0)
+        {
+          glnx_set_error_from_errno (error);
+          goto out;
+        }
+    }
+
+  if (mode != (mode_t) -1)
+    {
+      if (fchmod (fd, mode) != 0)
+        {
+          glnx_set_error_from_errno (error);
+          goto out;
+        }
+    }
+
   if (renameat (dfd, tmppath, dfd, subpath) != 0)
     {
       glnx_set_error_from_errno (error);
diff --git a/glnx-fdio.h b/glnx-fdio.h
index a380c1f..a90544a 100644
--- a/glnx-fdio.h
+++ b/glnx-fdio.h
@@ -80,11 +80,22 @@ glnx_file_replace_contents_at (int                   dfd,
                                const char           *subpath,
                                const guint8         *buf,
                                gsize                 len,
-                               int                   mode,
                                GLnxFileReplaceFlags  flags,
                                GCancellable         *cancellable,
                                GError              **error);
 
+gboolean
+glnx_file_replace_contents_with_perms_at (int                   dfd,
+                                          const char           *subpath,
+                                          const guint8         *buf,
+                                          gsize                 len,
+                                          mode_t                mode,
+                                          uid_t                 uid,
+                                          gid_t                 gid,
+                                          GLnxFileReplaceFlags  flags,
+                                          GCancellable         *cancellable,
+                                          GError              **error);
+
 char *
 glnx_readlinkat_malloc (int            dfd,
                         const char    *subpath,


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