[Libguestfs] [PATCH 1/3] ext2: add ext2fs_chmod and ext2fs_chown

Pino Toscano ptoscano at redhat.com
Wed Dec 2 18:11:34 UTC 2015


Implement two simple helpers to change permissions and ownership of an
existing path.
---
 src/ext2fs-c.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ext2fs.ml  |  2 ++
 src/ext2fs.mli |  2 ++
 3 files changed, 80 insertions(+)

diff --git a/src/ext2fs-c.c b/src/ext2fs-c.c
index f3ca7dc..f01ca9d 100644
--- a/src/ext2fs-c.c
+++ b/src/ext2fs-c.c
@@ -289,6 +289,82 @@ supermin_ext2fs_copy_dir_recursively_from_host (value fsv,
   CAMLreturn (Val_unit);
 }
 
+/* Change the permissions of 'path' to 'mode'.
+ */
+value
+supermin_ext2fs_chmod (value fsv, value pathv, value modev)
+{
+  CAMLparam3 (fsv, pathv, modev);
+  const char *path = String_val (pathv);
+  mode_t mode = Int_val (modev);
+  errcode_t err;
+  struct ext2_data data;
+
+  data = Ext2fs_val (fsv);
+  if (data.fs == NULL)
+    ext2_handle_closed ();
+
+  ext2_ino_t ino;
+  ++path;
+  if (*path == 0) {             /* "/" */
+    ino = EXT2_ROOT_INO;
+  } else {                      /* "/foo" */
+    err = ext2fs_namei (data.fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
+    if (err != 0)
+      ext2_error_to_exception ("ext2fs_namei", err, path);
+  }
+
+  struct ext2_inode inode;
+  err = ext2fs_read_inode (data.fs, ino, &inode);
+  if (err != 0)
+    ext2_error_to_exception ("ext2fs_read_inode", err, path);
+  inode.i_mode = (inode.i_mode & ~07777) | mode;
+  err = ext2fs_write_inode (data.fs, ino, &inode);
+  if (err != 0)
+    ext2_error_to_exception ("ext2fs_write_inode", err, path);
+
+  CAMLreturn (Val_unit);
+}
+
+/* Change the ownership of 'path' to 'uid' and 'gid'.
+ */
+value
+supermin_ext2fs_chown (value fsv, value pathv, value uidv, value gidv)
+{
+  CAMLparam4 (fsv, pathv, uidv, gidv);
+  const char *path = String_val (pathv);
+  int uid = Int_val (uidv);
+  int gid = Int_val (gidv);
+  errcode_t err;
+  struct ext2_data data;
+
+  data = Ext2fs_val (fsv);
+  if (data.fs == NULL)
+    ext2_handle_closed ();
+
+  ext2_ino_t ino;
+  ++path;
+  if (*path == 0) {             /* "/" */
+    ino = EXT2_ROOT_INO;
+  } else {                      /* "/foo" */
+    err = ext2fs_namei (data.fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
+    if (err != 0)
+      ext2_error_to_exception ("ext2fs_namei", err, path);
+  }
+
+  struct ext2_inode inode;
+  err = ext2fs_read_inode (data.fs, ino, &inode);
+  if (err != 0)
+    ext2_error_to_exception ("ext2fs_read_inode", err, path);
+  inode.i_uid = uid;
+  inode.i_gid = gid;
+  err = ext2fs_write_inode (data.fs, ino, &inode);
+  if (err != 0)
+    ext2_error_to_exception ("ext2fs_write_inode", err, path);
+
+  CAMLreturn (Val_unit);
+}
+
 static void
 ext2_mkdir (ext2_filsys fs,
             ext2_ino_t dir_ino, const char *dirname, const char *basename,
diff --git a/src/ext2fs.ml b/src/ext2fs.ml
index 9f3580e..7468aed 100644
--- a/src/ext2fs.ml
+++ b/src/ext2fs.ml
@@ -24,3 +24,5 @@ external ext2fs_close : t -> unit = "supermin_ext2fs_close"
 external ext2fs_read_bitmaps : t -> unit = "supermin_ext2fs_read_bitmaps"
 external ext2fs_copy_file_from_host : t -> string -> string -> unit = "supermin_ext2fs_copy_file_from_host"
 external ext2fs_copy_dir_recursively_from_host : t -> string -> string -> unit = "supermin_ext2fs_copy_dir_recursively_from_host"
+external ext2fs_chmod : t -> string -> Unix.file_perm -> unit = "supermin_ext2fs_chmod"
+external ext2fs_chown : t -> string -> int -> int -> unit = "supermin_ext2fs_chown"
diff --git a/src/ext2fs.mli b/src/ext2fs.mli
index 2a59b2d..7a60622 100644
--- a/src/ext2fs.mli
+++ b/src/ext2fs.mli
@@ -31,3 +31,5 @@ val ext2fs_close : t -> unit
 val ext2fs_read_bitmaps : t -> unit
 val ext2fs_copy_file_from_host : t -> string -> string -> unit
 val ext2fs_copy_dir_recursively_from_host : t -> string -> string -> unit
+val ext2fs_chmod : t -> string -> Unix.file_perm -> unit
+val ext2fs_chown : t -> string -> int -> int -> unit
-- 
2.1.0




More information about the Libguestfs mailing list