From 0d8ea21760a2a16eccea293fcc36be40c618e36b Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Mon, 29 Apr 2013 23:56:01 +0200 Subject: [PATCH] Improved local restore by not opening source or target files, if the source does not exist --- .../Database/LocalRestoreDatabase.cs | 5 ++ .../ForestHash/Operation/RestoreHandler.cs | 79 ++++++++++--------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/Duplicati/Library/Main/ForestHash/Database/LocalRestoreDatabase.cs b/Duplicati/Library/Main/ForestHash/Database/LocalRestoreDatabase.cs index 98082b05b..911a779ca 100644 --- a/Duplicati/Library/Main/ForestHash/Database/LocalRestoreDatabase.cs +++ b/Duplicati/Library/Main/ForestHash/Database/LocalRestoreDatabase.cs @@ -472,8 +472,13 @@ namespace Duplicati.Library.Main.ForestHash.Database do { var n = new FastSource(rd, m_blocksize); + var tid = n.TargetFileID; yield return n; + more = n.MoreData; + while(more && n.TargetFileID == tid) + more = rd.Read(); + } while (more); } } diff --git a/Duplicati/Library/Main/ForestHash/Operation/RestoreHandler.cs b/Duplicati/Library/Main/ForestHash/Operation/RestoreHandler.cs index f0f9aa5fc..f2bbc1b31 100644 --- a/Duplicati/Library/Main/ForestHash/Operation/RestoreHandler.cs +++ b/Duplicati/Library/Main/ForestHash/Operation/RestoreHandler.cs @@ -289,46 +289,49 @@ namespace Duplicati.Library.Main.ForestHash.Operation try { - using(var targetstream = System.IO.File.Open(targetpath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read)) - { - try - { - using(var sourcestream = System.IO.File.Open(sourcepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)) - { - foreach(var block in entry.Blocks) - { - if (sourcestream.Length > block.Offset) - { - sourcestream.Position = block.Offset; - - var size = sourcestream.Read(blockbuffer, 0, blockbuffer.Length); - if (size == block.Size) - { - var key = Convert.ToBase64String(hasher.ComputeHash(blockbuffer, 0, size)); - if (key == block.Hash) - { - if (options.FhDryrun) - patched = true; - else - { - targetstream.Position = block.Offset; - targetstream.Write(blockbuffer, 0, size); - } - - blockmarker.SetBlockRestored(targetfileid, block.Index, key, block.Size); - } - } + if (System.IO.File.Exists(sourcepath)) + { + using(var targetstream = System.IO.File.Open(targetpath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read)) + { + try + { + using(var sourcestream = System.IO.File.Open(sourcepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)) + { + foreach(var block in entry.Blocks) + { + if (sourcestream.Length > block.Offset) + { + sourcestream.Position = block.Offset; + + var size = sourcestream.Read(blockbuffer, 0, blockbuffer.Length); + if (size == block.Size) + { + var key = Convert.ToBase64String(hasher.ComputeHash(blockbuffer, 0, size)); + if (key == block.Hash) + { + if (options.FhDryrun) + patched = true; + else + { + targetstream.Position = block.Offset; + targetstream.Write(blockbuffer, 0, size); + } + + blockmarker.SetBlockRestored(targetfileid, block.Index, key, block.Size); + } + } + } } } - } - } - catch (Exception ex) - { - stat.LogWarning(string.Format("Failed to patch file: \"{0}\" with data from local file \"{1}\", message: {2}", targetpath, sourcepath, ex.Message), ex); - database.LogMessage("Warning", string.Format("Failed to patch file: \"{0}\" with data from local file \"{1}\", message: {2}", targetpath, sourcepath, ex.Message), ex, null); - } - } - } + } + catch (Exception ex) + { + stat.LogWarning(string.Format("Failed to patch file: \"{0}\" with data from local file \"{1}\", message: {2}", targetpath, sourcepath, ex.Message), ex); + database.LogMessage("Warning", string.Format("Failed to patch file: \"{0}\" with data from local file \"{1}\", message: {2}", targetpath, sourcepath, ex.Message), ex, null); + } + } + } + } catch (Exception ex) { stat.LogWarning(string.Format("Failed to patch file: \"{0}\" with local data, message: {1}", targetpath, ex.Message), ex);