New API: ntfsresize-size to allow shrinking NTFS (RHBZ#585223).
[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   if (mode < 0) {
202     reply_with_error ("%s: mode is negative", path);
203     return -1;
204   }
205
206   CHROOT_IN;
207   r = chmod (path, mode);
208   CHROOT_OUT;
209
210   if (r == -1) {
211     reply_with_perror ("%s: 0%o", path, mode);
212     return -1;
213   }
214
215   return 0;
216 }
217
218 int
219 do_chown (int owner, int group, const char *path)
220 {
221   int r;
222
223   CHROOT_IN;
224   r = chown (path, owner, group);
225   CHROOT_OUT;
226
227   if (r == -1) {
228     reply_with_perror ("%s: %d.%d", path, owner, group);
229     return -1;
230   }
231
232   return 0;
233 }
234
235 int
236 do_lchown (int owner, int group, const char *path)
237 {
238   int r;
239
240   CHROOT_IN;
241   r = lchown (path, owner, group);
242   CHROOT_OUT;
243
244   if (r == -1) {
245     reply_with_perror ("%s: %d.%d", path, owner, group);
246     return -1;
247   }
248
249   return 0;
250 }
251
252 int
253 do_exists (const char *path)
254 {
255   int r;
256
257   CHROOT_IN;
258   r = access (path, F_OK);
259   CHROOT_OUT;
260
261   return r == 0;
262 }
263
264 int
265 do_is_file (const char *path)
266 {
267   int r;
268   struct stat buf;
269
270   CHROOT_IN;
271   r = lstat (path, &buf);
272   CHROOT_OUT;
273
274   if (r == -1) {
275     if (errno != ENOENT && errno != ENOTDIR) {
276       reply_with_perror ("stat: %s", path);
277       return -1;
278     }
279     else
280       return 0;                 /* Not a file. */
281   }
282
283   return S_ISREG (buf.st_mode);
284 }
285
286 int
287 do_write_file (const char *path, const char *content, int size)
288 {
289   int fd;
290
291   if (size == 0)
292     size = strlen (content);
293
294   CHROOT_IN;
295   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
296   CHROOT_OUT;
297
298   if (fd == -1) {
299     reply_with_perror ("open: %s", path);
300     return -1;
301   }
302
303   if (xwrite (fd, content, size) == -1) {
304     reply_with_perror ("write");
305     close (fd);
306     return -1;
307   }
308
309   if (close (fd) == -1) {
310     reply_with_perror ("close: %s", path);
311     return -1;
312   }
313
314   return 0;
315 }
316
317 int
318 do_write (const char *path, const char *content, size_t size)
319 {
320   int fd;
321
322   CHROOT_IN;
323   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
324   CHROOT_OUT;
325
326   if (fd == -1) {
327     reply_with_perror ("open: %s", path);
328     return -1;
329   }
330
331   if (xwrite (fd, content, size) == -1) {
332     reply_with_perror ("write");
333     close (fd);
334     return -1;
335   }
336
337   if (close (fd) == -1) {
338     reply_with_perror ("close: %s", path);
339     return -1;
340   }
341
342   return 0;
343 }
344
345 char *
346 do_read_file (const char *path, size_t *size_r)
347 {
348   int fd;
349   struct stat statbuf;
350   char *r;
351
352   CHROOT_IN;
353   fd = open (path, O_RDONLY);
354   CHROOT_OUT;
355
356   if (fd == -1) {
357     reply_with_perror ("open: %s", path);
358     return NULL;
359   }
360
361   if (fstat (fd, &statbuf) == -1) {
362     reply_with_perror ("fstat: %s", path);
363     close (fd);
364     return NULL;
365   }
366
367   /* The actual limit on messages is smaller than this.  This
368    * check just limits the amount of memory we'll try and allocate
369    * here.  If the message is larger than the real limit, that will
370    * be caught later when we try to serialize the message.
371    */
372   if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
373     reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
374     close (fd);
375     return NULL;
376   }
377   r = malloc (statbuf.st_size);
378   if (r == NULL) {
379     reply_with_perror ("malloc");
380     close (fd);
381     return NULL;
382   }
383
384   if (xread (fd, r, statbuf.st_size) == -1) {
385     reply_with_perror ("read: %s", path);
386     close (fd);
387     free (r);
388     return NULL;
389   }
390
391   if (close (fd) == -1) {
392     reply_with_perror ("close: %s", path);
393     free (r);
394     return NULL;
395   }
396
397   /* Mustn't touch *size_r until we are sure that we won't return any
398    * error (RHBZ#589039).
399    */
400   *size_r = statbuf.st_size;
401   return r;
402 }
403
404 char *
405 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
406 {
407   int fd;
408   ssize_t r;
409   char *buf;
410
411   /* The actual limit on messages is smaller than this.  This check
412    * just limits the amount of memory we'll try and allocate in the
413    * function.  If the message is larger than the real limit, that
414    * will be caught later when we try to serialize the message.
415    */
416   if (count >= GUESTFS_MESSAGE_MAX) {
417     reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
418     return NULL;
419   }
420
421   CHROOT_IN;
422   fd = open (path, O_RDONLY);
423   CHROOT_OUT;
424
425   if (fd == -1) {
426     reply_with_perror ("open: %s", path);
427     return NULL;
428   }
429
430   buf = malloc (count);
431   if (buf == NULL) {
432     reply_with_perror ("malloc");
433     close (fd);
434     return NULL;
435   }
436
437   r = pread (fd, buf, count, offset);
438   if (r == -1) {
439     reply_with_perror ("pread: %s", path);
440     close (fd);
441     free (buf);
442     return NULL;
443   }
444
445   if (close (fd) == -1) {
446     reply_with_perror ("close: %s", path);
447     close (fd);
448     free (buf);
449     return NULL;
450   }
451
452   /* Mustn't touch *size_r until we are sure that we won't return any
453    * error (RHBZ#589039).
454    */
455   *size_r = r;
456   return buf;
457 }
458
459 int
460 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
461 {
462   int fd;
463   ssize_t r;
464
465   CHROOT_IN;
466   fd = open (path, O_WRONLY);
467   CHROOT_OUT;
468
469   if (fd == -1) {
470     reply_with_perror ("open: %s", path);
471     return -1;
472   }
473
474   r = pwrite (fd, content, size, offset);
475   if (r == -1) {
476     reply_with_perror ("pwrite: %s", path);
477     close (fd);
478     return -1;
479   }
480
481   if (close (fd) == -1) {
482     reply_with_perror ("close: %s", path);
483     close (fd);
484     return -1;
485   }
486
487   return r;
488 }
489
490 /* This runs the 'file' command. */
491 char *
492 do_file (const char *path)
493 {
494   char *out, *err;
495   int r, freeit = 0;
496   char *buf;
497   int len;
498
499   if (STREQLEN (path, "/dev/", 5))
500     buf = (char *) path;
501   else {
502     buf = sysroot_path (path);
503     if (!buf) {
504       reply_with_perror ("malloc");
505       return NULL;
506     }
507     freeit = 1;
508   }
509
510   /* file(1) manpage claims "file returns 0 on success, and non-zero on
511    * error", but this is evidently not true.  It always returns 0, in
512    * every scenario I can think up.  So check the target is readable
513    * first.
514    */
515   if (access (buf, R_OK) == -1) {
516     if (freeit) free (buf);
517     reply_with_perror ("access: %s", path);
518     return NULL;
519   }
520
521   r = command (&out, &err, "file", "-zbsL", buf, NULL);
522   if (freeit) free (buf);
523
524   if (r == -1) {
525     free (out);
526     reply_with_error ("%s: %s", path, err);
527     free (err);
528     return NULL;
529   }
530   free (err);
531
532   /* We need to remove the trailing \n from output of file(1). */
533   len = strlen (out);
534   if (out[len-1] == '\n')
535     out[len-1] = '\0';
536
537   return out;                   /* caller frees */
538 }
539
540 /* zcat | file */
541 char *
542 do_zfile (const char *method, const char *path)
543 {
544   int len;
545   const char *zcat;
546   char *cmd;
547   FILE *fp;
548   char line[256];
549
550   if (STREQ (method, "gzip") || STREQ (method, "compress"))
551     zcat = "zcat";
552   else if (STREQ (method, "bzip2"))
553     zcat = "bzcat";
554   else {
555     reply_with_error ("unknown method");
556     return NULL;
557   }
558
559   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
560     reply_with_perror ("asprintf");
561     return NULL;
562   }
563
564   if (verbose)
565     fprintf (stderr, "%s\n", cmd);
566
567   fp = popen (cmd, "r");
568   if (fp == NULL) {
569     reply_with_perror ("%s", cmd);
570     free (cmd);
571     return NULL;
572   }
573
574   free (cmd);
575
576   if (fgets (line, sizeof line, fp) == NULL) {
577     reply_with_perror ("fgets");
578     fclose (fp);
579     return NULL;
580   }
581
582   if (fclose (fp) == -1) {
583     reply_with_perror ("fclose");
584     return NULL;
585   }
586
587   len = strlen (line);
588   if (len > 0 && line[len-1] == '\n')
589     line[len-1] = '\0';
590
591   return strdup (line);
592 }
593
594 int64_t
595 do_filesize (const char *path)
596 {
597   int r;
598   struct stat buf;
599
600   CHROOT_IN;
601   r = stat (path, &buf);        /* follow symlinks */
602   CHROOT_OUT;
603
604   if (r == -1) {
605     reply_with_perror ("%s", path);
606     return -1;
607   }
608
609   return buf.st_size;
610 }