Ticket #2875: 2875-ftp-access-2.diff

File 2875-ftp-access-2.diff, 4.2 KB (added by adiroiban, 4 years ago)
  • twisted/test/test_ftp.py

     
    25882588        d = self.shell.access(('foo',))
    25892589        return self.assertFailure(d, ftp.FileNotFoundError)
    25902590
     2591    def test_accessNotAllowed(self):
     2592        """
     2593        access should fail on a resource for which we can't list its content.
     2594        """
     2595        path = 'foo'
     2596        self.createDirectory(path)
     2597        self.root.child(path).chmod(0000)
     2598        d = self.shell.access((path,))
    25912599
     2600        def revert_permissions(result_or_failure):
     2601            """Folder permissions are reverted so that any clean function
     2602            will be allowed to remove the folder.
     2603            """
     2604            self.root.child(path).chmod(0700)
     2605            self.root.child(path).remove()
     2606            return result_or_failure
     2607
     2608        d.addBoth(revert_permissions)
     2609        return self.assertFailure(d, ftp.PermissionDeniedError)
     2610
     2611    if not os.name == 'posix':
     2612        test_accessNotAllowed.skip = "test_accessNotAllowed not supported"
     2613
     2614    def test_accessNoExecutePermissions(self):
     2615        """
     2616        access should fail on a resource for which we can't list its content.
     2617        """
     2618        path = 'foo'
     2619        self.createDirectory(path)
     2620        self.root.child(path).chmod(0600)
     2621        d = self.shell.access((path,))
     2622
     2623        def revert_permissions(result_or_failure):
     2624            """Folder permissions are reverted so that any cleanup will be
     2625            allowed to remove the folder.
     2626            """
     2627            self.root.child(path).chmod(0700)
     2628            self.root.child(path).remove()
     2629            return result_or_failure
     2630
     2631        d.addBoth(revert_permissions)
     2632        return self.assertFailure(d, ftp.PermissionDeniedError)
     2633
     2634    if not os.name == 'posix':
     2635        test_accessNoExecutePermissions.skip = (
     2636            "test_accessNoExecutePermissions not supported")
     2637
    25922638    def test_openForReading(self):
    25932639        """
    25942640        Check that openForReading returns an object providing C{ftp.IReadFile}.
  • twisted/protocols/ftp.py

     
    16721672        """
    16731673        return defer.fail(PermissionDeniedError("STOR not allowed"))
    16741674
     1675    def access(self, path):
     1676        """
     1677        See L{IFTPShell.access}.
    16751678
    1676     def access(self, path):
     1679        If path doesn't exists, it returns L{FileNotFoundError}.
     1680        On Unix systems, it checks path access by trying to change current
     1681        working directory.
     1682        On non-Unix systems, it checks path access by issuing a folder
     1683        listing command.
     1684        """
    16771685        p = self._path(path)
    16781686        if not p.exists():
    16791687            # Again, win32 doesn't report a sane error after, so let's fail
    1680             # early if we can
     1688            # early if we can.
    16811689            return defer.fail(FileNotFoundError(path))
    1682         # For now, just see if we can os.listdir() it
    1683         try:
    1684             p.listdir()
    1685         except (IOError, OSError), e:
    1686             return errnoToFailure(e.errno, path)
    1687         except:
    1688             return defer.fail()
    1689         else:
     1690
     1691        def check_access_chdir():
     1692            '''Check access by changing working folder.'''
     1693            current_path = os.getcwd()
     1694            try:
     1695                os.chdir(p.path)
     1696            except (IOError, OSError), e:
     1697                return errnoToFailure(e.errno, path)
     1698            except:
     1699                return defer.fail()
     1700            finally:
     1701                os.chdir(current_path)
     1702
    16901703            return defer.succeed(None)
    16911704
     1705        def check_access_listdir():
     1706            '''Check access by listing folder.'''
     1707            try:
     1708                os.listdir(p.path)
     1709            except (IOError, OSError), e:
     1710                return errnoToFailure(e.errno, path)
     1711            except:
     1712                return defer.fail()
    16921713
     1714            return defer.succeed(None)
     1715
     1716        if os.name == 'posix':
     1717            return check_access_chdir()
     1718        else:
     1719            return check_access_listdir()
     1720
    16931721    def stat(self, path, keys=()):
    16941722        p = self._path(path)
    16951723        if p.isdir():