-
Notifications
You must be signed in to change notification settings - Fork 75
Missing SFTP file attribute constants #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the interest.
33024 == 100400 in octal (read only).
Will need to see code that reproduces the read only file case to help further. |
Can you show output of: In shell:
In python:
Please show complete python code, including login code. |
The setup for the sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))
session = Session()
session.handshake(sock)
session.userauth_password(user, pwd)
sftp = session.sftp_init() The output from the ls command (login in with same user, pwd as the code) sftp> ls -lh test.txt
-rw-rw-rw- ? 0 0 0B Oct 13 14:41 test.txt |
What is the owner of the file and what is the login user?
The login user needs to have owner or group status in order to change permissions. Exception is raised otherwise. |
The owner of the file should be Entire code: def chmod(sftp, path, permissions):
stat = sftp.stat(path)
print("current permission is", oct(stat.permissions))
stat.permissions = LIBSSH2_SFTP_S_IRWXU
print("setting permission to be", oct(stat.permissions))
try:
if sftp.setstat(path, stat) == 0:
stat = sftp.stat(path)
print("success, new permission is", oct(stat.permissions))
else:
print("failed to set stat")
except Exception:
raise
host = "example.com"
user = "togtja"
pwd = "123password"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))
session = Session()
session.handshake(sock)
session.userauth_password(user, pwd)
sftp = session.sftp_init()
chmod(sftp, "test.txt", None) output:
I can create a file using |
According to the shell, it is not. Does There is a test that creates a file locally and changes permissions with ssh2-python. Will need to show some code to reproduce an issue to be able to help further. |
Hmm it seems that the sftp server only accepts permissions of "444" and "666", so it's not an issue with (ssh2-python/linssh2). chmod(sftp, "test.txt", 0o444)
chmod(sftp, "test.txt", 0o666) The first chmod outputs:
However it fails on the second one
If I do the same in the console: sftp> ls -l
-rw-rw-rw- 1 user group 0 Oct 13 12:41 test.txt
sftp> chmod 444 test.txt
Changing mode on test.txt
sftp> ls -l
-r--r--r-- 1 user group 0 Oct 13 12:41 test.txt
sftp> chmod 666 test.txt
Changing mode on test.txt
sftp> ls -l
-rw-rw-rw- 1 user group 0 Oct 13 12:41 test.txt Here I expect it to behave the same. And I am able to do it using CURL (with libssh2 1.9.0) |
To be able to help with this please show: Complete code that reproduces the issue. Will also need to show login user, user owning file and python code using appropriate masks from Please also show result of |
Alot of this is similar from before, such as login user, the same login user should be the owner of the file, but all files on the file server, including those not made by my login user, are all owned by def chmod(sftp, path, permissions):
perm = 0
if permissions & 0o400:
perm |= LIBSSH2_SFTP_S_IRUSR
if permissions & 0o200:
perm |= LIBSSH2_SFTP_S_IWUSR
if permissions & 0o100:
perm |= LIBSSH2_SFTP_S_IXUSR
if permissions & 0o040:
perm |= LIBSSH2_SFTP_S_IRGRP
if permissions & 0o020:
perm |= LIBSSH2_SFTP_S_IWGRP
if permissions & 0o010:
perm |= LIBSSH2_SFTP_S_IXGRP
if permissions & 0o004:
perm |= LIBSSH2_SFTP_S_IROTH
if permissions & 0o002:
perm |= LIBSSH2_SFTP_S_IWOTH
if permissions & 0o001:
perm |= LIBSSH2_SFTP_S_IXOTH
stat = sftp.stat(path)
print("current permission is", oct(stat.permissions))
stat.permissions = perm
print("setting permission to be", oct(stat.permissions))
try:
if sftp.setstat(path, stat) == 0:
stat = sftp.stat(path)
print("success, new permission is", oct(stat.permissions))
else:
print("failed to set stat")
except SFTPProtocolError as e:
print("error", e)
raise
host = "example.com"
user = "togtja"
pwd = "123password"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))
session = Session()
session.handshake(sock)
session.userauth_password(user, pwd)
sftp = session.sftp_init()
chmod(sftp, "test.txt", 0o444)
chmod(sftp, "test.txt", 0o666)
Before and after doing:
AFTER:
For your sake I also did it with pure
BEFORE:
AFTER:
If trying to go back to 666, with a file with only read access stat = sftp.stat("test.txt")
stat.permissions = LIBSSH2_SFTP_S_IRUSR | LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IROTH | LIBSSH2_SFTP_S_IWUSR | LIBSSH2_SFTP_S_IWGRP | LIBSSH2_SFTP_S_IWOTH
sftp.setstat("test.txt", stat) this happens:
For both these example, the same code to create the |
Thanks for the code to reproduce. So it looks like I assume this is also the case when setting size, time et al - #103 will probably also be resolved by setting correct attribute. Eg:
This fails. Setting flag before setstat sets permissions correctly.
Having to use attribute flags is not documented anywhere from what I can see and some permission flags work without SFTP attribute flags are not implemented in the current release - will have to wait for next release to use them. |
Thanks for raising. A complete example for setstat would be good to have if anyone wants to make a PR. There does not seem to be much libssh2 documentation on SFTP attributes. |
Bug reports
I tried to set permission on a file on a SFTP server. I use normally use FileZilla to interact with the server, but I needed a fast way to change a lot of files, file permission. So I thought I would write a python script.
I noticed 2 things, if I only have read access to the file, I could not change the permission, giving me a
ssh2.exceptions.SFTPProtocolError
, I can using the same login, change the permission in FileZilla.The second and more important issue, was that I could not change the permission at all using the code described below:
Steps to reproduce:
The out put is this when trying to change the permission of a file from '666' to '755'
Expected behaviour:
I expect that I can change permission on files with only read-access, as long as the authenticated user, has privileges todo so
I expected that the new file permission would be '755' after calling
setstat
with changed permissionsActual behaviour:
When I only read access has been set on a file it throws a
ssh2.exceptions.SFTPProtocolError
When trying to setstat it does not change, even though the
setstat()
returned a 0Additional info:
I am uncertain of which libssh2 I am running, as I just pip installed
ssh2-python
and it was up and running, but if I were to hazard a guess, I'd say 1.9.0The text was updated successfully, but these errors were encountered: