[Pkg-libvirt-commits] [libguestfs] 159/165: v2v: Refactor common code for listing files in packages.

Hilko Bengen bengen at moszumanska.debian.org
Sat Aug 30 08:25:25 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit 77a963886fc803de8f776ad0ba8d30634ebb9a95
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Mon Jun 23 17:10:50 2014 +0100

    v2v: Refactor common code for listing files in packages.
    
    At some point we should make these into real libguestfs inspection
    APIs.  However they are fairly hard to implement, at least for RPM.
---
 v2v/convert_linux.ml |  2 +-
 v2v/lib_linux.ml     | 48 ++++++++++++++++++++++++++++++++----------------
 v2v/lib_linux.mli    |  8 +++++++-
 3 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/v2v/convert_linux.ml b/v2v/convert_linux.ml
index f41a27b..739179b 100644
--- a/v2v/convert_linux.ml
+++ b/v2v/convert_linux.ml
@@ -106,7 +106,7 @@ Grub1/grub-legacy error was: %s")
 
       (* Check it's not owned by an installed application. *)
       let dirs = List.filter (
-        fun d -> not (Lib_linux.file_owned verbose g inspect d)
+        fun d -> not (Lib_linux.is_file_owned verbose g inspect d)
       ) dirs in
 
       (* Remove any unowned xenpv directories. *)
diff --git a/v2v/lib_linux.ml b/v2v/lib_linux.ml
index dc33607..580c5ec 100644
--- a/v2v/lib_linux.ml
+++ b/v2v/lib_linux.ml
@@ -116,16 +116,41 @@ let remove verbose g inspect packages =
         format (String.concat " " packages)
   )
 
-let file_owned verbose g inspect file =
-  let root = inspect.i_root in
-  let package_format = g#inspect_get_package_format root in
+let file_list_of_package verbose (g : Guestfs.guestfs) inspect name =
+  let package_format = g#inspect_get_package_format inspect.i_root in
+
   match package_format with
   | "rpm" ->
-      let cmd = [| "rpm"; "-qf"; file |] in
-      (try ignore (g#command cmd); true with Guestfs.Error _ -> false)
+    let cmd = [| "rpm"; "-ql"; name |] in
+    if verbose then eprintf "%s\n%!" (String.concat " " (Array.to_list cmd));
+    Array.to_list (g#command_lines cmd)
+  | format ->
+    error (f_"don't know how to get list of files from package using %s")
+      format
+
+let rec file_owner verbose g inspect path =
+  let package_format = g#inspect_get_package_format inspect.i_root in
+  match package_format with
+  | "rpm" ->
+      (* Although it is possible in RPM for multiple packages to own
+       * a file, this deliberately only returns one package.
+       *)
+      let cmd = [| "rpm"; "-qf"; "--qf"; "%{NAME}"; path |] in
+      if verbose then eprintf "%s\n%!" (String.concat " " (Array.to_list cmd));
+      (try g#command cmd
+       with Guestfs.Error msg as exn ->
+         if string_find msg "is not owned" >= 0 then
+           raise Not_found
+         else
+           raise exn
+      )
 
   | format ->
-    error (f_"don't know how to find package owner using %s") format
+    error (f_"don't know how to find file owner using %s") format
+
+and is_file_owned verbose g inspect path =
+  try file_owner verbose g inspect path; true
+  with Not_found -> false
 
 type kernel_info = {
   base_package : string;          (* base package, eg. "kernel-PAE" *)
@@ -139,16 +164,7 @@ type kernel_info = {
  * understand what on earth it was doing.  - RWMJ
  *)
 let inspect_linux_kernel verbose (g : Guestfs.guestfs) inspect path =
-  let root = inspect.i_root in
-
-  let base_package =
-    let package_format = g#inspect_get_package_format root in
-    match package_format with
-    | "rpm" ->
-      let cmd = [| "rpm"; "-qf"; "--qf"; "%{NAME}"; path |] in
-      g#command cmd
-    | format ->
-      error (f_"don't know how to inspect kernel using %s") format in
+  let base_package = file_owner verbose g inspect path in
 
   (* Try to get kernel version by examination of the binary.
    * See supermin.git/src/kernel.ml
diff --git a/v2v/lib_linux.mli b/v2v/lib_linux.mli
index 5c642de..94983c6 100644
--- a/v2v/lib_linux.mli
+++ b/v2v/lib_linux.mli
@@ -31,7 +31,13 @@ val install : bool -> Guestfs.guestfs -> Types.inspect -> string list -> unit
 val remove : bool -> Guestfs.guestfs -> Types.inspect -> string list -> unit
 (** Uninstall package(s). *)
 
-val file_owned : bool -> Guestfs.guestfs -> Types.inspect -> string -> bool
+val file_list_of_package : bool -> Guestfs.guestfs -> Types.inspect -> string -> string list
+(** Return list of files owned by package. *)
+
+val file_owner : bool -> Guestfs.guestfs -> Types.inspect -> string -> string
+(** Return the name of the package that owns a file. *)
+
+val is_file_owned : bool -> Guestfs.guestfs -> Types.inspect -> string -> bool
 (** Returns true if the file is owned by an installed package. *)
 
 type kernel_info = {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list