Pino Toscano
2016-Aug-08 15:21 UTC
[Libguestfs] [PATCH v2] v2v: disk: strip only common extension (RHBZ#1365005)
In disk input mode, to determine the guest name strip the extension from the filename only if it's a very well extension for disk images. --- v2v/input_disk.ml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/v2v/input_disk.ml b/v2v/input_disk.ml index 17ad61d..d56c476 100644 --- a/v2v/input_disk.ml +++ b/v2v/input_disk.ml @@ -42,9 +42,23 @@ class input_disk input_format disk = object * the filename passed in. Users can override this using the * `-on name' option. *) - let name = Filename.basename disk in let name - try Filename.chop_extension name with Invalid_argument _ -> name in + let name = Filename.basename disk in + (* Remove the extension (or suffix), only if it's one usually + * used for disk images. *) + let suffixes = [ + ".img"; ".qcow2"; ".raw"; ".vmdk"; + "-sda"; + ] in + let rec loop = function + | suff :: xs -> + if Filename.check_suffix name suff then + Filename.chop_suffix name suff + else + loop xs + | [] -> name + in + loop suffixes in if name = "" then error (f_"-i disk: invalid input filename (%s)") disk; -- 2.7.4
Richard W.M. Jones
2016-Aug-08 15:32 UTC
Re: [Libguestfs] [PATCH v2] v2v: disk: strip only common extension (RHBZ#1365005)
On Mon, Aug 08, 2016 at 05:21:35PM +0200, Pino Toscano wrote:> In disk input mode, to determine the guest name strip the extension from > the filename only if it's a very well extension for disk images. > --- > v2v/input_disk.ml | 18 ++++++++++++++++-- > 1 file changed, 16 insertions(+), 2 deletions(-) > > diff --git a/v2v/input_disk.ml b/v2v/input_disk.ml > index 17ad61d..d56c476 100644 > --- a/v2v/input_disk.ml > +++ b/v2v/input_disk.ml > @@ -42,9 +42,23 @@ class input_disk input_format disk = object > * the filename passed in. Users can override this using the > * `-on name' option. > *) > - let name = Filename.basename disk in > let name > - try Filename.chop_extension name with Invalid_argument _ -> name in > + let name = Filename.basename disk in > + (* Remove the extension (or suffix), only if it's one usually > + * used for disk images. *) > + let suffixes = [ > + ".img"; ".qcow2"; ".raw"; ".vmdk"; > + "-sda"; > + ] in > + let rec loop = function > + | suff :: xs -> > + if Filename.check_suffix name suff then > + Filename.chop_suffix name suff > + else > + loop xs > + | [] -> name > + in > + loop suffixes in > if name = "" then > error (f_"-i disk: invalid input filename (%s)") disk;ACK Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Apparently Analagous Threads
- [PATCH] v2v: disk: strip only common extension (RHBZ#1365005)
- [PATCH 1/2] v2v: -i disk: Move code that creates name from disk to separate module.
- [PATCH 0/2] v2v: -i ova: Derive the name from the OVA filename.
- [PATCH] sparsify, v2v: use Common_utils.absolute_path
- [PATCH 1/2] v2v: register also aliases of input/output modules