New API: lvresize-free to extend LVs into percentage of free space.
[libguestfs.git] / daemon / file.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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 <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "../src/guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 int
33 do_touch (const char *path)
34 {
35   int fd;
36   int r;
37
38   CHROOT_IN;
39   fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
40   CHROOT_OUT;
41
42   if (fd == -1) {
43     reply_with_perror ("open: %s", path);
44     return -1;
45   }
46
47   r = futimens (fd, NULL);
48   if (r == -1) {
49     reply_with_perror ("futimens: %s", path);
50     close (fd);
51     return -1;
52   }
53
54   if (close (fd) == -1) {
55     reply_with_perror ("close: %s", path);
56     return -1;
57   }
58
59   return 0;
60 }
61
62 char *
63 do_cat (const char *path)
64 {
65   int fd;
66   int alloc, size, r, max;
67   char *buf, *buf2;
68
69   CHROOT_IN;
70   fd = open (path, O_RDONLY);
71   CHROOT_OUT;
72
73   if (fd == -1) {
74     reply_with_perror ("open: %s", path);
75     return NULL;
76   }
77
78   /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes.  If it's
79    * larger than that, we need to return an error instead (for
80    * correctness).
81    */
82   max = GUESTFS_MESSAGE_MAX - 1000;
83   buf = NULL;
84   size = alloc = 0;
85
86   for (;;) {
87     if (size >= alloc) {
88       alloc += 8192;
89       if (alloc > max) {
90         reply_with_error ("%s: file is too large for message buffer",
91                           path);
92         free (buf);
93         close (fd);
94         return NULL;
95       }
96       buf2 = realloc (buf, alloc);
97       if (buf2 == NULL) {
98         reply_with_perror ("realloc");
99         free (buf);
100         close (fd);
101         return NULL;
102       }
103       buf = buf2;
104     }
105
106     r = read (fd, buf + size, alloc - size);
107     if (r == -1) {
108       reply_with_perror ("read: %s", path);
109       free (buf);
110       close (fd);
111       return NULL;
112     }
113     if (r == 0) {
114       buf[size] = '\0';
115       break;
116     }
117     if (r > 0)
118       size += r;
119   }
120
121   if (close (fd) == -1) {
122     reply_with_perror ("close: %s", path);
123     free (buf);
124     return NULL;
125   }
126
127   return buf;                   /* caller will free */
128 }
129
130 char **
131 do_read_lines (const char *path)
132 {
133   char **r = NULL;
134   int size = 0, alloc = 0;
135   FILE *fp;
136   char *line = NULL;
137   size_t len = 0;
138   ssize_t n;
139
140   CHROOT_IN;
141   fp = fopen (path, "r");
142   CHROOT_OUT;
143
144   if (!fp) {
145     reply_with_perror ("fopen: %s", path);
146     return NULL;
147   }
148
149   while ((n = getline (&line, &len, fp)) != -1) {
150     /* Remove either LF or CRLF. */
151     if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
152       line[n-2] = '\0';
153     else if (n >= 1 && line[n-1] == '\n')
154       line[n-1] = '\0';
155
156     if (add_string (&r, &size, &alloc, line) == -1) {
157       free (line);
158       fclose (fp);
159       return NULL;
160     }
161   }
162
163   free (line);
164
165   if (add_string (&r, &size, &alloc, NULL) == -1) {
166     fclose (fp);
167     return NULL;
168   }
169
170   if (fclose (fp) == EOF) {
171     reply_with_perror ("fclose: %s", path);
172     free_strings (r);
173     return NULL;
174   }
175
176   return r;
177 }
178
179 int
180 do_rm (const char *path)
181 {
182   int r;
183
184   CHROOT_IN;
185   r = unlink (path);
186   CHROOT_OUT;
187
188   if (r == -1) {
189     reply_with_perror ("%s", path);
190     return -1;
191   }
192
193   return 0;
194 }
195
196 int
197 do_chmod (int mode, const char *path)
198 {
199   int r;
200
201   CHROOT_IN;
202   r = chmod (path, mode);
203   CHROOT_OUT;
204
205   if (r == -1) {
206     reply_with_perror ("%s: 0%o", path, mode);
207     return -1;
208   }
209
210   return 0;
211 }
212
213 int
214 do_chown (int owner, int group, const char *path)
215 {
216   int r;
217
218   CHROOT_IN;
219   r = chown (path, owner, group);
220   CHROOT_OUT;
221
222   if (r == -1) {
223     reply_with_perror ("%s: %d.%d", path, owner, group);
224     return -1;
225   }
226
227   return 0;
228 }
229
230 int
231 do_lchown (int owner, int group, const char *path)
232 {
233   int r;
234
235   CHROOT_IN;
236   r = lchown (path, owner, group);
237   CHROOT_OUT;
238
239   if (r == -1) {
240     reply_with_perror ("%s: %d.%d", path, owner, group);
241     return -1;
242   }
243
244   return 0;
245 }
246
247 int
248 do_exists (const char *path)
249 {
250   int r;
251
252   CHROOT_IN;
253   r = access (path, F_OK);
254   CHROOT_OUT;
255
256   return r == 0;
257 }
258
259 int
260 do_is_file (const char *path)
261 {
262   int r;
263   struct stat buf;
264
265   CHROOT_IN;
266   r = lstat (path, &buf);
267   CHROOT_OUT;
268
269   if (r == -1) {
270     if (errno != ENOENT && errno != ENOTDIR) {
271       reply_with_perror ("stat: %s", path);
272       return -1;
273     }
274     else
275       return 0;                 /* Not a file. */
276   }
277
278   return S_ISREG (buf.st_mode);
279 }
280
281 int
282 do_write_file (const char *path, const char *content, int size)
283 {
284   int fd;
285
286   if (size == 0)
287     size = strlen (content);
288
289   CHROOT_IN;
290   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
291   CHROOT_OUT;
292
293   if (fd == -1) {
294     reply_with_perror ("open: %s", path);
295     return -1;
296   }
297
298   if (xwrite (fd, content, size) == -1) {
299     reply_with_perror ("write");
300     close (fd);
301     return -1;
302   }
303
304   if (close (fd) == -1) {
305     reply_with_perror ("close: %s", path);
306     return -1;
307   }
308
309   return 0;
310 }
311
312 char *
313 do_read_file (const char *path, size_t *size_r)
314 {
315   int fd;
316   struct stat statbuf;
317   char *r;
318
319   CHROOT_IN;
320   fd = open (path, O_RDONLY);
321   CHROOT_OUT;
322
323   if (fd == -1) {
324     reply_with_perror ("open: %s", path);
325     return NULL;
326   }
327
328   if (fstat (fd, &statbuf) == -1) {
329     reply_with_perror ("fstat: %s", path);
330     close (fd);
331     return NULL;
332   }
333
334   *size_r = statbuf.st_size;
335   /* The actual limit on messages is smaller than this.  This
336    * check just limits the amount of memory we'll try and allocate
337    * here.  If the message is larger than the real limit, that will
338    * be caught later when we try to serialize the message.
339    */
340   if (*size_r >= GUESTFS_MESSAGE_MAX) {
341     reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
342     close (fd);
343     return NULL;
344   }
345   r = malloc (*size_r);
346   if (r == NULL) {
347     reply_with_perror ("malloc");
348     close (fd);
349     return NULL;
350   }
351
352   if (xread (fd, r, *size_r) == -1) {
353     reply_with_perror ("read: %s", path);
354     close (fd);
355     free (r);
356     return NULL;
357   }
358
359   if (close (fd) == -1) {
360     reply_with_perror ("close: %s", path);
361     free (r);
362     return NULL;
363   }
364
365   return r;
366 }
367
368 char *
369 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
370 {
371   int fd;
372   ssize_t r;
373   char *buf;
374
375   /* The actual limit on messages is smaller than this.  This check
376    * just limits the amount of memory we'll try and allocate in the
377    * function.  If the message is larger than the real limit, that
378    * will be caught later when we try to serialize the message.
379    */
380   if (count >= GUESTFS_MESSAGE_MAX) {
381     reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
382     return NULL;
383   }
384
385   CHROOT_IN;
386   fd = open (path, O_RDONLY);
387   CHROOT_OUT;
388
389   if (fd == -1) {
390     reply_with_perror ("open: %s", path);
391     return NULL;
392   }
393
394   buf = malloc (count);
395   if (buf == NULL) {
396     reply_with_perror ("malloc");
397     close (fd);
398     return NULL;
399   }
400
401   r = pread (fd, buf, count, offset);
402   if (r == -1) {
403     reply_with_perror ("pread: %s", path);
404     close (fd);
405     free (buf);
406     return NULL;
407   }
408
409   if (close (fd) == -1) {
410     reply_with_perror ("close: %s", path);
411     close (fd);
412     free (buf);
413     return NULL;
414   }
415
416   *size_r = r;
417   return buf;
418 }
419
420 /* This runs the 'file' command. */
421 char *
422 do_file (const char *path)
423 {
424   char *out, *err;
425   int r, freeit = 0;
426   char *buf;
427   int len;
428
429   if (STREQLEN (path, "/dev/", 5))
430     buf = (char *) path;
431   else {
432     buf = sysroot_path (path);
433     if (!buf) {
434       reply_with_perror ("malloc");
435       return NULL;
436     }
437     freeit = 1;
438   }
439
440   /* file(1) manpage claims "file returns 0 on success, and non-zero on
441    * error", but this is evidently not true.  It always returns 0, in
442    * every scenario I can think up.  So check the target is readable
443    * first.
444    */
445   if (access (buf, R_OK) == -1) {
446     if (freeit) free (buf);
447     reply_with_perror ("access: %s", path);
448     return NULL;
449   }
450
451   r = command (&out, &err, "file", "-zbsL", buf, NULL);
452   if (freeit) free (buf);
453
454   if (r == -1) {
455     free (out);
456     reply_with_error ("%s: %s", path, err);
457     free (err);
458     return NULL;
459   }
460   free (err);
461
462   /* We need to remove the trailing \n from output of file(1). */
463   len = strlen (out);
464   if (out[len-1] == '\n')
465     out[len-1] = '\0';
466
467   return out;                   /* caller frees */
468 }
469
470 /* zcat | file */
471 char *
472 do_zfile (const char *method, const char *path)
473 {
474   int len;
475   const char *zcat;
476   char *cmd;
477   FILE *fp;
478   char line[256];
479
480   if (STREQ (method, "gzip") || STREQ (method, "compress"))
481     zcat = "zcat";
482   else if (STREQ (method, "bzip2"))
483     zcat = "bzcat";
484   else {
485     reply_with_error ("unknown method");
486     return NULL;
487   }
488
489   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
490     reply_with_perror ("asprintf");
491     return NULL;
492   }
493
494   if (verbose)
495     fprintf (stderr, "%s\n", cmd);
496
497   fp = popen (cmd, "r");
498   if (fp == NULL) {
499     reply_with_perror ("%s", cmd);
500     free (cmd);
501     return NULL;
502   }
503
504   free (cmd);
505
506   if (fgets (line, sizeof line, fp) == NULL) {
507     reply_with_perror ("fgets");
508     fclose (fp);
509     return NULL;
510   }
511
512   if (fclose (fp) == -1) {
513     reply_with_perror ("fclose");
514     return NULL;
515   }
516
517   len = strlen (line);
518   if (len > 0 && line[len-1] == '\n')
519     line[len-1] = '\0';
520
521   return strdup (line);
522 }
523
524 int64_t
525 do_filesize (const char *path)
526 {
527   int r;
528   struct stat buf;
529
530   CHROOT_IN;
531   r = stat (path, &buf);        /* follow symlinks */
532   CHROOT_OUT;
533
534   if (r == -1) {
535     reply_with_perror ("%s", path);
536     return -1;
537   }
538
539   return buf.st_size;
540 }