X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Ftar.c;h=9e45dee94af7f953f0da7b2a8def086a27c711c2;hb=eb7ec6170eca278fb910e18ca1f7ca81c6b507c0;hp=ce3ca7862e74b3e01648b2683e9ce772004fd0da;hpb=53ca11ee44f71ed056f72df9769ca93b9a35e1a7;p=libguestfs.git diff --git a/daemon/tar.c b/daemon/tar.c index ce3ca78..9e45dee 100644 --- a/daemon/tar.c +++ b/daemon/tar.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009-2011 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 @@ -13,7 +13,7 @@ * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include @@ -25,7 +25,7 @@ #include "read-file.h" -#include "../src/guestfs_protocol.h" +#include "guestfs_protocol.h" #include "daemon.h" #include "actions.h" #include "optgroups.h" @@ -33,22 +33,17 @@ int optgroup_xz_available (void) { - int r = access ("/usr/bin/xz", X_OK); - return r == 0; + return prog_exists ("xz"); } -/* Redirect errors from the tar command to the error file, then - * provide functions for reading it in. We overwrite the file each - * time, and since it's small and stored on the appliance we don't - * bother to delete it. - */ -static const char *error_file = "/tmp/error"; - +/* Read the error file. Returns a string that the caller must free. */ static char * -read_error_file (void) +read_error_file (char *error_file) { size_t len; - char *str = read_file (error_file, &len); + char *str; + + str = read_file (error_file, &len); if (str == NULL) { str = strdup ("(no error)"); if (str == NULL) { @@ -79,6 +74,16 @@ do_tXz_in (const char *dir, const char *filter) int err, r; FILE *fp; char *cmd; + char error_file[] = "/tmp/tarXXXXXX"; + int fd; + + fd = mkstemp (error_file); + if (fd == -1) { + reply_with_perror ("mkstemp"); + return -1; + } + + close (fd); /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */ if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s", @@ -86,7 +91,8 @@ do_tXz_in (const char *dir, const char *filter) err = errno; r = cancel_receive (); errno = err; - if (r != -2) reply_with_perror ("asprintf"); + reply_with_perror ("asprintf"); + unlink (error_file); return -1; } @@ -98,7 +104,8 @@ do_tXz_in (const char *dir, const char *filter) err = errno; r = cancel_receive (); errno = err; - if (r != -2) reply_with_perror ("%s", cmd); + reply_with_perror ("%s", cmd); + unlink (error_file); free (cmd); return -1; } @@ -107,36 +114,39 @@ do_tXz_in (const char *dir, const char *filter) /* The semantics of fwrite are too undefined, so write to the * file descriptor directly instead. */ - int fd = fileno (fp); + fd = fileno (fp); r = receive_file (write_cb, &fd); if (r == -1) { /* write error */ - if (cancel_receive () != -2) { - char *errstr = read_error_file (); - reply_with_error ("write error on directory: %s: %s", dir, errstr); - free (errstr); - } + cancel_receive (); + char *errstr = read_error_file (error_file); + reply_with_error ("write error on directory: %s: %s", dir, errstr); + free (errstr); + unlink (error_file); pclose (fp); return -1; } if (r == -2) { /* cancellation from library */ + /* This error is ignored by the library since it initiated the + * cancel. Nevertheless we must send an error reply here. + */ + reply_with_error ("file upload cancelled"); pclose (fp); - /* Do NOT send any error. */ + unlink (error_file); return -1; } if (pclose (fp) != 0) { - if (r == -1) /* if r == 0, file transfer ended already */ - r = cancel_receive (); - if (r != -2) { - char *errstr = read_error_file (); - reply_with_error ("tar subcommand failed on directory: %s: %s", - dir, errstr); - free (errstr); - } + char *errstr = read_error_file (error_file); + reply_with_error ("tar subcommand failed on directory: %s: %s", + dir, errstr); + free (errstr); + unlink (error_file); return -1; } + unlink (error_file); + return 0; }