X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Ffile.c;h=7a0f8f92df193cd4cacd45978ef97e880fabc17f;hp=212aff28e9b6cfdc03f487b16fab1af8695f13c8;hb=41ee105aabaf1e9a7efc437b87d9536d3dc14a75;hpb=08c27722647d14a30648dd02e221e1e803d1189e diff --git a/daemon/file.c b/daemon/file.c index 212aff2..7a0f8f9 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -44,11 +44,7 @@ do_touch (const char *path) return -1; } -#ifdef HAVE_FUTIMENS r = futimens (fd, NULL); -#else - r = futimes (fd, NULL); -#endif if (r == -1) { reply_with_perror ("futimens: %s", path); close (fd); @@ -91,7 +87,7 @@ do_cat (const char *path) if (size >= alloc) { alloc += 8192; if (alloc > max) { - reply_with_error ("cat: %s: file is too large for message buffer", + reply_with_error ("%s: file is too large for message buffer", path); free (buf); close (fd); @@ -190,7 +186,7 @@ do_rm (const char *path) CHROOT_OUT; if (r == -1) { - reply_with_perror ("unlink: %s", path); + reply_with_perror ("%s", path); return -1; } @@ -202,12 +198,17 @@ do_chmod (int mode, const char *path) { int r; + if (mode < 0) { + reply_with_error ("%s: mode is negative", path); + return -1; + } + CHROOT_IN; r = chmod (path, mode); CHROOT_OUT; if (r == -1) { - reply_with_perror ("chmod: %s: 0%o", path, mode); + reply_with_perror ("%s: 0%o", path, mode); return -1; } @@ -224,7 +225,24 @@ do_chown (int owner, int group, const char *path) CHROOT_OUT; if (r == -1) { - reply_with_perror ("chown: %s: %d.%d", path, owner, group); + reply_with_perror ("%s: %d.%d", path, owner, group); + return -1; + } + + return 0; +} + +int +do_lchown (int owner, int group, const char *path) +{ + int r; + + CHROOT_IN; + r = lchown (path, owner, group); + CHROOT_OUT; + + if (r == -1) { + reply_with_perror ("%s: %d.%d", path, owner, group); return -1; } @@ -325,7 +343,7 @@ do_read_file (const char *path, size_t *size_r) * be caught later when we try to serialize the message. */ if (*size_r >= GUESTFS_MESSAGE_MAX) { - reply_with_error ("read_file: %s: file is too large for the protocol, use guestfs_download instead", path); + reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path); close (fd); return NULL; } @@ -352,6 +370,58 @@ do_read_file (const char *path, size_t *size_r) return r; } +char * +do_pread (const char *path, int count, int64_t offset, size_t *size_r) +{ + int fd; + ssize_t r; + char *buf; + + /* The actual limit on messages is smaller than this. This check + * just limits the amount of memory we'll try and allocate in the + * function. If the message is larger than the real limit, that + * will be caught later when we try to serialize the message. + */ + if (count >= GUESTFS_MESSAGE_MAX) { + reply_with_error ("%s: count is too large for the protocol, use smaller reads", path); + return NULL; + } + + CHROOT_IN; + fd = open (path, O_RDONLY); + CHROOT_OUT; + + if (fd == -1) { + reply_with_perror ("open: %s", path); + return NULL; + } + + buf = malloc (count); + if (buf == NULL) { + reply_with_perror ("malloc"); + close (fd); + return NULL; + } + + r = pread (fd, buf, count, offset); + if (r == -1) { + reply_with_perror ("pread: %s", path); + close (fd); + free (buf); + return NULL; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", path); + close (fd); + free (buf); + return NULL; + } + + *size_r = r; + return buf; +} + /* This runs the 'file' command. */ char * do_file (const char *path) @@ -361,7 +431,7 @@ do_file (const char *path) char *buf; int len; - if (strncmp (path, "/dev/", 5) == 0) + if (STREQLEN (path, "/dev/", 5)) buf = (char *) path; else { buf = sysroot_path (path); @@ -388,7 +458,7 @@ do_file (const char *path) if (r == -1) { free (out); - reply_with_error ("file: %s: %s", path, err); + reply_with_error ("%s: %s", path, err); free (err); return NULL; } @@ -412,12 +482,12 @@ do_zfile (const char *method, const char *path) FILE *fp; char line[256]; - if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0) + if (STREQ (method, "gzip") || STREQ (method, "compress")) zcat = "zcat"; - else if (strcmp (method, "bzip2") == 0) + else if (STREQ (method, "bzip2")) zcat = "bzcat"; else { - reply_with_error ("zfile: unknown method"); + reply_with_error ("unknown method"); return NULL; } @@ -439,13 +509,13 @@ do_zfile (const char *method, const char *path) free (cmd); if (fgets (line, sizeof line, fp) == NULL) { - reply_with_perror ("zfile: fgets"); + reply_with_perror ("fgets"); fclose (fp); return NULL; } if (fclose (fp) == -1) { - reply_with_perror ("zfile: fclose"); + reply_with_perror ("fclose"); return NULL; } @@ -455,3 +525,21 @@ do_zfile (const char *method, const char *path) return strdup (line); } + +int64_t +do_filesize (const char *path) +{ + int r; + struct stat buf; + + CHROOT_IN; + r = stat (path, &buf); /* follow symlinks */ + CHROOT_OUT; + + if (r == -1) { + reply_with_perror ("%s", path); + return -1; + } + + return buf.st_size; +}