chmod support

30 views
Skip to first unread message

Brian Rak

unread,
Jun 20, 2011, 12:17:11 PM6/20/11
to Python FTP server library - Discussion group
This patch adds support for the SITE CHMOD command. This is a bit
limited on windows, see http://docs.python.org/library/os.html#os.chmod
for details on that.

diff --git a/storage/pyftpdlib/ftpserver.py b/storage/pyftpdlib/
ftpserver.py
--- a/pyftpdlib/ftpserver.py
+++ b/pyftpdlib/ftpserver.py
@@ -186,6 +186,7 @@ proto_cmds = {
'RNTO' : dict(perm='f', auth=True, arg=True, help='Syntax:
RNTO <SP> file-name (file renaming (destination name)).'),
'SITE' : dict(perm=None, auth=False, arg=True, help='Syntax:
SITE <SP> site-command (execute the specified SITE command).'),
'SITE HELP' : dict(perm=None, auth=False, arg=None, help='Syntax:
SITE HELP [<SP> site-command] (show SITE command help).'),
+ 'SITE CHMOD' : dict(perm='w', auth=True, arg=True, help='Syntax:
SITE CHMOD permissions file-name.'),
'SIZE' : dict(perm='l', auth=True, arg=True, help='Syntax:
HELP <SP> file-name (get file size).'),
'STAT' : dict(perm='l', auth=False, arg=None, help='Syntax:
STAT [<SP> path name] (status information [list files]).'),
'STOR' : dict(perm='w', auth=True, arg=True, help='Syntax:
STOR <SP> file-name (store a file).'),
@@ -1347,6 +1348,9 @@ class AbstractedFS(object):

# --- Wrapper methods around os.* calls

+ def chmod(self, path, mode):
+ os.chmod(path, mode)
+
def chdir(self, path):
"""Change the current directory."""
# temporarily join the specified directory to see if we have
@@ -1972,6 +1976,9 @@ class FTPHandler(object, asynchat.async_chat):
self.log_cmd(cmd, arg, 550, msg)
return
arg = self.fs.ftp2fs(arg or self.fs.cwd)
+ elif cmd == 'SITE CHMOD':
+ perms = arg.split(" ",2)[0]
+ arg = self.fs.ftp2fs(arg.split(" ",2)[1] or
self.fs.cwd)
else: # LIST, NLST, MLSD, MLST
arg = self.fs.ftp2fs(arg or self.fs.cwd)

@@ -1993,7 +2000,12 @@ class FTPHandler(object, asynchat.async_chat):
return

# call the proper ftp_* method
- self.process_command(cmd, arg)
+ if cmd == 'SITE CHMOD':
+ # SITE CHMOD accepts multiple paramters, but
self.fs.validpath will fail if we try to pass them both in
+ # They are split out before the validation, then
combined again after
+ self.process_command(cmd, "%s %s" % (perms, arg))
+ else:
+ self.process_command(cmd, arg)

def process_command(self, cmd, *args, **kwargs):
"""Process command by calling the corresponding ftp_* class
@@ -3290,6 +3302,25 @@ class FTPHandler(object, asynchat.async_chat):

# --- support for deprecated cmds

+ def ftp_SITE_CHMOD(self,line):
+ if line:
+ args = line.split(" ",2)
+ perms = int(args[0],8)
+ filename = args[1]
+ if self.fs.lexists(filename):
+ try:
+ fd = self.run_as_current_user(self.fs.chmod,
filename, perms)
+ except IOError, err:
+ why = _strerror(err)
+ self.respond('550 %s.' % why)
+ return
+ self.respond('200 SITE CHMOD command successful')
+ else:
+ self.respond('550 No such file or directory.')
+ else:
+ raise ValueError('Invalid number of arguments')
+
+
# RFC-1123 requires that the server treat XCUP, XCWD, XMKD, XPWD
# and XRMD commands as synonyms for CDUP, CWD, MKD, LIST and RMD.
# Such commands are obsoleted but some ftp clients (e.g. Windows

Brian Rak

unread,
Jun 20, 2011, 3:05:40 PM6/20/11
to Python FTP server library - Discussion group
Heh, just noticed this is already available in the SVN version. I
definitely like that implementation a bit better then mine too.

On Jun 20, 12:17 pm, Brian Rak <d...@devicenull.org> wrote:
> This patch adds support for the SITE CHMOD command.  This is a bit
> limited on windows, seehttp://docs.python.org/library/os.html#os.chmod
Reply all
Reply to author
Forward
0 new messages