~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

  • Committer: Robert Collins
  • Date: 2006-02-15 08:11:37 UTC
  • mto: (1534.1.24 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060215081137-4c27377517e96dd1
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import os
23
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
24
    SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
 
25
from bzrlib.osutils import pathjoin
 
26
from bzrlib.trace import mutter
25
27
 
26
28
 
27
29
class StubServer (ServerInterface):
 
30
    def __init__(self, test_case):
 
31
        ServerInterface.__init__(self)
 
32
        self._test_case = test_case
 
33
 
28
34
    def check_auth_password(self, username, password):
29
35
        # all are allowed
 
36
        self._test_case.log('sftpserver - authorizing: %s' % (username,))
30
37
        return AUTH_SUCCESSFUL
31
38
 
32
39
    def check_channel_request(self, kind, chanid):
 
40
        self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid))
33
41
        return OPEN_SUCCEEDED
34
42
 
35
43
 
43
51
    def chattr(self, attr):
44
52
        # python doesn't have equivalents to fchown or fchmod, so we have to
45
53
        # use the stored filename
 
54
        mutter('Changing permissions on %s to %s', self.filename, attr)
46
55
        try:
47
56
            SFTPServer.set_file_attr(self.filename, attr)
48
57
        except OSError, e:
50
59
 
51
60
 
52
61
class StubSFTPServer (SFTPServerInterface):
53
 
    def __init__(self, server, root):
 
62
    def __init__(self, server, root, home=None):
54
63
        SFTPServerInterface.__init__(self, server)
55
64
        self.root = root
56
 
        
 
65
        if home is None:
 
66
            self.home = self.root
 
67
        else:
 
68
            self.home = home[len(self.root):]
 
69
        if (len(self.home) > 0) and (self.home[0] == '/'):
 
70
            self.home = self.home[1:]
 
71
        server._test_case.log('sftpserver - new connection')
 
72
 
57
73
    def _realpath(self, path):
58
74
        return self.root + self.canonicalize(path)
59
75
 
 
76
    def canonicalize(self, path):
 
77
        if os.path.isabs(path):
 
78
            return os.path.normpath(path)
 
79
        else:
 
80
            return os.path.normpath('/' + os.path.join(self.home, path))
 
81
 
 
82
    def chattr(self, path, attr):
 
83
        try:
 
84
            SFTPServer.set_file_attr(path, attr)
 
85
        except OSError, e:
 
86
            return SFTPServer.convert_errno(e.errno)
 
87
        return SFTP_OK
 
88
 
60
89
    def list_folder(self, path):
61
90
        path = self._realpath(path)
62
91
        try:
63
92
            out = [ ]
64
93
            flist = os.listdir(path)
65
94
            for fname in flist:
66
 
                attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
 
95
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
67
96
                attr.filename = fname
68
97
                out.append(attr)
69
98
            return out
87
116
    def open(self, path, flags, attr):
88
117
        path = self._realpath(path)
89
118
        try:
90
 
            fd = os.open(path, flags)
 
119
            if hasattr(os, 'O_BINARY'):
 
120
                flags |= os.O_BINARY
 
121
            if getattr(attr, 'st_mode', None):
 
122
                fd = os.open(path, flags, attr.st_mode)
 
123
            else:
 
124
                fd = os.open(path, flags)
91
125
        except OSError, e:
92
126
            return SFTPServer.convert_errno(e.errno)
93
127
        if (flags & os.O_CREAT) and (attr is not None):
 
128
            attr._flags &= ~attr.FLAG_PERMISSIONS
94
129
            SFTPServer.set_file_attr(path, attr)
95
130
        if flags & os.O_WRONLY:
96
 
            fstr = 'w'
 
131
            fstr = 'wb'
97
132
        elif flags & os.O_RDWR:
98
 
            fstr = 'r+'
 
133
            fstr = 'rb+'
99
134
        else:
100
135
            # O_RDONLY (== 0)
101
 
            fstr = 'r'
 
136
            fstr = 'rb'
102
137
        try:
103
138
            f = os.fdopen(fd, fstr)
104
139
        except OSError, e:
129
164
    def mkdir(self, path, attr):
130
165
        path = self._realpath(path)
131
166
        try:
132
 
            os.mkdir(path)
 
167
            # Using getattr() in case st_mode is None or 0
 
168
            # both evaluate to False
 
169
            if getattr(attr, 'st_mode', None):
 
170
                os.mkdir(path, attr.st_mode)
 
171
            else:
 
172
                os.mkdir(path)
133
173
            if attr is not None:
 
174
                attr._flags &= ~attr.FLAG_PERMISSIONS
134
175
                SFTPServer.set_file_attr(path, attr)
135
176
        except OSError, e:
136
177
            return SFTPServer.convert_errno(e.errno)