From: Richard Jones Date: Mon, 20 Apr 2009 09:19:29 +0000 (+0100) Subject: Add 'checksum' command. X-Git-Tag: 1.0.2~4 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=aef3d2013fee188c9607f35657c45df88503cd64;p=libguestfs.git Add 'checksum' command. --- diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 1c1609d..1886639 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -22,6 +22,7 @@ guestfsd_SOURCES = \ actions.h \ augeas.c \ blockdev.c \ + checksum.c \ command.c \ daemon.h \ devsparts.c \ diff --git a/daemon/checksum.c b/daemon/checksum.c new file mode 100644 index 0000000..bdbef13 --- /dev/null +++ b/daemon/checksum.c @@ -0,0 +1,85 @@ +/* libguestfs - the guestfsd daemon + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include +#include +#include +#include + +#include "../src/guestfs_protocol.h" +#include "daemon.h" +#include "actions.h" + +char * +do_checksum (const char *csumtype, const char *path) +{ + 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"; + 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"); + return NULL; + } + snprintf (buf, len, "/sysroot%s", path); + + r = command (&out, &err, program, buf, NULL); + free (buf); + if (r == -1) { + reply_with_error ("%s: %s", program, err); + free (out); + free (err); + return NULL; + } + + free (err); + + /* Split it at the first whitespace. */ + len = strcspn (out, " \t\n"); + out[len] = '\0'; + + return out; /* Caller frees. */ +} diff --git a/src/generator.ml b/src/generator.ml index 34b2241..4ca3dc2 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -1294,6 +1294,73 @@ C can also be a named pipe. See also C, C."); + ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [], + [InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "crc"; "/new"]], "935282863"); + InitBasicFS, TestLastFail ( + [["checksum"; "crc"; "/new"]]); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249"); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83"); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec"); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2"); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d"); + InitBasicFS, TestOutput ( + [["write_file"; "/new"; "test\n"; "0"]; + ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123")], + "compute MD5, SHAx or CRC checksum of file", + "\ +This call computes the MD5, SHAx or CRC checksum of the +file named C. + +The type of checksum to compute is given by the C +parameter which must have one of the following values: + +=over 4 + +=item C + +Compute the cyclic redundancy check (CRC) specified by POSIX +for the C command. + +=item C + +Compute the MD5 hash (using the C program). + +=item C + +Compute the SHA1 hash (using the C program). + +=item C + +Compute the SHA224 hash (using the C program). + +=item C + +Compute the SHA256 hash (using the C program). + +=item C + +Compute the SHA384 hash (using the C program). + +=item C + +Compute the SHA512 hash (using the C program). + +=back + +The checksum is returned as a printable string."); + ] let all_functions = non_daemon_functions @ daemon_functions