/// <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 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;
        }
Esempio n. 4
0
        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;
            }
        }