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"
42 fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
46 reply_with_perror ("open: %s", path);
51 r = futimens (fd, NULL);
53 r = futimes (fd, NULL);
56 reply_with_perror ("futimens: %s", path);
61 if (close (fd) == -1) {
62 reply_with_perror ("close: %s", path);
73 int alloc, size, r, max;
80 fd = open (path, O_RDONLY);
84 reply_with_perror ("open: %s", path);
88 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
89 * larger than that, we need to return an error instead (for
92 max = GUESTFS_MESSAGE_MAX - 1000;
100 reply_with_error ("cat: %s: file is too large for message buffer",
106 buf2 = realloc (buf, alloc);
108 reply_with_perror ("realloc");
116 r = read (fd, buf + size, alloc - size);
118 reply_with_perror ("read: %s", path);
131 if (close (fd) == -1) {
132 reply_with_perror ("close: %s", path);
137 return buf; /* caller will free */
141 do_read_lines (char *path)
144 int size = 0, alloc = 0;
151 ABS_PATH (path, NULL);
154 fp = fopen (path, "r");
158 reply_with_perror ("fopen: %s", path);
162 while ((n = getline (&line, &len, fp)) != -1) {
163 /* Remove either LF or CRLF. */
164 if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
166 else if (n >= 1 && line[n-1] == '\n')
169 if (add_string (&r, &size, &alloc, line) == -1) {
178 if (add_string (&r, &size, &alloc, NULL) == -1) {
183 if (fclose (fp) == EOF) {
184 reply_with_perror ("fclose: %s", path);
205 reply_with_perror ("unlink: %s", path);
213 do_chmod (int mode, char *path)
221 r = chmod (path, mode);
225 reply_with_perror ("chmod: %s: 0%o", path, mode);
233 do_chown (int owner, int group, char *path)
241 r = chown (path, owner, group);
245 reply_with_perror ("chown: %s: %d.%d", path, owner, group);
253 do_exists (char *path)
261 r = access (path, F_OK);
268 do_is_file (char *path)
277 r = lstat (path, &buf);
281 if (errno != ENOENT && errno != ENOTDIR) {
282 reply_with_perror ("stat: %s", path);
286 return 0; /* Not a file. */
289 return S_ISREG (buf.st_mode);
293 do_write_file (char *path, char *content, int size)
301 size = strlen (content);
304 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
308 reply_with_perror ("open: %s", path);
312 if (xwrite (fd, content, size) == -1) {
313 reply_with_perror ("write");
318 if (close (fd) == -1) {
319 reply_with_perror ("close: %s", path);
327 do_read_file (char *path, size_t *size_r)
334 ABS_PATH (path, NULL);
337 fd = open (path, O_RDONLY);
341 reply_with_perror ("open: %s", path);
345 if (fstat (fd, &statbuf) == -1) {
346 reply_with_perror ("fstat: %s", path);
351 *size_r = statbuf.st_size;
352 /* The actual limit on messages is smaller than this. This
353 * check just limits the amount of memory we'll try and allocate
354 * here. If the message is larger than the real limit, that will
355 * be caught later when we try to serialize the message.
357 if (*size_r >= GUESTFS_MESSAGE_MAX) {
358 reply_with_error ("read_file: %s: file is too large for the protocol, use guestfs_download instead", path);
362 r = malloc (*size_r);
364 reply_with_perror ("malloc");
369 if (xread (fd, r, *size_r) == -1) {
370 reply_with_perror ("read: %s", path);
376 if (close (fd) == -1) {
377 reply_with_perror ("close: %s", path);
385 /* This runs the 'file' command. */
394 NEED_ROOT_OR_IS_DEVICE (path, NULL);
395 ABS_PATH (path, NULL);
397 if (strncmp (path, "/dev/", 5) == 0)
400 buf = sysroot_path (path);
402 reply_with_perror ("malloc");
408 /* file(1) manpage claims "file returns 0 on success, and non-zero on
409 * error", but this is evidently not true. It always returns 0, in
410 * every scenario I can think up. So check the target is readable
413 if (access (buf, R_OK) == -1) {
414 if (freeit) free (buf);
415 reply_with_perror ("access: %s", path);
419 r = command (&out, &err, "file", "-zbsL", buf, NULL);
420 if (freeit) free (buf);
424 reply_with_error ("file: %s: %s", path, err);
430 /* We need to remove the trailing \n from output of file(1). */
432 if (out[len-1] == '\n')
435 return out; /* caller frees */
440 do_zfile (char *method, char *path)
449 ABS_PATH (path, NULL);
451 if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0)
453 else if (strcmp (method, "bzip2") == 0)
456 reply_with_error ("zfile: unknown method");
460 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
461 reply_with_perror ("asprintf");
466 fprintf (stderr, "%s\n", cmd);
468 fp = popen (cmd, "r");
470 reply_with_perror ("%s", cmd);
477 if (fgets (line, sizeof line, fp) == NULL) {
478 reply_with_perror ("zfile: fgets");
483 if (fclose (fp) == -1) {
484 reply_with_perror ("zfile: fclose");
489 if (len > 0 && line[len-1] == '\n')
492 return strdup (line);