X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Ffile.c;h=0b50eebd7fa66b00a284d81e6b1647d91b4bcb28;hp=7e2f062a39f76632f4703022de6e524b06ca88eb;hb=fd84feb8229f792e22e3cbfa9bf7e8156947eb2f;hpb=2eb19f526164a978c373a760deb30854d56b62ce diff --git a/daemon/file.c b/daemon/file.c index 7e2f062..0b50eeb 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); @@ -369,6 +365,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 ("pread: %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) @@ -378,7 +426,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); @@ -429,9 +477,9 @@ 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");