From b7b54478fcfb9dae463fa16cfab032a686ce6d54 Mon Sep 17 00:00:00 2001 From: Gilbert Chen Date: Thu, 29 Jun 2017 22:19:41 -0400 Subject: [PATCH] Don't compute file hashes when DUPLICACY_SKIP_FILE_HASH is set; handle vertical backup-style hashes in restore --- src/duplicacy_backupmanager.go | 4 ++-- src/duplicacy_config.go | 35 +++++++++++++++++++++++++++++++- src/duplicacy_snapshotmanager.go | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/duplicacy_backupmanager.go b/src/duplicacy_backupmanager.go index bfb7eea..dbe8b46 100644 --- a/src/duplicacy_backupmanager.go +++ b/src/duplicacy_backupmanager.go @@ -1233,7 +1233,7 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun // Verify the download by hash hash := hex.EncodeToString(hasher.Sum(nil)) - if hash != entry.Hash { + if hash != entry.Hash && hash != "" && entry.Hash != "" && !strings.HasPrefix(entry.Hash, "#") { LOG_ERROR("DOWNLOAD_HASH", "File %s has a mismatched hash: %s instead of %s (in-place)", fullPath, "", entry.Hash) return false @@ -1306,7 +1306,7 @@ func (manager *BackupManager) RestoreFile(chunkDownloader *ChunkDownloader, chun } hash := hex.EncodeToString(hasher.Sum(nil)) - if hash != entry.Hash { + if hash != entry.Hash && hash != "" && entry.Hash != "" && !strings.HasPrefix(entry.Hash, "#") { LOG_ERROR("DOWNLOAD_HASH", "File %s has a mismatched hash: %s instead of %s", entry.Path, hash, entry.Hash) return false diff --git a/src/duplicacy_config.go b/src/duplicacy_config.go index e4401b8..6c90d85 100644 --- a/src/duplicacy_config.go +++ b/src/duplicacy_config.go @@ -225,8 +225,41 @@ func (config *Config) NewKeyedHasher(key []byte) hash.Hash { } } +var SkipFileHash = false + +func init() { + if value, found := os.LookupEnv("DUPLICACY_SKIP_FILE_HASH"); found && value != "" && value != "0" { + SkipFileHash = true + } +} + +// Implement a dummy hasher to be used when SkipFileHash is true. +type DummyHasher struct { +} + +func (hasher *DummyHasher) Write(p []byte) (int, error) { + return len(p), nil +} + +func (hasher *DummyHasher) Sum(b []byte) []byte { + return []byte("") +} + +func (hasher *DummyHasher) Reset() { +} + +func (hasher *DummyHasher) Size() int { + return 0 +} + +func (hasher *DummyHasher) BlockSize() int { + return 0 +} + func (config *Config) NewFileHasher() hash.Hash { - if config.CompressionLevel == DEFAULT_COMPRESSION_LEVEL { + if SkipFileHash { + return &DummyHasher {} + } else if config.CompressionLevel == DEFAULT_COMPRESSION_LEVEL { hasher, _ := blake2.New(&blake2.Config{ Size: 32 }) return hasher } else { diff --git a/src/duplicacy_snapshotmanager.go b/src/duplicacy_snapshotmanager.go index 220d497..b03fe1c 100644 --- a/src/duplicacy_snapshotmanager.go +++ b/src/duplicacy_snapshotmanager.go @@ -1084,7 +1084,7 @@ func (manager *SnapshotManager) RetrieveFile(snapshot *Snapshot, file *Entry, ou if alternateHash { fileHash = "#" + fileHash } - if strings.ToLower(fileHash) != strings.ToLower(file.Hash) { + if strings.ToLower(fileHash) != strings.ToLower(file.Hash) && !SkipFileHash { LOG_WARN("SNAPSHOT_HASH", "File %s has mismatched hashes: %s vs %s", file.Path, file.Hash, fileHash) return false }