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

[Libguestfs] [PATCH v8 41/42] daemon: Reimplement ‘device_index’ API in OCaml.



---
 daemon/devsparts.c        | 21 ---------------------
 daemon/devsparts.ml       | 11 +++++++++++
 daemon/devsparts.mli      |  6 ++----
 generator/actions_core.ml |  1 +
 4 files changed, 14 insertions(+), 25 deletions(-)

diff --git a/daemon/devsparts.c b/daemon/devsparts.c
index 12e779326..7c65be1dc 100644
--- a/daemon/devsparts.c
+++ b/daemon/devsparts.c
@@ -33,27 +33,6 @@
 #include "daemon.h"
 #include "actions.h"
 
-int
-do_device_index (const char *device)
-{
-  size_t i;
-  int ret = -1;
-  CLEANUP_FREE_STRING_LIST char **devices = do_list_devices ();
-
-  if (devices == NULL)
-    return -1;
-
-  for (i = 0; devices[i] != NULL; ++i) {
-    if (STREQ (device, devices[i]))
-      ret = (int) i;
-  }
-
-  if (ret == -1)
-    reply_with_error ("device not found");
-
-  return ret;
-}
-
 #define GUESTFSDIR "/dev/disk/guestfs"
 
 char **
diff --git a/daemon/devsparts.ml b/daemon/devsparts.ml
index 273612516..4d273f59e 100644
--- a/daemon/devsparts.ml
+++ b/daemon/devsparts.ml
@@ -109,3 +109,14 @@ let is_whole_device device =
 
   try ignore (stat devpath); true
   with Unix_error ((ENOENT|ENOTDIR), _, _) -> false
+
+let device_index device =
+  (* This is the algorithm which was used by the C version.  Why
+   * can't we use drive_index from C_utils?  XXX
+   *)
+  let rec loop i = function
+    | [] -> failwithf "%s: device not found" device
+    | dev :: devices when dev = device -> i
+    | _ :: devices -> loop (i+1) devices
+  in
+  loop 0 (list_devices ())
diff --git a/daemon/devsparts.mli b/daemon/devsparts.mli
index 8be47e752..4afb36bec 100644
--- a/daemon/devsparts.mli
+++ b/daemon/devsparts.mli
@@ -18,10 +18,8 @@
 
 val list_devices : unit -> string list
 val list_partitions : unit -> string list
-
-val nr_devices : unit -> int
-
 val part_to_dev : string -> string
 val part_to_partnum : string -> int
-
 val is_whole_device : string -> bool
+val nr_devices : unit -> int
+val device_index : string -> int
diff --git a/generator/actions_core.ml b/generator/actions_core.ml
index c3421133e..ea0735676 100644
--- a/generator/actions_core.ml
+++ b/generator/actions_core.ml
@@ -7419,6 +7419,7 @@ instead of, or after calling C<guestfs_zero_free_space>." };
   { defaults with
     name = "device_index"; added = (1, 19, 7);
     style = RInt "index", [String (Device, "device")], [];
+    impl = OCaml "Devsparts.device_index";
     tests = [
       InitEmpty, Always, TestResult (
         [["device_index"; "/dev/sda"]], "ret == 0"), []
-- 
2.13.0


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