diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 1ed8c0e2..5120b2a9 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -360,6 +360,7 @@ int rhizome_hash_file(rhizome_manifest *m, const char *filename, char *hash_out) if (m && m->payloadEncryption) return WHY("Encryption of payloads not implemented"); + size_t filesize = 0; SHA512_CTX context; SHA512_Init(&context); if (filename[0]) { @@ -378,11 +379,16 @@ int rhizome_hash_file(rhizome_manifest *m, const char *filename, char *hash_out) } if (r > 0) SHA512_Update(&context, buffer, r); + filesize += r; } fclose(f); } SHA512_End(&context, (char *)hash_out); - str_toupper_inplace(hash_out); + // Empty files (including null filename) have no hash. + if (filesize > 0) + str_toupper_inplace(hash_out); + else + hash_out[0] = '\0'; return 0; } diff --git a/testdefs_rhizome.sh b/testdefs_rhizome.sh index 958b06fd..3ac85ec0 100644 --- a/testdefs_rhizome.sh +++ b/testdefs_rhizome.sh @@ -274,15 +274,19 @@ extract_manifest_crypt() { } compute_filehash() { - local _var="$1" + local _filehashvar="$1" local _file="$2" - local _filesize="$3" - local _hash=$($servald rhizome hash file "$_file") || error "$servald failed to compute file hash" - [ -z "${_hash//[0-9a-fA-F]/}" ] || error "file hash contains non-hex: $_hash" - [ "${#_hash}" -eq 128 ] || error "file hash incorrect length: $_hash" - local _size=$(( $(cat "$filename" | wc -c) + 0 )) - [ -n "$_var" ] && eval $_var="\$_hash" - [ -n "$_filesize" ] && eval $_filesize="\$_size" + local _filesizevar="$3" + local _hash= + local _size=0 + if [ -s "$_file" ]; then + local _hash=$($servald rhizome hash file "$_file") || error "$servald failed to compute file hash" + [ -z "${_hash//[0-9a-fA-F]/}" ] || error "file hash contains non-hex: $_hash" + [ "${#_hash}" -eq 128 ] || error "file hash incorrect length: $_hash" + local _size=$(( $(cat "$filename" | wc -c) + 0 )) + fi + [ -n "$_filehashvar" ] && eval $_filehashvar="\$_hash" + [ -n "$_filesizevar" ] && eval $_filesizevar="\$_size" } rhizome_http_server_started() {