Skip to content

Commit

Permalink
Calculate file hash during in-place restore
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertchen committed Jul 21, 2017
1 parent d881ac9 commit 2d1ea86
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions integration_tests/sparse_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ${DUPLICACY} check --files -stats
rm file1 file3

${DUPLICACY} restore -r 1
${DUPLICACY} -v restore -r 1 -overwrite -stats -hash

ls -lsh file3

Expand Down
11 changes: 7 additions & 4 deletions src/duplicacy_backupmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun
if inPlace {
// In inplace mode, we only consider chunks in the existing file with the same offsets, so we
// break the original file at offsets retrieved from the backup
fileHasher := manager.config.NewFileHasher()
buffer := make([]byte, 64 * 1024)
err = nil
for i := entry.StartChunk; i <= entry.EndChunk; i++ {
Expand All @@ -1212,6 +1213,7 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun
n, err := existingFile.Read(buffer[:n])
if n > 0 {
hasher.Write(buffer[:n])
fileHasher.Write(buffer[:n])
count += n
}
if err == io.EOF {
Expand All @@ -1235,6 +1237,7 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun
break
}
}
fileHash = hex.EncodeToString(fileHasher.Sum(nil))
} else {
// If it is not inplace, we want to reuse any chunks in the existing file regardless their offets, so
// we run the chunk maker to split the original file.
Expand All @@ -1253,10 +1256,10 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun
fileHash = hash
return nil, false
})
if fileHash == entry.Hash && fileHash != "" {
LOG_TRACE("DOWNLOAD_SKIP", "File %s unchanged (by hash)", entry.Path)
return false
}
}
if fileHash == entry.Hash && fileHash != "" {
LOG_TRACE("DOWNLOAD_SKIP", "File %s unchanged (by hash)", entry.Path)
return false
}
}

Expand Down

0 comments on commit 2d1ea86

Please sign in to comment.