From fc15567be2b3c183232e7a7513552cf28b2c418a Mon Sep 17 00:00:00 2001 From: Patrick McCarty Date: Wed, 7 Sep 2016 12:10:50 -0700 Subject: [PATCH] Allow relative paths for hashdump subcommand To make the 'hashdump' semantics easier to understand, permit relative paths on the command line, with or without the -p option. Also, simplify the output to print only a summary of the action, and the resulting hash. Signed-off-by: Patrick McCarty --- src/hashdump.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/hashdump.c b/src/hashdump.c index 8e5922e0..4c054145 100644 --- a/src/hashdump.c +++ b/src/hashdump.c @@ -32,6 +32,8 @@ /* outputs the hash of a file */ +static bool use_prefix = false; + static struct option opts[] = { { "no-xattrs", 0, NULL, 'n' }, { "basepath", 1, NULL, 'b' }, @@ -49,14 +51,14 @@ static void usage(const char *name) printf(" -n, --no-xattrs Ignore extended attributes\n"); printf(" -b, --basepath Optional argument for leading path to filename\n"); printf("\n"); - printf("The filename is the name as it would appear in a Manifest file.\n"); + printf("The filename is the name of a file on the filesystem.\n"); printf("\n"); } int hashdump_main(int argc, char **argv) { struct file *file; - char *fullname; + char *fullname = NULL; int ret; file = calloc(1, sizeof(struct file)); @@ -85,6 +87,7 @@ int hashdump_main(int argc, char **argv) free(file); return EXIT_FAILURE; } + use_prefix = true; break; case 'h': usage(argv[0]); @@ -102,14 +105,9 @@ int hashdump_main(int argc, char **argv) exit(-1); } - // mk_full_filename expects absolute filenames (eg: from Manifest) - if (argv[optind][0] == '/') { - file->filename = strdup(argv[optind]); - if (!file->filename) { - abort(); - } - } else { - string_or_die(&file->filename, "/%s", argv[optind]); + file->filename = strdup(argv[optind]); + if (!file->filename) { + abort(); } ret = set_path_prefix(NULL); @@ -119,10 +117,19 @@ int hashdump_main(int argc, char **argv) return EXIT_FAILURE; } - printf("Calculating hash %s xattrs for: (%s) ... %s\n", - (file->use_xattrs ? "with" : "without"), path_prefix, file->filename); - fullname = mk_full_filename(path_prefix, file->filename); - printf("fullname=%s\n", fullname); + // Accept relative paths if no path_prefix set on command line + if (use_prefix) { + fullname = mk_full_filename(path_prefix, file->filename); + } else { + fullname = strdup(file->filename); + if (!fullname) { + abort(); + } + } + + printf("Calculating hash %s xattrs for: %s\n", + (file->use_xattrs ? "with" : "without"), fullname); + populate_file_struct(file, fullname); ret = compute_hash(file, fullname); if (ret != 0) {