~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-01-02 22:37:32 UTC
  • mfrom: (1185.50.33 bzr-jam-integration)
  • Revision ID: robertc@robertcollins.net-20060102223732-d5221b37ff0f7888
Merge in John Meinels integration branch.

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