Add 'checksum' command.

This commit is contained in:
Richard Jones
2009-04-20 10:19:29 +01:00
parent 170f262f04
commit aef3d2013f
3 changed files with 153 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ guestfsd_SOURCES = \
actions.h \
augeas.c \
blockdev.c \
checksum.c \
command.c \
daemon.h \
devsparts.c \

85
daemon/checksum.c Normal file
View File

@@ -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 <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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. */
}

View File

@@ -1294,6 +1294,73 @@ C<filename> can also be a named pipe.
See also C<guestfs_upload>, C<guestfs_cat>.");
("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<path>.
The type of checksum to compute is given by the C<csumtype>
parameter which must have one of the following values:
=over 4
=item C<crc>
Compute the cyclic redundancy check (CRC) specified by POSIX
for the C<cksum> command.
=item C<md5>
Compute the MD5 hash (using the C<md5sum> program).
=item C<sha1>
Compute the SHA1 hash (using the C<sha1sum> program).
=item C<sha224>
Compute the SHA224 hash (using the C<sha224sum> program).
=item C<sha256>
Compute the SHA256 hash (using the C<sha256sum> program).
=item C<sha384>
Compute the SHA384 hash (using the C<sha384sum> program).
=item C<sha512>
Compute the SHA512 hash (using the C<sha512sum> program).
=back
The checksum is returned as a printable string.");
]
let all_functions = non_daemon_functions @ daemon_functions