Simplified version of PscpTransfer class - Movied LoginDialog calls outside - Made calls synchronous...move async outside - Make pieces unit-testable - Work around issue in Process.StandardOuput/StandardError blocking on calls Alternate workaround...native process start w/correct stream reading behavior (e.g. peek doens't block) http://stackoverflow.com/questions/6655613/why-does-standardoutput-read-block-when-startinfo-redirectstandardinput-is-set
Exemplo n.º 1
0
        /// <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)
                {
                    ErrorMsg   = "Session invalid",
                    StatusCode = ResultStatusCode.Error
                };
            }
            else
            {
                string targetPath = path.Path;
                if (targetPath == null || targetPath == ".")
                {
                    targetPath = string.Format("/home/{0}", session.Username);
                    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);
        }
Exemplo n.º 2
0
        /// <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 = string.Format("/home/{0}", session.Username);
                    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;
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        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(ScpConfig.PscpLocation))
            };
            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);*/
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }