[Libguestfs] [PATCH v2 02/11] New API: btrfs_subvolume_get_default

Hu Tao
Thu, 11 Dec 2014 06:11:30 -0000

btrfs_subvolume_get_default is for getting the default subvolume of
a btrfs filesystem.

Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 daemon/btrfs.c       | 39 +++++++++++++++++++++++++++++++++++++++
 generator/actions.ml | 18 ++++++++++++++++++
 src/MAX_PROC_NR      |  2 +-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index 2e9859d..9f4c5f4 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -553,6 +553,45 @@ do_btrfs_subvolume_set_default (int64_t id, const char *fs)
   return 0;
 }

+int64_t
+do_btrfs_subvolume_get_default (const mountable_t *fs)
+{
+  const size_t MAX_ARGS = 64;
+  const char *argv[MAX_ARGS];
+  size_t i = 0;
+  char *fs_buf = NULL;
+  CLEANUP_FREE char *err = NULL;
+  CLEANUP_FREE char *out = NULL;
+  int r;
+  int64_t ret = -1;
+
+  fs_buf = mount (fs);
+  if (fs_buf == NULL)
+    goto error;
+
+  ADD_ARG (argv, i, str_btrfs);
+  ADD_ARG (argv, i, "subvolume");
+  ADD_ARG (argv, i, "get-default");
+  ADD_ARG (argv, i, fs_buf);
+  ADD_ARG (argv, i, NULL);
+
+  r = commandv (&out, &err, argv);
+  if (r == -1) {
+    reply_with_error ("%s: %s", fs_buf, err);
+    goto error;
+  }
+  if (sscanf (out, "ID %" SCNi64, &ret) != 1) {
+    reply_with_error ("%s: could not parse subvolume id: %s.",
argv[0], out);
+    ret = -1;
+    goto error;
+  }
+
+error:
+  if (fs_buf && umount (fs_buf, fs) != 0)
+    return -1;
+  return ret;
+}
+
 int
 do_btrfs_filesystem_sync (const char *fs)
 {
diff --git a/generator/actions.ml b/generator/actions.ml
index ba97eb3..4e47a02 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -12032,6 +12032,24 @@ Set readahead (in 512-byte sectors) for the device.

 This uses the L<blockdev(8)> command." };

+  { defaults with
+    name = "btrfs_subvolume_get_default";
+    style = RInt64 "id", [Mountable_or_Path "fs"], [];
+    proc_nr = Some 425;
+    optional = Some "btrfs"; camel_name =
"BTRFSSubvolumeGetDefault";
+    tests = [
+      InitPartition, Always, TestResult (
+        [["mkfs_btrfs"; "/dev/sda1"; "";
""; "NOARG"; ""; "NOARG";
"NOARG"; ""; ""];
+         ["btrfs_subvolume_get_default"; "/dev/sda1"]],
"ret > 0"), [];
+      InitPartition, Always, TestResult (
+        [["mkfs_btrfs"; "/dev/sda1"; "";
""; "NOARG"; ""; "NOARG";
"NOARG"; ""; ""];
+         ["mount"; "/dev/sda1"; "/"];
+         ["btrfs_subvolume_get_default"; "/"]], "ret
> 0"), []
+    ];
+    shortdesc = "get the default subvolume or snapshot of a
filesystem";
+    longdesc = "\
+Get the default subvolume or snapshot of a filesystem mounted at
C<mountpoint>." };
+
 ]

 (* Non-API meta-commands available only in guestfish.
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 9524ef4..5e4a522 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-424
+425
--
1.9.3