This project is read-only.

SftpFile.cs - "File" versus "Directory"

Dec 31, 2010 at 4:01 PM

Hi Oleg,

I am using  SftpClient.ListDirectory("path") to get a collection of SftpFile objects.

Is there a property on the SftpFile object that allows me to distinguish whether the object represents a file or a directory?

Thanks,

Ted.

Coordinator
Dec 31, 2010 at 4:06 PM

hhm, 

currently I think not, 

Ithink I will have to parse drwxrwxrwx string.

Unfortunately this is limitation ov SFTP protocol version 3, later versions added more attributes.

 

I will add this info later, probably next year already.

Please let me know if I missed anything else in SFTP functionality as I personally dont use it much in projects.

 

Thanks,

Oleg

Dec 31, 2010 at 4:16 PM

Thanks Oleg.

Another thing that would be nice in the SftpFile object is to store the basic name ("FileName.ext") and the path information( "/path/to/FileName.ext").  Similar to the System.IO.FileInfo object:

 

System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"C:\Windows\System32\cmd.exe");

Console.WriteLine(fileInfo.Name);  // displays "cmd.exe"

Console.WriteLine(fileInfo.FullName); // displays "C:\Windows\System32\cmd.exe"

Console.WriteLine(fileInfo.DirectoryName); // displays "C:\Windows\System32"

 

Ted.

Dec 31, 2010 at 4:59 PM
olegkap wrote:

Ithink I will have to parse drwxrwxrwx string. 

 

Actually I think you're 98% there with SftpFile.Permissions.

 

const int S_IFDIR = 16384;

bool isDirectory = ((sftpFile.Permissions & S_IFDIR) == S_IFDIR);

 

DISCLAIMER: I know enough to be dangerous.  I don't know if the above corresponds to the published specs, or if it works all the time, etc. 

 

Ted.

Coordinator
Dec 31, 2010 at 5:11 PM

actually this is something I coundt figure out yet how to use it, the permmsion information,

Can you point me out to where I can find more information about that?

If wo I could probably wrap this functionality into more convenient way to use then just int32.

 

Thanks,

Oleg

Dec 31, 2010 at 5:39 PM
Edited Dec 31, 2010 at 5:40 PM

Hi Oleg!

The SFTP spec that I used was here: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02.  Section 5 says "The `permissions' field contains a bit mask of file permissions as defined by posix ".  They refer to a document that does not appear to be relevant.

I got the values of the S_IFDIR flag from here: http://en.wikipedia.org/wiki/Sys/stat.h.  The Wikipedia description says "<sys/stat.h> is the header in the C POSIX library for the C programming language that contains constructs that facilitate getting information about files attributes", which appears to be quite relevant.

 

I have modified my copy of SftpFile.cs as follows:

    /// <summary>
    /// Represents SFTP file information
    /// </summary>
    public class SftpFile
    {
        // POSIX Permission bit mask
        private const int S_IFDIR = 16384;

        /// <summary>
        /// Gets whether this object is a directory
        /// </summary>
        public Boolean IsDirectory
        {
            get
            {
                return ((this.Permissions & S_IFDIR) == S_IFDIR);
            }
        }

...

 

 

I have also modified my copy of SftpClient.cs as follows:

    /// <summary>
    /// 
    /// </summary>
    public class SftpClient : BaseClient
    {

        /// <summary>
        /// Retrieves list of files (only) in remote directory.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>List of directory entries</returns>
        public IEnumerable<SftpFile> GetFiles(string path)
        {
            // Get a raw listing of files and directories
            IEnumerable<SftpFile> listDirectory = this.ListDirectory(path);

            // Filter the list to a list of files only
            var getFiles = (from sftpFile in listDirectory
                            where sftpFile.IsDirectory == false
                            select sftpFile);

            return getFiles;
        }

...

 

I chose the method name "GetFiles()" to mirror the System.IO.DirectoryInfo.GetFiles() method.

 

Ted.