Rev 3509: Fix ftp transport so that it handles the 'mode' parameter when provided. in file:///v/home/vila/src/bzr/experimental/more-ftp/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Jun 27 18:26:10 BST 2008


At file:///v/home/vila/src/bzr/experimental/more-ftp/

------------------------------------------------------------
revno: 3509
revision-id: v.ladeuil+lp at free.fr-20080627172606-zn1z2ekknau3qa3c
parent: pqm at pqm.ubuntu.com-20080624011313-cq85foncf9mozmen
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: fix-chmod
timestamp: Fri 2008-06-27 19:26:06 +0200
message:
  Fix ftp transport so that it handles the 'mode' parameter when provided.
  
  * ftp.py:
  (FtpTransport.put_file, FtpTransport.mkdir,
  FtpTransport._try_append): Use _setmode.
  (FtpTransport._setmode): Fix implementation (parameters were
  reversed and mode not converted).
  
  * ftp_server.py:
  (test_filesystem): New filesystem implementing chmod.
  (test_authorizer.authorize): Use our filesystem when authorized.
  (ftp_channel.cmd_site): Implement SITE CHOWN.
modified:
  bzrlib/tests/ftp_server.py     ftpserver.py-20071019102346-61jbvdkrr70igauv-1
  bzrlib/transport/ftp.py        ftp.py-20051116161804-58dc9506548c2a53
-------------- next part --------------
=== modified file 'bzrlib/tests/ftp_server.py'
--- a/bzrlib/tests/ftp_server.py	2008-04-24 07:22:53 +0000
+++ b/bzrlib/tests/ftp_server.py	2008-06-27 17:26:06 +0000
@@ -37,6 +37,15 @@
     )
 
 
+class test_filesystem(medusa.filesys.os_filesystem):
+    """A custom filesystem wrapper to add missing functionalities."""
+
+    def chmod(self, path, mode):
+        p = self.normalize (self.path_module.join (self.wd, path))
+        return os.chmod(self.translate(p), mode)
+
+
+
 class test_authorizer(object):
     """A custom Authorizer object for running the test suite.
 
@@ -64,7 +73,7 @@
             and password != self.secured_password):
             return 0, 'Password invalid.', None
         else:
-            return 1, 'OK.', medusa.filesys.os_filesystem(self.root)
+            return 1, 'OK.', test_filesystem(self.root)
 
 
 class ftp_channel(medusa.ftp_server.ftp_channel):
@@ -153,6 +162,25 @@
             except:
                 self.respond ('550 error creating directory.')
 
+    def cmd_site(self, line):
+        command, args = line[1].split(' ', 1)
+        if command.lower() == 'chmod':
+            try:
+                mode, path = args.split()
+                mode = int(mode, 8)
+            except ValueError:
+                self.command_not_understood(' '.join(line))
+                return
+            try:
+                # Yes path and mode are reversed
+                self.filesystem.chmod(path, mode)
+                self.respond('200 SITE CHMOD command successful')
+            except AttributeError:
+                # The chmod method is not available in read-only
+                self.command_not_authorized(' '.join(line))
+        else:
+            self.command_not_understood(' '.join(line))
+
 
 class ftp_server(medusa.ftp_server.ftp_server):
     """Customize the behavior of the Medusa ftp_server.

=== modified file 'bzrlib/transport/ftp.py'
--- a/bzrlib/transport/ftp.py	2008-05-20 02:25:14 +0000
+++ b/bzrlib/transport/ftp.py	2008-06-27 17:26:06 +0000
@@ -310,6 +310,7 @@
             try:
                 f.storbinary('STOR '+tmp_abspath, fp)
                 self._rename_and_overwrite(tmp_abspath, abspath, f)
+                self._setmode(relpath, mode)
                 if bytes is not None:
                     return len(bytes)
                 else:
@@ -351,6 +352,7 @@
             mutter("FTP mkd: %s", abspath)
             f = self._get_FTP()
             f.mkd(abspath)
+            self._setmode(relpath, mode)
         except ftplib.error_perm, e:
             self._translate_perm_error(e, abspath,
                 unknown_exc=errors.FileExists)
@@ -411,8 +413,7 @@
             conn = ftp.transfercmd(cmd)
             conn.sendall(text)
             conn.close()
-            if mode:
-                self._setmode(relpath, mode)
+            self._setmode(relpath, mode)
             ftp.getresp()
         except ftplib.error_perm, e:
             self._translate_perm_error(e, abspath, extra='error appending',
@@ -432,16 +433,18 @@
         Only set permissions if the FTP server supports the 'SITE CHMOD'
         extension.
         """
-        try:
-            mutter("FTP site chmod: setting permissions to %s on %s",
-                str(mode), self._remote_path(relpath))
-            ftp = self._get_FTP()
-            cmd = "SITE CHMOD %s %s" % (self._remote_path(relpath), str(mode))
-            ftp.sendcmd(cmd)
-        except ftplib.error_perm, e:
-            # Command probably not available on this server
-            warning("FTP Could not set permissions to %s on %s. %s",
-                    str(mode), self._remote_path(relpath), str(e))
+        if mode:
+            try:
+                mutter("FTP site chmod: setting permissions to %s on %s",
+                    str(mode), self._remote_path(relpath))
+                ftp = self._get_FTP()
+                cmd = "SITE CHMOD %s %s" % (oct(mode),
+                                            self._remote_path(relpath))
+                ftp.sendcmd(cmd)
+            except ftplib.error_perm, e:
+                # Command probably not available on this server
+                warning("FTP Could not set permissions to %s on %s. %s",
+                        str(mode), self._remote_path(relpath), str(e))
 
     # TODO: jam 20060516 I believe ftp allows you to tell an ftp server
     #       to copy something to another machine. And you may be able



More information about the bazaar-commits mailing list