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.
21 #define _GNU_SOURCE /* for futimens(2) and getline(3) */
30 #include "../src/guestfs_protocol.h"
35 do_touch (const char *path)
43 fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
47 reply_with_perror ("open: %s", path);
51 if (futimens (fd, NULL) == -1) {
52 reply_with_perror ("futimens: %s", path);
62 do_cat (const char *path)
65 int alloc, size, r, max;
72 fd = open (path, O_RDONLY);
76 reply_with_perror ("open: %s", path);
80 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
81 * larger than that, we need to return an error instead (for
84 max = GUESTFS_MESSAGE_MAX - 1000;
92 reply_with_error ("cat: %s: file is too large for message buffer",
98 buf2 = realloc (buf, alloc);
100 reply_with_perror ("realloc");
108 r = read (fd, buf + size, alloc - size);
110 reply_with_perror ("read: %s", path);
123 if (close (fd) == -1) {
124 reply_with_perror ("close: %s", path);
129 return buf; /* caller will free */
133 do_read_lines (const char *path)
136 int size = 0, alloc = 0;
143 ABS_PATH (path, NULL);
146 fp = fopen (path, "r");
150 reply_with_perror ("fopen: %s", path);
154 while ((n = getline (&line, &len, fp)) != -1) {
155 /* Remove either LF or CRLF. */
156 if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
158 else if (n >= 1 && line[n-1] == '\n')
161 if (add_string (&r, &size, &alloc, line) == -1) {
170 if (add_string (&r, &size, &alloc, NULL) == -1) {
175 if (fclose (fp) == EOF) {
176 reply_with_perror ("fclose: %s", path);
185 do_rm (const char *path)
197 reply_with_perror ("unlink: %s", path);
205 do_chmod (int mode, const char *path)
213 r = chmod (path, mode);
217 reply_with_perror ("chmod: %s: 0%o", path, mode);
225 do_chown (int owner, int group, const char *path)
233 r = chown (path, owner, group);
237 reply_with_perror ("chown: %s: %d.%d", path, owner, group);
245 do_exists (const char *path)
253 r = access (path, F_OK);
260 do_is_file (const char *path)
269 r = lstat (path, &buf);
273 if (errno != ENOENT && errno != ENOTDIR) {
274 reply_with_perror ("stat: %s", path);
278 return 0; /* Not a file. */
281 return S_ISREG (buf.st_mode);
285 do_write_file (const char *path, const char *content, int size)
293 size = strlen (content);
296 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
300 reply_with_perror ("open: %s", path);
304 if (xwrite (fd, content, size) == -1) {
305 reply_with_perror ("write");
310 if (close (fd) == -1) {
311 reply_with_perror ("close: %s", path);
318 /* This runs the 'file' command. */
320 do_file (const char *path)
323 int r, len, freeit = 0;
326 /*NEED_ROOT (NULL); - no: we allow people to run this on /dev devices. */
327 ABS_PATH (path, NULL);
329 if (strncmp (path, "/dev/", 5) == 0)
332 len = strlen (path) + 9;
335 reply_with_perror ("malloc");
338 snprintf (buf, len, "/sysroot%s", path);
342 /* file(1) manpage claims "file returns 0 on success, and non-zero on
343 * error", but this is evidently not true. It always returns 0, in
344 * every scenario I can think up. So check the target is readable
347 if (access (buf, R_OK) == -1) {
348 if (freeit) free (buf);
349 reply_with_perror ("access: %s", path);
353 r = command (&out, &err, "file", "-bsL", buf, NULL);
354 if (freeit) free (buf);
358 reply_with_error ("file: %s: %s", path, err);
364 /* We need to remove the trailing \n from output of file(1). */
366 if (out[len-1] == '\n')
369 return out; /* caller frees */