~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

Merge from integration.

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:
57
66
    def _realpath(self, path):
58
67
        return self.root + self.canonicalize(path)
59
68
 
 
69
    def chattr(self, path, attr):
 
70
        try:
 
71
            SFTPServer.set_file_attr(path, attr)
 
72
        except OSError, e:
 
73
            return SFTPServer.convert_errno(e.errno)
 
74
        return SFTP_OK
 
75
 
60
76
    def list_folder(self, path):
61
77
        path = self._realpath(path)
62
78
        try:
63
79
            out = [ ]
64
80
            flist = os.listdir(path)
65
81
            for fname in flist:
66
 
                attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
 
82
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
67
83
                attr.filename = fname
68
84
                out.append(attr)
69
85
            return out
87
103
    def open(self, path, flags, attr):
88
104
        path = self._realpath(path)
89
105
        try:
90
 
            fd = os.open(path, flags)
 
106
            if hasattr(os, 'O_BINARY'):
 
107
                flags |= os.O_BINARY
 
108
            if (attr is not None) and hasattr(attr, 'st_mode'):
 
109
                fd = os.open(path, flags, attr.st_mode)
 
110
            else:
 
111
                fd = os.open(path, flags)
91
112
        except OSError, e:
92
113
            return SFTPServer.convert_errno(e.errno)
93
114
        if (flags & os.O_CREAT) and (attr is not None):
 
115
            attr._flags &= ~attr.FLAG_PERMISSIONS
94
116
            SFTPServer.set_file_attr(path, attr)
95
117
        if flags & os.O_WRONLY:
96
 
            fstr = 'w'
 
118
            fstr = 'wb'
97
119
        elif flags & os.O_RDWR:
98
 
            fstr = 'r+'
 
120
            fstr = 'rb+'
99
121
        else:
100
122
            # O_RDONLY (== 0)
101
 
            fstr = 'r'
 
123
            fstr = 'rb'
102
124
        try:
103
125
            f = os.fdopen(fd, fstr)
104
126
        except OSError, e:
129
151
    def mkdir(self, path, attr):
130
152
        path = self._realpath(path)
131
153
        try:
132
 
            os.mkdir(path)
 
154
            if attr is not None and hasattr(attr, 'st_mode'):
 
155
                os.mkdir(path, attr.st_mode)
 
156
            else:
 
157
                os.mkdir(path)
133
158
            if attr is not None:
 
159
                attr._flags &= ~attr.FLAG_PERMISSIONS
134
160
                SFTPServer.set_file_attr(path, attr)
135
161
        except OSError, e:
136
162
            return SFTPServer.convert_errno(e.errno)