By changing to IEnumerable, it is possible to iterate only a portion of the list, which is useful when not all entries are needed (e.g., when testing a connection). All existing backends have been updated, and any which were able to be changed to yield return results in a straightforward way now do. Many backends had a try/catch in the List() method. Due to the fact that yield returns can't be placed within a try/catch block, these have been refactored to either scope the try/catch to the parts that (should) be the only places throwing exceptions, so that exceptions are still caught and handled. Note that lazy evaluation may cause some changes in behavior - exceptions that were previously thrown at the point of invokation of List() may now be thrown while it is being enumerated. I believe this will not be problematic though, as the only well-known exception seems to be FolderMissingException, which should be thrown by Test(), but TestList() attempts to enumerate the list to force this exception. Any places that require the legacy behavior can get it by simply converting the lazy enumerable to a List()
125 lines
3.8 KiB
C#
125 lines
3.8 KiB
C#
// Copyright (C) 2015, The Duplicati Team
|
|
// http://www.duplicati.com, info@duplicati.com
|
|
//
|
|
// This library is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful, but
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
using System;
|
|
using System.Linq;
|
|
using Duplicati.Library.Interface;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace Duplicati.UnitTest
|
|
{
|
|
public class SizeOmittingBackend : IBackend, IStreamingBackend
|
|
{
|
|
static SizeOmittingBackend() { WrappedBackend = "file"; }
|
|
|
|
public static string WrappedBackend { get; set; }
|
|
|
|
private IStreamingBackend m_backend;
|
|
public SizeOmittingBackend()
|
|
{
|
|
}
|
|
|
|
public SizeOmittingBackend(string url, Dictionary<string, string> options)
|
|
{
|
|
var u = new Library.Utility.Uri(url).SetScheme(WrappedBackend).ToString();
|
|
m_backend = (IStreamingBackend)Library.DynamicLoader.BackendLoader.GetBackend(u, options);
|
|
}
|
|
|
|
#region IStreamingBackend implementation
|
|
public void Put(string remotename, Stream stream)
|
|
{
|
|
m_backend.Put(remotename, stream);
|
|
}
|
|
public void Get(string remotename, Stream stream)
|
|
{
|
|
m_backend.Get(remotename, stream);
|
|
}
|
|
#endregion
|
|
|
|
#region IBackend implementation
|
|
public IEnumerable<IFileEntry> List()
|
|
{
|
|
return
|
|
from n in m_backend.List()
|
|
where !n.IsFolder
|
|
select new Library.Interface.FileEntry(n.Name);
|
|
}
|
|
public void Put(string remotename, string filename)
|
|
{
|
|
m_backend.Put(remotename, filename);
|
|
}
|
|
public void Get(string remotename, string filename)
|
|
{
|
|
m_backend.Get(remotename, filename);
|
|
}
|
|
public void Delete(string remotename)
|
|
{
|
|
m_backend.Delete(remotename);
|
|
}
|
|
public void Test()
|
|
{
|
|
m_backend.Test();
|
|
}
|
|
public void CreateFolder()
|
|
{
|
|
m_backend.CreateFolder();
|
|
}
|
|
public string DisplayName
|
|
{
|
|
get
|
|
{
|
|
return "Size Omitting Backend";
|
|
}
|
|
}
|
|
public string ProtocolKey
|
|
{
|
|
get
|
|
{
|
|
return "omitsize";
|
|
}
|
|
}
|
|
public IList<ICommandLineArgument> SupportedCommands
|
|
{
|
|
get
|
|
{
|
|
if (m_backend == null)
|
|
try { return Duplicati.Library.DynamicLoader.BackendLoader.GetSupportedCommands(WrappedBackend + "://"); }
|
|
catch { }
|
|
|
|
return m_backend.SupportedCommands;
|
|
}
|
|
}
|
|
public string Description
|
|
{
|
|
get
|
|
{
|
|
return "A testing backend that does not return size information";
|
|
}
|
|
}
|
|
#endregion
|
|
#region IDisposable implementation
|
|
public void Dispose()
|
|
{
|
|
if (m_backend != null)
|
|
try { m_backend.Dispose(); }
|
|
finally { m_backend = null; }
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|