62
62
:param dir_mode: The mode for all directories
63
63
:param include_base: If false, only check the subdirectories
65
assert os.path.isdir(base)
65
t = test.get_transport()
67
check_mode(test, base, dir_mode)
67
test.assertTransportMode(t, base, dir_mode)
68
68
for root, dirs, files in os.walk(base):
70
p = os.path.join(root, d)
71
check_mode(test, p, dir_mode)
70
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [d]])
71
test.assertTransportMode(t, p, dir_mode)
73
73
p = os.path.join(root, f)
74
check_mode(test, p, file_mode)
77
def assertEqualMode(test, mode, mode_test):
78
test.assertEqual(mode, mode_test,
79
'mode mismatch %o != %o' % (mode, mode_test))
82
class TestPermissions(TestCaseInTempDir):
74
p = '/'.join([urlutils.quote(x) for x in root.split('/\\') + [f]])
75
test.assertTransportMode(t, p, file_mode)
78
class TestPermissions(TestCaseWithTransport):
84
80
def test_new_files(self):
85
81
if sys.platform == 'win32':
86
82
raise TestSkipped('chmod has no effect on win32')
88
b = Branch.initialize(u'.')
90
open('a', 'wb').write('foo\n')
84
t = self.make_branch_and_tree('.')
86
with open('a', 'wb') as f: f.write('foo\n')
87
# ensure check_mode_r works with capital-letter file-ids like TREE_ROOT
94
# Delete them because we are modifying the filesystem underneath them
96
91
chmod_r('.bzr', 0644, 0755)
97
92
check_mode_r(self, '.bzr', 0644, 0755)
101
assertEqualMode(self, 0755, b.control_files._dir_mode)
102
assertEqualMode(self, 0644, b.control_files._file_mode)
94
# although we are modifying the filesystem
95
# underneath the objects, they are not locked, and thus it must
96
# be safe for most operations. But here we want to observe a
97
# mode change in the control bits, which current do not refresh
98
# when a new lock is taken out.
99
t = WorkingTree.open('.')
101
self.assertEqualMode(0755, b.control_files._dir_mode)
102
self.assertEqualMode(0644, b.control_files._file_mode)
103
self.assertEqualMode(0755, b.bzrdir._get_dir_mode())
104
self.assertEqualMode(0644, b.bzrdir._get_file_mode())
104
106
# Modifying a file shouldn't break the permissions
105
open('a', 'wb').write('foo2\n')
107
with open('a', 'wb') as f: f.write('foo2\n')
107
109
# The mode should be maintained after commit
108
110
check_mode_r(self, '.bzr', 0644, 0755)
110
112
# Adding a new file should maintain the permissions
111
open('b', 'wb').write('new b\n')
113
with open('b', 'wb') as f: f.write('new b\n')
113
115
t.commit('new b')
114
116
check_mode_r(self, '.bzr', 0644, 0755)
117
118
# Recursively update the modes of all files
118
119
chmod_r('.bzr', 0664, 0775)
119
120
check_mode_r(self, '.bzr', 0664, 0775)
122
assertEqualMode(self, 0775, b.control_files._dir_mode)
123
assertEqualMode(self, 0664, b.control_files._file_mode)
121
t = WorkingTree.open('.')
123
self.assertEqualMode(0775, b.control_files._dir_mode)
124
self.assertEqualMode(0664, b.control_files._file_mode)
125
self.assertEqualMode(0775, b.bzrdir._get_dir_mode())
126
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
125
open('a', 'wb').write('foo3\n')
128
with open('a', 'wb') as f: f.write('foo3\n')
127
130
check_mode_r(self, '.bzr', 0664, 0775)
129
open('c', 'wb').write('new c\n')
132
with open('c', 'wb') as f: f.write('new c\n')
131
134
t.commit('new c')
132
135
check_mode_r(self, '.bzr', 0664, 0775)
137
def test_new_files_group_sticky_bit(self):
138
if sys.platform == 'win32':
139
raise TestSkipped('chmod has no effect on win32')
140
elif sys.platform == 'darwin' or 'freebsd' in sys.platform:
141
# FreeBSD-based platforms create temp dirs with the 'wheel' group,
142
# which users are not likely to be in, and this prevents us from
143
# setting the sgid bit
144
os.chown(self.test_dir, os.getuid(), os.getgid())
146
t = self.make_branch_and_tree('.')
134
149
# Test the group sticky bit
136
150
# Recursively update the modes of all files
137
151
chmod_r('.bzr', 0664, 02775)
138
152
check_mode_r(self, '.bzr', 0664, 02775)
141
assertEqualMode(self, 02775, b.control_files._dir_mode)
142
assertEqualMode(self, 0664, b.control_files._file_mode)
153
t = WorkingTree.open('.')
155
self.assertEqualMode(02775, b.control_files._dir_mode)
156
self.assertEqualMode(0664, b.control_files._file_mode)
157
self.assertEqualMode(02775, b.bzrdir._get_dir_mode())
158
self.assertEqualMode(0664, b.bzrdir._get_file_mode())
144
open('a', 'wb').write('foo4\n')
160
with open('a', 'wb') as f: f.write('foo4\n')
146
162
check_mode_r(self, '.bzr', 0664, 02775)
148
open('d', 'wb').write('new d\n')
164
with open('d', 'wb') as f: f.write('new d\n')
150
166
t.commit('new d')
151
167
check_mode_r(self, '.bzr', 0664, 02775)
153
def test_disable_set_mode(self):
154
# TODO: jam 20051215 Ultimately, this test should probably test that
155
# extra chmod calls aren't being made
156
import bzrlib.lockable_files
158
b = Branch.initialize(u'.')
159
self.assertNotEqual(None, b.control_files._dir_mode)
160
self.assertNotEqual(None, b.control_files._file_mode)
162
bzrlib.lockable_files.LockableFiles._set_dir_mode = False
163
b = Branch.open(u'.')
164
self.assertEqual(None, b.control_files._dir_mode)
165
self.assertNotEqual(None, b.control_files._file_mode)
167
bzrlib.lockable_files.LockableFiles._set_file_mode = False
168
b = Branch.open(u'.')
169
self.assertEqual(None, b.control_files._dir_mode)
170
self.assertEqual(None, b.control_files._file_mode)
172
bzrlib.lockable_files.LockableFiles._set_dir_mode = True
173
b = Branch.open(u'.')
174
self.assertNotEqual(None, b.control_files._dir_mode)
175
self.assertEqual(None, b.control_files._file_mode)
177
bzrlib.lockable_files.LockableFiles._set_file_mode = True
178
b = Branch.open(u'.')
179
self.assertNotEqual(None, b.control_files._dir_mode)
180
self.assertNotEqual(None, b.control_files._file_mode)
182
bzrlib.lockable_files.LockableFiles._set_dir_mode = True
183
bzrlib.lockable_files.LockableFiles._set_file_mode = True
185
def test_new_branch(self):
186
if sys.platform == 'win32':
187
raise TestSkipped('chmod has no effect on win32')
190
mode = stat.S_IMODE(os.stat('a').st_mode)
191
b = Branch.initialize('a')
192
assertEqualMode(self, mode, b.control_files._dir_mode)
193
assertEqualMode(self, mode & ~07111, b.control_files._file_mode)
197
b = Branch.initialize('b')
198
assertEqualMode(self, 02777, b.control_files._dir_mode)
199
assertEqualMode(self, 00666, b.control_files._file_mode)
200
check_mode_r(self, 'b/.bzr', 00666, 02777)
204
b = Branch.initialize('c')
205
assertEqualMode(self, 02750, b.control_files._dir_mode)
206
assertEqualMode(self, 00640, b.control_files._file_mode)
207
check_mode_r(self, 'c/.bzr', 00640, 02750)
211
b = Branch.initialize('d')
212
assertEqualMode(self, 0700, b.control_files._dir_mode)
213
assertEqualMode(self, 0600, b.control_files._file_mode)
214
check_mode_r(self, 'd/.bzr', 00600, 0700)
217
170
class TestSftpPermissions(TestCaseWithSFTPServer):
222
175
# Though it would be nice to test that SFTP to a server
223
176
# which does support chmod has the right effect
225
from bzrlib.transport.sftp import SFTPTransport
227
# We don't actually use it directly, we just want to
228
# keep the connection open, since StubSFTPServer only
229
# allows 1 connection
231
_transport = SFTPTransport(self._sftp_url)
178
# bodge around for stubsftpserver not letting use connect
180
_t = self.get_transport()
233
182
os.mkdir('local')
234
b_local = Branch.initialize(u'local')
235
t_local = b_local.working_tree()
236
open('local/a', 'wb').write('foo\n')
183
t_local = self.make_branch_and_tree('local')
184
b_local = t_local.branch
185
with open('local/a', 'wb') as f: f.write('foo\n')
238
187
t_local.commit('foo')
240
189
# Delete them because we are modifying the filesystem underneath them
242
190
chmod_r('local/.bzr', 0644, 0755)
243
191
check_mode_r(self, 'local/.bzr', 0644, 0755)
245
b_local = Branch.open(u'local')
246
t_local = b_local.working_tree()
247
assertEqualMode(self, 0755, b_local.control_files._dir_mode)
248
assertEqualMode(self, 0644, b_local.control_files._file_mode)
193
t = WorkingTree.open('local')
195
self.assertEqualMode(0755, b_local.control_files._dir_mode)
196
self.assertEqualMode(0644, b_local.control_files._file_mode)
197
self.assertEqualMode(0755, b_local.bzrdir._get_dir_mode())
198
self.assertEqualMode(0644, b_local.bzrdir._get_file_mode())
251
# Why does self._sftp_url end with a slash????
252
sftp_url = self._sftp_url + 'sftp'
253
b_sftp = Branch.initialize(sftp_url)
201
sftp_url = self.get_url('sftp')
202
b_sftp = ControlDir.create_branch_and_repo(sftp_url)
255
204
b_sftp.pull(b_local)