Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
50 changes: 39 additions & 11 deletions fspec-hash.c → fspec-b3sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

static char *argv0;

int cflag = 0;

static void
usage(void)
{
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -63,20 +70,41 @@ 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);
}

int
main(int argc, char *argv[])
{
argv0 = argc ? argv[0] : "fspec-hash";
argv0 = argc ? argv[0] : "fspec-b3sum";

ARGBEGIN {
case 'c':
cflag = 1;
break;
} ARGEND

if (argc)
++argv, --argc;
if (argc)
Expand Down