public void ListDirHostNoKey() { SessionData session = new SessionData { Username = ScpConfig.UserName, Password = ScpConfig.Password, Host = ScpConfig.UnKnownHost, Port = 22 }; PscpClient client = new PscpClient(ScpConfig.DefaultOptions, session); ListDirectoryResult res = client.ListDirectory(new BrowserFileInfo { Path = "." }); Assert.AreEqual(ResultStatusCode.Error, res.StatusCode); Assert.AreEqual(res.Files.Count, 0); Log.InfoFormat("Result: {0}", res); }
public void ListDirBadPath() { SessionData session = new SessionData { Username = ScpConfig.UserName, Password = ScpConfig.Password, Host = ScpConfig.KnownHost, Port = 22 }; PscpClient client = new PscpClient(ScpConfig.DefaultOptions, session); ListDirectoryResult res = client.ListDirectory(new BrowserFileInfo { Path = "some_non_existant_dir" }); Assert.AreEqual(ResultStatusCode.Error, res.StatusCode); Assert.AreEqual(0, res.Files.Count); Assert.IsTrue(res.ErrorMsg != null && res.ErrorMsg.StartsWith("Unable to open")); Log.InfoFormat("Result: {0}", res); }
void DoTransfer() { try { PscpClient client = new PscpClient(this.Options, this.Request.Session); int estSizeKB = Int32.MaxValue; FileTransferResult res = client.CopyFiles( this.Request.SourceFiles, this.Request.TargetFile, (complete, cancelAll, s) => { string msg; if (s.PercentComplete > 0) { estSizeKB = Math.Min(estSizeKB, s.BytesTransferred * 100 / s.PercentComplete); string units = estSizeKB > 1024 * 10 ? "MB" : "KB"; int divisor = units == "MB" ? 1024 : 1; msg = string.Format( "{0}, ({1} of {2} {3}, {4})", s.Filename, s.BytesTransferred / divisor, estSizeKB / divisor, units, s.TimeLeft); } else { // < 1% completed msg = string.Format("{0}, ({1} KB, {2})", s.Filename, s.BytesTransferred, s.TimeLeft); } this.UpdateStatus(s.PercentComplete, Status.Running, msg); }); this.EndTime = DateTime.Now; switch (res.StatusCode) { case ResultStatusCode.Success: double duration = (EndTime.Value - StartTime.Value).TotalSeconds; this.UpdateStatus(100, Status.Complete, String.Format("Duration {0:#,###} s", duration)); break; case ResultStatusCode.RetryAuthentication: case ResultStatusCode.Error: this.UpdateStatus(this.PercentComplete, Status.Error, res.ErrorMsg); break; } } catch (ThreadAbortException) { this.UpdateStatus(this.PercentComplete, Status.Canceled, ""); } catch (Exception ex) { Log.Error("Error running transfer, id=" + this.Id, ex); this.UpdateStatus(0, Status.Error, ex.Message); } }
public void ListDirSuccess() { SessionData session = new SessionData { Username = ScpConfig.UserName, Password = ScpConfig.Password, Host = ScpConfig.KnownHost, Port = 22 }; PscpClient client = new PscpClient(ScpConfig.DefaultOptions, session); ListDirectoryResult res = client.ListDirectory(new BrowserFileInfo { Path = "." }); Assert.AreEqual(ResultStatusCode.Success, res.StatusCode); Assert.Greater(res.Files.Count, 0); foreach (BrowserFileInfo file in res.Files) { Log.Info(file); } Log.InfoFormat("Result: {0}", res); }
public void LocalToRemote() { SessionData session = new SessionData { Username = ScpConfig.UserName, Password = ScpConfig.Password, Host = ScpConfig.KnownHost, Port = 22 }; List<BrowserFileInfo> sourceFiles = new List<BrowserFileInfo> { //new BrowserFileInfo(new FileInfo(Path.GetTempFileName())) new BrowserFileInfo(new FileInfo( @"D:\Downloads\vs2012_winexp_enu.iso" )) }; BrowserFileInfo target = new BrowserFileInfo { Path = string.Format("/home/{0}/", session.Username), Source = SourceType.Remote }; PscpOptions options = new PscpOptions { PscpLocation = ScpConfig.PscpLocation, TimeoutMs = 5000 }; PscpClient client = new PscpClient(options, session); PscpResult res = client.CopyFiles( sourceFiles, target, (complete, cancelAll, status) => { Log.InfoFormat( "complete={0}, cancelAll={1}, fileName={2}, pctComplete={3}", complete, cancelAll, status.Filename, status.PercentComplete); }); Log.InfoFormat("Result: {0}", res); /* ListDirectoryResult res = client.ListDirectory(new BrowserFileInfo { Path = "." }); Assert.AreEqual(ResultStatusCode.Success, res.StatusCode); Assert.Greater(res.Files.Count, 0); foreach (BrowserFileInfo file in res.Files) { Log.Info(file); } Log.InfoFormat("Result: {0}", res);*/ }
/// <summary> /// Sync call to list directory contents /// </summary> /// <param name="session"></param> /// <param name="path"></param> /// <returns></returns> public ListDirectoryResult ListDirectory(SessionData session, BrowserFileInfo path) { ListDirectoryResult result; if (session == null || session.Username == null) { result = new ListDirectoryResult(path); result.ErrorMsg = "Session invalid"; result.StatusCode = ResultStatusCode.Error; } else { string targetPath = path.Path; if (targetPath == null || targetPath == ".") { targetPath = ScpUtils.GetHomeDirectory(session); Log.InfoFormat("Defaulting path: {0}->{1}", path.Path, targetPath); path.Path = targetPath; } PscpClient client = new PscpClient(this.Options, session); result = client.ListDirectory(path); } return result; }