From cdaf814cd8e87dd55ac76240e87627337fd40d90 Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Tue, 5 Apr 2022 14:01:20 +1200 Subject: [PATCH 1/2] Rename fspec-hash -> fspec-b3sum. --- Makefile | 10 +++++----- fspec-hash.c => fspec-b3sum.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) rename fspec-hash.c => fspec-b3sum.c (97%) diff --git a/Makefile b/Makefile index 335114a..6828d36 100644 --- a/Makefile +++ b/Makefile @@ -9,15 +9,15 @@ CFLAGS+=-Wall -Wpedantic COMMON_OBJ=fatal.o parse.o reallocarray.o .PHONY: all -all: fspec-hash fspec-sort fspec-sync fspec-tar +all: fspec-b3sum fspec-sort fspec-sync fspec-tar -$(COMMON_OBJ) fspec-hash.o fspec-sort.o fspec-tar.o: common.h +$(COMMON_OBJ) fspec-b3sum.o fspec-sort.o fspec-tar.o: common.h libcommon.a: $(COMMON_OBJ) $(AR) $(ARFLAGS) $@ $(COMMON_OBJ) -fspec-hash: fspec-hash.o libcommon.a - $(CC) $(LDFLAGS) -o $@ fspec-hash.o libcommon.a $(BLAKE3_LDLIBS) +fspec-b3sum: fspec-b3sum.o libcommon.a + $(CC) $(LDFLAGS) -o $@ fspec-b3sum.o libcommon.a $(BLAKE3_LDLIBS) fspec-sort: fspec-sort.o libcommon.a $(CC) $(LDFLAGS) -o $@ fspec-sort.o libcommon.a @@ -31,7 +31,7 @@ fspec-tar: fspec-tar.o libcommon.a .PHONY: clean clean: rm -f\ - fspec-hash fspec-hash.o\ + fspec-b3sum fspec-b3sum.o\ fspec-sort fspec-sort.o\ fspec-tar fspec-tar.o\ libcommon.a $(COMMON_OBJ) diff --git a/fspec-hash.c b/fspec-b3sum.c similarity index 97% rename from fspec-hash.c rename to fspec-b3sum.c index f982f02..0210208 100644 --- a/fspec-hash.c +++ b/fspec-b3sum.c @@ -76,7 +76,7 @@ fspec(char *pos, size_t len) int main(int argc, char *argv[]) { - argv0 = argc ? argv[0] : "fspec-hash"; + argv0 = argc ? argv[0] : "fspec-b3sum"; if (argc) ++argv, --argc; if (argc) From f1b5adeea74166fd2fd851150bfa196a975372eb Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Tue, 5 Apr 2022 14:02:23 +1200 Subject: [PATCH 2/2] Implement fspec-b3sum -c (check). --- fspec-b3sum.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/fspec-b3sum.c b/fspec-b3sum.c index 0210208..f129522 100644 --- a/fspec-b3sum.c +++ b/fspec-b3sum.c @@ -8,6 +8,8 @@ static char *argv0; +int cflag = 0; + static void usage(void) { @@ -18,14 +20,17 @@ usage(void) static void fspec(char *pos, size_t len) { - char *source, *end; - int reg = 0, hash = 1; + char *path, *source, *end, *hashline; + int reg = 0; end = memchr(pos, '\n', len); assert(end); if (fwrite(pos, 1, end - pos + 1, stdout) != end - pos + 1) fatal("write:"); *end = 0; + + hashline = 0; + path = pos; source = pos + 1; len -= end + 1 - pos; pos = end + 1; @@ -41,17 +46,19 @@ fspec(char *pos, size_t len) } else if (len >= 7 && memcmp(pos, "source=", 7) == 0) { source = pos + 7; } else if (len >= 7 && memcmp(pos, "blake3=", 7) == 0) { - hash = 0; + hashline = pos + 7; } len -= end + 1 - pos; pos = end + 1; } - if (reg && hash) { + + if (reg && (!hashline || cflag)) { FILE *file; blake3_hasher ctx; char buf[16384]; + char hexsum[BLAKE3_OUT_LEN*2+1]; + unsigned char sum[BLAKE3_OUT_LEN]; size_t len; - unsigned char out[BLAKE3_OUT_LEN]; file = fopen(source, "rb"); if (!file) @@ -63,12 +70,26 @@ fspec(char *pos, size_t len) } while (len == sizeof(buf)); if (ferror(file)) fatal("read %s:", source); - blake3_hasher_finalize(&ctx, out, sizeof(out)); fclose(file); - fputs("blake3=", stdout); - for (size_t i = 0; i < sizeof(out); ++i) - printf("%02x", out[i]); - fputc('\n', stdout); + + blake3_hasher_finalize(&ctx, sum, sizeof(sum)); + for (size_t i = 0; i < sizeof(sum); ++i) { + static char *h = "0123456789abcdef"; + hexsum[i*2] = h[(sum[i]&0xf0)>>4]; + hexsum[i*2+1] = h[sum[i]&0x0f]; + } + hexsum[sizeof(hexsum) - 1] = 0; + if (hashline && cflag && strcmp(hashline, hexsum)) { + fprintf(stderr, "b3sum check failed:\n"); + fprintf(stderr, " path=%s\n", path); + fprintf(stderr, " source=%s\n", source); + fprintf(stderr, " expected=%s\n", hashline); + fprintf(stderr, " got=%s\n", hexsum); + exit(1); + } + + if (!hashline) + fprintf(stdout, "blake3=%s\n", hexsum); } fputc('\n', stdout); } @@ -77,6 +98,13 @@ int main(int argc, char *argv[]) { argv0 = argc ? argv[0] : "fspec-b3sum"; + + ARGBEGIN { + case 'c': + cflag = 1; + break; + } ARGEND + if (argc) ++argv, --argc; if (argc)