X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fchecksum.c;h=e12a8a8997f06c76a647f82281373e37fdfff36a;hp=4ee62318567f514bf76504fe099c80d89e76db3d;hb=a0b7045e0abb7e3a7ff4d1324f16e50de94bed55;hpb=af29c84cef791381ac3aebae30d8831b7de77e36 diff --git a/daemon/checksum.c b/daemon/checksum.c index 4ee6231..e12a8a8 100644 --- a/daemon/checksum.c +++ b/daemon/checksum.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "../src/guestfs_protocol.h" #include "daemon.h" @@ -51,18 +53,19 @@ program_of_csum (const char *csumtype) } static char * -checksum (const char *csumtype, const char *path) +checksum (const char *csumtype, int fd) { const char *program; char *out, *err; - int r; + int flags, r; int len; program = program_of_csum (csumtype); if (program == NULL) return NULL; - r = command (&out, &err, program, path, NULL); + flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd; + r = commandf (&out, &err, flags, program, NULL); if (r == -1) { reply_with_error ("%s: %s", program, err); free (out); @@ -82,20 +85,113 @@ checksum (const char *csumtype, const char *path) char * do_checksum (const char *csumtype, const char *path) { - /* Make the path relative to /sysroot. */ - char *buf = sysroot_path (path); - if (!buf) { - reply_with_perror ("malloc"); + int fd; + + CHROOT_IN; + fd = open (path, O_RDONLY); + CHROOT_OUT; + + if (fd == -1) { + reply_with_perror ("%s", path); return NULL; } - char *r = checksum (csumtype, buf); - free (buf); - return r; + return checksum (csumtype, fd); } char * do_checksum_device (const char *csumtype, const char *device) { - return checksum (csumtype, device); + int fd; + + fd = open (device, O_RDONLY); + if (fd == -1) { + reply_with_perror ("%s", device); + return NULL; + } + + return checksum (csumtype, fd); +} + +/* Has one FileOut parameter. */ +int +do_checksums_out (const char *csumtype, const char *dir) +{ + struct stat statbuf; + int r; + + const char *program = program_of_csum (csumtype); + if (program == NULL) + return -1; + + char *sysrootdir = sysroot_path (dir); + if (!sysrootdir) { + reply_with_perror ("malloc"); + return -1; + } + + r = stat (sysrootdir, &statbuf); + if (r == -1) { + reply_with_perror ("%s", dir); + free (sysrootdir); + return -1; + } + if (!S_ISDIR (statbuf.st_mode)) { + reply_with_error ("%s: not a directory", dir); + free (sysrootdir); + return -1; + } + + char *cmd; + if (asprintf_nowarn (&cmd, "cd %Q && find -type f -print0 | xargs -0 %s", + sysrootdir, program) == -1) { + reply_with_perror ("asprintf"); + free (sysrootdir); + return -1; + } + free (sysrootdir); + + if (verbose) + fprintf (stderr, "%s\n", cmd); + + FILE *fp = popen (cmd, "r"); + if (fp == NULL) { + reply_with_perror ("%s", cmd); + free (cmd); + return -1; + } + free (cmd); + + /* Now we must send the reply message, before the file contents. After + * this there is no opportunity in the protocol to send any error + * message back. Instead we can only cancel the transfer. + */ + reply (NULL, NULL); + + char str[GUESTFS_MAX_CHUNK_SIZE]; + + while ((r = fread (str, 1, GUESTFS_MAX_CHUNK_SIZE, fp)) > 0) { + if (send_file_write (str, r) < 0) { + pclose (fp); + return -1; + } + } + + if (ferror (fp)) { + perror (dir); + send_file_end (1); /* Cancel. */ + pclose (fp); + return -1; + } + + if (pclose (fp) != 0) { + perror (dir); + send_file_end (1); /* Cancel. */ + return -1; + } + + if (send_file_end (0)) /* Normal end of file. */ + return -1; + + return 0; }