From 75c1df70e09f6b4b3f8f1aeefb3001d745321a1a Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Tue, 30 Jun 2015 20:59:31 +0200
Subject: [PATCH] mllib: add an optional filter for rm_rf_only_files
This way it is possible to use rm_rf_only_files, but not removing
specific files.
(cherry picked from commit 775e68c4613fabaa3b48ea3dc8d45ac74b93682b)
---
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 a2513ea..5c46994 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -542,13 +542,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 4cf9259..391a216 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -111,12 +111,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 detect_file_type : string -> [`GZip | `Tar | `XZ | `Zip | `Unknown]
--
1.8.3.1