summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2012-12-10 14:55:24 -0500
committerColin Walters <walters@verbum.org>2012-12-10 14:58:58 -0500
commita4643adfb5b09a16fb703389aec86c5e321efb35 (patch)
treeccdf7d1eb17f494e27e607036bb71fc9550abca9
parenta98e4be8ef5c57b8f79f896b7bf13142499a17c3 (diff)
downloadlibgsystem-a4643adfb5b09a16fb703389aec86c5e321efb35.tar.gz
fileutils: Add API to map a file readonly and to chmod
Will be used by gnome-ostree.
-rw-r--r--gsystem-file-utils.c61
-rw-r--r--gsystem-file-utils.h9
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,