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);
47 r = futimens (fd, NULL);
49 reply_with_perror ("futimens: %s", path);
54 if (close (fd) == -1) {
55 reply_with_perror ("close: %s", path);
63 do_cat (const char *path)
66 int alloc, size, r, max;
70 fd = open (path, O_RDONLY);
74 reply_with_perror ("open: %s", path);
78 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
79 * larger than that, we need to return an error instead (for
82 max = GUESTFS_MESSAGE_MAX - 1000;
90 reply_with_error ("cat: %s: file is too large for message buffer",
96 buf2 = realloc (buf, alloc);
98 reply_with_perror ("realloc");
106 r = read (fd, buf + size, alloc - size);
108 reply_with_perror ("read: %s", path);
121 if (close (fd) == -1) {
122 reply_with_perror ("close: %s", path);
127 return buf; /* caller will free */
131 do_read_lines (const char *path)
134 int size = 0, alloc = 0;
141 fp = fopen (path, "r");
145 reply_with_perror ("fopen: %s", path);
149 while ((n = getline (&line, &len, fp)) != -1) {
150 /* Remove either LF or CRLF. */
151 if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
153 else if (n >= 1 && line[n-1] == '\n')
156 if (add_string (&r, &size, &alloc, line) == -1) {
165 if (add_string (&r, &size, &alloc, NULL) == -1) {
170 if (fclose (fp) == EOF) {
171 reply_with_perror ("fclose: %s", path);
180 do_rm (const char *path)
189 reply_with_perror ("unlink: %s", path);
197 do_chmod (int mode, const char *path)
202 r = chmod (path, mode);
206 reply_with_perror ("chmod: %s: 0%o", path, mode);
214 do_chown (int owner, int group, const char *path)
219 r = chown (path, owner, group);
223 reply_with_perror ("chown: %s: %d.%d", path, owner, group);
231 do_lchown (int owner, int group, const char *path)
236 r = lchown (path, owner, group);
240 reply_with_perror ("lchown: %s: %d.%d", path, owner, group);
248 do_exists (const char *path)
253 r = access (path, F_OK);
260 do_is_file (const char *path)
266 r = lstat (path, &buf);
270 if (errno != ENOENT && errno != ENOTDIR) {
271 reply_with_perror ("stat: %s", path);
275 return 0; /* Not a file. */
278 return S_ISREG (buf.st_mode);
282 do_write_file (const char *path, const char *content, int size)
287 size = strlen (content);
290 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
294 reply_with_perror ("open: %s", path);
298 if (xwrite (fd, content, size) == -1) {
299 reply_with_perror ("write");
304 if (close (fd) == -1) {
305 reply_with_perror ("close: %s", path);
313 do_read_file (const char *path, size_t *size_r)
320 fd = open (path, O_RDONLY);
324 reply_with_perror ("open: %s", path);
328 if (fstat (fd, &statbuf) == -1) {
329 reply_with_perror ("fstat: %s", path);
334 *size_r = statbuf.st_size;
335 /* The actual limit on messages is smaller than this. This
336 * check just limits the amount of memory we'll try and allocate
337 * here. If the message is larger than the real limit, that will
338 * be caught later when we try to serialize the message.
340 if (*size_r >= GUESTFS_MESSAGE_MAX) {
341 reply_with_error ("read_file: %s: file is too large for the protocol, use guestfs_download instead", path);
345 r = malloc (*size_r);
347 reply_with_perror ("malloc");
352 if (xread (fd, r, *size_r) == -1) {
353 reply_with_perror ("read: %s", path);
359 if (close (fd) == -1) {
360 reply_with_perror ("close: %s", path);
369 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
375 /* The actual limit on messages is smaller than this. This check
376 * just limits the amount of memory we'll try and allocate in the
377 * function. If the message is larger than the real limit, that
378 * will be caught later when we try to serialize the message.
380 if (count >= GUESTFS_MESSAGE_MAX) {
381 reply_with_error ("pread: %s: count is too large for the protocol, use smaller reads", path);
386 fd = open (path, O_RDONLY);
390 reply_with_perror ("open: %s", path);
394 buf = malloc (count);
396 reply_with_perror ("malloc");
401 r = pread (fd, buf, count, offset);
403 reply_with_perror ("pread: %s", path);
409 if (close (fd) == -1) {
410 reply_with_perror ("close: %s", path);
420 /* This runs the 'file' command. */
422 do_file (const char *path)
429 if (STREQLEN (path, "/dev/", 5))
432 buf = sysroot_path (path);
434 reply_with_perror ("malloc");
440 /* file(1) manpage claims "file returns 0 on success, and non-zero on
441 * error", but this is evidently not true. It always returns 0, in
442 * every scenario I can think up. So check the target is readable
445 if (access (buf, R_OK) == -1) {
446 if (freeit) free (buf);
447 reply_with_perror ("access: %s", path);
451 r = command (&out, &err, "file", "-zbsL", buf, NULL);
452 if (freeit) free (buf);
456 reply_with_error ("file: %s: %s", path, err);
462 /* We need to remove the trailing \n from output of file(1). */
464 if (out[len-1] == '\n')
467 return out; /* caller frees */
472 do_zfile (const char *method, const char *path)
480 if (STREQ (method, "gzip") || STREQ (method, "compress"))
482 else if (STREQ (method, "bzip2"))
485 reply_with_error ("zfile: unknown method");
489 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
490 reply_with_perror ("asprintf");
495 fprintf (stderr, "%s\n", cmd);
497 fp = popen (cmd, "r");
499 reply_with_perror ("%s", cmd);
506 if (fgets (line, sizeof line, fp) == NULL) {
507 reply_with_perror ("zfile: fgets");
512 if (fclose (fp) == -1) {
513 reply_with_perror ("zfile: fclose");
518 if (len > 0 && line[len-1] == '\n')
521 return strdup (line);
525 do_filesize (const char *path)
531 r = stat (path, &buf); /* follow symlinks */
535 reply_with_perror ("filesize: %s", path);