[Libguestfs] [PATCH v2] customize: add --truncate-recursive option

Maros Zatko mzatko at redhat.com
Thu Mar 5 15:42:45 UTC 2015


Allows user to recursively truncate files in PATH. e.g.:
virt-builder --truncate-recursive /var/log

Relates to RHBZ#119673
---
 builder/cmdline.ml         | 3 ++-
 customize/customize_run.ml | 4 ++++
 generator/customize.ml     | 8 ++++++++
 mllib/common_utils.ml      | 8 ++++++++
 mllib/common_utils.mli     | 2 ++
 5 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index b7e7e07..debc789 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -313,7 +313,8 @@ read the man page virt-builder(1).
           | `Delete _ | `Edit _ | `FirstbootCommand _ | `FirstbootPackages _
           | `FirstbootScript _ | `Hostname _ | `Link _ | `Mkdir _
           | `Password _ | `RootPassword _ | `Scrub _ | `SSHInject _
-          | `Truncate _ | `Timezone _ | `Upload _ | `Write _ | `Chmod _
+          | `Timezone _ | `Truncate _ | `TruncateRecursive _
+          | `Upload _ | `Write _ | `Chmod _
           | `CommandsFromFile _ | `CopyIn _ -> false
         ) ops.ops in
         if requires_execute_on_guest then
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
index 921bc7e..73b4d8a 100644
--- a/customize/customize_run.ml
+++ b/customize/customize_run.ml
@@ -251,6 +251,10 @@ exec >>%s 2>&1
       msg (f_"Truncating: %s") path;
       g#truncate path
 
+    | `TruncateRecursive path ->
+      msg (f_"Recursively truncating: %s") path;
+      truncate_recursive g path
+
     | `Timezone tz ->
       msg (f_"Setting the timezone: %s") tz;
       if not (Timezone.set_timezone g root tz) then
diff --git a/generator/customize.ml b/generator/customize.ml
index f7ec3f2..bfb37f7 100644
--- a/generator/customize.ml
+++ b/generator/customize.ml
@@ -326,6 +326,14 @@ This command truncates \"path\" to a zero-length file. The file must exist
 already.";
   };
 
+  { op_name = "truncate-recursive";
+    op_type = String "PATH";
+    op_discrim = "`TruncateRecursive";
+    op_shortdesc = "Recursively truncate all files in directory";
+    op_pod_longdesc = "\
+This command recursively truncates all files under \"path\" to zero-length.";
+  };
+
   { op_name = "timezone";
     op_type = String "TIMEZONE";
     op_discrim = "`Timezone";
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 76d8b79..0219b7e 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -619,6 +619,14 @@ let rm_rf_only_files (g : Guestfs.guestfs) dir =
     List.iter g#rm files
   )
 
+let truncate_recursive (g : Guestfs.guestfs) dir =
+  if g#is_dir dir then (
+    let files = Array.map (Filename.concat dir) (g#find dir) in
+    let files = Array.to_list files in
+    let files = List.filter g#is_file files in
+    List.iter g#truncate files
+  )
+
 (* Detect type of a file. *)
 let detect_file_type filename =
   let chan = open_in filename in
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 28ba648..15bd54e 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -129,6 +129,8 @@ val rm_rf_only_files : Guestfs.guestfs -> string -> unit
 
     XXX Could be faster with a specific API for doing this. *)
 
+val truncate_recursive : Guestfs.guestfs -> string -> unit
+
 val detect_file_type : string -> [`GZip | `Tar | `XZ | `Zip | `Unknown]
 (** Detect type of a file. *)
 
-- 
1.9.3




More information about the Libguestfs mailing list