daemon: move make_exclude_from_file as common helper

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.
This commit is contained in:
Pino Toscano
2017-02-13 14:20:59 +01:00
parent 468d335fdf
commit 35d97daa5a
3 changed files with 65 additions and 57 deletions

View File

@@ -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;
}