1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
28 #include "../src/guestfs_protocol.h"
33 do_touch (const char *path)
39 fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
43 reply_with_perror ("open: %s", path);
48 r = futimens (fd, NULL);
50 r = futimes (fd, NULL);
53 reply_with_perror ("futimens: %s", path);
58 if (close (fd) == -1) {
59 reply_with_perror ("close: %s", path);
67 do_cat (const char *path)
70 int alloc, size, r, max;
74 fd = open (path, O_RDONLY);
78 reply_with_perror ("open: %s", path);
82 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
83 * larger than that, we need to return an error instead (for
86 max = GUESTFS_MESSAGE_MAX - 1000;
94 reply_with_error ("cat: %s: file is too large for message buffer",
100 buf2 = realloc (buf, alloc);
102 reply_with_perror ("realloc");
110 r = read (fd, buf + size, alloc - size);
112 reply_with_perror ("read: %s", path);
125 if (close (fd) == -1) {
126 reply_with_perror ("close: %s", path);
131 return buf; /* caller will free */
135 do_read_lines (const char *path)
138 int size = 0, alloc = 0;
145 fp = fopen (path, "r");
149 reply_with_perror ("fopen: %s", path);
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')
157 else if (n >= 1 && line[n-1] == '\n')
160 if (add_string (&r, &size, &alloc, line) == -1) {
169 if (add_string (&r, &size, &alloc, NULL) == -1) {
174 if (fclose (fp) == EOF) {
175 reply_with_perror ("fclose: %s", path);
184 do_rm (const char *path)
193 reply_with_perror ("unlink: %s", path);
201 do_chmod (int mode, const char *path)
206 r = chmod (path, mode);
210 reply_with_perror ("chmod: %s: 0%o", path, mode);
218 do_chown (int owner, int group, const char *path)
223 r = chown (path, owner, group);
227 reply_with_perror ("chown: %s: %d.%d", path, owner, group);
235 do_lchown (int owner, int group, const char *path)
240 r = lchown (path, owner, group);
244 reply_with_perror ("lchown: %s: %d.%d", path, owner, group);
252 do_exists (const char *path)
257 r = access (path, F_OK);
264 do_is_file (const char *path)
270 r = lstat (path, &buf);
274 if (errno != ENOENT && errno != ENOTDIR) {
275 reply_with_perror ("stat: %s", path);
279 return 0; /* Not a file. */
282 return S_ISREG (buf.st_mode);
286 do_write_file (const char *path, const char *content, int size)
291 size = strlen (content);
294 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
298 reply_with_perror ("open: %s", path);
302 if (xwrite (fd, content, size) == -1) {
303 reply_with_perror ("write");
308 if (close (fd) == -1) {
309 reply_with_perror ("close: %s", path);
317 do_read_file (const char *path, size_t *size_r)
324 fd = open (path, O_RDONLY);
328 reply_with_perror ("open: %s", path);
332 if (fstat (fd, &statbuf) == -1) {
333 reply_with_perror ("fstat: %s", path);
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.
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);
349 r = malloc (*size_r);
351 reply_with_perror ("malloc");
356 if (xread (fd, r, *size_r) == -1) {
357 reply_with_perror ("read: %s", path);
363 if (close (fd) == -1) {
364 reply_with_perror ("close: %s", path);
373 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
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.
384 if (count >= GUESTFS_MESSAGE_MAX) {
385 reply_with_error ("pread: %s: count is too large for the protocol, use smaller reads", path);
390 fd = open (path, O_RDONLY);
394 reply_with_perror ("open: %s", path);
398 buf = malloc (count);
400 reply_with_perror ("malloc");
405 r = pread (fd, buf, count, offset);
407 reply_with_perror ("pread: %s", path);
413 if (close (fd) == -1) {
414 reply_with_perror ("close: %s", path);
424 /* This runs the 'file' command. */
426 do_file (const char *path)
433 if (strncmp (path, "/dev/", 5) == 0)
436 buf = sysroot_path (path);
438 reply_with_perror ("malloc");
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
449 if (access (buf, R_OK) == -1) {
450 if (freeit) free (buf);
451 reply_with_perror ("access: %s", path);
455 r = command (&out, &err, "file", "-zbsL", buf, NULL);
456 if (freeit) free (buf);
460 reply_with_error ("file: %s: %s", path, err);
466 /* We need to remove the trailing \n from output of file(1). */
468 if (out[len-1] == '\n')
471 return out; /* caller frees */
476 do_zfile (const char *method, const char *path)
484 if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0)
486 else if (strcmp (method, "bzip2") == 0)
489 reply_with_error ("zfile: unknown method");
493 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
494 reply_with_perror ("asprintf");
499 fprintf (stderr, "%s\n", cmd);
501 fp = popen (cmd, "r");
503 reply_with_perror ("%s", cmd);
510 if (fgets (line, sizeof line, fp) == NULL) {
511 reply_with_perror ("zfile: fgets");
516 if (fclose (fp) == -1) {
517 reply_with_perror ("zfile: fclose");
522 if (len > 0 && line[len-1] == '\n')
525 return strdup (line);