New API call: pread
[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 char *
373 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
374 {
375   int fd;
376   ssize_t r;
377   char *buf;
378
379   /* The actual limit on messages is smaller than this.  This check
380    * just limits the amount of memory we'll try and allocate in the
381    * function.  If the message is larger than the real limit, that
382    * will be caught later when we try to serialize the message.
383    */
384   if (count >= GUESTFS_MESSAGE_MAX) {
385     reply_with_error ("pread: %s: count is too large for the protocol, use smaller reads", path);
386     return NULL;
387   }
388
389   CHROOT_IN;
390   fd = open (path, O_RDONLY);
391   CHROOT_OUT;
392
393   if (fd == -1) {
394     reply_with_perror ("open: %s", path);
395     return NULL;
396   }
397
398   buf = malloc (count);
399   if (buf == NULL) {
400     reply_with_perror ("malloc");
401     close (fd);
402     return NULL;
403   }
404
405   r = pread (fd, buf, count, offset);
406   if (r == -1) {
407     reply_with_perror ("pread: %s", path);
408     close (fd);
409     free (buf);
410     return NULL;
411   }
412
413   if (close (fd) == -1) {
414     reply_with_perror ("close: %s", path);
415     close (fd);
416     free (buf);
417     return NULL;
418   }
419
420   *size_r = r;
421   return buf;
422 }
423
424 /* This runs the 'file' command. */
425 char *
426 do_file (const char *path)
427 {
428   char *out, *err;
429   int r, freeit = 0;
430   char *buf;
431   int len;
432
433   if (strncmp (path, "/dev/", 5) == 0)
434     buf = (char *) path;
435   else {
436     buf = sysroot_path (path);
437     if (!buf) {
438       reply_with_perror ("malloc");
439       return NULL;
440     }
441     freeit = 1;
442   }
443
444   /* file(1) manpage claims "file returns 0 on success, and non-zero on
445    * error", but this is evidently not true.  It always returns 0, in
446    * every scenario I can think up.  So check the target is readable
447    * first.
448    */
449   if (access (buf, R_OK) == -1) {
450     if (freeit) free (buf);
451     reply_with_perror ("access: %s", path);
452     return NULL;
453   }
454
455   r = command (&out, &err, "file", "-zbsL", buf, NULL);
456   if (freeit) free (buf);
457
458   if (r == -1) {
459     free (out);
460     reply_with_error ("file: %s: %s", path, err);
461     free (err);
462     return NULL;
463   }
464   free (err);
465
466   /* We need to remove the trailing \n from output of file(1). */
467   len = strlen (out);
468   if (out[len-1] == '\n')
469     out[len-1] = '\0';
470
471   return out;                   /* caller frees */
472 }
473
474 /* zcat | file */
475 char *
476 do_zfile (const char *method, const char *path)
477 {
478   int len;
479   const char *zcat;
480   char *cmd;
481   FILE *fp;
482   char line[256];
483
484   if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0)
485     zcat = "zcat";
486   else if (strcmp (method, "bzip2") == 0)
487     zcat = "bzcat";
488   else {
489     reply_with_error ("zfile: unknown method");
490     return NULL;
491   }
492
493   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
494     reply_with_perror ("asprintf");
495     return NULL;
496   }
497
498   if (verbose)
499     fprintf (stderr, "%s\n", cmd);
500
501   fp = popen (cmd, "r");
502   if (fp == NULL) {
503     reply_with_perror ("%s", cmd);
504     free (cmd);
505     return NULL;
506   }
507
508   free (cmd);
509
510   if (fgets (line, sizeof line, fp) == NULL) {
511     reply_with_perror ("zfile: fgets");
512     fclose (fp);
513     return NULL;
514   }
515
516   if (fclose (fp) == -1) {
517     reply_with_perror ("zfile: fclose");
518     return NULL;
519   }
520
521   len = strlen (line);
522   if (len > 0 && line[len-1] == '\n')
523     line[len-1] = '\0';
524
525   return strdup (line);
526 }