[libgsystem] fileutils: Add chown() wrapper



commit fe898ee93bdc8a625c460876c8b49428c8977219
Author: Colin Walters <walters verbum org>
Date:   Sun Jan 6 06:42:04 2013 -0500

    fileutils: Add chown() wrapper
    
    And while we're here, fix up chmod() wrapper to handle EINTR, and not
    prefix the error message (since the others don't).

 gsystem-file-utils.c |   62 +++++++++++++++++++++++++++++++++++++++++++++----
 gsystem-file-utils.h |    6 +++++
 2 files changed, 63 insertions(+), 5 deletions(-)
---
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 6d53a6b..8e5b4dc 100644
--- a/gsystem-file-utils.c
+++ b/gsystem-file-utils.c
@@ -480,6 +480,48 @@ gs_file_unlink (GFile          *path,
 }
 
 /**
+ * gs_file_chown:
+ * @path: Path to file
+ * @owner: UNIX owner
+ * @group: UNIX group
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Merely wraps UNIX chown().
+ *
+ * Returns: %TRUE on success, %FALSE on error
+ */
+gboolean
+gs_file_chown (GFile          *path,
+               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
+    res = chown (gs_file_get_path_cached (path), owner, group);
+  while (G_UNLIKELY (res != 0 && errno == EINTR));
+
+  if (res < 0)
+    {
+      int errsv = errno;
+      g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+                           g_strerror (errsv));
+      goto out;
+    }
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+/**
  * gs_file_chmod:
  * @path: Path to file
  * @mode: UNIX mode
@@ -496,17 +538,27 @@ gs_file_chmod (GFile          *path,
                GCancellable   *cancellable,
                GError        **error)
 {
+  gboolean ret = FALSE;
+  int res;
+
   if (g_cancellable_set_error_if_cancelled (cancellable, error))
     return FALSE;
 
-  if (chmod (gs_file_get_path_cached (path), mode) < 0)
+  do
+    res = chmod (gs_file_get_path_cached (path), mode);
+  while (G_UNLIKELY (res != 0 && errno == EINTR));
+
+  if (res < 0)
     {
       int errsv = errno;
-      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
-                   "Failed to chmod %s: ", gs_file_get_path_cached (path));
-      return FALSE;
+      g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+                           g_strerror (errsv));
+      goto out;
     }
-  return TRUE;
+
+  ret = TRUE;
+ out:
+  return ret;
 }
 
 /**
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index abd8e7c..b88ce31 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -58,6 +58,12 @@ gboolean gs_file_unlink (GFile          *path,
                          GCancellable   *cancellable,
                          GError        **error);
 
+gboolean gs_file_chown (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]