[Libguestfs] [PATCH 7/7] tar-out: Add list of excluded patterns (--exclude=...) (RHBZ#847881).

Richard W.M. Jones rjones at redhat.com
Tue Aug 14 14:51:13 UTC 2012


From: "Richard W.M. Jones" <rjones at redhat.com>

---
 daemon/tar.c                   |   64 ++++++++++++++++++++++++++++++++++++----
 generator/generator_actions.ml |    7 ++++-
 2 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/daemon/tar.c b/daemon/tar.c
index f46733d..6a894e6 100644
--- a/daemon/tar.c
+++ b/daemon/tar.c
@@ -237,14 +237,53 @@ do_txz_in (const char *dir)
   return do_tar_in (dir, "xz");
 }
 
+/* Turn list 'excludes' into list of " --excludes=..." strings, all
+ * properly quoted.  Caller must free the returned string.
+ */
+static char *
+make_excludes_args (char *const *excludes)
+{
+  DECLARE_STRINGSBUF (strings);
+  size_t i;
+  char *s, *ret;
+
+  for (i = 0; excludes[i] != NULL; ++i) {
+    if (asprintf_nowarn (&s, " --exclude=%Q", excludes[i]) == -1) {
+      reply_with_perror ("asprintf");
+      free_stringslen (strings.argv, strings.size);
+      return NULL;
+    }
+    if (!add_string_nodup (&strings, s) == -1) {
+      free (s);
+      return NULL;
+    }
+  }
+
+  if (end_stringsbuf (&strings) == -1)
+    return NULL;
+
+  ret = concat_strings (&strings);
+  if (!ret) {
+    reply_with_perror ("concat");
+    free_stringslen (strings.argv, strings.size);
+    return NULL;
+  }
+
+  free_stringslen (strings.argv, strings.size);
+
+  return ret;
+}
+
 /* Has one FileOut parameter. */
 /* Takes optional arguments, consult optargs_bitmask. */
 int
-do_tar_out (const char *dir, const char *compress, int numericowner)
+do_tar_out (const char *dir, const char *compress, int numericowner,
+            char *const *excludes)
 {
   const char *filter;
   int r;
   FILE *fp;
+  char *excludes_args;
   char *cmd;
   char buf[GUESTFS_MAX_CHUNK_SIZE];
 
@@ -269,13 +308,28 @@ do_tar_out (const char *dir, const char *compress, int numericowner)
   if (!(optargs_bitmask & GUESTFS_TAR_OUT_NUMERICOWNER_BITMASK))
     numericowner = 0;
 
+  if (!(optargs_bitmask & GUESTFS_TAR_OUT_EXCLUDES_BITMASK)) {
+    excludes_args = make_excludes_args (excludes);
+    if (!excludes_args)
+      return -1;
+  } else {
+    excludes_args = strdup ("");
+    if (excludes_args == NULL) {
+      reply_with_perror ("strdup");
+      return -1;
+    }
+  }
+
   /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
-  if (asprintf_nowarn (&cmd, "tar -C %R%s%s -cf - .",
+  if (asprintf_nowarn (&cmd, "tar -C %R%s%s%s -cf - .",
                        dir, filter,
-                       numericowner ? " --numeric-owner" : "") == -1) {
+                       numericowner ? " --numeric-owner" : "",
+                       excludes_args) == -1) {
     reply_with_perror ("asprintf");
+    free (excludes_args);
     return -1;
   }
+  free (excludes_args);
 
   if (verbose)
     fprintf (stderr, "%s\n", cmd);
@@ -325,7 +379,7 @@ int
 do_tgz_out (const char *dir)
 {
   optargs_bitmask = GUESTFS_TAR_OUT_COMPRESS_BITMASK;
-  return do_tar_out (dir, "gzip", 0);
+  return do_tar_out (dir, "gzip", 0, NULL);
 }
 
 /* Has one FileOut parameter. */
@@ -333,5 +387,5 @@ int
 do_txz_out (const char *dir)
 {
   optargs_bitmask = GUESTFS_TAR_OUT_COMPRESS_BITMASK;
-  return do_tar_out (dir, "bzip2", 0);
+  return do_tar_out (dir, "bzip2", 0, NULL);
 }
diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index 19c81be..7db88ce 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -3434,7 +3434,7 @@ compression types)." };
 
   { defaults with
     name = "tar_out";
-    style = RErr, [String "directory"; FileOut "tarfile"], [OString "compress"; OBool "numericowner"];
+    style = RErr, [String "directory"; FileOut "tarfile"], [OString "compress"; OBool "numericowner"; OStringList "excludes"];
     proc_nr = Some 70;
     once_had_no_optargs = true;
     cancellable = true;
@@ -3454,6 +3454,11 @@ The other optional arguments are:
 
 =over 4
 
+=item C<excludes>
+
+A list of wildcards.  Files are excluded if they match any of the
+wildcards.
+
 =item C<numericowner>
 
 If set to true, the output tar file will contain UID/GID numbers
-- 
1.7.10.4




More information about the Libguestfs mailing list