Ticket #5713: conchunixflag.patch

File conchunixflag.patch, 4.7 KB (added by magmatt, 4 years ago)

conch patch to factor out flag-making code

  • twisted/conch/interfaces.py

    diff --git a/twisted/conch/interfaces.py b/twisted/conch/interfaces.py
    index d42811a..73d931e 100644
    a b class ISFTPServer(Interface): 
    124124        @param filename: a string representing the file to open.
    125125
    126126        @param flags: an integer of the flags to open the file with, ORed together.
    127         The flags and their values are listed at the bottom of this file.
     127        The flags and their values are listed at the bottom of
     128        L{twisted.conch.ssh.filetransfer}, prefixed with C{FXF_}.
    128129
    129130        @param attrs: a list of attributes to open the file with.  It is a
    130131        dictionary, consisting of 0 or more keys.  The possible keys are::
  • new file twisted/conch/test/test_unix.py

    diff --git a/twisted/conch/test/test_unix.py b/twisted/conch/test/test_unix.py
    new file mode 100644
    index 0000000..dc12992
    - +  
     1# Copyright (c) Twisted Matrix Laboratories.
     2# See LICENSE for details.
     3
     4"""
     5Tests for L{twisted.conch.unix}.
     6"""
     7
     8import os
     9
     10from twisted.conch.unix import mkOpenFlags
     11from twisted.conch.ssh.filetransfer import (FXF_READ, FXF_WRITE, FXF_APPEND,
     12                                            FXF_CREAT, FXF_TRUNC, FXF_EXCL)
     13from twisted.trial.unittest import TestCase
     14
     15
     16
     17class mkOpenFlagsTest(TestCase):
     18
     19
     20    def _testFlag(self, flag, os_flag):
     21        """
     22        Test that the given C{sftp_flag} is converted to C{os_flag} by
     23        L{mkOpenFlags}.
     24       
     25        Also test that when ORed with FXF_APPEND, FXF_CREAT, FXF_TRUNC or
     26        FXF_EXCL, the corresponding OR takes place on C{os_flag}.
     27        """
     28        result = mkOpenFlags(flag)
     29        self.assertEqual(result, os_flag)
     30       
     31        result = mkOpenFlags(flag | FXF_APPEND)
     32        self.assertEqual(result, os_flag | os.O_APPEND)
     33       
     34        result = mkOpenFlags(flag | FXF_CREAT)
     35        self.assertEqual(result, os_flag | os.O_CREAT)
     36       
     37        result = mkOpenFlags(flag | FXF_TRUNC)
     38        self.assertEqual(result, os_flag | os.O_TRUNC)
     39
     40        result = mkOpenFlags(flag | FXF_EXCL)
     41        self.assertEqual(result, os_flag | os.O_EXCL)
     42
     43
     44    def test_FXF_READ(self):
     45        """
     46        C{FXF_READ} should turn into C{os.O_RDONLY}
     47        """
     48        self._testFlag(FXF_READ, os.O_RDONLY)
     49
     50
     51    def test_FXF_WRITE(self):
     52        """
     53        C{FXF_WRITE} should turn into C{os.O_WRONLY}
     54        """
     55        self._testFlag(FXF_WRITE, os.O_WRONLY)
     56
     57
     58    def test_FXF_READ_or_FXF_WRITE(self):
     59        """
     60        C{FXF_READ | FXF_WRITE} should turn into C{os.O_RDWR}
     61        """
     62        self._testFlag(FXF_READ | FXF_WRITE, os.O_RDWR)
     63       
  • twisted/conch/unix.py

    diff --git a/twisted/conch/unix.py b/twisted/conch/unix.py
    index 3a44be0..c5b624f 100644
    a b class SFTPServerForUnixConchUser: 
    378378    def extendedRequest(self, extName, extData):
    379379        raise NotImplementedError
    380380
     381
     382
     383def mkOpenFlags(flags):
     384    """
     385    Convert SFTP-specific flags to flags suitable for use with C{os.open}.
     386   
     387    @param flags: An ORing of the C{FXF_}-prefixed flags in
     388        L{twisted.conch.ssh.filetransfer}.
     389   
     390    @rtype: int
     391    @return: An integer suitable for use as the flags argument to C{os.open}
     392    """
     393    ret = os.O_RDONLY
     394    if flags & FXF_WRITE == FXF_WRITE:
     395        ret = os.O_WRONLY
     396    if flags & FXF_WRITE == FXF_WRITE and flags & FXF_READ == FXF_READ:
     397        ret = os.O_RDWR
     398
     399    mapping = {
     400        FXF_APPEND: os.O_APPEND,
     401        FXF_CREAT: os.O_CREAT,
     402        FXF_TRUNC: os.O_TRUNC,
     403        FXF_EXCL: os.O_EXCL,
     404    }
     405    for FXF_flag, os_flag in mapping.items():
     406        if flags & FXF_flag == FXF_flag:
     407            ret |= os_flag
     408    return ret
     409
     410
     411
    381412class UnixSFTPFile:
    382413
    383414    interface.implements(ISFTPFile)
    384415
    385416    def __init__(self, server, filename, flags, attrs):
    386417        self.server = server
    387         openFlags = 0
    388         if flags & FXF_READ == FXF_READ and flags & FXF_WRITE == 0:
    389             openFlags = os.O_RDONLY
    390         if flags & FXF_WRITE == FXF_WRITE and flags & FXF_READ == 0:
    391             openFlags = os.O_WRONLY
    392         if flags & FXF_WRITE == FXF_WRITE and flags & FXF_READ == FXF_READ:
    393             openFlags = os.O_RDWR
    394         if flags & FXF_APPEND == FXF_APPEND:
    395             openFlags |= os.O_APPEND
    396         if flags & FXF_CREAT == FXF_CREAT:
    397             openFlags |= os.O_CREAT
    398         if flags & FXF_TRUNC == FXF_TRUNC:
    399             openFlags |= os.O_TRUNC
    400         if flags & FXF_EXCL == FXF_EXCL:
    401             openFlags |= os.O_EXCL
     418        openFlags = mkOpenFlags(flags)
    402419        if "permissions" in attrs:
    403420            mode = attrs["permissions"]
    404421            del attrs["permissions"]