Blame SOURCES/0200-mllib-add-and-use-last_part_of.patch

ffd6ed
From 9a303cb6aaa775f2eed1c8433541d9cddb199f36 Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Wed, 1 Jul 2015 14:35:31 +0200
ffd6ed
Subject: [PATCH] mllib: add and use last_part_of
ffd6ed
ffd6ed
Collect this small snippet to get the part of a string after the last
ffd6ed
occurrency of a character; replace with it the current snippets doing
ffd6ed
the same.
ffd6ed
ffd6ed
Should be just code motion.
ffd6ed
ffd6ed
(cherry picked from commit a614f3451d1e2cc4f29b1bc7f0d88519c432b2c8)
ffd6ed
---
ffd6ed
 customize/password.ml                     |  5 +++--
ffd6ed
 mllib/common_utils.ml                     |  7 +++++++
ffd6ed
 mllib/common_utils.mli                    |  3 +++
ffd6ed
 sysprep/sysprep_operation_user_account.ml |  5 +++--
ffd6ed
 v2v/convert_linux.ml                      | 10 +++-------
ffd6ed
 v2v/utils.ml                              | 16 ++++++++++------
ffd6ed
 6 files changed, 29 insertions(+), 17 deletions(-)
ffd6ed
ffd6ed
diff --git a/customize/password.ml b/customize/password.ml
ffd6ed
index 2bbfbbc..c6a155d 100644
ffd6ed
--- a/customize/password.ml
ffd6ed
+++ b/customize/password.ml
ffd6ed
@@ -99,8 +99,9 @@ let rec set_linux_passwords ~prog ?password_crypto (g : Guestfs.guestfs) root pa
ffd6ed
   List.iter (
ffd6ed
     fun userpath ->
ffd6ed
       let user =
ffd6ed
-        let i = String.rindex userpath '/' in
ffd6ed
-        String.sub userpath (i+1) (String.length userpath -i-1) in
ffd6ed
+        match last_part_of userpath '/' with
ffd6ed
+        | Some x -> x
ffd6ed
+        | None -> error ~prog "password: missing '/' in %s" userpath in
ffd6ed
       try
ffd6ed
         (* Each line is: "user:[!!]password:..."
ffd6ed
          * !! at the front of the password field means the account is locked.
ffd6ed
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
ffd6ed
index 5c46994..8f0f065 100644
ffd6ed
--- a/mllib/common_utils.ml
ffd6ed
+++ b/mllib/common_utils.ml
ffd6ed
@@ -608,3 +608,10 @@ let guest_arch_compatible guest_arch =
ffd6ed
   | x, y when x = y -> true
ffd6ed
   | "x86_64", ("i386"|"i486"|"i586"|"i686") -> true
ffd6ed
   | _ -> false
ffd6ed
+
ffd6ed
+(** Return the last part of a string, after the specified separator. *)
ffd6ed
+let last_part_of str sep =
ffd6ed
+  try
ffd6ed
+    let i = String.rindex str sep in
ffd6ed
+    Some (String.sub str (i+1) (String.length str - (i+1)))
ffd6ed
+  with Not_found -> None
ffd6ed
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
ffd6ed
index 391a216..ad2b4c7 100644
ffd6ed
--- a/mllib/common_utils.mli
ffd6ed
+++ b/mllib/common_utils.mli
ffd6ed
@@ -136,3 +136,6 @@ val absolute_path : string -> string
ffd6ed
 val guest_arch_compatible : string -> bool
ffd6ed
 (** Are guest arch and host_cpu compatible, in terms of being able
ffd6ed
     to run commands in the libguestfs appliance? *)
ffd6ed
+
ffd6ed
+val last_part_of : string -> char -> string option
ffd6ed
+(** Return the last part of a string, after the specified separator. *)
ffd6ed
diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml
ffd6ed
index 2d231cd..d479a89 100644
ffd6ed
--- a/sysprep/sysprep_operation_user_account.ml
ffd6ed
+++ b/sysprep/sysprep_operation_user_account.ml
ffd6ed
@@ -68,8 +68,9 @@ let user_account_perform ~verbose ~quiet g root side_effects =
ffd6ed
         let uid = g#aug_get uid in
ffd6ed
         let uid = int_of_string uid in
ffd6ed
         let username =
ffd6ed
-          let i = String.rindex userpath '/' in
ffd6ed
-          String.sub userpath (i+1) (String.length userpath -i-1) in
ffd6ed
+          match last_part_of userpath '/' with
ffd6ed
+          | Some x -> x
ffd6ed
+          | None -> error ~prog "user-accounts: missing '/' in %s" userpath in
ffd6ed
         if uid >= uid_min && uid <= uid_max
ffd6ed
            && check_remove_user username then (
ffd6ed
           (* Get the home before removing the passwd entry. *)
ffd6ed
diff --git a/v2v/convert_linux.ml b/v2v/convert_linux.ml
ffd6ed
index a1f5550..8264e80 100644
ffd6ed
--- a/v2v/convert_linux.ml
ffd6ed
+++ b/v2v/convert_linux.ml
ffd6ed
@@ -790,13 +790,9 @@ let rec convert ~verbose ~keep_serial_console (g : G.guestfs) inspect source =
ffd6ed
        *)
ffd6ed
       let mkinitrd_kv =
ffd6ed
         let modpath = kernel.ki_modpath in
ffd6ed
-        let len = String.length modpath in
ffd6ed
-        try
ffd6ed
-          let i = String.rindex modpath '/' in
ffd6ed
-          String.sub modpath (i+1) (len - (i+1))
ffd6ed
-        with
ffd6ed
-          Not_found ->
ffd6ed
-            invalid_arg (sprintf "invalid module path: %s" modpath) in
ffd6ed
+        match last_part_of modpath '/' with
ffd6ed
+        | Some x -> x
ffd6ed
+        | None -> invalid_arg (sprintf "invalid module path: %s" modpath) in
ffd6ed
 
ffd6ed
       if g#is_file ~followsymlinks:true "/sbin/dracut" then (
ffd6ed
         (* Dracut. *)
ffd6ed
diff --git a/v2v/utils.ml b/v2v/utils.ml
ffd6ed
index e49c47b..0e06913 100644
ffd6ed
--- a/v2v/utils.ml
ffd6ed
+++ b/v2v/utils.ml
ffd6ed
@@ -175,9 +175,11 @@ let find_virtio_win_drivers ~verbose virtio_win =
ffd6ed
         let paths = List.filter (g#is_file ~followsymlinks:false) paths in
ffd6ed
         List.map (
ffd6ed
           fun path ->
ffd6ed
-            let i = String.rindex path '/' in
ffd6ed
-            let len = String.length path in
ffd6ed
-            let basename = String.sub path (i+1) (len - (i+1)) in
ffd6ed
+            let basename =
ffd6ed
+              match last_part_of path '/' with
ffd6ed
+              | Some x -> x
ffd6ed
+              | None ->
ffd6ed
+                error "v2v/find_virtio_win_drivers: missing '/' in %s" path in
ffd6ed
             (path, sprintf "%s:%s" virtio_win path,
ffd6ed
              basename,
ffd6ed
              fun () -> g#read_file path)
ffd6ed
@@ -198,9 +200,11 @@ let find_virtio_win_drivers ~verbose virtio_win =
ffd6ed
           let lc_basename = String.lowercase basename in
ffd6ed
 
ffd6ed
           let extension =
ffd6ed
-            let i = String.rindex lc_basename '.' in
ffd6ed
-            let len = String.length lc_basename in
ffd6ed
-            String.sub lc_basename (i+1) (len - (i+1)) in
ffd6ed
+            match last_part_of lc_basename '.' with
ffd6ed
+            | Some x -> x
ffd6ed
+            | None ->
ffd6ed
+              error "v2v/find_virtio_win_drivers: missing '.' in %s"
ffd6ed
+                lc_basename in
ffd6ed
 
ffd6ed
           (* Skip files without specific extensions. *)
ffd6ed
           if extension <> "cat" && extension <> "inf" &&
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed