Fix "rhizome hash file" command for nil files

Now returns an empty result for zero-length file or zero-length file name

Test framework only invokes on non-zero-length files
This commit is contained in:
Andrew Bettison 2013-09-30 16:11:35 +09:30
parent ae5ae2d394
commit 394870502d
2 changed files with 19 additions and 9 deletions

View File

@ -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;
}

View File

@ -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() {