protected override async Task PerformIO() { bool hadToConnect = _client.ConnectIfNeeded(); List <string> listFilePaths; bool isDirectory = _client.GetAttributes(FromPath).IsDirectory; if (isDirectory) { if (DoCancel) { return; } //Only include files (not sub-directories) in the list listFilePaths = (await Task.Factory.FromAsync(_client.BeginListDirectory(FromPath, null, null), _client.EndListDirectory)) .Where(x => !x.IsDirectory) .Select(x => x.FullName).ToList(); } else //Copying a file { listFilePaths = new List <string> { FromPath }; } CountTotal = listFilePaths.Count; for (int i = 0; i < CountTotal; i++) { if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } string path = listFilePaths[i]; try { string fileName = Path.GetFileName(path); string toPathFull = ToPath; if (isDirectory) { toPathFull = ODFileUtils.CombinePaths(ToPath, fileName, '/'); } if (FileExists(_client, toPathFull)) { throw new Exception(); //Throw so that we can iterate CountFailed } string fromPathFull = FromPath; if (isDirectory) { fromPathFull = ODFileUtils.CombinePaths(FromPath, fileName, '/'); } if (IsCopy) { //Throws if fails. await Task.Run(() => { TaskStateDownload stateDown = new Download(_host, _user, _pass) { Folder = Path.GetDirectoryName(fromPathFull).Replace('\\', '/'), FileName = Path.GetFileName(fromPathFull), ProgressHandler = ProgressHandler, HasExceptions = HasExceptions }; stateDown.Execute(ProgressHandler != null); while (!stateDown.IsDone) { stateDown.DoCancel = DoCancel; } if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } TaskStateUpload stateUp = new Upload(_host, _user, _pass) { Folder = Path.GetDirectoryName(toPathFull).Replace('\\', '/'), FileName = Path.GetFileName(toPathFull), FileContent = stateDown.FileContent, ProgressHandler = ProgressHandler, HasExceptions = HasExceptions }; stateUp.Execute(ProgressHandler != null); while (!stateUp.IsDone) { stateUp.DoCancel = DoCancel; } if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } }); } else //Moving { await Task.Run(() => { SftpFile file = _client.Get(path); _client.CreateDirectoriesIfNeeded(ToPath); file.MoveTo(toPathFull); }); } CountSuccess++; } catch (Exception) { CountFailed++; } finally { if (IsCopy) { OnProgress(i + 1, "?currentVal files of ?maxVal files copied", CountTotal, ""); } else { OnProgress(i + 1, "?currentVal files of ?maxVal files moved", CountTotal, ""); } } } _client.DisconnectIfNeeded(hadToConnect); }
public static async Task <IEnumerable <SftpFile> > ListDirectoryAsync(SftpClient sftp, string path) { Func <string, AsyncCallback, object, IAsyncResult> begin = (bpath, callback, state) => sftp.BeginListDirectory(bpath, callback, state, null); return(await SSHRetryReset(sftp, async() => { return await Task.Factory.FromAsync(begin, sftp.EndListDirectory, path, null); })); }
private async Task <IEnumerable <Tuple <string, string> > > GetRemoteListingAsync(string remotePath, string localPath, bool recursive, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(remotePath)) { throw new ArgumentNullException(nameof(remotePath)); } if (string.IsNullOrWhiteSpace(localPath)) { throw new ArgumentNullException(nameof(localPath)); } if (cancellationToken == null) { throw new ArgumentNullException(nameof(cancellationToken)); } cancellationToken.ThrowIfCancellationRequested(); string initialWorkingDirectory = _sftpClient.WorkingDirectory; _sftpClient.ChangeDirectory(remotePath); List <Tuple <string, string> > listing = new List <Tuple <string, string> >(); SftpFile currentDirectory = _sftpClient.Get(_sftpClient.WorkingDirectory); IEnumerable <SftpFile> items = await Task.Factory.FromAsync(_sftpClient.BeginListDirectory(currentDirectory.FullName, null, null), _sftpClient.EndListDirectory); string nextLocalPath = Path.Combine(localPath, currentDirectory.Name); foreach (SftpFile file in items.Where(i => i.IsRegularFile)) { listing.Add(new Tuple <string, string>(Path.Combine(nextLocalPath, file.Name), file.FullName)); } if (recursive) { foreach (SftpFile directory in items.Where(i => i.IsDirectory && i.Name != "." && i.Name != "..")) { listing.AddRange(await GetRemoteListingAsync(directory.FullName, nextLocalPath, recursive, cancellationToken)); } } _sftpClient.ChangeDirectory(initialWorkingDirectory); return(listing); }
public void Test_Sftp_Call_EndListDirectory_Twice() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var ar = sftp.BeginListDirectory("/", null, null); var result = sftp.EndListDirectory(ar); var result1 = sftp.EndListDirectory(ar); } }
public static Task <IEnumerable <SftpFile> > ListDirectoryAsync(this SftpClient client, string path, Action <int> listCallback = null, TaskFactory <IEnumerable <SftpFile> > factory = null, TaskCreationOptions creationOptions = default(TaskCreationOptions), TaskScheduler scheduler = null) { return((factory = factory ?? Task <IEnumerable <SftpFile> > .Factory).FromAsync( client.BeginListDirectory(path, null, null, listCallback), client.EndListDirectory, creationOptions, scheduler ?? factory.Scheduler ?? TaskScheduler.Current)); }
protected override async Task PerformIO() { bool hadToConnect = _client.ConnectIfNeeded(); _client.CreateDirectoriesIfNeeded(FolderPath); List <SftpFile> listFiles = (await Task.Factory.FromAsync(_client.BeginListDirectory(FolderPath, null, null), _client.EndListDirectory)).ToList(); listFiles = listFiles.FindAll(x => !x.FullName.EndsWith(".")); //Sftp has 2 "extra" files that are "." and "..". I think it's for explorer navigation. ListFolderPathLower = listFiles.Select(x => x.FullName.ToLower()).ToList(); ListFolderPathsDisplay = listFiles.Select(x => x.FullName).ToList(); _client.DisconnectIfNeeded(hadToConnect); }
public void Test_Sftp_EndUploadFile_Invalid_Async_Handle() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var async1 = sftp.BeginListDirectory("/", null, null); var filename = Path.GetTempFileName(); this.CreateTestFile(filename, 100); var async2 = sftp.BeginUploadFile(File.OpenRead(filename), "test", null, null); sftp.EndUploadFile(async1); } }
/// <summary> /// Get all file names, inclusive path, of the directory on ftp server /// </summary> /// <param name="settings">ftp server settings</param> /// <param name="recursive">Enumerate sub directories</param> /// <returns></returns> public async Task <IEnumerable <string> > GetFileNamesAsync(FTPSettings settings, bool recursive) { var connectionInfo = CreateConnectionInfo(settings); var fileNames = new List <string>(); var directoryNames = new List <string>(); using (var sftp = new SftpClient(connectionInfo)) { sftp.Connect(); await Task.Factory.FromAsync(sftp.BeginListDirectory(GetServerPath(settings.ServerPath), null, null), (IAsyncResult result) => { var files = sftp.EndListDirectory(result); foreach (var file in files) { if (file.IsDirectory) { if (file.Name == "." || file.Name == ".." || !recursive) { continue; } directoryNames.Add(file.Name); } else { fileNames.Add(file.FullName); } } }); sftp.Disconnect(); } if (recursive && directoryNames.Count > 0) { var childSettings = new FTPSettings { ServerUrl = settings.ServerUrl, ServerPort = settings.ServerPort, Password = settings.Password, Username = settings.Username }; foreach (var directoryName in directoryNames) { childSettings.ServerPath = String.Join("/", settings.ServerPath, directoryName); fileNames.AddRange(await GetFileNamesAsync(childSettings, true)); } } return(fileNames); }
public async Task <IEnumerable <SftpFile> > GetAllFileFromPath(string remoteDirectory) { using (var sftp = new SftpClient(_host, _username, _password)) { sftp.Connect(); var res = await Task.Factory.FromAsync( (asyncCallback, state) => sftp.BeginListDirectory(remoteDirectory, asyncCallback, state), sftp.EndListDirectory, null); return(res.Where(f => !f.Name.StartsWith("."))); } }
public async Task <List <string> > ListSettlements(string subAccount) { using (var sftp = new SftpClient(_connectionInfo)) { sftp.Connect(); var files = await Task.Factory.FromAsync(sftp.BeginListDirectory($"/settlements/inbox/xml/{_settings.OrgNo}/{subAccount}", null, null), sftp.EndListDirectory); var settlements = files .Where(f => f.IsRegularFile) .Where(f => string.Equals(Path.GetExtension(f.Name), ".xml", StringComparison.OrdinalIgnoreCase)) .Select(f => Path.GetFileNameWithoutExtension(f.Name).Split("-")[1]) .ToList(); return(settlements); } }
/// <summary> /// List files in the given <paramref name="path"/> asynchronously. /// </summary> /// <param name="client">The SFTP client to use to list files.</param> /// <param name="path">The path to list files from.</param> /// <param name="cancellationToken">Cancellation token </param> /// <returns></returns> public static Task <IEnumerable <SftpFile> > ListDirectoryAsync(this SftpClient client, string path, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException("Path must not be null or white space."); } cancellationToken.ThrowIfCancellationRequested(); return(Task.Factory.FromAsync( client.BeginListDirectory(path, null, null), result => client.EndListDirectory(result))); }
private async Task GetFileListRecursivelyAsync(string prefix, Regex pattern, List <FileSpec> list) { var files = await Task.Factory.FromAsync(_client.BeginListDirectory(prefix, null, null), _client.EndListDirectory).AnyContext(); foreach (var file in files) { if (file.IsDirectory) { if (file.Name == "." || file.Name == "..") { continue; } await GetFileListRecursivelyAsync(String.Concat(prefix, "/", file.Name), pattern, list).AnyContext(); continue; } if (!file.IsRegularFile) { continue; } string path = file.FullName.TrimStart('/'); if (pattern != null && !pattern.IsMatch(path)) { continue; } list.Add(new FileSpec { Path = path, Created = file.LastWriteTimeUtc, Modified = file.LastWriteTimeUtc, Size = file.Length }); } }
public void Test_Sftp_Ensure_Async_Delegates_Called_For_BeginFileUpload_BeginFileDownload_BeginListDirectory() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); string remoteFileName = Path.GetRandomFileName(); string localFileName = Path.GetRandomFileName(); bool uploadDelegateCalled = false; bool downloadDelegateCalled = false; bool listDirectoryDelegateCalled = false; IAsyncResult asyncResult; // Test for BeginUploadFile. CreateTestFile(localFileName, 1); using (var fileStream = File.OpenRead(localFileName)) { asyncResult = sftp.BeginUploadFile(fileStream, remoteFileName, delegate(IAsyncResult ar) { sftp.EndUploadFile(ar); uploadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(uploadDelegateCalled, "BeginUploadFile"); // Test for BeginDownloadFile. asyncResult = null; using (var fileStream = File.OpenWrite(localFileName)) { asyncResult = sftp.BeginDownloadFile(remoteFileName, fileStream, delegate(IAsyncResult ar) { sftp.EndDownloadFile(ar); downloadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(downloadDelegateCalled, "BeginDownloadFile"); // Test for BeginListDirectory. asyncResult = null; asyncResult = sftp.BeginListDirectory(sftp.WorkingDirectory, delegate(IAsyncResult ar) { sftp.EndListDirectory(ar); listDirectoryDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } Assert.IsTrue(listDirectoryDelegateCalled, "BeginListDirectory"); } }
public void BeginListDirectoryTest() { ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value string path = string.Empty; // TODO: Initialize to an appropriate value AsyncCallback asyncCallback = null; // TODO: Initialize to an appropriate value object state = null; // TODO: Initialize to an appropriate value Action<int> listCallback = null; // TODO: Initialize to an appropriate value IAsyncResult expected = null; // TODO: Initialize to an appropriate value IAsyncResult actual; actual = target.BeginListDirectory(path, asyncCallback, state, listCallback); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
public void Test_Sftp_Ensure_Async_Delegates_Called_For_BeginFileUpload_BeginFileDownload_BeginListDirectory() { RemoveAllFiles(); using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); string remoteFileName = Path.GetRandomFileName(); string localFileName = Path.GetRandomFileName(); bool uploadDelegateCalled = false; bool downloadDelegateCalled = false; bool listDirectoryDelegateCalled = false; IAsyncResult asyncResult; // Test for BeginUploadFile. CreateTestFile(localFileName, 1); using (var fileStream = File.OpenRead(localFileName)) { asyncResult = sftp.BeginUploadFile(fileStream, remoteFileName, delegate(IAsyncResult ar) { sftp.EndUploadFile(ar); uploadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(uploadDelegateCalled, "BeginUploadFile"); // Test for BeginDownloadFile. asyncResult = null; using (var fileStream = File.OpenWrite(localFileName)) { asyncResult = sftp.BeginDownloadFile(remoteFileName, fileStream, delegate(IAsyncResult ar) { sftp.EndDownloadFile(ar); downloadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(downloadDelegateCalled, "BeginDownloadFile"); // Test for BeginListDirectory. asyncResult = null; asyncResult = sftp.BeginListDirectory(sftp.WorkingDirectory, delegate(IAsyncResult ar) { sftp.EndListDirectory(ar); listDirectoryDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } Assert.IsTrue(listDirectoryDelegateCalled, "BeginListDirectory"); } }