Skip to content

Commit

Permalink
🪲 Fix hash algorithm to ensure that hash is complete in every cases
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed Oct 23, 2015
1 parent 932e329 commit fdc3725
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/fim/internal/hash/FileHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,8 @@ protected FileHash hashFile(Path file, long fileSize) throws IOException

if (false == frontHasher.hashComplete())
{
throw new RuntimeException("Fim is not working correctly. Some Hasher have not completed: " +
"small=" + frontHasher.getSmallBlockHasher().hashComplete() + ", " +
"medium=" + frontHasher.getMediumBlockHasher().hashComplete() + ", " +
"full=" + frontHasher.getFullHasher().hashComplete());
throw new RuntimeException(String.format("Fim is not working correctly for file '%s' (size=%d). Some Hasher have not completed: small=%s, medium=%s, full=%s",
file, fileSize, frontHasher.getSmallBlockHasher().hashComplete(), frontHasher.getMediumBlockHasher().hashComplete(), frontHasher.getFullHasher().hashComplete()));
}

return frontHasher.getFileHash();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/fim/internal/hash/FrontHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ else if (smallBlockHasher.isActive() && mediumBlockHasher.isActive())
return nextSmallRange;
}

if (nextSmallRange.getTo() < nextMediumRange.getFrom())
if (nextSmallRange.getTo() <= nextMediumRange.getFrom())
{
// Next small block is before the next medium block
return nextSmallRange;
}

if (nextMediumRange.getTo() < nextSmallRange.getFrom())
if (nextMediumRange.getTo() <= nextSmallRange.getFrom())
{
// Next medium block is before the next small block
return nextMediumRange;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/fim/model/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public String toString()
return MoreObjects.toStringHelper(this)
.add("from", from)
.add("to", to)
.add("length", to - from)
.toString();
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/fim/internal/hash/FileHasherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ public void hashA_3MB_File() throws IOException
new Range[]{new Range(_1_MB, _2_MB), new Range(_2_MB, _3_MB)});
}

@Test
public void hashA_4MB_File() throws IOException
{
checkFileHash((4 * _1_MB) + (6 * _1_KB) + 594,
new Range[]{new Range(_4_KB, _8_KB), new Range(_2_MB, _2_MB + _4_KB), new Range(4 * _1_MB, (4 * _1_MB) + _4_KB)},
new Range[]{new Range(_1_MB, _2_MB), new Range(_2_MB, _3_MB), new Range(_3_MB, 4 * _1_MB)});
}

@Test
public void hashA_60MB_File() throws IOException
{
Expand Down

0 comments on commit fdc3725

Please sign in to comment.