d08d3cc9d1144282bb7b94c8020536c7bea21653
[libguestfs.git] / daemon / compress.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25
26 #include "guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 /* Has one FileOut parameter. */
31 static int
32 do_compressX_out (const char *file, const char *filter, int is_device)
33 {
34   int r;
35   FILE *fp;
36   char *cmd;
37   char buf[GUESTFS_MAX_CHUNK_SIZE];
38
39   /* The command will look something like:
40    *   gzip -c /sysroot%s     # file
41    * or:
42    *   gzip -c < %s           # device
43    *
44    * We have to quote the file or device name.
45    *
46    * The unnecessary redirect for devices is there because lzop
47    * unhelpfully refuses to compress anything that isn't a regular
48    * file.
49    */
50   if (!is_device) {
51     if (asprintf_nowarn (&cmd, "%s %R", filter, file) == -1) {
52       reply_with_perror ("asprintf");
53       return -1;
54     }
55   } else {
56     if (asprintf_nowarn (&cmd, "%s < %Q", filter, file) == -1) {
57       reply_with_perror ("asprintf");
58       return -1;
59     }
60   }
61
62   if (verbose)
63     fprintf (stderr, "%s\n", cmd);
64
65   fp = popen (cmd, "r");
66   if (fp == NULL) {
67     reply_with_perror ("%s", cmd);
68     free (cmd);
69     return -1;
70   }
71   free (cmd);
72
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.
76    */
77   reply (NULL, NULL);
78
79   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
80     if (send_file_write (buf, r) < 0) {
81       pclose (fp);
82       return -1;
83     }
84   }
85
86   if (ferror (fp)) {
87     perror (file);
88     send_file_end (1);          /* Cancel. */
89     pclose (fp);
90     return -1;
91   }
92
93   if (pclose (fp) != 0) {
94     perror (file);
95     send_file_end (1);          /* Cancel. */
96     return -1;
97   }
98
99   if (send_file_end (0))        /* Normal end of file. */
100     return -1;
101
102   return 0;
103 }
104
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);    \
109     return -1;                                                          \
110   }
111
112 static int
113 get_filter (const char *ctype, int level, char *ret, size_t n)
114 {
115   if (STREQ (ctype, "compress")) {
116     CHECK_SUPPORTED ("compress");
117     if (level != -1) {
118       reply_with_error ("compress: cannot use optional level parameter with this compression type");
119       return -1;
120     }
121     snprintf (ret, n, "compress -c");
122     return 0;
123   }
124   else if (STREQ (ctype, "gzip")) {
125     CHECK_SUPPORTED ("gzip");
126     if (level == -1)
127       snprintf (ret, n, "gzip -c");
128     else if (level >= 1 && level <= 9)
129       snprintf (ret, n, "gzip -c -%d", level);
130     else {
131       reply_with_error ("gzip: incorrect value for level parameter");
132       return -1;
133     }
134     return 0;
135   }
136   else if (STREQ (ctype, "bzip2")) {
137     CHECK_SUPPORTED ("bzip2");
138     if (level == -1)
139       snprintf (ret, n, "bzip2 -c");
140     else if (level >= 1 && level <= 9)
141       snprintf (ret, n, "bzip2 -c -%d", level);
142     else {
143       reply_with_error ("bzip2: incorrect value for level parameter");
144       return -1;
145     }
146     return 0;
147   }
148   else if (STREQ (ctype, "xz")) {
149     CHECK_SUPPORTED ("xz");
150     if (level == -1)
151       snprintf (ret, n, "xz -c");
152     else if (level >= 0 && level <= 9)
153       snprintf (ret, n, "xz -c -%d", level);
154     else {
155       reply_with_error ("xz: incorrect value for level parameter");
156       return -1;
157     }
158     return 0;
159   }
160   else if (STREQ (ctype, "lzop")) {
161     CHECK_SUPPORTED ("lzop");
162     if (level == -1)
163       snprintf (ret, n, "lzop -c");
164     else if (level >= 1 && level <= 9)
165       snprintf (ret, n, "lzop -c -%d", level);
166     else {
167       reply_with_error ("lzop: incorrect value for level parameter");
168       return -1;
169     }
170     return 0;
171   }
172
173   reply_with_error ("unknown compression type");
174   return -1;
175 }
176
177 /* Has one FileOut parameter. */
178 /* Takes optional arguments, consult optargs_bitmask. */
179 int
180 do_compress_out (const char *ctype, const char *file, int level)
181 {
182   char filter[64];
183
184   if (!(optargs_bitmask & GUESTFS_COMPRESS_OUT_LEVEL_BITMASK))
185     level = -1;
186
187   if (get_filter (ctype, level, filter, sizeof filter) == -1)
188     return -1;
189
190   return do_compressX_out (file, filter, 0);
191 }
192
193 /* Has one FileOut parameter. */
194 /* Takes optional arguments, consult optargs_bitmask. */
195 int
196 do_compress_device_out (const char *ctype, const char *file, int level)
197 {
198   char filter[64];
199
200   if (!(optargs_bitmask & GUESTFS_COMPRESS_DEVICE_OUT_LEVEL_BITMASK))
201     level = -1;
202
203   if (get_filter (ctype, level, filter, sizeof filter) == -1)
204     return -1;
205
206   return do_compressX_out (file, filter, 1);
207 }