[libgsystem] fileutils: Add API to map a file readonly and to chmod



commit a4643adfb5b09a16fb703389aec86c5e321efb35
Author: Colin Walters <walters verbum org>
Date:   Mon Dec 10 14:55:24 2012 -0500

    fileutils: Add API to map a file readonly and to chmod
    
    Will be used by gnome-ostree.

 gsystem-file-utils.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gsystem-file-utils.h |    9 +++++++
 2 files changed, 70 insertions(+), 0 deletions(-)
---
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 209f87a..1595ee2 100644
--- a/gsystem-file-utils.c
+++ b/gsystem-file-utils.c
@@ -129,6 +129,37 @@ gs_file_map_noatime (GFile         *file,
 }
 
 /**
+ * gs_file_map_readonly:
+ * @file: a #GFile
+ * @cancellable:
+ * @error:
+ *
+ * Return a #GBytes which references a readonly view of the contents of
+ * @file.  This function uses #GMappedFile internally.
+ *
+ * Returns: (transfer full): a newly referenced #GBytes
+ */
+GBytes *
+gs_file_map_readonly (GFile         *file,
+                      GCancellable  *cancellable,
+                      GError       **error)
+{
+  GMappedFile *mfile;
+  GBytes *ret;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return NULL;
+
+  mfile = g_mapped_file_new (gs_file_get_path_cached (file), FALSE, error);
+  if (!mfile)
+    return NULL;
+
+  ret = g_mapped_file_get_bytes (mfile);
+  g_mapped_file_unref (mfile);
+  return ret;
+}
+
+/**
  * gs_file_get_path_cached:
  *
  * Like g_file_get_path(), but returns a constant copy so callers
@@ -233,6 +264,36 @@ gs_file_unlink (GFile          *path,
 }
 
 /**
+ * gs_file_chmod:
+ * @path: Path to file
+ * @mode: UNIX mode
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Merely wraps UNIX chmod().
+ *
+ * Returns: %TRUE on success, %FALSE on error
+ */
+gboolean
+gs_file_chmod (GFile          *path,
+               guint           mode,
+               GCancellable   *cancellable,
+               GError        **error)
+{
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  if (chmod (gs_file_get_path_cached (path), mode) < 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;
+    }
+  return TRUE;
+}
+
+/**
  * gs_file_ensure_directory:
  * @dir: Path to create as directory
  * @with_parents: Also create parent directories
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index 93aa568..bf07b3a 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -36,6 +36,10 @@ GMappedFile *gs_file_map_noatime (GFile         *file,
                                   GCancellable  *cancellable,
                                   GError       **error);
 
+GBytes *gs_file_map_readonly (GFile         *file,
+                              GCancellable  *cancellable,
+                              GError       **error);
+
 gboolean gs_file_rename (GFile          *from,
                          GFile          *to,
                          GCancellable   *cancellable,
@@ -45,6 +49,11 @@ gboolean gs_file_unlink (GFile          *path,
                          GCancellable   *cancellable,
                          GError        **error);
 
+gboolean gs_file_chmod (GFile          *path,
+                        guint           mode,
+                        GCancellable   *cancellable,
+                        GError        **error);
+
 gboolean gs_file_ensure_directory (GFile          *dir,
                                    gboolean        with_parents,
                                    GCancellable   *cancellable,



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