Maros Zatko
2015-Mar-05 15:42 UTC
[Libguestfs] [PATCH v2] customize: add --truncate-recursive option
Allows user to recursively truncate all files in a directory. Related to RHBZ#119673 Maros Zatko (1): customize: add --truncate-recursive option 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(-) -- 1.9.3
Maros Zatko
2015-Mar-05 15:42 UTC
[Libguestfs] [PATCH v2] customize: add --truncate-recursive option
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
Richard W.M. Jones
2015-Mar-23 12:39 UTC
Re: [Libguestfs] [PATCH v2] customize: add --truncate-recursive option
Thanks - ACKed and pushed. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Possibly Parallel Threads
- [PATCH] customize: add --truncate-recursive option
- [PATCH v2] customize: add --truncate-recursive option
- [PATCH] customize: add --move
- [PATCH] customize: Give an error if --truncate-recursive path does not exist (RHBZ#1345809).
- [PATCH 1/3] mllib: add an optional filter for rm_rf_only_files