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);
326 /* This runs the 'file' command. */
331 int r, len, freeit = 0;
334 NEED_ROOT_OR_IS_DEVICE (path, NULL);
335 ABS_PATH (path, NULL);
337 if (strncmp (path, "/dev/", 5) == 0)
340 len = strlen (path) + 9;
343 reply_with_perror ("malloc");
346 snprintf (buf, len, "/sysroot%s", path);
350 /* file(1) manpage claims "file returns 0 on success, and non-zero on
351 * error", but this is evidently not true. It always returns 0, in
352 * every scenario I can think up. So check the target is readable
355 if (access (buf, R_OK) == -1) {
356 if (freeit) free (buf);
357 reply_with_perror ("access: %s", path);
361 r = command (&out, &err, "file", "-bsL", buf, NULL);
362 if (freeit) free (buf);
366 reply_with_error ("file: %s: %s", path, err);
372 /* We need to remove the trailing \n from output of file(1). */
374 if (out[len-1] == '\n')
377 return out; /* caller frees */
382 do_zfile (char *method, char *path)
390 ABS_PATH (path, NULL);
392 len = 2 * strlen (path) + 64;
395 reply_with_perror ("malloc");
399 if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0)
400 strcpy (cmd, "zcat");
401 else if (strcmp (method, "bzip2") == 0)
402 strcpy (cmd, "bzcat");
405 reply_with_error ("zfile: unknown method");
409 strcat (cmd, " /sysroot");
410 shell_quote (cmd + strlen (cmd), len - strlen (cmd), path);
411 strcat (cmd, " | file -bsL -");
413 fp = popen (cmd, "r");
415 reply_with_perror ("%s", cmd);
422 if (fgets (line, sizeof line, fp) == NULL) {
423 reply_with_perror ("zfile: fgets");
428 if (fclose (fp) == -1) {
429 reply_with_perror ("zfile: fclose");
434 if (len > 0 && line[len-1] == '\n')
437 return strdup (line);