[Libguestfs] [PATCH 1/3] mllib: add an optional filter for rm_rf_only_files

Pino Toscano ptoscano at redhat.com
Wed Jul 1 13:12:53 UTC 2015


This way it is possible to use rm_rf_only_files, but not removing
specific files.
---
 mllib/common_utils.ml  | 8 +++++++-
 mllib/common_utils.mli | 5 ++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 516cff3..3737b4c 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -640,13 +640,19 @@ let rmdir_on_exit =
  * without removing the actual directory structure.  Also if 'dir' is
  * not a directory or doesn't exist, ignore it.
  *
+ * The optional filter is used to filter out files which will be
+ * removed: files returning true are not removed.
+ *
  * XXX Could be faster with a specific API for doing this.
  *)
-let rm_rf_only_files (g : Guestfs.guestfs) dir =
+let rm_rf_only_files (g : Guestfs.guestfs) ?filter 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
+    let files = match filter with
+    | None -> files
+    | Some f -> List.filter (fun x -> not (f x)) files in
     List.iter g#rm files
   )
 
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index bf0cba6..a5b0a68 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -140,12 +140,15 @@ val unlink_on_exit : string -> unit
 val rmdir_on_exit : string -> unit
 (** Remove a temporary directory on exit (using [rm -rf]). *)
 
-val rm_rf_only_files : Guestfs.guestfs -> string -> unit
+val rm_rf_only_files : Guestfs.guestfs -> ?filter:(string -> bool) -> string -> unit
 (** Using the libguestfs API, recursively remove only files from the
     given directory.  Useful for cleaning [/var/cache] etc in sysprep
     without removing the actual directory structure.  Also if [dir] is
     not a directory or doesn't exist, ignore it.
 
+    The optional [filter] is used to filter out files which will be
+    removed: files returning true are not removed.
+
     XXX Could be faster with a specific API for doing this. *)
 
 val truncate_recursive : Guestfs.guestfs -> string -> unit
-- 
2.1.0




More information about the Libguestfs mailing list