~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

[merge] update from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
 
60
60
 
61
61
class StubSFTPServer (SFTPServerInterface):
62
 
    def __init__(self, server, root):
 
62
    def __init__(self, server, root, home=None):
63
63
        SFTPServerInterface.__init__(self, server)
64
64
        self.root = root
65
 
        
 
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
 
66
73
    def _realpath(self, path):
67
74
        return self.root + self.canonicalize(path)
68
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
 
69
82
    def chattr(self, path, attr):
70
83
        try:
71
84
            SFTPServer.set_file_attr(path, attr)
105
118
        try:
106
119
            if hasattr(os, 'O_BINARY'):
107
120
                flags |= os.O_BINARY
108
 
            if (attr is not None) and hasattr(attr, 'st_mode'):
 
121
            if getattr(attr, 'st_mode', None):
109
122
                fd = os.open(path, flags, attr.st_mode)
110
123
            else:
111
124
                fd = os.open(path, flags)
151
164
    def mkdir(self, path, attr):
152
165
        path = self._realpath(path)
153
166
        try:
154
 
            if attr is not None and hasattr(attr, 'st_mode'):
 
167
            # Using getattr() in case st_mode is None or 0
 
168
            # both evaluate to False
 
169
            if getattr(attr, 'st_mode', None):
155
170
                os.mkdir(path, attr.st_mode)
156
171
            else:
157
172
                os.mkdir(path)