[Libguestfs] [PATCH 3/4] sysprep: Add a new operation to remove editor backup files (RHBZ#1401320).

Richard W.M. Jones
Wed, 14 Dec 2016 10:26:46 -0000

Remove editor backup files such as *~ and *.bak wherever
they occur within the guest filesystem.

This also includes a test.
---
 sysprep/Makefile.am                       |  2 +
 sysprep/sysprep_operation_backup_files.ml | 65 +++++++++++++++++++++++++++++++
 sysprep/test-virt-sysprep-backup-files.sh | 61 +++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 sysprep/sysprep_operation_backup_files.ml
 create mode 100755 sysprep/test-virt-sysprep-backup-files.sh

diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index e52f0d3..c9ce3b0 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -29,6 +29,7 @@ EXTRA_DIST = \
 # Filenames sysprep_operation_<name>.ml in alphabetical order.
 operations = \
 	abrt_data \
+	backup_files \
 	bash_history \
 	blkid_tab \
 	ca_certificates \
@@ -179,6 +180,7 @@ TESTS = \
 if ENABLE_APPLIANCE
 TESTS += \
 	test-virt-sysprep.sh \
+	test-virt-sysprep-backup-files.sh \
 	test-virt-sysprep-passwords.sh

 if HAVE_FUSE
diff --git a/sysprep/sysprep_operation_backup_files.ml
b/sysprep/sysprep_operation_backup_files.ml
new file mode 100644
index 0000000..ec570f3
--- /dev/null
+++ b/sysprep/sysprep_operation_backup_files.ml
@@ -0,0 +1,65 @@
+(* virt-sysprep
+ * Copyright (C) 2012-2016 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Printf
+
+open Sysprep_operation
+open Common_gettext.Gettext
+open Visit
+open Fnmatch
+
+module G = Guestfs
+
+let globs = List.sort compare [
+  "*.bak";
+  "*~";
+]
+let globs_as_pod = String.concat "\n" (List.map ((^) " ")
globs)
+
+let backup_files_perform (g : Guestfs.guestfs) root side_effects +  visit
g#ocaml_handle "/" (
+    fun dir filename { G.st_mode = mode } _ ->
+      match dir, filename, mode with
+      | _, None, _ -> () (* root directory, ignore *)
+      | _, Some _, mode when not (is_reg mode) -> () (* ignore non-reg files
*)
+      | dir, Some filename, _ ->
+         (* Check the filename against all of the globs, and if it
+          * matches any then delete it.
+          *)
+         let matching glob = fnmatch glob filename [FNM_NOESCAPE] in
+         if List.exists matching globs then (
+           let path = full_path dir (Some filename) in
+           g#rm_f path
+         )
+  )
+
+let op = {
+  defaults with
+    name = "backup-files";
+    enabled_by_default = true;
+    heading = s_"Remove editor backup files from the guest";
+    pod_description = Some (
+      sprintf (f_"\
+The following files are removed from anywhere in the guest
+filesystem:
+
+%s") globs_as_pod);
+    perform_on_filesystems = Some backup_files_perform;
+}
+
+let () = register_operation op
diff --git a/sysprep/test-virt-sysprep-backup-files.sh
b/sysprep/test-virt-sysprep-backup-files.sh
new file mode 100755
index 0000000..3d36997
--- /dev/null
+++ b/sysprep/test-virt-sysprep-backup-files.sh
@@ -0,0 +1,61 @@
+#!/bin/bash -
+# libguestfs virt-sysprep test script
+# Copyright (C) 2016 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+export LANG=C
+set -e
+
+# Test removal of editor backup files.
+
+if [ "$(guestfish get-backend)" = "uml" ]; then
+    echo "$0: skipping test because uml backend does not support
qcow2"
+    exit 77
+fi
+
+if [ ! -s ../test-data/phony-guests/fedora.img ]; then
+    echo "$0: skipping test because there is no phony Fedora test
image"
+    exit 77
+fi
+
+rm -f test-backup-files.qcow2
+rm -f test-backup-files-before
+rm -f test-backup-files-after
+
+# Add some backup files to the Fedora image.
+guestfish -- \
+          disk-create test-backup-files.qcow2 qcow2 -1 \
+            backingfile:../test-data/phony-guests/fedora.img \
+            backingformat:raw
+guestfish -a test-backup-files.qcow2 -i <<'EOF'
+find / | cat > test-backup-files-before
+touch /bin/test~
+touch /etc/fstab.bak
+EOF
+
+# Run virt-sysprep backup-files operation only.
+
+virt-sysprep -x --format qcow2 -a test-backup-files.qcow2 \
+    --enable backup-files
+
+# Check the file list is the same as above.
+guestfish -a test-backup-files.qcow2 -i find / > test-backup-files-after
+
+diff -u test-backup-files-before test-backup-files-after
+
+rm test-backup-files.qcow2
+rm test-backup-files-before
+rm test-backup-files-after
--
2.10.2