Add "rhizome hash file" command

Also fixed a file descriptor leak in rhizome_hash_file() -- missing fclose()
This commit is contained in:
Andrew Bettison 2012-05-21 13:16:44 +09:30
parent 05a430bc3a
commit f4293e8ab3
2 changed files with 25 additions and 3 deletions

View File

@ -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","[<variable>]",NULL},CLIFLAG_STANDALONE,
"Get specified configuration variable."},
{app_rhizome_hash_file,{"rhizome","hash","file","<filepath>",NULL},CLIFLAG_STANDALONE,
"Compute the Rhizome hash of a file"},
{app_rhizome_add_file,{"rhizome","add","file","<author_sid>","<pin>","<filepath>","[<manifestpath>]",NULL},CLIFLAG_STANDALONE,
"Add a file to Rhizome and optionally write its manifest to the given path"},
{app_rhizome_list,{"rhizome","list","[<service>]","[<sender_sid>]","[<recipient_sid>]","[<offset>]","[<limit>]",NULL},CLIFLAG_STANDALONE,

View File

@ -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;
}