v2v: Unquote path when removing VirtualBox Guest Additions (RHBZ#1296606).

This commit is contained in:
Richard W.M. Jones
2016-01-11 13:38:36 +00:00
parent 2e728ed0dc
commit 64af88a1b7
5 changed files with 74 additions and 2 deletions

View File

@@ -367,6 +367,7 @@ v2v_unit_tests_BOBJECTS = \
DOM.cmo \
OVF.cmo \
windows.cmo \
linux.cmo \
v2v_unit_tests.cmo
v2v_unit_tests_XOBJECTS = $(v2v_unit_tests_BOBJECTS:.cmo=.cmx)

View File

@@ -515,8 +515,13 @@ let rec convert ~keep_serial_console (g : G.guestfs) inspect source =
let lines = filter_map (
fun line ->
if Str.string_match rex line 0 then (
let vboxuninstall = Str.matched_group 1 line ^ "/uninstall.sh" in
Some vboxuninstall
let path = Str.matched_group 1 line in
let path = Linux.shell_unquote path in
if String.length path >= 1 && path.[0] = '/' then (
let vboxuninstall = path ^ "/uninstall.sh" in
Some vboxuninstall
)
else None
)
else None
) lines in

View File

@@ -178,3 +178,47 @@ let rec file_owner g inspect path =
and is_file_owned g inspect path =
try file_owner g inspect path; true
with Not_found -> false
let rec shell_unquote str =
let len = String.length str in
if len >= 2 then (
if String.is_prefix str "'" && String.is_suffix str "'" then
String.sub str 1 (len-2)
else if String.is_prefix str "\"" && String.is_suffix str "\"" then
shell_unquote_double str len
else
str
)
else str
(* https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
* but note we don't do any variable expansion etc so really we just
* handle backslash here.
*)
and shell_unquote_double str len =
let i = ref 1 and j = ref 0 in
while !i < len-1 (* ignore final quote *) do
if is_backslash_sequence str !i len then
incr i;
incr i;
incr j
done;
let outlen = !j in
let outstr = String.create outlen in
let i = ref 1 and j = ref 0 in
while !i < len-1 do
if is_backslash_sequence str !i len then
incr i;
outstr.[!j] <- str.[!i];
incr i;
incr j
done;
outstr
and is_backslash_sequence str i len =
i < len-2 (* ignore final character before the final quote *) &&
str.[i] = '\\' &&
(str.[i+1] = '$' || str.[i+1] = '`' || str.[i+1] = '"'
|| str.[i+1] = '\\' || str.[i+1] = '\n')

View File

@@ -39,3 +39,11 @@ val file_owner : Guestfs.guestfs -> Types.inspect -> string -> string
val is_file_owned : Guestfs.guestfs -> Types.inspect -> string -> bool
(** Returns true if the file is owned by an installed package. *)
val shell_unquote : string -> string
(** If the string looks like a shell quoted string, then attempt to
unquote it.
This is just intended to deal with quoting in configuration files
(like ones under /etc/sysconfig), and it doesn't deal with some
situations such as $variable interpolation. *)

View File

@@ -765,6 +765,19 @@ let test_virtio_iso_path_matches_guest_os ctx =
) all_windows
) paths
let test_shell_unquote ctx =
let printer = identity in
assert_equal ~printer "a" (Linux.shell_unquote "a");
assert_equal ~printer "b" (Linux.shell_unquote "'b'");
assert_equal ~printer "c" (Linux.shell_unquote "\"c\"");
assert_equal ~printer "dd" (Linux.shell_unquote "\"dd\"");
assert_equal ~printer "e\\e" (Linux.shell_unquote "\"e\\\\e\"");
assert_equal ~printer "f\\" (Linux.shell_unquote "\"f\\\\\"");
assert_equal ~printer "\\g" (Linux.shell_unquote "\"\\\\g\"");
assert_equal ~printer "h\\-h" (Linux.shell_unquote "\"h\\-h\"");
assert_equal ~printer "i`" (Linux.shell_unquote "\"i\\`\"");
assert_equal ~printer "j\"" (Linux.shell_unquote "\"j\\\"\"")
(* Suites declaration. *)
let suite =
"virt-v2v" >:::
@@ -774,6 +787,7 @@ let suite =
"Utils.drive_index" >:: test_drive_index;
"Windows.virtio_iso_path_matches_guest_os" >::
test_virtio_iso_path_matches_guest_os;
"Linux.shell_unquote" >:: test_shell_unquote;
]
let () =