aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorPaul Spooren <mail@aparcar.org>2020-07-16 22:19:32 -1000
committerDaniel Golle <daniel@makrotopia.org>2020-11-09 10:54:29 +0000
commitfcd3e13779ca644bfbb0940f39fdf13203bf8211 (patch)
tree0fdacc9787a29be5003de5797b741c229076faf5 /scripts
parentfac98f1c54a16073e28ab11adf195f9917a30ef6 (diff)
downloadupstream-fcd3e13779ca644bfbb0940f39fdf13203bf8211.tar.gz
upstream-fcd3e13779ca644bfbb0940f39fdf13203bf8211.tar.bz2
upstream-fcd3e13779ca644bfbb0940f39fdf13203bf8211.zip
scripts: mkhash fail on hashing a folder
mkhash currently returns the hash of an empty input when trying to hash a folder. This can be missleading in caseswhere e.g. an env variable is undefined which should contain a filename. `mkhash ./path/to/$FILE` would exit with code 0 and return a legit looking checksum. A better behaviour would be to fail with exit code 1, which imitates the behaviour of `md5sum` and `sha256sum`. To avoid hashing of folders the `stat()` is checked. Hashing empty inputs result in the following checksums: md5: d41d8cd98f00b204e9800998ecf8427e sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 Signed-off-by: Paul Spooren <mail@aparcar.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/mkhash.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/scripts/mkhash.c b/scripts/mkhash.c
index ce156e979d..ced4f3c34a 100644
--- a/scripts/mkhash.c
+++ b/scripts/mkhash.c
@@ -85,6 +85,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
+#include <sys/stat.h>
#define ARRAY_SIZE(_n) (sizeof(_n) / sizeof((_n)[0]))
@@ -770,6 +771,13 @@ static int hash_file(struct hash_type *t, const char *filename, bool add_filenam
if (!filename || !strcmp(filename, "-")) {
str = t->func(stdin);
} else {
+ struct stat path_stat;
+ stat(filename, &path_stat);
+ if (S_ISDIR(path_stat.st_mode)) {
+ fprintf(stderr, "Failed to open '%s': Is a directory\n", filename);
+ return 1;
+ }
+
FILE *f = fopen(filename, "r");
if (!f) {