From 82ef3ed2301a8934ee347b66cd0ef8a90a5ff556 Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Thu, 9 Mar 2017 09:21:03 +0100 Subject: [PATCH] Tested the fail-over, and changed it so it works regardless of how the list method is called. --- .../Library/Compression/FileArchiveZip.cs | 98 +++++++------------ 1 file changed, 37 insertions(+), 61 deletions(-) diff --git a/Duplicati/Library/Compression/FileArchiveZip.cs b/Duplicati/Library/Compression/FileArchiveZip.cs index a141fe22c..b70fb080b 100644 --- a/Duplicati/Library/Compression/FileArchiveZip.cs +++ b/Duplicati/Library/Compression/FileArchiveZip.cs @@ -28,6 +28,7 @@ using SharpCompress.Archives.Zip; using SharpCompress.Writers; using SharpCompress.Writers.Zip; using SharpCompress.Readers; +using System.Linq; namespace Duplicati.Library.Compression { @@ -183,23 +184,6 @@ namespace Duplicati.Library.Compression } - public IEnumerable ListItems() - { - if (!m_using_reader) - { - foreach (var e in Archive.Entries) - yield return e; - } - else - { - using (var fs = new System.IO.FileStream(m_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) - using (var rd = SharpCompress.Readers.Zip.ZipReader.Open(fs, new ReaderOptions() { LookForHeader = false })) - while (rd.MoveToNextEntry()) - yield return (IEntry)rd.Entry; - } - - } - /// /// Constructs a new zip instance. /// If the file exists and has a non-zero length we read it, @@ -284,24 +268,7 @@ namespace Duplicati.Library.Compression /// A list of files matching the prefix public string[] ListFiles(string prefix) { - List results = new List(); - foreach (var e in ListItems()) - { - if (prefix == null) - { - results.Add(e.Key); - } - else - { - if (e.Key.StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision)) - results.Add(e.Key); - //Some old archives may have been created with windows style paths - else if (e.Key.Replace('\\', '/').StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision)) - results.Add(e.Key); - } - } - - return results.ToArray(); + return ListFilesWithSize(prefix).Select(x => x.Key).ToArray(); } /// @@ -311,25 +278,18 @@ namespace Duplicati.Library.Compression /// A list of files matching the prefix public IEnumerable> ListFilesWithSize(string prefix) { - List> results = new List>(); - foreach (var e in ListItems()) - { - if (prefix == null) - { - results.Add(new KeyValuePair(e.Key, e.Size)); - } - else - { - if (e.Key.StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision)) - results.Add(new KeyValuePair(e.Key, e.Size)); - //Some old archives may have been created with windows style paths - else if (e.Key.Replace('\\', '/').StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision)) - results.Add(new KeyValuePair(e.Key, e.Size)); - } - } + LoadEntryTable(); + var q = m_entryDict.Values.AsEnumerable(); + if (!string.IsNullOrEmpty(prefix)) + q = q.Where(x => + x.Key.StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision) + || + x.Key.Replace('\\', '/').StartsWith(prefix, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision) + ); - return results; + return q.Select(x => new KeyValuePair(x.Key, x.Size)).ToArray(); } + /// /// Opens an file for reading /// @@ -354,21 +314,16 @@ namespace Duplicati.Library.Compression } /// - /// Internal function that returns a ZipEntry for a filename, or null if no such file exists + /// Helper method to load the entry table /// - /// The name of the file to find - /// The ZipEntry for the file or null if no such file was found - private IEntry GetEntry(string file) + private void LoadEntryTable() { - if (m_isWriting) - throw new InvalidOperationException("Cannot read while writing"); - if (m_entryDict == null) { try { var d = new Dictionary(Duplicati.Library.Utility.Utility.ClientFilenameStringComparer); - foreach (var en in ListItems()) + foreach (var en in Archive.Entries) d[en.Key] = en; m_entryDict = d; } @@ -380,9 +335,30 @@ namespace Duplicati.Library.Compression throw; SwitchToReader(); - return GetEntry(file); + + var d = new Dictionary(Duplicati.Library.Utility.Utility.ClientFilenameStringComparer); + + using (var fs = new System.IO.FileStream(m_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (var rd = SharpCompress.Readers.Zip.ZipReader.Open(fs, new ReaderOptions() { LookForHeader = false })) + while (rd.MoveToNextEntry()) + d[rd.Entry.Key] = rd.Entry; + + m_entryDict = d; } } + } + + /// + /// Internal function that returns a ZipEntry for a filename, or null if no such file exists + /// + /// The name of the file to find + /// The ZipEntry for the file or null if no such file was found + private IEntry GetEntry(string file) + { + if (m_isWriting) + throw new InvalidOperationException("Cannot read while writing"); + + LoadEntryTable(); IEntry e; if (m_entryDict.TryGetValue(file, out e))