mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
lib: Move guestfs_device_index impl from daemon to library.
This function doesn't work reliably with the proposed change to device name translation. The reason is that strings returned by Devsparts.list_devices contained translated names, so their indexes did not correspond to the untranslated names used outside the appliance.. We can avoid this and make the function much simpler and faster by implementing it on the library side instead.
This commit is contained in:
@@ -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 ())
|
||||
|
||||
@@ -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+)"
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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";
|
||||
|
||||
18
lib/drives.c
18
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user