~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

Merge doc updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
22
import os
23
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
24
    SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
 
25
import sys
25
26
 
26
27
from bzrlib.osutils import pathjoin
27
28
from bzrlib.trace import mutter
64
65
 
65
66
    def __init__(self, server, root, home=None):
66
67
        SFTPServerInterface.__init__(self, server)
 
68
        # All paths are actually relative to 'root'.
 
69
        # this is like implementing chroot().
67
70
        self.root = root
68
71
        if home is None:
69
 
            self.home = self.root
 
72
            self.home = ''
70
73
        else:
 
74
            assert home.startswith(self.root), \
 
75
                    "home must be a subdirectory of root (%s vs %s)" \
 
76
                    % (home, root)
71
77
            self.home = home[len(self.root):]
72
 
        if (len(self.home) > 0) and (self.home[0] == '/'):
 
78
        if self.home.startswith('/'):
73
79
            self.home = self.home[1:]
74
80
        server._test_case.log('sftpserver - new connection')
75
81
 
76
82
    def _realpath(self, path):
 
83
        # paths returned from self.canonicalize() always start with
 
84
        # a path separator. So if 'root' is just '/', this would cause
 
85
        # a double slash at the beginning '//home/dir'. 
 
86
        if self.root == '/':
 
87
            return self.canonicalize(path)
77
88
        return self.root + self.canonicalize(path)
78
89
 
79
 
    def canonicalize(self, path):
80
 
        if os.path.isabs(path):
81
 
            return os.path.normpath(path)
82
 
        else:
83
 
            return os.path.normpath('/' + os.path.join(self.home, path))
 
90
    if sys.platform == 'win32':
 
91
        def canonicalize(self, path):
 
92
            # Win32 sftp paths end up looking like
 
93
            #     sftp://host@foo/h:/foo/bar
 
94
            # which means absolute paths look like:
 
95
            #     /h:/foo/bar
 
96
            # and relative paths stay the same:
 
97
            #     foo/bar
 
98
            # win32 needs to use the Unicode APIs. so we require the 
 
99
            # paths to be utf8 (Linux just uses bytestreams)
 
100
            thispath = path.decode('utf8')
 
101
            if path.startswith('/'):
 
102
                # Abspath H:/foo/bar
 
103
                return os.path.normpath(thispath[1:])
 
104
            else:
 
105
                return os.path.normpath(os.path.join(self.home, thispath))
 
106
    else:
 
107
        def canonicalize(self, path):
 
108
            if os.path.isabs(path):
 
109
                return os.path.normpath(path)
 
110
            else:
 
111
                return os.path.normpath('/' + os.path.join(self.home, path))
84
112
 
85
113
    def chattr(self, path, attr):
86
114
        try:
93
121
        path = self._realpath(path)
94
122
        try:
95
123
            out = [ ]
96
 
            flist = os.listdir(path)
 
124
            # TODO: win32 incorrectly lists paths with non-ascii if path is not
 
125
            # unicode. However on Linux the server should only deal with
 
126
            # bytestreams and posix.listdir does the right thing 
 
127
            if sys.platform == 'win32':
 
128
                flist = [f.encode('utf8') for f in os.listdir(path)]
 
129
            else:
 
130
                flist = os.listdir(path)
97
131
            for fname in flist:
98
132
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
99
133
                attr.filename = fname
119
153
    def open(self, path, flags, attr):
120
154
        path = self._realpath(path)
121
155
        try:
122
 
            if hasattr(os, 'O_BINARY'):
123
 
                flags |= os.O_BINARY
 
156
            flags |= getattr(os, 'O_BINARY', 0)
124
157
            if getattr(attr, 'st_mode', None):
125
158
                fd = os.open(path, flags, attr.st_mode)
126
159
            else:
127
 
                fd = os.open(path, flags)
 
160
                # os.open() defaults to 0777 which is
 
161
                # an odd default mode for files
 
162
                fd = os.open(path, flags, 0666)
128
163
        except OSError, e:
129
164
            return SFTPServer.convert_errno(e.errno)
 
165
 
130
166
        if (flags & os.O_CREAT) and (attr is not None):
131
167
            attr._flags &= ~attr.FLAG_PERMISSIONS
132
168
            SFTPServer.set_file_attr(path, attr)
139
175
            fstr = 'rb'
140
176
        try:
141
177
            f = os.fdopen(fd, fstr)
142
 
        except OSError, e:
 
178
        except (IOError, OSError), e:
143
179
            return SFTPServer.convert_errno(e.errno)
144
180
        fobj = StubSFTPHandle()
145
181
        fobj.filename = path