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