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"
33 program_of_csum (const char *csumtype)
35 if (STRCASEEQ (csumtype, "crc"))
37 else if (STRCASEEQ (csumtype, "md5"))
39 else if (STRCASEEQ (csumtype, "sha1"))
41 else if (STRCASEEQ (csumtype, "sha224"))
43 else if (STRCASEEQ (csumtype, "sha256"))
45 else if (STRCASEEQ (csumtype, "sha384"))
47 else if (STRCASEEQ (csumtype, "sha512"))
50 reply_with_error ("unknown checksum type, expecting crc|md5|sha1|sha224|sha256|sha384|sha512");
56 checksum (const char *csumtype, int fd)
63 program = program_of_csum (csumtype);
67 flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd;
68 r = commandf (&out, &err, flags, program, NULL);
70 reply_with_error ("%s: %s", program, err);
78 /* Split it at the first whitespace. */
79 len = strcspn (out, " \t\n");
82 return out; /* Caller frees. */
86 do_checksum (const char *csumtype, const char *path)
91 fd = open (path, O_RDONLY);
95 reply_with_perror ("%s", path);
99 return checksum (csumtype, fd);
103 do_checksum_device (const char *csumtype, const char *device)
107 fd = open (device, O_RDONLY);
109 reply_with_perror ("%s", device);
113 return checksum (csumtype, fd);
116 /* Has one FileOut parameter. */
118 do_checksums_out (const char *csumtype, const char *dir)
123 const char *program = program_of_csum (csumtype);
127 char *sysrootdir = sysroot_path (dir);
129 reply_with_perror ("malloc");
133 r = stat (sysrootdir, &statbuf);
135 reply_with_perror ("%s", dir);
139 if (!S_ISDIR (statbuf.st_mode)) {
140 reply_with_error ("%s: not a directory", dir);
146 if (asprintf_nowarn (&cmd, "cd %Q && find -type f -print0 | xargs -0 %s",
147 sysrootdir, program) == -1) {
148 reply_with_perror ("asprintf");
155 fprintf (stderr, "%s\n", cmd);
157 FILE *fp = popen (cmd, "r");
159 reply_with_perror ("%s", cmd);
165 /* Now we must send the reply message, before the file contents. After
166 * this there is no opportunity in the protocol to send any error
167 * message back. Instead we can only cancel the transfer.
171 char str[GUESTFS_MAX_CHUNK_SIZE];
173 while ((r = fread (str, 1, GUESTFS_MAX_CHUNK_SIZE, fp)) > 0) {
174 if (send_file_write (str, r) < 0) {
182 send_file_end (1); /* Cancel. */
187 if (pclose (fp) != 0) {
189 send_file_end (1); /* Cancel. */
193 if (send_file_end (0)) /* Normal end of file. */