2 import errno, glob, grp, pwd, stat, tempfile, subprocess
6 from bup import git, metadata, vfs
7 from bup.helpers import clear_errors, detect_fakeroot, is_superuser, resolve_parent
8 from bup.xstat import utime, lutime
9 from buptest import no_lingering_errors, test_tempdir
10 import bup.helpers as helpers
14 bup_tmp = os.path.realpath('../../../t/tmp')
15 bup_path = top_dir + '/bup'
16 start_dir = os.getcwd()
21 cmd_str = ' '.join(cmd)
22 print >> sys.stderr, cmd_str
23 rc = subprocess.call(cmd)
25 print >> sys.stderr, 'terminated by signal', - rc
28 print >> sys.stderr, 'returned exit status', rc
31 print >> sys.stderr, 'subprocess call failed:', e
36 assert(sys.platform.startswith('linux'))
37 # Set up testfs with user_xattr, etc.
38 if subprocess.call(['modprobe', 'loop']) != 0:
40 subprocess.call(['umount', 'testfs'])
41 ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
42 ex('mke2fs', '-F', '-j', '-m', '0', 'testfs.img')
43 ex('rm', '-rf', 'testfs')
45 ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
46 # Hide, so that tests can't create risks.
47 os.chown('testfs', 0, 0)
48 os.chmod('testfs', 0o700)
53 subprocess.call(['umount', 'testfs'])
54 helpers.unlink('testfs.img')
58 def test_clean_up_archive_path():
59 with no_lingering_errors():
60 cleanup = metadata._clean_up_path_for_archive
61 WVPASSEQ(cleanup('foo'), 'foo')
62 WVPASSEQ(cleanup('/foo'), 'foo')
63 WVPASSEQ(cleanup('///foo'), 'foo')
64 WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
65 WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
66 WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
67 WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
68 WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
69 WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
70 WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
71 WVPASSEQ(cleanup('./foo/.'), 'foo')
72 WVPASSEQ(cleanup('./foo/..'), '.')
73 WVPASSEQ(cleanup('//./..//.../..//.'), '.')
74 WVPASSEQ(cleanup('//./..//..././/.'), '...')
75 WVPASSEQ(cleanup('/////.'), '.')
76 WVPASSEQ(cleanup('/../'), '.')
77 WVPASSEQ(cleanup(''), '.')
81 def test_risky_path():
82 with no_lingering_errors():
83 risky = metadata._risky_path
85 WVPASS(risky('///foo'))
86 WVPASS(risky('/../foo'))
87 WVPASS(risky('../foo'))
88 WVPASS(risky('foo/..'))
89 WVPASS(risky('foo/../'))
90 WVPASS(risky('foo/../bar'))
93 WVFAIL(risky('foo///'))
94 WVFAIL(risky('./foo'))
95 WVFAIL(risky('foo/.'))
96 WVFAIL(risky('./foo/.'))
97 WVFAIL(risky('foo/bar'))
98 WVFAIL(risky('foo/./bar'))
102 def test_clean_up_extract_path():
103 with no_lingering_errors():
104 cleanup = metadata._clean_up_extract_path
105 WVPASSEQ(cleanup('/foo'), 'foo')
106 WVPASSEQ(cleanup('///foo'), 'foo')
107 WVFAIL(cleanup('/../foo'))
108 WVFAIL(cleanup('../foo'))
109 WVFAIL(cleanup('foo/..'))
110 WVFAIL(cleanup('foo/../'))
111 WVFAIL(cleanup('foo/../bar'))
112 WVPASSEQ(cleanup('foo'), 'foo')
113 WVPASSEQ(cleanup('foo/'), 'foo/')
114 WVPASSEQ(cleanup('foo///'), 'foo///')
115 WVPASSEQ(cleanup('./foo'), './foo')
116 WVPASSEQ(cleanup('foo/.'), 'foo/.')
117 WVPASSEQ(cleanup('./foo/.'), './foo/.')
118 WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
119 WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
120 WVPASSEQ(cleanup('/'), '.')
121 WVPASSEQ(cleanup('./'), './')
122 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
123 WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
127 def test_metadata_method():
128 with no_lingering_errors(), test_tempdir('bup-tmetadata-') as tmpdir:
129 bup_dir = tmpdir + '/bup'
130 data_path = tmpdir + '/foo'
132 ex('touch', data_path + '/file')
133 ex('ln', '-s', 'file', data_path + '/symlink')
134 test_time1 = 13 * 1000000000
135 test_time2 = 42 * 1000000000
136 utime(data_path + '/file', (0, test_time1))
137 lutime(data_path + '/symlink', (0, 0))
138 utime(data_path, (0, test_time2))
139 ex(bup_path, '-d', bup_dir, 'init')
140 ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
141 ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
142 git.check_repo_or_die(bup_dir)
143 top = vfs.RefList(None)
144 n = top.lresolve('/test/latest' + resolve_parent(data_path))
146 WVPASS(m.mtime == test_time2)
147 WVPASS(len(n.subs()) == 2)
148 WVPASS(n.name == 'foo')
149 WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
151 if sub.name == 'file':
153 WVPASS(m.mtime == test_time1)
154 elif sub.name == 'symlink':
160 if helpers.saved_errors:
161 return str(helpers.saved_errors[0])
166 def test_from_path_error():
167 if is_superuser() or detect_fakeroot():
169 with no_lingering_errors(), test_tempdir('bup-tmetadata-') as tmpdir:
170 path = tmpdir + '/foo'
172 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
173 WVPASSEQ(m.path, path)
175 metadata.from_path(path, archive_path=path, save_symlinks=True)
176 if metadata.get_linux_file_attr:
177 print >> sys.stderr, 'saved_errors:', helpers.saved_errors
178 WVPASS(len(helpers.saved_errors) == 1)
179 errmsg = _first_err()
180 WVPASS(errmsg.startswith('read Linux attr'))
184 def _linux_attr_supported(path):
185 # Expects path to denote a regular file or a directory.
186 if not metadata.get_linux_file_attr:
189 metadata.get_linux_file_attr(path)
191 if e.errno in (errno.ENOTTY, errno.ENOSYS, errno.EOPNOTSUPP):
199 def test_apply_to_path_restricted_access():
200 if is_superuser() or detect_fakeroot():
202 if sys.platform.startswith('cygwin'):
203 return # chmod 000 isn't effective.
204 with no_lingering_errors(), test_tempdir('bup-tmetadata-') as tmpdir:
205 parent = tmpdir + '/foo'
206 path = parent + '/bar'
210 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
211 WVPASSEQ(m.path, path)
212 os.chmod(parent, 000)
213 m.apply_to_path(path)
214 print >> sys.stderr, 'saved_errors:', helpers.saved_errors
215 expected_errors = ['utime: ']
216 if m.linux_attr and _linux_attr_supported(tmpdir):
217 expected_errors.append('Linux chattr: ')
218 if metadata.xattr and m.linux_xattr:
219 expected_errors.append("xattr.set '")
220 WVPASS(len(helpers.saved_errors) == len(expected_errors))
221 for i in xrange(len(expected_errors)):
222 WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
227 def test_restore_over_existing_target():
228 with no_lingering_errors(), test_tempdir('bup-tmetadata-') as tmpdir:
229 path = tmpdir + '/foo'
231 dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
233 open(path, 'w').close()
234 file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
235 # Restore dir over file.
236 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
237 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
238 # Restore dir over dir.
239 WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
240 WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
241 # Restore file over dir.
242 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
243 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
244 # Restore file over file.
245 WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
246 WVPASS(stat.S_ISREG(os.stat(path).st_mode))
247 # Restore file over non-empty dir.
250 open(path + '/bar', 'w').close()
251 WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
252 # Restore dir over non-empty dir.
253 os.remove(path + '/bar')
254 os.mkdir(path + '/bar')
255 WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
258 from bup.metadata import posix1e
261 def POSIX1E_ACL_SUPPORT_IS_MISSING():
265 from bup.metadata import xattr
268 def test_handling_of_incorrect_existing_linux_xattrs():
269 if not is_superuser() or detect_fakeroot():
270 WVMSG('skipping test -- not superuser')
272 if not setup_testfs():
273 WVMSG('unable to load loop module; skipping dependent tests')
275 for f in glob.glob('testfs/*'):
278 open(path, 'w').close()
279 xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
280 m = metadata.from_path(path, archive_path=path, save_symlinks=True)
281 xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
282 m.apply_to_path(path, restore_numeric_ids=False)
283 WVPASSEQ(xattr.list(path), ['user.foo'])
284 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
285 xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
286 m.apply_to_path(path, restore_numeric_ids=False)
287 WVPASSEQ(xattr.list(path), ['user.foo'])
288 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
289 xattr.remove(path, 'foo', namespace=xattr.NS_USER)
290 m.apply_to_path(path, restore_numeric_ids=False)
291 WVPASSEQ(xattr.list(path), ['user.foo'])
292 WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')