X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fchecksum.c;h=f7e2d125ec6bf7fce3181e5e973813d6f88b7e18;hp=3654b650715af3165427e198474e5c92cdedf061;hb=41ee105aabaf1e9a7efc437b87d9536d3dc14a75;hpb=56bef498f46ac3dd580f4bde3c8f3ed2fe688826 diff --git a/daemon/checksum.c b/daemon/checksum.c index 3654b65..f7e2d12 100644 --- a/daemon/checksum.c +++ b/daemon/checksum.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,52 +22,48 @@ #include #include #include +#include #include "../src/guestfs_protocol.h" #include "daemon.h" #include "actions.h" -char * -do_checksum (char *csumtype, char *path) +static const char * +program_of_csum (const char *csumtype) { - const char *program; - char *buf; - char *out, *err; - int r, len; - - NEED_ROOT (NULL); - ABS_PATH (path, NULL); - - if (strcasecmp (csumtype, "crc") == 0) - program = "cksum"; - else if (strcasecmp (csumtype, "md5") == 0) - program = "md5sum"; - else if (strcasecmp (csumtype, "sha1") == 0) - program = "sha1sum"; - else if (strcasecmp (csumtype, "sha224") == 0) - program = "sha224sum"; - else if (strcasecmp (csumtype, "sha256") == 0) - program = "sha256sum"; - else if (strcasecmp (csumtype, "sha384") == 0) - program = "sha384sum"; - else if (strcasecmp (csumtype, "sha512") == 0) - program = "sha512sum"; + if (STRCASEEQ (csumtype, "crc")) + return "cksum"; + else if (STRCASEEQ (csumtype, "md5")) + return "md5sum"; + else if (STRCASEEQ (csumtype, "sha1")) + return "sha1sum"; + else if (STRCASEEQ (csumtype, "sha224")) + return "sha224sum"; + else if (STRCASEEQ (csumtype, "sha256")) + return "sha256sum"; + else if (STRCASEEQ (csumtype, "sha384")) + return "sha384sum"; + else if (STRCASEEQ (csumtype, "sha512")) + return "sha512sum"; else { reply_with_error ("unknown checksum type, expecting crc|md5|sha1|sha224|sha256|sha384|sha512"); return NULL; } +} - /* Make the path relative to /sysroot. */ - len = strlen (path) + 9; - buf = malloc (len); - if (!buf) { - reply_with_perror ("malloc"); +static char * +checksum (const char *csumtype, const char *path) +{ + const char *program; + char *out, *err; + int r; + int len; + + program = program_of_csum (csumtype); + if (program == NULL) return NULL; - } - snprintf (buf, len, "/sysroot%s", path); - r = command (&out, &err, program, buf, NULL); - free (buf); + r = command (&out, &err, program, path, NULL); if (r == -1) { reply_with_error ("%s: %s", program, err); free (out); @@ -83,3 +79,107 @@ do_checksum (char *csumtype, char *path) return out; /* Caller frees. */ } + +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"); + return NULL; + } + + char *r = checksum (csumtype, buf); + free (buf); + return r; +} + +char * +do_checksum_device (const char *csumtype, const char *device) +{ + return checksum (csumtype, device); +} + +/* 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; +}