From 2d1ea86d8e5ab6bc68da00880c05fa58610750d1 Mon Sep 17 00:00:00 2001 From: Gilbert Chen Date: Fri, 21 Jul 2017 15:19:11 -0400 Subject: [PATCH] Calculate file hash during in-place restore --- integration_tests/sparse_test.sh | 1 + src/duplicacy_backupmanager.go | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/integration_tests/sparse_test.sh b/integration_tests/sparse_test.sh index b5475da..fd3a333 100755 --- a/integration_tests/sparse_test.sh +++ b/integration_tests/sparse_test.sh @@ -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 diff --git a/src/duplicacy_backupmanager.go b/src/duplicacy_backupmanager.go index 88c7fae..ff8a095 100644 --- a/src/duplicacy_backupmanager.go +++ b/src/duplicacy_backupmanager.go @@ -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++ { @@ -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 { @@ -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. @@ -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 } }