From 9ca6522abf9a68b75fde7067225cfda42019c48a Mon Sep 17 00:00:00 2001
From: "Raphael S. Carvalho" <raphaelsc@cloudius-systems.com>
Date: Tue, 20 May 2014 17:22:58 -0300
Subject: [PATCH] mkfs: Code refactoring and allow instances of the same shared
 object

Besides refactoring the code, this patch makes mkfs support more than
one instance of the same shared object within the same mkfs instance,
i.e. by releasing the resources at the function prologue.

Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 tools/mkfs/mkfs.cc | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/tools/mkfs/mkfs.cc b/tools/mkfs/mkfs.cc
index 3957030b8..d2a3b58f0 100644
--- a/tools/mkfs/mkfs.cc
+++ b/tools/mkfs/mkfs.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Cloudius Systems, Ltd.
+ * Copyright (C) 2013-2014 Cloudius Systems, Ltd.
  *
  * This work is open source software, licensed under the terms of the
  * BSD license as described in the LICENSE file in the top-level directory.
@@ -15,21 +15,32 @@
 using namespace osv;
 using namespace std;
 
+// Created to guarantee that shared objects resources will
+// be surely released at the function prologue.
+void run_cmd(const char *cmdpath, vector<string> args)
+{
+    int ret;
+    auto ok = run(cmdpath, args, &ret);
+    assert(ok && ret == 0);
+}
+
 void mkfs()
 {
-    /* Create zfs device, then /etc/mnttab which is required by libzfs */
+    // Create zfs device, then /etc/mnttab which is required by libzfs
     zfsdev::zfsdev_init();
+
+    // Manually create /etc/mnttab, a file required by libzfs.
     mkdir("/etc", 0755);
     int fd = creat("/etc/mnttab", 0644);
     assert(fd != -1);
     close(fd);
 
-    int ret;
-    auto ok = run("/zpool.so",
-            {"zpool", "create", "-f", "-R", "/zfs", "osv", "/dev/vblk0.1"}, &ret);
-    assert(ok && ret == 0);
-    ok = run("/zfs.so", {"zfs", "create", "osv/zfs"}, &ret);
-    assert(ok && ret == 0);
+    // Create zpool named osv
+    run_cmd("/zpool.so",
+        {"zpool", "create", "-f", "-R", "/zfs", "osv", "/dev/vblk0.1"});
+
+    // Create a zfs dataset within the pool named osv.
+    run_cmd("/zfs.so", {"zfs", "create", "osv/zfs"});
 }
 
 int main(int ac, char** av)
-- 
GitLab