~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

  • Committer: Aaron Bentley
  • Date: 2006-04-07 22:46:52 UTC
  • mfrom: (1645 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1727.
  • Revision ID: aaron.bentley@utoronto.ca-20060407224652-4925bc3735b926f8
Merged latest bzr.dev

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):
49
51
    def chattr(self, attr):
50
52
        # python doesn't have equivalents to fchown or fchmod, so we have to
51
53
        # use the stored filename
 
54
        mutter('Changing permissions on %s to %s', self.filename, attr)
52
55
        try:
53
56
            SFTPServer.set_file_attr(self.filename, attr)
54
57
        except OSError, e:
56
59
 
57
60
 
58
61
class StubSFTPServer (SFTPServerInterface):
59
 
    def __init__(self, server, root):
 
62
    def __init__(self, server, root, home=None):
60
63
        SFTPServerInterface.__init__(self, server)
61
64
        self.root = root
62
 
        
 
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
 
63
73
    def _realpath(self, path):
64
74
        return self.root + self.canonicalize(path)
65
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
 
66
89
    def list_folder(self, path):
67
90
        path = self._realpath(path)
68
91
        try:
69
92
            out = [ ]
70
93
            flist = os.listdir(path)
71
94
            for fname in flist:
72
 
                attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
 
95
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
73
96
                attr.filename = fname
74
97
                out.append(attr)
75
98
            return out
93
116
    def open(self, path, flags, attr):
94
117
        path = self._realpath(path)
95
118
        try:
96
 
            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)
97
125
        except OSError, e:
98
126
            return SFTPServer.convert_errno(e.errno)
99
127
        if (flags & os.O_CREAT) and (attr is not None):
 
128
            attr._flags &= ~attr.FLAG_PERMISSIONS
100
129
            SFTPServer.set_file_attr(path, attr)
101
130
        if flags & os.O_WRONLY:
102
 
            fstr = 'w'
 
131
            fstr = 'wb'
103
132
        elif flags & os.O_RDWR:
104
 
            fstr = 'r+'
 
133
            fstr = 'rb+'
105
134
        else:
106
135
            # O_RDONLY (== 0)
107
 
            fstr = 'r'
 
136
            fstr = 'rb'
108
137
        try:
109
138
            f = os.fdopen(fd, fstr)
110
139
        except OSError, e:
135
164
    def mkdir(self, path, attr):
136
165
        path = self._realpath(path)
137
166
        try:
138
 
            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)
139
173
            if attr is not None:
 
174
                attr._flags &= ~attr.FLAG_PERMISSIONS
140
175
                SFTPServer.set_file_attr(path, attr)
141
176
        except OSError, e:
142
177
            return SFTPServer.convert_errno(e.errno)