From f4293e8ab30ce299f6331bab866a474f6c09f6b4 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Mon, 21 May 2012 13:16:44 +0930 Subject: [PATCH] Add "rhizome hash file" command Also fixed a file descriptor leak in rhizome_hash_file() -- missing fclose() --- commandline.c | 16 +++++++++++++++- rhizome_bundle.c | 12 ++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/commandline.c b/commandline.c index eb0a860f..0af92b16 100644 --- a/commandline.c +++ b/commandline.c @@ -294,7 +294,7 @@ int cli_arg(int argc, const char *const *argv, command_line_option *o, char *arg ) { const char *value = argv[i]; if (validator && !(*validator)(value)) - return setReason("Invalid argument %d '%s': \"%s\"", i + 1, argname, value); + return WHYF("Invalid argument %d '%s': \"%s\"", i + 1, argname, value); *dst = value; return 0; } @@ -1073,6 +1073,18 @@ int cli_optional_sid(const char *arg) return !arg[0] || validateSid(arg); } +int app_rhizome_hash_file(int argc, const char *const *argv, struct command_line_option *o) +{ + const char *filepath; + cli_arg(argc, argv, o, "filepath", &filepath, NULL, ""); + char hexhash[SHA512_DIGEST_STRING_LENGTH]; + if (rhizome_hash_file(filepath, hexhash)) + return -1; + cli_puts(hexhash); + cli_delim("\n"); + return 0; +} + int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_option *o) { const char *filepath, *manifestpath, *authorSid, *pin; @@ -1571,6 +1583,8 @@ command_line_option command_line_options[]={ "Set specified configuration variable."}, {app_config_get,{"config","get","[]",NULL},CLIFLAG_STANDALONE, "Get specified configuration variable."}, + {app_rhizome_hash_file,{"rhizome","hash","file","",NULL},CLIFLAG_STANDALONE, + "Compute the Rhizome hash of a file"}, {app_rhizome_add_file,{"rhizome","add","file","","","","[]",NULL},CLIFLAG_STANDALONE, "Add a file to Rhizome and optionally write its manifest to the given path"}, {app_rhizome_list,{"rhizome","list","[]","[]","[]","[]","[]",NULL},CLIFLAG_STANDALONE, diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 50917a9a..cc7b3936 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -174,17 +174,25 @@ int rhizome_hash_file(const char *filename,char *hash_out) implementation. */ FILE *f = fopen(filename, "r"); - if (!f) - return WHYF("Could not read %s to calculate SHA512 hash.", filename); + if (!f) { + WHY_perror("fopen"); + return WHYF("Could not open %s to calculate SHA512 hash.", filename); + } SHA512_CTX context; SHA512_Init(&context); while (!feof(f)) { unsigned char buffer[8192]; int r = fread(buffer, 1, 8192, f); + if (r == -1) { + WHY_perror("fread"); + fclose(f); + return WHYF("Error reading %s to calculate SHA512 hash", filename); + } if (r > 0) SHA512_Update(&context, buffer, r); } SHA512_End(&context, (char *)hash_out); + fclose(f); return 0; }