From 0232e722826cfda0f6042da983f9eb871f24e946 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 20 Apr 2009 15:54:22 +0100 Subject: Added tar-in, tar-out, tgz-in, tgz-out commands. --- daemon/tar.c | 279 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 279 insertions(+) create mode 100644 daemon/tar.c (limited to 'daemon/tar.c') diff --git a/daemon/tar.c b/daemon/tar.c new file mode 100644 index 00000000..ecf919db --- /dev/null +++ b/daemon/tar.c @@ -0,0 +1,279 @@ +/* libguestfs - the guestfsd daemon + * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include +#include +#include +#include + +#include "../src/guestfs_protocol.h" +#include "daemon.h" +#include "actions.h" + +static int +fwrite_cb (void *fp_ptr, const void *buf, int len) +{ + FILE *fp = *(FILE **)fp_ptr; + return fwrite (buf, len, 1, fp) == 1 ? 0 : -1; +} + +/* Has one FileIn parameter. */ +int +do_tar_in (const char *dir) +{ + int err, r, len; + FILE *fp; + char *cmd; + + if (!root_mounted || dir[0] != '/') { + cancel_receive (); + reply_with_error ("tar-in: root must be mounted and path must be absolute"); + return -1; + } + + /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */ + len = 2 * strlen (dir) + 32; + cmd = malloc (len); + if (!cmd) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("malloc"); + return -1; + } + strcpy (cmd, "tar -C /sysroot"); + shell_quote (cmd+15, len-15, dir); + strcat (cmd, " -xf -"); + + fp = popen (cmd, "w"); + if (fp == NULL) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("%s", cmd); + return -1; + } + + r = receive_file (fwrite_cb, &fp); + if (r == -1) { /* write error */ + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("write: %s", dir); + pclose (fp); + return -1; + } + if (r == -2) { /* cancellation from library */ + pclose (fp); + /* Do NOT send any error. */ + return -1; + } + + if (pclose (fp) == -1) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("pclose: %s", dir); + return -1; + } + + return 0; +} + +/* Has one FileOut parameter. */ +int +do_tar_out (const char *dir) +{ + int r, len; + FILE *fp; + char *cmd; + char buf[GUESTFS_MAX_CHUNK_SIZE]; + + NEED_ROOT (-1); + ABS_PATH (dir, -1); + + /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */ + len = 2 * strlen (dir) + 32; + cmd = malloc (len); + if (!cmd) { + reply_with_perror ("malloc"); + return -1; + } + strcpy (cmd, "tar -C /sysroot"); + shell_quote (cmd+15, len-15, dir); + strcat (cmd, " -cf - ."); + + fp = popen (cmd, "r"); + if (fp == NULL) { + reply_with_perror ("%s", cmd); + return -1; + } + + /* Now we must send the reply message, before the file contents. After + * this there is no opportunity in the protocol to send any error + * message back. Instead we can only cancel the transfer. + */ + reply (NULL, NULL); + + while ((r = fread (buf, 1, sizeof buf, fp)) > 0) { + if (send_file_write (buf, r) < 0) { + pclose (fp); + return -1; + } + } + + if (r == -1) { + perror (dir); + send_file_end (1); /* Cancel. */ + pclose (fp); + return -1; + } + + if (pclose (fp) == -1) { + perror (dir); + send_file_end (1); /* Cancel. */ + return -1; + } + + send_file_end (0); /* Normal end of file. */ + return 0; +} + +/* Has one FileIn parameter. */ +int +do_tgz_in (const char *dir) +{ + int err, r, len; + FILE *fp; + char *cmd; + + if (!root_mounted || dir[0] != '/') { + cancel_receive (); + reply_with_error ("tar-in: root must be mounted and path must be absolute"); + return -1; + } + + /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */ + len = 2 * strlen (dir) + 32; + cmd = malloc (len); + if (!cmd) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("malloc"); + return -1; + } + strcpy (cmd, "tar -C /sysroot"); + shell_quote (cmd+15, len-15, dir); + strcat (cmd, " -zxf -"); + + fp = popen (cmd, "w"); + if (fp == NULL) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("%s", cmd); + return -1; + } + + r = receive_file (fwrite_cb, &fp); + if (r == -1) { /* write error */ + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("write: %s", dir); + pclose (fp); + return -1; + } + if (r == -2) { /* cancellation from library */ + pclose (fp); + /* Do NOT send any error. */ + return -1; + } + + if (pclose (fp) == -1) { + err = errno; + cancel_receive (); + errno = err; + reply_with_perror ("pclose: %s", dir); + return -1; + } + + return 0; +} + +/* Has one FileOut parameter. */ +int +do_tgz_out (const char *dir) +{ + int r, len; + FILE *fp; + char *cmd; + char buf[GUESTFS_MAX_CHUNK_SIZE]; + + NEED_ROOT (-1); + ABS_PATH (dir, -1); + + /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */ + len = 2 * strlen (dir) + 32; + cmd = malloc (len); + if (!cmd) { + reply_with_perror ("malloc"); + return -1; + } + strcpy (cmd, "tar -C /sysroot"); + shell_quote (cmd+15, len-15, dir); + strcat (cmd, " -zcf - ."); + + fp = popen (cmd, "r"); + if (fp == NULL) { + reply_with_perror ("%s", cmd); + return -1; + } + + /* Now we must send the reply message, before the file contents. After + * this there is no opportunity in the protocol to send any error + * message back. Instead we can only cancel the transfer. + */ + reply (NULL, NULL); + + while ((r = fread (buf, 1, sizeof buf, fp)) > 0) { + if (send_file_write (buf, r) < 0) { + pclose (fp); + return -1; + } + } + + if (r == -1) { + perror (dir); + send_file_end (1); /* Cancel. */ + pclose (fp); + return -1; + } + + if (pclose (fp) == -1) { + perror (dir); + send_file_end (1); /* Cancel. */ + return -1; + } + + send_file_end (0); /* Normal end of file. */ + return 0; +} -- cgit