[PATCH 1/2] mllib: add and use last_part_of
by Pino Toscano
Collect this small snippet to get the part of a string after the last
occurrency of a character; replace with it the current snippets doing
the same.
Should be just code motion.
---
customize/password.ml | 5 +++--
mllib/common_utils.ml | 7 +++++++
mllib/common_utils.mli | 3 +++
sysprep/sysprep_operation_user_account.ml | 5 +++--
v2v/convert_linux.ml | 10 +++-------
v2v/utils.ml | 16 ++++++++++------
6 files changed, 29 insertions(+), 17 deletions(-)
diff --git a/customize/password.ml b/customize/password.ml
index 25ce901..d91c4b5 100644
--- a/customize/password.ml
+++ b/customize/password.ml
@@ -98,8 +98,9 @@ let rec set_linux_passwords ?password_crypto (g : Guestfs.guestfs) root password
List.iter (
fun userpath ->
let user =
- let i = String.rindex userpath '/' in
- String.sub userpath (i+1) (String.length userpath -i-1) in
+ match last_part_of userpath '/' with
+ | Some x -> x
+ | None -> error "password: missing '/' in %s" userpath in
try
(* Each line is: "user:[!!]password:..."
* !! at the front of the password field means the account is locked.
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 3737b4c..083c5d5 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -738,3 +738,10 @@ let guest_arch_compatible guest_arch =
| x, y when x = y -> true
| "x86_64", ("i386"|"i486"|"i586"|"i686") -> true
| _ -> false
+
+(** Return the last part of a string, after the specified separator. *)
+let last_part_of str sep =
+ try
+ let i = String.rindex str sep in
+ Some (String.sub str (i+1) (String.length str - (i+1)))
+ with Not_found -> None
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index a5b0a68..550f37f 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -178,3 +178,6 @@ val mkdir_p : string -> int -> unit
val guest_arch_compatible : string -> bool
(** Are guest arch and host_cpu compatible, in terms of being able
to run commands in the libguestfs appliance? *)
+
+val last_part_of : string -> char -> string option
+(** Return the last part of a string, after the specified separator. *)
diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml
index 3f3b142..78c60d0 100644
--- a/sysprep/sysprep_operation_user_account.ml
+++ b/sysprep/sysprep_operation_user_account.ml
@@ -69,8 +69,9 @@ let user_account_perform g root side_effects =
let uid = g#aug_get uid in
let uid = int_of_string uid in
let username =
- let i = String.rindex userpath '/' in
- String.sub userpath (i+1) (String.length userpath -i-1) in
+ match last_part_of userpath '/' with
+ | Some x -> x
+ | None -> error "user-accounts: missing '/' in %s" userpath in
if uid >= uid_min && uid <= uid_max
&& check_remove_user username then (
changed := true;
diff --git a/v2v/convert_linux.ml b/v2v/convert_linux.ml
index 7967c0f..f5a716f 100644
--- a/v2v/convert_linux.ml
+++ b/v2v/convert_linux.ml
@@ -795,13 +795,9 @@ let rec convert ~keep_serial_console (g : G.guestfs) inspect source =
*)
let mkinitrd_kv =
let modpath = kernel.ki_modpath in
- let len = String.length modpath in
- try
- let i = String.rindex modpath '/' in
- String.sub modpath (i+1) (len - (i+1))
- with
- Not_found ->
- invalid_arg (sprintf "invalid module path: %s" modpath) in
+ match last_part_of modpath '/' with
+ | Some x -> x
+ | None -> invalid_arg (sprintf "invalid module path: %s" modpath) in
if g#is_file ~followsymlinks:true "/sbin/dracut" then (
(* Dracut. *)
diff --git a/v2v/utils.ml b/v2v/utils.ml
index 2aeba45..976fe85 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -176,9 +176,11 @@ let find_virtio_win_drivers virtio_win =
let paths = List.filter (g#is_file ~followsymlinks:false) paths in
List.map (
fun path ->
- let i = String.rindex path '/' in
- let len = String.length path in
- let basename = String.sub path (i+1) (len - (i+1)) in
+ let basename =
+ match last_part_of path '/' with
+ | Some x -> x
+ | None ->
+ error "v2v/find_virtio_win_drivers: missing '/' in %s" path in
(path, sprintf "%s:%s" virtio_win path,
basename,
fun () -> g#read_file path)
@@ -199,9 +201,11 @@ let find_virtio_win_drivers virtio_win =
let lc_basename = String.lowercase basename in
let extension =
- let i = String.rindex lc_basename '.' in
- let len = String.length lc_basename in
- String.sub lc_basename (i+1) (len - (i+1)) in
+ match last_part_of lc_basename '.' with
+ | Some x -> x
+ | None ->
+ error "v2v/find_virtio_win_drivers: missing '.' in %s"
+ lc_basename in
(* Skip files without specific extensions. *)
if extension <> "cat" && extension <> "inf" &&
--
2.1.0
9 years, 5 months
[PATCH 1/3] mllib: add an optional filter for rm_rf_only_files
by Pino Toscano
This way it is possible to use rm_rf_only_files, but not removing
specific files.
---
mllib/common_utils.ml | 8 +++++++-
mllib/common_utils.mli | 5 ++++-
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 516cff3..3737b4c 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -640,13 +640,19 @@ let rmdir_on_exit =
* without removing the actual directory structure. Also if 'dir' is
* not a directory or doesn't exist, ignore it.
*
+ * The optional filter is used to filter out files which will be
+ * removed: files returning true are not removed.
+ *
* XXX Could be faster with a specific API for doing this.
*)
-let rm_rf_only_files (g : Guestfs.guestfs) dir =
+let rm_rf_only_files (g : Guestfs.guestfs) ?filter dir =
if g#is_dir dir then (
let files = Array.map (Filename.concat dir) (g#find dir) in
let files = Array.to_list files in
let files = List.filter g#is_file files in
+ let files = match filter with
+ | None -> files
+ | Some f -> List.filter (fun x -> not (f x)) files in
List.iter g#rm files
)
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index bf0cba6..a5b0a68 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -140,12 +140,15 @@ val unlink_on_exit : string -> unit
val rmdir_on_exit : string -> unit
(** Remove a temporary directory on exit (using [rm -rf]). *)
-val rm_rf_only_files : Guestfs.guestfs -> string -> unit
+val rm_rf_only_files : Guestfs.guestfs -> ?filter:(string -> bool) -> string -> unit
(** Using the libguestfs API, recursively remove only files from the
given directory. Useful for cleaning [/var/cache] etc in sysprep
without removing the actual directory structure. Also if [dir] is
not a directory or doesn't exist, ignore it.
+ The optional [filter] is used to filter out files which will be
+ removed: files returning true are not removed.
+
XXX Could be faster with a specific API for doing this. *)
val truncate_recursive : Guestfs.guestfs -> string -> unit
--
2.1.0
9 years, 5 months
[PATCH 1/2] utils: Add a test of the guestfs_int_drive_name function.
by Richard W.M. Jones
This function didn't have a unit test before.
---
src/test-utils.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/src/test-utils.c b/src/test-utils.c
index 8e1491f..c5e2f08 100644
--- a/src/test-utils.c
+++ b/src/test-utils.c
@@ -1,5 +1,5 @@
/* libguestfs
- * Copyright (C) 2014 Red Hat Inc.
+ * Copyright (C) 2014-2015 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -162,6 +162,32 @@ test_validate_guid (void)
assert (guestfs_int_validate_guid ("21EC2020-3AEA-1069-A2DD-08002B30309D") == 1);
}
+/* Test guestfs_int_drive_name. */
+static void
+test_drive_name (void)
+{
+ char s[10];
+
+ guestfs_int_drive_name (0, s);
+ assert (STREQ (s, "a"));
+ guestfs_int_drive_name (25, s);
+ assert (STREQ (s, "z"));
+ guestfs_int_drive_name (26, s);
+ assert (STREQ (s, "aa"));
+ guestfs_int_drive_name (27, s);
+ assert (STREQ (s, "ab"));
+ guestfs_int_drive_name (51, s);
+ assert (STREQ (s, "az"));
+ guestfs_int_drive_name (52, s);
+ assert (STREQ (s, "ba"));
+ guestfs_int_drive_name (701, s);
+ assert (STREQ (s, "zz"));
+ guestfs_int_drive_name (702, s);
+ assert (STREQ (s, "aaa"));
+ guestfs_int_drive_name (18277, s);
+ assert (STREQ (s, "zzz"));
+}
+
int
main (int argc, char *argv[])
{
@@ -169,6 +195,7 @@ main (int argc, char *argv[])
test_concat ();
test_join ();
test_validate_guid ();
+ test_drive_name ();
exit (EXIT_SUCCESS);
}
--
2.3.1
9 years, 5 months
[PATCH v4 0/7] uuid: add btrfs uuid change support and set_uuid_random
by Chen Hanxiao
- Btrfs-progs v4.1 introduced new feature of changing
uuid of btrfs partition.
This patch add support of this.
- Introduce set_uuid_random
- uuids.c did a lot of deplicated work for changing uuid
of fs. Use existing functions.
v4: introduce get_random_uuid
improve testcases
squash internal API patches
v3.1: fix typos
v3: set errno if feature is not available.
Chen Hanxiao (7):
uuid: add support to change uuid of btrfs partition
uuid: use existing function of ext2
uuid: use newly introduced xfs_set_uuid of xfs
uuid: use newly introduced swap_set_uuid
daemon: add get_random_uuid
daemon: add functions for setting random uuid of fs
New API: set_uuid_random
appliance/packagelist.in | 1 +
daemon/btrfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++
daemon/daemon.h | 11 ++++++
daemon/ext2.c | 15 ++++++++
daemon/guestfsd.c | 19 ++++++++++
daemon/swap.c | 29 +++++++++++++++
daemon/uuids.c | 71 +++++++++++++++++-------------------
daemon/xfs.c | 14 ++++++++
generator/actions.ml | 16 +++++++++
src/MAX_PROC_NR | 2 +-
tests/btrfs/test-btrfs-misc.pl | 36 +++++++++++++++++++
11 files changed, 255 insertions(+), 40 deletions(-)
--
2.1.0
9 years, 5 months