It will be useful also for APIs different than tar-out, so move it to
guestfsd.c, and add it a parameter to specify the function name that
invoked it.
This is mostly code motion.
---
daemon/daemon.h | 2 ++
daemon/guestfsd.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
daemon/tar.c | 58 +--------------------------------------------------
3 files changed, 65 insertions(+), 57 deletions(-)
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 8097bfc..793074d 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -147,6 +147,8 @@ extern int random_name (char *template);
extern char *get_random_uuid (void);
+extern char *make_exclude_from_file (const char *function, char *const *excludes);
+
extern int asprintf_nowarn (char **strp, const char *fmt, ...);
/*-- in names.c (auto-generated) --*/
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 57e3886..a848fe1 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -1247,6 +1247,68 @@ get_random_uuid (void)
}
+/**
+ * Turn list C<excludes> into a temporary file, and return a string
+ * containing the temporary file name. Caller must unlink the file
+ * and free the string.
+ *
+ * C<function> is the function that invoked this helper, and it is
+ * used mainly for errors/debugging.
+ */
+char *
+make_exclude_from_file (const char *function, char *const *excludes)
+{
+ size_t i;
+ int fd;
+ char template[] = "/tmp/excludesXXXXXX";
+ char *ret;
+
+ fd = mkstemp (template);
+ if (fd == -1) {
+ reply_with_perror ("mkstemp");
+ return NULL;
+ }
+
+ for (i = 0; excludes[i] != NULL; ++i) {
+ if (strchr (excludes[i], '\n')) {
+ reply_with_error ("%s: excludes file patterns cannot contain \\n
character",
+ function);
+ goto error;
+ }
+
+ if (xwrite (fd, excludes[i], strlen (excludes[i])) == -1 ||
+ xwrite (fd, "\n", 1) == -1) {
+ reply_with_perror ("write");
+ goto error;
+ }
+
+ if (verbose)
+ fprintf (stderr, "%s: adding excludes pattern '%s'\n",
+ function, excludes[i]);
+ }
+
+ if (close (fd) == -1) {
+ reply_with_perror ("close");
+ fd = -1;
+ goto error;
+ }
+ fd = -1;
+
+ ret = strdup (template);
+ if (ret == NULL) {
+ reply_with_perror ("strdup");
+ goto error;
+ }
+
+ return ret;
+
+ error:
+ if (fd >= 0)
+ close (fd);
+ unlink (template);
+ return NULL;
+}
+
void
cleanup_free_mountable (mountable_t *mountable)
{
diff --git a/daemon/tar.c b/daemon/tar.c
index ea8b342..c23aa0a 100644
--- a/daemon/tar.c
+++ b/daemon/tar.c
@@ -268,62 +268,6 @@ do_txz_in (const char *dir)
return do_tar_in (dir, "xz", 0, 0, 0);
}
-/* Turn list 'excludes' into a temporary file, and return a string
- * containing the temporary file name. Caller must unlink the file
- * and free the string.
- */
-static char *
-make_exclude_from_file (char *const *excludes)
-{
- size_t i;
- int fd;
- char template[] = "/tmp/excludesXXXXXX";
- char *ret;
-
- fd = mkstemp (template);
- if (fd == -1) {
- reply_with_perror ("mkstemp");
- return NULL;
- }
-
- for (i = 0; excludes[i] != NULL; ++i) {
- if (strchr (excludes[i], '\n')) {
- reply_with_error ("tar-out: excludes file patterns cannot contain \\n
character");
- goto error;
- }
-
- if (xwrite (fd, excludes[i], strlen (excludes[i])) == -1 ||
- xwrite (fd, "\n", 1) == -1) {
- reply_with_perror ("write");
- goto error;
- }
-
- if (verbose)
- fprintf (stderr, "tar-out: adding excludes pattern '%s'\n",
excludes[i]);
- }
-
- if (close (fd) == -1) {
- reply_with_perror ("close");
- fd = -1;
- goto error;
- }
- fd = -1;
-
- ret = strdup (template);
- if (ret == NULL) {
- reply_with_perror ("strdup");
- goto error;
- }
-
- return ret;
-
- error:
- if (fd >= 0)
- close (fd);
- unlink (template);
- return NULL;
-}
-
/* Has one FileOut parameter. */
/* Takes optional arguments, consult optargs_bitmask. */
int
@@ -367,7 +311,7 @@ do_tar_out (const char *dir, const char *compress, int numericowner,
numericowner = 0;
if ((optargs_bitmask & GUESTFS_TAR_OUT_EXCLUDES_BITMASK)) {
- exclude_from_file = make_exclude_from_file (excludes);
+ exclude_from_file = make_exclude_from_file ("tar-out", excludes);
if (!exclude_from_file)
return -1;
}
--
2.9.3