[libgsystem] fileutil: New gs_file_lchown() API



commit 1c1a7c15029176928534d28fb1fb5f17adf7c776
Author: Colin Walters <walters verbum org>
Date:   Fri Aug 30 12:11:48 2013 -0400

    fileutil: New gs_file_lchown() API
    
    Needed for ostree to chown symbolic links.

 gsystem-file-utils.c |   72 +++++++++++++++++++++++++++++++++++++------------
 gsystem-file-utils.h |    6 ++++
 2 files changed, 60 insertions(+), 18 deletions(-)
---
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 5ed6ed1..babae9d 100644
--- a/gsystem-file-utils.c
+++ b/gsystem-file-utils.c
@@ -937,6 +937,38 @@ gs_file_unlink (GFile          *path,
   return TRUE;
 }
 
+static gboolean
+chown_internal (GFile          *path,
+                gboolean        dereference_links,
+                guint32         owner,
+                guint32         group,
+                GCancellable   *cancellable,
+                GError        **error)
+{
+  gboolean ret = FALSE;
+  int res;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  do
+    if (dereference_links)
+      res = chown (gs_file_get_path_cached (path), owner, group);
+    else
+      res = lchown (gs_file_get_path_cached (path), owner, group);
+  while (G_UNLIKELY (res != 0 && errno == EINTR));
+
+  if (res < 0)
+    {
+      _set_error_from_errno (error);
+      goto out;
+    }
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
 /**
  * gs_file_chown:
  * @path: Path to file
@@ -956,25 +988,29 @@ gs_file_chown (GFile          *path,
                GCancellable   *cancellable,
                GError        **error)
 {
-  gboolean ret = FALSE;
-  int res;
-
-  if (g_cancellable_set_error_if_cancelled (cancellable, error))
-    return FALSE;
-
-  do
-    res = chown (gs_file_get_path_cached (path), owner, group);
-  while (G_UNLIKELY (res != 0 && errno == EINTR));
-
-  if (res < 0)
-    {
-      _set_error_from_errno (error);
-      goto out;
-    }
+  return chown_internal (path, TRUE, owner, group, cancellable, error);
+}
 
-  ret = TRUE;
- out:
-  return ret;
+/**
+ * gs_file_lchown:
+ * @path: Path to file
+ * @owner: UNIX owner
+ * @group: UNIX group
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Merely wraps UNIX lchown().
+ *
+ * Returns: %TRUE on success, %FALSE on error
+ */
+gboolean
+gs_file_lchown (GFile          *path,
+                guint32         owner,
+                guint32         group,
+                GCancellable   *cancellable,
+                GError        **error)
+{
+  return chown_internal (path, FALSE, owner, group, cancellable, error);
 }
 
 /**
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index aae9944..6075e60 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -111,6 +111,12 @@ gboolean gs_file_chown (GFile          *path,
                         GCancellable   *cancellable,
                         GError        **error);
 
+gboolean gs_file_lchown (GFile          *path,
+                         guint32         owner,
+                         guint32         group,
+                         GCancellable   *cancellable,
+                         GError        **error);
+
 gboolean gs_file_chmod (GFile          *path,
                         guint           mode,
                         GCancellable   *cancellable,


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