From 2ddaafb1b90169e4cb35b2e47116a95ff9045f6b Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 9 Sep 2016 14:45:05 +0100 Subject: [PATCH] v2v: -i disk: Move code that creates name from disk to separate module. Simple code motion. (cherry picked from commit 312379c8a61cd4a16aa7c80c1adda3081e601d43) --- v2v/Makefile.am | 2 ++ v2v/input_disk.ml | 21 ++------------------- v2v/name_from_disk.ml | 41 +++++++++++++++++++++++++++++++++++++++++ v2v/name_from_disk.mli | 24 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 v2v/name_from_disk.ml create mode 100644 v2v/name_from_disk.mli diff --git a/v2v/Makefile.am b/v2v/Makefile.am index c1ebb17..f9a0796 100644 --- a/v2v/Makefile.am +++ b/v2v/Makefile.am @@ -62,6 +62,7 @@ SOURCES_MLI = \ inspect_source.mli \ linux.mli \ modules_list.mli \ + name_from_disk.mli \ output_glance.mli \ output_libvirt.mli \ output_local.mli \ @@ -84,6 +85,7 @@ SOURCES_ML = \ xml.ml \ uefi.ml \ utils.ml \ + name_from_disk.ml \ vCenter.ml \ domainxml.ml \ DOM.ml \ diff --git a/v2v/input_disk.ml b/v2v/input_disk.ml index b6c9089..9a2ef08 100644 --- a/v2v/input_disk.ml +++ b/v2v/input_disk.ml @@ -23,6 +23,7 @@ open Common_utils open Types open Utils +open Name_from_disk class input_disk input_format disk = object inherit input @@ -42,25 +43,7 @@ class input_disk input_format disk = object * the filename passed in. Users can override this using the * `-on name' option. *) - let name = - 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; + let name = name_from_disk disk in (* Get the absolute path to the disk file. *) let disk_absolute = absolute_path disk in diff --git a/v2v/name_from_disk.ml b/v2v/name_from_disk.ml new file mode 100644 index 0000000..73caf34 --- /dev/null +++ b/v2v/name_from_disk.ml @@ -0,0 +1,41 @@ +(* virt-v2v + * Copyright (C) 2009-2016 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + *) + +open Common_gettext.Gettext +open Common_utils + +let name_from_disk disk = + 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 + let name = loop suffixes in + if name = "" then + error (f_"invalid input filename (%s)") disk; + name diff --git a/v2v/name_from_disk.mli b/v2v/name_from_disk.mli new file mode 100644 index 0000000..db3ee17 --- /dev/null +++ b/v2v/name_from_disk.mli @@ -0,0 +1,24 @@ +(* virt-v2v + * Copyright (C) 2009-2016 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + *) + +(** Derive the source name from a disk name. *) + +val name_from_disk : string -> string +(** Take a disk name and derive from it a suitable source name. + + Used in particular by [-i disk] mode. *) -- 2.7.4