1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "guestfs_protocol.h"
30 /* Has one FileOut parameter. */
32 do_compressX_out (const char *file, const char *filter, int is_device)
37 char buf[GUESTFS_MAX_CHUNK_SIZE];
39 /* The command will look something like:
40 * gzip -c /sysroot%s # file
42 * gzip -c < %s # device
44 * We have to quote the file or device name.
46 * The unnecessary redirect for devices is there because lzop
47 * unhelpfully refuses to compress anything that isn't a regular
51 if (asprintf_nowarn (&cmd, "%s %R", filter, file) == -1) {
52 reply_with_perror ("asprintf");
56 if (asprintf_nowarn (&cmd, "%s < %Q", filter, file) == -1) {
57 reply_with_perror ("asprintf");
63 fprintf (stderr, "%s\n", cmd);
65 fp = popen (cmd, "r");
67 reply_with_perror ("%s", cmd);
73 /* Now we must send the reply message, before the file contents. After
74 * this there is no opportunity in the protocol to send any error
75 * message back. Instead we can only cancel the transfer.
79 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
80 if (send_file_write (buf, r) < 0) {
88 send_file_end (1); /* Cancel. */
93 if (pclose (fp) != 0) {
95 send_file_end (1); /* Cancel. */
99 if (send_file_end (0)) /* Normal end of file. */
105 #define CHECK_SUPPORTED(prog) \
106 if (!prog_exists (prog)) { \
107 /* note: substring "not supported" must appear in this error */ \
108 reply_with_error ("compression type %s is not supported", prog); \
113 get_filter (const char *ctype, int level, char *ret, size_t n)
115 if (STREQ (ctype, "compress")) {
116 CHECK_SUPPORTED ("compress");
118 reply_with_error ("compress: cannot use optional level parameter with this compression type");
121 snprintf (ret, n, "compress -c");
124 else if (STREQ (ctype, "gzip")) {
125 CHECK_SUPPORTED ("gzip");
127 snprintf (ret, n, "gzip -c");
128 else if (level >= 1 && level <= 9)
129 snprintf (ret, n, "gzip -c -%d", level);
131 reply_with_error ("gzip: incorrect value for level parameter");
136 else if (STREQ (ctype, "bzip2")) {
137 CHECK_SUPPORTED ("bzip2");
139 snprintf (ret, n, "bzip2 -c");
140 else if (level >= 1 && level <= 9)
141 snprintf (ret, n, "bzip2 -c -%d", level);
143 reply_with_error ("bzip2: incorrect value for level parameter");
148 else if (STREQ (ctype, "xz")) {
149 CHECK_SUPPORTED ("xz");
151 snprintf (ret, n, "xz -c");
152 else if (level >= 0 && level <= 9)
153 snprintf (ret, n, "xz -c -%d", level);
155 reply_with_error ("xz: incorrect value for level parameter");
160 else if (STREQ (ctype, "lzop")) {
161 CHECK_SUPPORTED ("lzop");
163 snprintf (ret, n, "lzop -c");
164 else if (level >= 1 && level <= 9)
165 snprintf (ret, n, "lzop -c -%d", level);
167 reply_with_error ("lzop: incorrect value for level parameter");
173 reply_with_error ("unknown compression type");
177 /* Has one FileOut parameter. */
178 /* Takes optional arguments, consult optargs_bitmask. */
180 do_compress_out (const char *ctype, const char *file, int level)
184 if (!(optargs_bitmask & GUESTFS_COMPRESS_OUT_LEVEL_BITMASK))
187 if (get_filter (ctype, level, filter, sizeof filter) == -1)
190 return do_compressX_out (file, filter, 0);
193 /* Has one FileOut parameter. */
194 /* Takes optional arguments, consult optargs_bitmask. */
196 do_compress_device_out (const char *ctype, const char *file, int level)
200 if (!(optargs_bitmask & GUESTFS_COMPRESS_DEVICE_OUT_LEVEL_BITMASK))
203 if (get_filter (ctype, level, filter, sizeof filter) == -1)
206 return do_compressX_out (file, filter, 1);