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)
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);
66 do_cat (const char *path)
69 int alloc, size, r, max;
76 fd = open (path, O_RDONLY);
80 reply_with_perror ("open: %s", path);
84 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
85 * larger than that, we need to return an error instead (for
88 max = GUESTFS_MESSAGE_MAX - 1000;
96 reply_with_error ("cat: %s: file is too large for message buffer",
102 buf2 = realloc (buf, alloc);
104 reply_with_perror ("realloc");
112 r = read (fd, buf + size, alloc - size);
114 reply_with_perror ("read: %s", path);
127 if (close (fd) == -1) {
128 reply_with_perror ("close: %s", path);
133 return buf; /* caller will free */
137 do_read_lines (const char *path)
140 int size = 0, alloc = 0;
147 ABS_PATH (path, NULL);
150 fp = fopen (path, "r");
154 reply_with_perror ("fopen: %s", path);
158 while ((n = getline (&line, &len, fp)) != -1) {
159 /* Remove either LF or CRLF. */
160 if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
162 else if (n >= 1 && line[n-1] == '\n')
165 if (add_string (&r, &size, &alloc, line) == -1) {
174 if (add_string (&r, &size, &alloc, NULL) == -1) {
179 if (fclose (fp) == EOF) {
180 reply_with_perror ("fclose: %s", path);
189 do_rm (const char *path)
201 reply_with_perror ("unlink: %s", path);
209 do_chmod (int mode, const char *path)
217 r = chmod (path, mode);
221 reply_with_perror ("chmod: %s: 0%o", path, mode);
229 do_chown (int owner, int group, const char *path)
237 r = chown (path, owner, group);
241 reply_with_perror ("chown: %s: %d.%d", path, owner, group);
249 do_exists (const char *path)
257 r = access (path, F_OK);
264 do_is_file (const char *path)
273 r = lstat (path, &buf);
277 if (errno != ENOENT && errno != ENOTDIR) {
278 reply_with_perror ("stat: %s", path);
282 return 0; /* Not a file. */
285 return S_ISREG (buf.st_mode);
289 do_write_file (const char *path, const char *content, int size)
297 size = strlen (content);
300 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
304 reply_with_perror ("open: %s", path);
308 if (xwrite (fd, content, size) == -1) {
309 reply_with_perror ("write");
314 if (close (fd) == -1) {
315 reply_with_perror ("close: %s", path);
322 /* This runs the 'file' command. */
324 do_file (const char *path)
327 int r, len, freeit = 0;
330 NEED_ROOT_OR_IS_DEVICE (path, NULL);
331 ABS_PATH (path, NULL);
333 if (strncmp (path, "/dev/", 5) == 0)
336 len = strlen (path) + 9;
339 reply_with_perror ("malloc");
342 snprintf (buf, len, "/sysroot%s", path);
346 /* file(1) manpage claims "file returns 0 on success, and non-zero on
347 * error", but this is evidently not true. It always returns 0, in
348 * every scenario I can think up. So check the target is readable
351 if (access (buf, R_OK) == -1) {
352 if (freeit) free (buf);
353 reply_with_perror ("access: %s", path);
357 r = command (&out, &err, "file", "-bsL", buf, NULL);
358 if (freeit) free (buf);
362 reply_with_error ("file: %s: %s", path, err);
368 /* We need to remove the trailing \n from output of file(1). */
370 if (out[len-1] == '\n')
373 return out; /* caller frees */