1 #define _POSIX_C_SOURCE 200809L
18 /* flags 0, close mmap, flags 1, return mmap fd */
19 int zpm_hash(char *path, char *hash, uint32_t flags) {
23 struct sha256_state md;
25 unsigned char tmp[32];
28 fd = open(path, O_RDONLY);
30 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
33 if (fstat(fd, &sbuf) == -1) {
34 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
37 /* not a regular file? */
38 if (!S_ISREG(sbuf.st_mode)) {
39 /* TODO this is ok, just stored differently */
40 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
44 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
47 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
53 sha256_process(&md, content, sbuf.st_size);
54 sha256_done(&md, tmp);
56 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
59 munmap(content, sbuf.st_size);
60 return flags ? fd : 1;