Richard W.M. Jones
2020-Feb-20 17:04 UTC
[Libguestfs] [PATCH] lib: Move guestfs_device_index impl from daemon to library.
Although the commit message ties this to https://bugzilla.redhat.com/1804207, in fact I believe this commit could be applied independently. It's a simple optimization. Rich.
Richard W.M. Jones
2020-Feb-20 17:04 UTC
[Libguestfs] [PATCH] lib: Move guestfs_device_index impl from daemon to library.
This function didn't work reliably with the change to device name translation made in the previous commit. The reason is that strings returned by Devsparts.list_devices contained translated names, so their indexes did not correspond to the untranslated names. We can avoid all this and make the function much simpler and faster by implementing it on the library side instead. --- daemon/devsparts.ml | 11 ----------- daemon/inspect_utils.ml | 6 +----- generator/actions_core.ml | 35 +++++++++++++++++------------------ generator/proc_nr.ml | 1 - lib/drives.c | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 35 deletions(-) diff --git a/daemon/devsparts.ml b/daemon/devsparts.ml index c082c32fb..59e66e82e 100644 --- a/daemon/devsparts.ml +++ b/daemon/devsparts.ml @@ -111,14 +111,3 @@ 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/inspect_utils.ml b/daemon/inspect_utils.ml index ea444afe6..e9aa8221f 100644 --- a/daemon/inspect_utils.ml +++ b/daemon/inspect_utils.ml @@ -148,11 +148,7 @@ and is_dir_nocase path * the old C inspection code. XXX fix function and callers *) let is_partition partition - try - let device = Devsparts.part_to_dev partition in - ignore (Devsparts.device_index device); - true - with _ -> false + try Devsparts.part_to_dev partition <> partition with _ -> false let re_major_minor = PCRE.compile "(\\d+)\\.(\\d+)" let re_major_no_minor = PCRE.compile "(\\d+)" diff --git a/generator/actions_core.ml b/generator/actions_core.ml index 692015e27..eca52d347 100644 --- a/generator/actions_core.ml +++ b/generator/actions_core.ml @@ -739,6 +739,23 @@ Converted to F</dev/VG/LV> form using C<guestfs_lvm_canonical_lv_name>. Other strings are returned unmodified." }; + { defaults with + name = "device_index"; added = (1, 19, 7); + style = RInt "index", [String (Device, "device")], []; + tests = [ + InitEmpty, Always, TestResult ( + [["device_index"; "/dev/sda"]], "ret == 0"), [] + ]; + shortdesc = "convert device to index"; + longdesc = "\ +This function takes a device name (eg. \"/dev/sdb\") and +returns the index of the device in the list of devices. + +Index numbers start from 0. The named device must exist, +for example as a string returned from C<guestfs_list_devices>. + +See also C<guestfs_list_devices>, C<guestfs_part_to_dev>." }; + { defaults with name = "shutdown"; added = (1, 19, 16); style = RErr, [], []; @@ -7423,24 +7440,6 @@ different operation that turns free space in the filesystem into zeroes. It is valid to call C<guestfs_fstrim> either 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"), [] - ]; - shortdesc = "convert device to index"; - longdesc = "\ -This function takes a device name (eg. \"/dev/sdb\") and -returns the index of the device in the list of devices. - -Index numbers start from 0. The named device must exist, -for example as a string returned from C<guestfs_list_devices>. - -See also C<guestfs_list_devices>, C<guestfs_part_to_dev>." }; - { defaults with name = "nr_devices"; added = (1, 19, 15); impl = OCaml "Devsparts.nr_devices"; diff --git a/generator/proc_nr.ml b/generator/proc_nr.ml index 11a557076..5604662f4 100644 --- a/generator/proc_nr.ml +++ b/generator/proc_nr.ml @@ -345,7 +345,6 @@ let proc_nr = [ 332, "btrfs_fsck"; 333, "filesystem_available"; 334, "fstrim"; -335, "device_index"; 336, "nr_devices"; 337, "xfs_info"; 338, "pvchange_uuid"; diff --git a/lib/drives.c b/lib/drives.c index bba6ff74e..46af66db4 100644 --- a/lib/drives.c +++ b/lib/drives.c @@ -1148,3 +1148,21 @@ free_drive_source (struct drive_source *src) free_drive_servers (src->servers, src->nr_servers); } } + +int +guestfs_impl_device_index (guestfs_h *g, const char *device) +{ + size_t len; + ssize_t r = -1; + + /* /dev/hd etc. */ + if (STRPREFIX (device, "/dev/") && + strchr (device+5, '/') == NULL && /* not an LV name */ + device[5] != 'm' && /* not /dev/md - RHBZ#1414682 */ + ((len = strcspn (device+5, "d")) > 0 && len <= 2)) + r = guestfs_int_drive_index (device+5+len+1); + + if (r == -1) + error (g, _("%s: device not found"), device); + return r; +} -- 2.25.0
Apparently Analagous Threads
- guestfs_list_filesystems: skip block devices which cannot hold file system
- [PATCH v3 00/23] Reimplement many daemon APIs in OCaml.
- [PATCH v2 00/23] Reimplement many daemon APIs in OCaml.
- [PATCH 00/27] Reimplement many daemon APIs in OCaml.
- [PATCH v2 1/3] daemon: Reimplement 'part_get_mbr_part_type' API in OCaml.