Skip to content

Commit

Permalink
Change IBackend and IStreamingBackend to return Task for Put()
Browse files Browse the repository at this point in the history
  • Loading branch information
seantempleton committed Mar 4, 2019
1 parent 3de04da commit 0c122ff
Show file tree
Hide file tree
Showing 33 changed files with 328 additions and 242 deletions.
5 changes: 3 additions & 2 deletions Duplicati/CommandLine/BackendTester/Program.cs
Expand Up @@ -23,6 +23,7 @@
using Duplicati.Library.Interface;
using System.Linq;
using System.Globalization;
using System.Threading;

namespace Duplicati.CommandLine.BackendTester
{
Expand Down Expand Up @@ -470,10 +471,10 @@ private static void Uploadfile(string localfilename, int i, string remotefilenam
{
using (System.IO.FileStream fs = new System.IO.FileStream(localfilename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
using (NonSeekableStream nss = new NonSeekableStream(fs))
(backend as Library.Interface.IStreamingBackend).Put(remotefilename, nss);
(backend as Library.Interface.IStreamingBackend).Put(remotefilename, nss, CancellationToken.None).Wait();
}
else
backend.Put(remotefilename, localfilename);
backend.Put(remotefilename, localfilename, CancellationToken.None).Wait();

e = null;
}
Expand Down
5 changes: 3 additions & 2 deletions Duplicati/CommandLine/BackendTool/Program.cs
Expand Up @@ -17,10 +17,11 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using Duplicati.Library.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using Duplicati.Library.Interface;
using System.Threading;

namespace Duplicati.CommandLine.BackendTool
{
Expand Down Expand Up @@ -144,7 +145,7 @@ public static int RealMain(string[] _args)
if (args.Count > 3)
throw new UserInformationException(string.Format("too many arguments: {0}", string.Join(",", args)), "BackendToolTooManyArguments");

backend.Put(Path.GetFileName(args[2]), args[2]);
backend.Put(Path.GetFileName(args[2]), args[2], CancellationToken.None).Wait();

return 0;
}
Expand Down
15 changes: 8 additions & 7 deletions Duplicati/CommandLine/RecoveryTool/Recompress.cs
@@ -1,13 +1,14 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Duplicati.Library.Interface;
using Duplicati.Library.Main;
using Duplicati.Library.Main.Volumes;
using Duplicati.Library.Utility;
using Newtonsoft.Json.Linq;
using Duplicati.Library.Interface;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

namespace Duplicati.CommandLine.RecoveryTool
{
public static class Recompress
Expand Down Expand Up @@ -253,7 +254,7 @@ public static int Run(List<string> args, Dictionary<string, string> options, Lib
if (reupload)
{
Console.Write(" reuploading ...");
backend.Put((new FileInfo(localFileTarget)).Name, localFileTarget);
backend.Put((new FileInfo(localFileTarget)).Name, localFileTarget, CancellationToken.None).Wait();
backend.Delete(remoteFile.File.Name);
File.Delete(localFileTarget);
}
Expand Down
22 changes: 13 additions & 9 deletions Duplicati/Library/Backend/AlternativeFTP/AlternativeFTPBackend.cs
Expand Up @@ -18,17 +18,19 @@
//
#endregion

using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.FtpClient;
using System.Net.Security;
using System.Security.Authentication;
using Duplicati.Library.Interface;
using Uri = System.Uri;
using System.Threading;
using System.Threading.Tasks;
using CoreUtility = Duplicati.Library.Utility.Utility;
using Duplicati.Library.Common.IO;

using Uri = System.Uri;

namespace Duplicati.Library.Backend.AlternativeFTP
{
// ReSharper disable once RedundantExtendsListEntry
Expand Down Expand Up @@ -304,7 +306,7 @@ private IEnumerable<IFileEntry> List(string filename, bool stripFile)
return list;
}

public void Put(string remotename, System.IO.Stream input)
public Task Put(string remotename, System.IO.Stream input, CancellationToken cancelToken)
{
string remotePath = remotename;
long streamLen = -1;
Expand Down Expand Up @@ -355,7 +357,7 @@ public void Put(string remotename, System.IO.Stream input)
{
if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen)
{
return;
return Task.FromResult(true);
}

throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen), "AftpListVerifySizeFailure");
Expand All @@ -374,13 +376,15 @@ public void Put(string remotename, System.IO.Stream input)

throw;
}

return Task.FromResult(true);
}

public void Put(string remotename, string localname)
public Task Put(string remotename, string localname, CancellationToken cancelToken)
{
using (System.IO.FileStream fs = System.IO.File.Open(localname, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
{
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}
}

Expand Down Expand Up @@ -485,7 +489,7 @@ public void Test()
{
try
{
Put(TEST_FILE_NAME, testStream);
Put(TEST_FILE_NAME, testStream, CancellationToken.None).Wait();
}
catch (Exception e)
{
Expand Down
20 changes: 12 additions & 8 deletions Duplicati/Library/Backend/AmazonCloudDrive/AmzCD.cs
Expand Up @@ -14,15 +14,17 @@
// 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 System.Collections.Generic;
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Duplicati.Library.Common.IO;

using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend.AmazonCloudDrive
{
public class AmzCD : IBackend, IStreamingBackend, IRenameEnabledBackend
Expand Down Expand Up @@ -313,7 +315,7 @@ public static string EscapeFiltersValue(string value)

#region IStreamingBackend implementation

public void Put(string remotename, System.IO.Stream stream)
public Task Put(string remotename, System.IO.Stream stream, CancellationToken cancelToken)
{
EnforceConsistencyDelay(remotename);

Expand Down Expand Up @@ -362,6 +364,8 @@ public void Put(string remotename, System.IO.Stream stream)
{
SetWaitUntil(remotename, DateTime.Now + m_delayTimeSpan);
}

return Task.FromResult(true);
}

public void Get(string remotename, System.IO.Stream stream)
Expand Down Expand Up @@ -428,10 +432,10 @@ public IEnumerable<IFileEntry> List()

}

public void Put(string remotename, string filename)
public Task Put(string remotename, string filename, CancellationToken cancelToken)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}

public void Get(string remotename, string filename)
Expand Down
12 changes: 7 additions & 5 deletions Duplicati/Library/Backend/AzureBlob/AzureBlobBackend.cs
Expand Up @@ -18,10 +18,11 @@
//
#endregion

using System;
using Duplicati.Library.Interface;
using System.Collections.Generic;
using System.IO;
using Duplicati.Library.Interface;
using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend.AzureBlob
{
Expand Down Expand Up @@ -87,18 +88,19 @@ public IEnumerable<IFileEntry> List()
return _azureBlob.ListContainerEntries();
}

public void Put(string remotename, string localname)
public Task Put(string remotename, string localname, CancellationToken cancelToken)
{
using (var fs = File.Open(localname,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}
}

public void Put(string remotename, Stream input)
public Task Put(string remotename, Stream input, CancellationToken cancelToken)
{
_azureBlob.AddFileStream(remotename, input);
return Task.FromResult(true);
}

public void Get(string remotename, string localname)
Expand Down
20 changes: 12 additions & 8 deletions Duplicati/Library/Backend/Backblaze/B2.cs
Expand Up @@ -14,14 +14,16 @@
// 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 System.Collections.Generic;
using Duplicati.Library.Utility;
using Duplicati.Library.Interface;
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using Duplicati.Library.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend.Backblaze
{
Expand Down Expand Up @@ -188,7 +190,7 @@ public IList<ICommandLineArgument> SupportedCommands
}
}

public void Put(string remotename, System.IO.Stream stream)
public Task Put(string remotename, System.IO.Stream stream, CancellationToken cancelToken)
{
TempFile tmp = null;

Expand Down Expand Up @@ -285,6 +287,8 @@ public void Put(string remotename, System.IO.Stream stream)
{
}
}

return Task.FromResult(true);
}

public void Get(string remotename, System.IO.Stream stream)
Expand Down Expand Up @@ -368,10 +372,10 @@ public IEnumerable<IFileEntry> List()
).ToList();
}

public void Put(string remotename, string filename)
public Task Put(string remotename, string filename, CancellationToken cancelToken)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}

public void Get(string remotename, string filename)
Expand Down
18 changes: 11 additions & 7 deletions Duplicati/Library/Backend/Box/BoxBackend.cs
Expand Up @@ -14,13 +14,15 @@
// 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 Duplicati.Library.Common.IO;
using System.Collections.Generic;
using Duplicati.Library.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend.Box
{
Expand Down Expand Up @@ -196,7 +198,7 @@ private IEnumerable<FileEntity> PagedFileListResponse(string parentid, bool only

#region IStreamingBackend implementation

public void Put(string remotename, System.IO.Stream stream)
public Task Put(string remotename, System.IO.Stream stream, CancellationToken cancelToken)
{
var createreq = new CreateItemRequest() {
Name = remotename,
Expand Down Expand Up @@ -237,6 +239,8 @@ public void Put(string remotename, System.IO.Stream stream)
m_filecache.Clear();
throw;
}

return Task.FromResult(true);
}

public void Get(string remotename, System.IO.Stream stream)
Expand All @@ -257,10 +261,10 @@ public System.Collections.Generic.IEnumerable<IFileEntry> List()
select (IFileEntry)new FileEntry(n.Name, n.Size, n.ModifiedAt, n.ModifiedAt) { IsFolder = n.Type == "folder" };
}

public void Put(string remotename, string filename)
public Task Put(string remotename, string filename, CancellationToken cancelToken)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}

public void Get(string remotename, string filename)
Expand Down
18 changes: 10 additions & 8 deletions Duplicati/Library/Backend/CloudFiles/CloudFiles.cs
Expand Up @@ -17,14 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Duplicati.Library.Interface;
using Duplicati.Library.Common.IO;

using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend
{
public class CloudFiles : IBackend, IStreamingBackend
Expand Down Expand Up @@ -194,10 +194,10 @@ public IEnumerable<IFileEntry> List()
} while (repeat);
}

public void Put(string remotename, string filename)
public Task Put(string remotename, string filename, CancellationToken cancelToken)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
Put(remotename, fs);
return Put(remotename, fs, cancelToken);
}

public void Get(string remotename, string filename)
Expand Down Expand Up @@ -314,7 +314,7 @@ public void Get(string remotename, System.IO.Stream stream)
}
}

public void Put(string remotename, System.IO.Stream stream)
public Task Put(string remotename, System.IO.Stream stream, CancellationToken cancelToken)
{
HttpWebRequest req = CreateRequest("/" + remotename, "");
req.Method = "PUT";
Expand Down Expand Up @@ -394,6 +394,8 @@ public void Put(string remotename, System.IO.Stream stream)
throw new Exception(Strings.CloudFiles.ETagVerificationError);
}
}

return Task.FromResult(true);
}

#endregion
Expand Down

0 comments on commit 0c122ff

Please sign in to comment.