[Libguestfs] [PATCH 1/2] package handlers: add possibility for final teardown

Pino Toscano ptoscano at redhat.com
Wed Sep 17 11:58:24 UTC 2014


Add a ph_fini callback to package handlers, so they can do teardown
operations, if needed, at the very end of the supermin run.

Currently all of the current package handlers do nothing.
---
 src/dpkg.ml             |  1 +
 src/package_handler.ml  | 10 ++++++++++
 src/package_handler.mli | 10 ++++++++++
 src/pacman.ml           |  1 +
 src/rpm.ml              |  1 +
 src/supermin.ml         |  4 +++-
 6 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/dpkg.ml b/src/dpkg.ml
index 1bb3f7f..ed899cf 100644
--- a/src/dpkg.ml
+++ b/src/dpkg.ml
@@ -191,6 +191,7 @@ let () =
   let ph = {
     ph_detect = dpkg_detect;
     ph_init = dpkg_init;
+    ph_fini = PHNoFini;
     ph_package_of_string = dpkg_package_of_string;
     ph_package_to_string = dpkg_package_to_string;
     ph_package_name = dpkg_package_name;
diff --git a/src/package_handler.ml b/src/package_handler.ml
index b1dffc0..74d68f4 100644
--- a/src/package_handler.ml
+++ b/src/package_handler.ml
@@ -59,6 +59,7 @@ let file_source file =
 type package_handler = {
   ph_detect : unit -> bool;
   ph_init : settings -> unit;
+  ph_fini : ph_fini;
   ph_package_of_string : string -> package option;
   ph_package_to_string : package -> string;
   ph_package_name : package -> string;
@@ -76,6 +77,9 @@ and ph_get_files =
 and ph_download_package =
 | PHDownloadPackage of (package -> string -> unit)
 | PHDownloadAllPackages of (PackageSet.t -> string -> unit)
+and ph_fini =
+| PHNoFini
+| PHFini of (unit -> unit)
 
 (* Suggested memoization functions. *)
 let get_memo_functions () =
@@ -140,6 +144,12 @@ let rec get_package_handler_name () =
   | Some (system, packager, _) -> sprintf "%s/%s" system packager
   | None -> assert false
 
+let package_handler_shutdown () =
+  let ph = get_package_handler () in
+  match ph.ph_fini with
+  | PHNoFini -> ()
+  | PHFini f -> f ()
+
 let get_all_requires pkgs =
   let ph = get_package_handler () in
   match ph.ph_get_requires with
diff --git a/src/package_handler.mli b/src/package_handler.mli
index 7e17981..7bdf0e8 100644
--- a/src/package_handler.mli
+++ b/src/package_handler.mli
@@ -99,6 +99,11 @@ type package_handler = {
       initializes.  The [settings] parameter is a struct of general
       settings and configuration. *)
 
+  ph_fini : ph_fini;
+  (** This is called at the end of the supermin processing.  It can
+      be used to do teardown operations for the package handler,
+      when no more package-related operations are going to be done. *)
+
   ph_package_of_string : string -> package option;
   (** Convert a string (from user input) into a package object.  If
       the package is not installed or the string is otherwise
@@ -159,6 +164,9 @@ and ph_get_files =
 and ph_download_package =
 | PHDownloadPackage of (package -> string -> unit)
 | PHDownloadAllPackages of (PackageSet.t -> string -> unit)
+and ph_fini =
+| PHNoFini
+| PHFini of (unit -> unit)
 
 (** Package handlers could use these memoization functions to convert
     from the {!package} type to an internal struct and back again, or
@@ -172,6 +180,8 @@ val list_package_handlers : unit -> unit
 
 val check_system : settings -> unit
 
+val package_handler_shutdown : unit -> unit
+
 val get_package_handler : unit -> package_handler
 
 val get_package_handler_name : unit -> string
diff --git a/src/pacman.ml b/src/pacman.ml
index 8b11ba8..2395796 100644
--- a/src/pacman.ml
+++ b/src/pacman.ml
@@ -227,6 +227,7 @@ let () =
   let ph = {
     ph_detect = pacman_detect;
     ph_init = pacman_init;
+    ph_fini = PHNoFini;
     ph_package_of_string = pacman_package_of_string;
     ph_package_to_string = pacman_package_to_string;
     ph_package_name = pacman_package_name;
diff --git a/src/rpm.ml b/src/rpm.ml
index 1195948..e0cdb39 100644
--- a/src/rpm.ml
+++ b/src/rpm.ml
@@ -394,6 +394,7 @@ let () =
   let fedora = {
     ph_detect = fedora_detect;
     ph_init = rpm_init;
+    ph_fini = PHNoFini;
     ph_package_of_string = rpm_package_of_string;
     ph_package_to_string = rpm_package_to_string;
     ph_package_name = rpm_package_name;
diff --git a/src/supermin.ml b/src/supermin.ml
index 0153977..2ee61a9 100644
--- a/src/supermin.ml
+++ b/src/supermin.ml
@@ -261,7 +261,9 @@ let main () =
        *)
       sprintf "( chmod -R +w %s ; rm -rf %s ) 2>/dev/null &"
         (quote old_outputdir) (quote old_outputdir) in
-    ignore (Sys.command cmd)
+    ignore (Sys.command cmd);
+
+  package_handler_shutdown ()
 
 let () =
   try main ()
-- 
1.9.3




More information about the Libguestfs mailing list