Skip to content

Commit 6bc57ab

Browse files
Fix empty file checksum
Reference: https://github.com/aboutcode-org/scancode-toolkit/pull/4149/files#r1983173751 Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 0906540 commit 6bc57ab

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

src/commoncode/hash.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ def checksum(location, name, base64=False):
217217
"""
218218
Return a checksum from the content of the file at ``location`` using the ``name`` checksum
219219
algorithm. The checksum is a string as a hexdigest or is base64-encoded is ``base64`` is True.
220+
221+
Return None if ``location`` is not a file or an empty file.
220222
"""
221223
if not filetype.is_file(location):
222224
return
@@ -290,6 +292,9 @@ def multi_checksums(location, checksum_names=("md5", "sha1", "sha256", "sha512",
290292
if not filetype.is_file(location):
291293
return {name: None for name in checksum_names}
292294
file_size = get_file_size(location)
295+
if file_size == 0:
296+
return {name: None for name in checksum_names}
297+
293298
hashers = {
294299
name: get_hasher_instance_by_name(name=name, total_length=file_size)
295300
for name in checksum_names

tests/data/hash/empty

Whitespace-only changes.

tests/test_hash.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,8 @@ def test_checksum_from_chunks_from_stream_is_same_as_plain(self):
194194
for _ in range(100):
195195
result2.update(chunk)
196196
assert result1 == result2.hexdigest()
197+
198+
def test_checksum_empty_file(self):
199+
test_file = self.get_test_loc("hash/empty")
200+
checksums = multi_checksums(location=test_file, checksum_names=("sha1",))
201+
assert checksums == {"sha1": None}

0 commit comments

Comments
 (0)