public override async Task UploadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     using (var webClient = _webClientFactory())
     using (webClient.Value.SetupUploadTransferProgress(spec.Progress, timeout))
         await TryUploadAsync(spec, webClient.Value).ConfigureAwait(false);
 }
        async Task TryDownloadAsync(TransferSpec spec, IWebClient webClient, IAbsoluteFilePath tmpFile) {
            try {
                tmpFile.RemoveReadonlyWhenExists();
                if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo))
                    webClient.SetAuthInfo(spec.Uri);
                using (webClient.HandleCancellationToken(spec))
                    await webClient.DownloadFileTaskAsync(spec.Uri, tmpFile.ToString()).ConfigureAwait(false);
                VerifyIfNeeded(spec, tmpFile);
                _fileOps.Move(tmpFile, spec.LocalFile);
            } catch (OperationCanceledException e) {
                _fileOps.DeleteIfExists(tmpFile.ToString());
                throw CreateTimeoutException(spec, e);
            } catch (WebException ex) {
                _fileOps.DeleteIfExists(tmpFile.ToString());
                var cancelledEx = ex.InnerException as OperationCanceledException;
                if (cancelledEx != null)
                    throw CreateTimeoutException(spec, cancelledEx);
                if (ex.Status == WebExceptionStatus.RequestCanceled)
                    throw CreateTimeoutException(spec, ex);

                var response = ex.Response as HttpWebResponse;
                if (response == null)
                    throw GenerateDownloadException(spec, ex);

                switch (response.StatusCode) {
                case HttpStatusCode.NotFound:
                    throw new RequestFailedException("Received a 404: NotFound response", ex);
                case HttpStatusCode.Forbidden:
                    throw new RequestFailedException("Received a 403: Forbidden response", ex);
                case HttpStatusCode.Unauthorized:
                    throw new RequestFailedException("Received a 401: Unauthorized response", ex);
                }
                throw GenerateDownloadException(spec, ex);
            }
        }
 protected override void OnError(TransferSpec spec, Exception e) {
     var msg = $"Failed upload of {spec.LocalFile} to {spec.Uri} ({e.Message})";
     if (spec.Progress.Tries <= 1)
         this.Logger().Error(msg);
     else
         this.Logger().Warn(msg);
 }
 public override void Download(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     spec.Progress.ResetZsyncLoopInfo();
     ProcessExitResult(_zsyncLauncher.RunAndProcess(GetParams(spec)), spec);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 public override async Task DownloadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     spec.Progress.ResetZsyncLoopInfo();
     ProcessExitResult(await _zsyncLauncher.RunAndProcessAsync(GetParams(spec)).ConfigureAwait(false),
         spec);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 public override void Download(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     ProcessExitResult(
         _rsyncLauncher.RunAndProcess(spec.Progress, spec.Uri.ToString(), spec.LocalFile.ToString(),
             spec.CancellationToken), spec);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 public override async Task UploadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     ProcessExitResult(
         await
             _rsyncLauncher.RunAndProcessAsync(spec.Progress, spec.LocalFile.ToString(), spec.Uri.ToString())
                 .ConfigureAwait(false), spec);
 }
 private void WrapInternal(Action action, TransferSpec spec) {
     OnStart(spec);
     try {
         action();
     } catch (Exception e) {
         OnError(spec, e);
         throw;
     }
     OnFinished(spec);
 }
 public override async Task DownloadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     ProcessExitResult(
         await
             _rsyncLauncher.RunAndProcessAsync(spec.Progress, spec.Uri.ToString(), spec.LocalFile.ToString(),
                     spec.CancellationToken)
                 .ConfigureAwait(false), spec);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 private async Task WrapInternal(Func<Task> task, TransferSpec spec) {
     OnStart(spec);
     try {
         await task().ConfigureAwait(false);
     } catch (Exception e) {
         OnError(spec, e);
         throw;
     }
     OnFinished(spec);
 }
 static void ProcessExitResult(ProcessExitResultWithOutput result, TransferSpec spec) {
     switch (result.ExitCode) {
     case 0:
         break;
     case -1:
         throw new RsyncSoftProgramException(
             $"Aborted/Killed (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 5:
         throw new RsyncSoftException(
             $"Server full (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 10:
         throw new RsyncSoftException(
             $"Connection refused (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError,
             result.StartInfo.Arguments);
     case 12:
         throw new RsyncSoftException(
             $"Could not retrieve file (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 14:
         throw new RsyncSoftException(
             $"Could not retrieve file due to IPC error (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 23:
         throw new RsyncSoftException(
             $"Could not retrieve file (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 24:
         throw new RsyncSoftException(
             $"Could not retrieve file (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 30:
         throw new RsyncSoftException(
             $"Could not retrieve file due to Timeout (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 35:
         throw new RsyncSoftException(
             $"Could not retrieve file due to Timeout (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 35584:
         throw new RsyncSoftProgramException(
             $"Stackdump (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case -1073741819:
         throw new RsyncCygwinProgramException(
             $"Cygwin fork/rebase problem (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     default:
         throw new RsyncException(
             $"Did not exit gracefully (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     }
 }
 protected override void OnError(TransferSpec spec, Exception e) {
     if (e is OperationCanceledException) {
         this.Logger()
             .Warn(
                 $"Cancelled download of {spec.Uri} to {spec.LocalFile}, try {spec.Progress.Tries} ({e.Message})");
         return;
     }
     var msg =
         $"Failed download of {spec.Uri} to {spec.LocalFile}, try {spec.Progress.Tries} ({e.Message})\nOutput: {spec.Progress.Output}\n\nError report: {e.Format(1)}";
     if (spec.Progress.Tries > 1)
         this.Logger().Warn(msg);
     else
         this.Logger().Error(msg);
 }
        static async Task TryUploadAsync(TransferSpec spec, IWebClient webClient) {
            try {
                if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo))
                    webClient.SetAuthInfo(spec.Uri);
                using (webClient.HandleCancellationToken(spec))
                    await webClient.UploadFileTaskAsync(spec.Uri, spec.LocalFile.ToString()).ConfigureAwait(false);
            } catch (OperationCanceledException e) {
                throw CreateTimeoutException(spec, e);
            } catch (WebException e) {
                var cancelledEx = e.InnerException as OperationCanceledException;
                if (cancelledEx != null)
                    throw CreateTimeoutException(spec, cancelledEx);
                if (e.Status == WebExceptionStatus.RequestCanceled)
                    throw CreateTimeoutException(spec, e);

                GenerateUploadException(spec, e);
            }
        }
 static void GenerateUploadException(TransferSpec spec, WebException e) {
     // TODO: Or should we rather abstract this away into the downloader exceptions instead?
     var r = e.Response as HttpWebResponse;
     if (r != null) {
         throw new HttpUploadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e) {
             StatusCode = r.StatusCode
         };
     }
     /*
     var r2 = e.Response as FtpWebResponse;
     if (r2 != null) {
         throw new FtpUploadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e) {
             StatusCode = r2.StatusCode
         };
     }
     */
     throw new UploadException(e.Message + ". " + CreateTransferExceptionMessage(spec), e);
 }
 static void ProcessExitResult(ProcessExitResultWithOutput result, TransferSpec spec) {
     switch (result.ExitCode) {
     case 0:
         break;
     case -1:
         throw new RsyncSoftProgramException(
             $"Aborted/Killed (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case 35584:
         throw new RsyncSoftProgramException(
             $"Stackdump (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     case -1073741819:
         throw new RsyncCygwinProgramException(
             $"Cygwin fork/rebase problem (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     default:
         throw new RsyncException(
             $"Did not exit gracefully (PID: {result.Id}, Status: {result.ExitCode}). {CreateTransferExceptionMessage(spec)}",
             result.StandardOutput + result.StandardError, result.StartInfo.Arguments);
     }
 }
 protected void Wrap(Action action, TransferSpec spec) {
     if (Common.Flags.Verbose)
         WrapInternal(action, spec);
     else
         action();
 }
 public override Task UploadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     return
         _fileCopy.CopyFileAsync(spec.LocalFile, GetPathFromUri(spec));
 }
 protected Task Wrap(Func<Task> task, TransferSpec spec)
     => Common.Flags.Verbose ? WrapInternal(task, spec) : task();
 public override async Task DownloadAsync(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     await _fileCopy.CopyFileAsync(GetPathFromUri(spec), spec.LocalFile).ConfigureAwait(false);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 protected abstract void OnError(TransferSpec spec, Exception exception);
 protected abstract void OnStart(TransferSpec spec);
 public abstract Task UploadAsync(TransferSpec spec);
 protected abstract void OnFinished(TransferSpec spec);
 static IAbsoluteFilePath GetPathFromUri(TransferSpec spec) => spec.Uri.LocalPath.ToAbsoluteFilePath();
 public abstract void Upload(TransferSpec spec);
 public override void Download(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     _fileCopy.CopyFile(GetPathFromUri(spec), spec.LocalFile);
     VerifyIfNeeded(spec, spec.LocalFile);
 }
 public override void Upload(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     _fileCopy.CopyFile(spec.LocalFile, GetPathFromUri(spec));
 }
 public static CancellationTokenRegistration HandleCancellationToken(this IWebClient webClient, TransferSpec spec) {
     var cancellationToken = spec.CancellationToken;
     if (cancellationToken != null)
         return cancellationToken.Register(webClient.CancelAsync);
     return default(CancellationTokenRegistration);
 }
 protected override void OnFinished(TransferSpec spec)
     => this.Logger().Info("Finished download of {0} to {1}", spec.Uri, spec.LocalFile);
 public override void Upload(TransferSpec spec) {
     spec.Progress.Tries++;
     ConfirmSchemeSupported(spec.Uri.Scheme);
     ProcessExitResult(
         _rsyncLauncher.RunAndProcess(spec.Progress, spec.LocalFile.ToString(), spec.Uri.ToString()), spec);
 }