>From 8e4eb015aa9a9dbe2bdf676bc591cc158dd90afe Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 28 Sep 2011 11:14:06 +0100 Subject: [PATCH] New APIs: gzip-out, xz-out, gzip-device-out, xz-device-out. These APIs let you copy compressed files or devices out from the disk image. Compression is useful for large images which are mostly zeroes. We cannot currently do sparseness detection, and compression gives us a form of zero detection for free. Example usage: $ guestfish -N fs gzip-device-out /dev/vda1 - > /tmp/vda1.gz $ ll /tmp/vda1.gz -rw-rw-r--. 1 rjones rjones 106822 Sep 28 11:16 /tmp/vda1.gz $ file /tmp/vda1.gz /tmp/vda1.gz: gzip compressed data, was "vda1", from Unix, last modified: Wed Sep 28 11:16:54 2011 $ file -z /tmp/vda1.gz /tmp/vda1.gz: Linux rev 1.0 ext2 filesystem data, UUID=2592fc6a-ab3a-48be-a9f6-6920148b3bda (gzip compressed data, was "vda1", from Unix, last modified: Wed Sep 28 11:16:54 2011) --- daemon/Makefile.am | 1 + daemon/compress.c | 127 ++++++++++++++++++++++++++++++++++++++++ generator/generator_actions.ml | 36 +++++++++++ po/POTFILES.in | 1 + src/MAX_PROC_NR | 2 +- 5 files changed, 166 insertions(+), 1 deletions(-) create mode 100644 daemon/compress.c diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 1664af0..e23ce86 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -94,6 +94,7 @@ guestfsd_SOURCES = \ checksum.c \ cmp.c \ command.c \ + compress.c \ cpmv.c \ daemon.h \ dd.c \ diff --git a/daemon/compress.c b/daemon/compress.c new file mode 100644 index 0000000..0c54e41 --- /dev/null +++ b/daemon/compress.c @@ -0,0 +1,127 @@ +/* libguestfs - the guestfsd daemon + * Copyright (C) 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 + * 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 "guestfs_protocol.h" +#include "daemon.h" +#include "actions.h" +#include "optgroups.h" + +/* Has one FileOut parameter. */ +static int +do_compressX_out (const char *file, const char *filter, int device) +{ + int r; + FILE *fp; + char *cmd; + char buf[GUESTFS_MAX_CHUNK_SIZE]; + + /* The command will look something like: + * gzip -c /sysroot%s # file + * or: + * gzip -c %s # device + * We have to quote the file or device name. + */ + if (!device) { + if (asprintf_nowarn (&cmd, "%s %R", filter, file) == -1) { + reply_with_perror ("asprintf"); + return -1; + } + } else { + if (asprintf_nowarn (&cmd, "%s %Q", filter, file) == -1) { + reply_with_perror ("asprintf"); + return -1; + } + } + + if (verbose) + fprintf (stderr, "%s\n", cmd); + + fp = popen (cmd, "r"); + if (fp == NULL) { + reply_with_perror ("%s", cmd); + free (cmd); + return -1; + } + free (cmd); + + /* 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 (ferror (fp)) { + perror (file); + send_file_end (1); /* Cancel. */ + pclose (fp); + return -1; + } + + if (pclose (fp) != 0) { + perror (file); + send_file_end (1); /* Cancel. */ + return -1; + } + + if (send_file_end (0)) /* Normal end of file. */ + return -1; + + return 0; +} + +/* Has one FileOut parameter. */ +int +do_gzip_out (const char *file) +{ + return do_compressX_out (file, "gzip -c", 0); +} + +/* Has one FileOut parameter. */ +int +do_xz_out (const char *file) +{ + return do_compressX_out (file, "xz -c", 0); +} + +/* Has one FileOut parameter. */ +int +do_gzip_device_out (const char *device) +{ + return do_compressX_out (device, "gzip -c", 1); +} + +/* Has one FileOut parameter. */ +int +do_xz_device_out (const char *device) +{ + return do_compressX_out (device, "xz -c", 1); +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index c3d74f5..b5f2bd5 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -6146,6 +6146,42 @@ C does not exist, then a new file is created. See also C."); + ("gzip_out", (RErr, [Pathname "file"; FileOut "zfile"], []), 291, [], + [], + "output gzip-compressed file", + "\ +This command gzip-compresses C and writes it out to the local +file C. + +For other forms of compression, see C."); + + ("xz_out", (RErr, [Pathname "file"; FileOut "zfile"], []), 292, [Optional "xz"], + [], + "output xz-compressed file", + "\ +This command xz-compresses C and writes it out to the local +file C. + +For other forms of compression, see C."); + + ("gzip_device_out", (RErr, [Device "device"; FileOut "zdevice"], []), 293, [], + [], + "output gzip-compressed device", + "\ +This command gzip-compresses C and writes it out to the local +file C. + +For other forms of compression, see C."); + + ("xz_device_out", (RErr, [Device "device"; FileOut "zdevice"], []), 294, [], + [], + "output xz-compressed device", + "\ +This command xz-compresses C and writes it out to the local +file C. + +For other forms of compression, see C."); + ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/po/POTFILES.in b/po/POTFILES.in index df54873..effc9ea 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -11,6 +11,7 @@ daemon/btrfs.c daemon/checksum.c daemon/cmp.c daemon/command.c +daemon/compress.c daemon/cpmv.c daemon/dd.c daemon/debug.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 8408670..26f42e6 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -290 +294 -- 1.7.6