generator.ml: use new "Pathname" designation
[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_exists (const char *path)
236 {
237   int r;
238
239   CHROOT_IN;
240   r = access (path, F_OK);
241   CHROOT_OUT;
242
243   return r == 0;
244 }
245
246 int
247 do_is_file (const char *path)
248 {
249   int r;
250   struct stat buf;
251
252   CHROOT_IN;
253   r = lstat (path, &buf);
254   CHROOT_OUT;
255
256   if (r == -1) {
257     if (errno != ENOENT && errno != ENOTDIR) {
258       reply_with_perror ("stat: %s", path);
259       return -1;
260     }
261     else
262       return 0;                 /* Not a file. */
263   }
264
265   return S_ISREG (buf.st_mode);
266 }
267
268 int
269 do_write_file (const char *path, const char *content, int size)
270 {
271   int fd;
272
273   if (size == 0)
274     size = strlen (content);
275
276   CHROOT_IN;
277   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
278   CHROOT_OUT;
279
280   if (fd == -1) {
281     reply_with_perror ("open: %s", path);
282     return -1;
283   }
284
285   if (xwrite (fd, content, size) == -1) {
286     reply_with_perror ("write");
287     close (fd);
288     return -1;
289   }
290
291   if (close (fd) == -1) {
292     reply_with_perror ("close: %s", path);
293     return -1;
294   }
295
296   return 0;
297 }
298
299 char *
300 do_read_file (const char *path, size_t *size_r)
301 {
302   int fd;
303   struct stat statbuf;
304   char *r;
305
306   CHROOT_IN;
307   fd = open (path, O_RDONLY);
308   CHROOT_OUT;
309
310   if (fd == -1) {
311     reply_with_perror ("open: %s", path);
312     return NULL;
313   }
314
315   if (fstat (fd, &statbuf) == -1) {
316     reply_with_perror ("fstat: %s", path);
317     close (fd);
318     return NULL;
319   }
320
321   *size_r = statbuf.st_size;
322   /* The actual limit on messages is smaller than this.  This
323    * check just limits the amount of memory we'll try and allocate
324    * here.  If the message is larger than the real limit, that will
325    * be caught later when we try to serialize the message.
326    */
327   if (*size_r >= GUESTFS_MESSAGE_MAX) {
328     reply_with_error ("read_file: %s: file is too large for the protocol, use guestfs_download instead", path);
329     close (fd);
330     return NULL;
331   }
332   r = malloc (*size_r);
333   if (r == NULL) {
334     reply_with_perror ("malloc");
335     close (fd);
336     return NULL;
337   }
338
339   if (xread (fd, r, *size_r) == -1) {
340     reply_with_perror ("read: %s", path);
341     close (fd);
342     free (r);
343     return NULL;
344   }
345
346   if (close (fd) == -1) {
347     reply_with_perror ("close: %s", path);
348     free (r);
349     return NULL;
350   }
351
352   return r;
353 }
354
355 /* This runs the 'file' command. */
356 char *
357 do_file (const char *path)
358 {
359   char *out, *err;
360   int r, freeit = 0;
361   char *buf;
362   int len;
363
364   REQUIRE_ROOT_OR_RESOLVE_DEVICE (path, return NULL);
365
366   if (strncmp (path, "/dev/", 5) == 0)
367     buf = (char *) path;
368   else {
369     buf = sysroot_path (path);
370     if (!buf) {
371       reply_with_perror ("malloc");
372       return NULL;
373     }
374     freeit = 1;
375   }
376
377   /* file(1) manpage claims "file returns 0 on success, and non-zero on
378    * error", but this is evidently not true.  It always returns 0, in
379    * every scenario I can think up.  So check the target is readable
380    * first.
381    */
382   if (access (buf, R_OK) == -1) {
383     if (freeit) free (buf);
384     reply_with_perror ("access: %s", path);
385     return NULL;
386   }
387
388   r = command (&out, &err, "file", "-zbsL", buf, NULL);
389   if (freeit) free (buf);
390
391   if (r == -1) {
392     free (out);
393     reply_with_error ("file: %s: %s", path, err);
394     free (err);
395     return NULL;
396   }
397   free (err);
398
399   /* We need to remove the trailing \n from output of file(1). */
400   len = strlen (out);
401   if (out[len-1] == '\n')
402     out[len-1] = '\0';
403
404   return out;                   /* caller frees */
405 }
406
407 /* zcat | file */
408 char *
409 do_zfile (const char *method, const char *path)
410 {
411   int len;
412   const char *zcat;
413   char *cmd;
414   FILE *fp;
415   char line[256];
416
417   if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0)
418     zcat = "zcat";
419   else if (strcmp (method, "bzip2") == 0)
420     zcat = "bzcat";
421   else {
422     reply_with_error ("zfile: unknown method");
423     return NULL;
424   }
425
426   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
427     reply_with_perror ("asprintf");
428     return NULL;
429   }
430
431   if (verbose)
432     fprintf (stderr, "%s\n", cmd);
433
434   fp = popen (cmd, "r");
435   if (fp == NULL) {
436     reply_with_perror ("%s", cmd);
437     free (cmd);
438     return NULL;
439   }
440
441   free (cmd);
442
443   if (fgets (line, sizeof line, fp) == NULL) {
444     reply_with_perror ("zfile: fgets");
445     fclose (fp);
446     return NULL;
447   }
448
449   if (fclose (fp) == -1) {
450     reply_with_perror ("zfile: fclose");
451     return NULL;
452   }
453
454   len = strlen (line);
455   if (len > 0 && line[len-1] == '\n')
456     line[len-1] = '\0';
457
458   return strdup (line);
459 }