3

I am converting my python program from paramiko to ssh2. I have succeeded in authenticating and I can get a directory listing. Where I am stuck is as I process through the directory listing how do I recognize whether the "file" is a directory or a file. I see the attributes but of those I can only see atime being something I will want to use (to know how old the file is). Once I have done the opendir and readdir (and so have a listing of files) how do I recognize whether each is a file or a directory?

When I do the readdir I am returned:

  • Length of filename
  • filename
  • attributes
    • atime
    • filesize
    • flags
    • gid
    • mtime
    • permissions
    • uid
efultz
  • 1,135
  • 2
  • 11
  • 26

2 Answers2

1

Haven't used ssh2-python myself but I would say to check the contents of flags. According to the library's documentation (as suggested by @NullPointerException) the possible values are:

LIBSSH2_SFTP_S_IFMT Type of file mask

LIBSSH2_SFTP_S_IFIFO Named pipe (fifo)

LIBSSH2_SFTP_S_IFCHR Character special (character device)

LIBSSH2_SFTP_S_IFDIR Directory

LIBSSH2_SFTP_S_IFBLK Block special (block device)

LIBSSH2_SFTP_S_IFREG Regular file

LIBSSH2_SFTP_S_IFLNK Symbolic link

LIBSSH2_SFTP_S_IFSOCK Socket

I would say flags is a bit field and you have to check if certain flag is "on" or not with a bitwise operator, for example, to check it it's a directory:

flags & LIBSSH2_SFTP_S_IFDIR == LIBSSH2_SFTP_S_IFDIR
luis.parravicini
  • 1,214
  • 11
  • 19
  • Apparently it's called `LIBSSH2_SFTP_S_IFDIR` in ssh2-python: https://ssh2-python.readthedocs.io/en/latest/sftp.html – NullUserException Sep 30 '19 at 19:25
  • I did find references to those but I can't seem to figure out how to translate the flags attribute (an example being 15) in order to figure out what I need. – efultz Sep 30 '19 at 19:53
  • I have the above code but it is not recognizing the entry is a directory loc1 is a tuple consisting of: `from ssh2.sftp import LIBSSH2_SFTP_S_IFDIR from ssh2.session import Session if loc1[2].flags & LIBSSH2_SFTP_S_IFDIR == LIBSSH2_SFTP_S_IFDIR: continue` `0 = {int} 7 1 = {str} '.config' 2 = {SFTPAttributes} atime = {long} filesize = {long} 4096 flags = {long} 15 gid = {long} mtime = {long} permissions = {long} uid = {long}` – efultz Oct 01 '19 at 13:12
  • sorry about the poor formatting - I tried but can't seem to get it to format cleanly – efultz Oct 01 '19 at 13:13
  • Haven't used ssh2 myself, so I'm just guessing. Why don't you update your question adding the code you are using? – luis.parravicini Oct 01 '19 at 13:17
-1
 for i in sftp.opendir(remote_path).readdir():
     print(i[1],i[2].permissions,LIBSSH2_SFTP_S_IFDIR,
            i[2].permissions&LIBSSH2_SFTP_S_IFDIR)
吕万胜
  • 1
  • 1
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '23 at 05:15