/// <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;
        }
        void LoadDirectory(BrowserFileInfo path, ListDirectoryResult result)
        {
            if (path.Path == null)
            {
                // special case for drives/mounts
                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    BrowserFileInfo bfiDrive = new BrowserFileInfo(drive);
                    result.Add(bfiDrive);
                    result.MountCount++;
                }
            }
            else if (Directory.Exists(path.Path))
            {
                // get new files
                DirectoryInfo newDir = new DirectoryInfo(path.Path);

                // add back (..) entry, if relevant
                if (newDir.Parent != null)
                {
                    // has valid parent dir
                    BrowserFileInfo bfiParent = new BrowserFileInfo(newDir.Parent);
                    bfiParent.Name = "..";
                    bfiParent.Type = FileType.ParentDirectory;
                    result.Add(bfiParent);
                }
                else
                {
                    // hit top of root so list drives, special case of null path
                    BrowserFileInfo bfiListDrives = new BrowserFileInfo
                    {
                        Path = null,
                        Name = "..",
                        Type = FileType.ParentDirectory,
                    };
                    result.Add(bfiListDrives);
                }

                // dirs
                foreach (DirectoryInfo di in newDir.GetDirectories())
                {
                    BrowserFileInfo bfi = new BrowserFileInfo(di);
                    result.Add(bfi);
                }

                // files
                foreach (FileInfo fi in newDir.GetFiles())
                {
                    BrowserFileInfo bfi = new BrowserFileInfo(fi);
                    result.Add(bfi);
                }
            }
            else
            {
                result.SetError(string.Format("Directory doesn't exist: {0}", path.Path), null);
            }
        }
 public static BrowserFileInfo NewDirectory(string dir)
 {
     BrowserFileInfo file = new BrowserFileInfo
     {
         Name = Path.GetFileName(dir),
         Path = dir,
         Type = FileType.Directory,
         Source = SourceType.Remote
     };
     return file;
 }
        public void Initialize(IBrowserPresenter presenter, BrowserFileInfo startingDir)
        {
            if (this.initialized) return;

            this.Presenter = presenter;
            this.Presenter.AuthRequest += (Presenter_AuthRequest);
            this.Bind(this.Presenter.ViewModel);

            this.Presenter.LoadDirectory(startingDir);
            this.ConfirmTransfer = true;
            this.initialized = true;
        }
        public ListDirectoryResult ListDirectory(SessionData session, BrowserFileInfo path)
        {
            Log.InfoFormat("GetFilesForPath, path={0}", path.Path);

            ListDirectoryResult result = new ListDirectoryResult(path);
            try
            {
                LoadDirectory(path, result);
            }
            catch (Exception ex)
            {
                result.SetError(null, ex);
                Log.ErrorFormat("Error loading directory: {0}", ex.Message);
            }

            return result;
        }
            public bool TryParseFileLine(string line, out BrowserFileInfo fileInfo)
            {
                bool success = false;

                if (line.StartsWith("Listing directory"))
                {
                    // ignore header line and current dir
                    fileInfo = null;
                }
                else
                {
                    Match match = regExFileLine.Match(line);
                    if (match.Success)
                    {
                        string name = match.Groups["FileName"].Value;

                        fileInfo = new BrowserFileInfo
                        {
                            Name = name,
                            Path = name,
                            Owner = match.Groups["OwnerName"].Value,
                            Group = match.Groups["GroupName"].Value,
                            Permissions = match.Groups["Permissions"].Value,
                            Source = SourceType.Remote
                        };
                        fileInfo.Type = fileInfo.Name == ".."
                            ? FileType.ParentDirectory
                            : fileInfo.Permissions != null && fileInfo.Permissions.StartsWith("d")
                                ? FileType.Directory : FileType.File;

                        DateTime lastMod;
                        if (TryParseTimestamp(match.Groups["Timestamp"].Value, out lastMod))
                        {
                            fileInfo.LastModTime = lastMod;
                        }
                        int blocks;
                        if (int.TryParse(match.Groups["BlockCount"].Value, out blocks))
                        {
                            fileInfo.Size = blocks;
                        }
                        success = true;
                    }
                    else
                    {
                        Log.WarnFormat("Could not parse directory listing entry: '{0}'", line);
                        fileInfo = null;
                    }
                }

                return success;
            }
        static string ToArgs(SessionData session, string password, List<BrowserFileInfo> source, BrowserFileInfo target)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("-r -agent ");  // default arguments
            if (!String.IsNullOrEmpty(session.PuttySession))
            {
                sb.Append("-load \"").Append(session.PuttySession).Append("\" ");
            }
            if (!String.IsNullOrEmpty(password))
            {
                sb.Append("-pw ").Append(password).Append(" ");
            }
            sb.AppendFormat("-P {0} ", session.Port);

            if (target.Source == SourceType.Remote)
            {
                // possible to send multiple files remotely at a time
                foreach(BrowserFileInfo file in source)
                {
                    sb.AppendFormat("\"{0}\" ", file.Path);
                }
                sb.AppendFormat(" {0}@{1}:\"{2}\"", session.Username, session.Host, EscapeForUnix(target.Path));
            }
            else
            {
                if (source.Count > 1)
                {
                    Log.WarnFormat("Not possible to transfer multiple remote files locally at one time.  Tranfering first only!");
                }
                sb.AppendFormat(" {0}@{1}:\"{2}\" ", session.Username, session.Host, EscapeForUnix(source[0].Path));
                sb.AppendFormat("\"{0}\"", target.Path);
            }

            return sb.ToString();
        }
        public ListDirectoryResult ListDirectory(BrowserFileInfo path)
        {
            lock (this)
            {
                //return this.DoListDirectory(path);
                ListDirectoryResult result = new ListDirectoryResult(path);

                RunPscp(
                    result,
                    ToArgs(this.Session, this.Session.Password, path.Path),
                    ToArgs(this.Session, "XXXXX", path.Path),
                    null,
                    null,
                    (lines) =>
                    {
                        // successful list
                        ScpLineParser parser = new ScpLineParser();
                        foreach (string rawLine in lines)
                        {
                            string line = rawLine.TrimEnd();
                            BrowserFileInfo fileInfo;
                            if (parser.TryParseFileLine(line, out fileInfo))
                            {
                                if (fileInfo.Name != ".")
                                {
                                    fileInfo.Path = MakePath(path.Path, fileInfo.Name);
                                    result.Add(fileInfo);
                                }
                            }
                        }
                    });

                return result;
            }
        }
        /// <summary>
        /// Copy files
        /// </summary>
        /// <param name="sourceFiles"></param>
        /// <param name="target"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public FileTransferResult CopyFiles(List<BrowserFileInfo> sourceFiles, BrowserFileInfo target, TransferUpdateCallback callback)
        {
            lock(this)
            {
                /// Known Issues:
                /// - If a large exe (or other file that the OS will virus scan) is tranfered, the operation will timeout.
                ///   After completion, the OS seems to block on the final write (while scanning).  During this time the process looks like it's
                ///   hanging and the timeout logic kicks in.  Hacked in "completed" logic into inlineOut/Err handlers but this is awkward

                FileTransferResult result = new FileTransferResult();

                string args = ToArgs(this.Session, this.Session.Password, sourceFiles, target);
                string argsToLog = ToArgs(this.Session, "XXXXX", sourceFiles, target);
                ScpLineParser parser = new ScpLineParser();
                RunPscp(
                    result, args, argsToLog,
                    (line) =>
                    {
                        bool completed = false;
                        if (callback != null)
                        {
                            FileTransferStatus status;
                            if (parser.TryParseTransferStatus(line, out status))
                            {
                                completed = status.PercentComplete == 100;
                                callback(completed, false, status);
                            }
                        }
                        return completed;
                    },
                    null, null);

                return result;
            }
        }
 public void Add(BrowserFileInfo fileInfo)
 {
     this.Files.Add(fileInfo);
     if (fileInfo.Type == FileType.File)
     {
         this.FileCount++;
     }
     else if (fileInfo.Type == FileType.Directory)
     {
         this.DirCount++;
     }
 }
Beispiel #11
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( @"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);*/
        }
 public void LoadDirectory(BrowserFileInfo dir)
 {
     if (this.BackgroundWorker.IsBusy)
     {
         this.ViewModel.Status = "Busying loading directory";
     }
     else
     {
         this.ViewModel.BrowserState = BrowserState.Working;
         Log.InfoFormat("LoadDirectory, path={0}", dir);
         this.BackgroundWorker.RunWorkerAsync(dir);
     }
 }
 public bool CanTransferFile(BrowserFileInfo source, BrowserFileInfo target)
 {
     return this.FileTransferPresenter.CanTransferFile(source, target);
 }
 public bool CanTransferFile(BrowserFileInfo source, BrowserFileInfo target)
 {
     return true;
 }
 public ListDirectoryResult(BrowserFileInfo path)
 {
     this.Path = path;
     this.Files = new List<BrowserFileInfo>();
 }
        public bool CanTransferFile(BrowserFileInfo source, BrowserFileInfo target)
        {
            //    Source        Target   Result
            // 1) WindowsFile   Local    if not the same, File.Copy
            // 2) Local         Local    File.Copy
            // 3) Remote        Local    RemoteCopy
            bool canTransfer = true;

            if (target.Type == FileType.File || target.Type == FileType.ParentDirectory)
            {
                // can't drop on file or parent dir (..)
                canTransfer = false;
            }
            else if (target.Source == source.Source)
            {
                // can't drop local-on-local or remote-on-remote
                canTransfer = false;
            }
            else if (target.Path == source.Path)
            {
                // can't drop on self
                canTransfer = false;
            }
            /*  Future...if/when local-on-local allowed
            else if (source.Source == SourceType.Local)
            {
                // can't drop into own folder...no op
                string sourceDir = Path.GetFullPath(Path.GetDirectoryName(source.Path));
                string targetDir = Path.GetFullPath(target.Path);
                canTransfer = sourceDir != targetDir;
            }*/
            return canTransfer;
        }
 public BrowserView(IBrowserPresenter presenter, BrowserFileInfo startingDir)
     : this()
 {
     Initialize(presenter, startingDir);
 }