]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tindex.py
Fix a bug where marginally old files couldn't be stored in the index
[bup.git] / lib / bup / t / tindex.py
1 import os
2 import time
3 from bup import index
4 from bup.helpers import *
5 import bup.xstat as xstat
6 from wvtest import *
7
8 @wvtest
9 def index_basic():
10     cd = os.path.realpath('../../../t')
11     WVPASS(cd)
12     sd = os.path.realpath(cd + '/sampledata')
13     WVPASSEQ(index.realpath(cd + '/sampledata'), cd + '/sampledata')
14     WVPASSEQ(os.path.realpath(cd + '/sampledata/x'), sd + '/x')
15     WVPASSEQ(os.path.realpath(cd + '/sampledata/etc'), os.path.realpath('/etc'))
16     WVPASSEQ(index.realpath(cd + '/sampledata/etc'), sd + '/etc')
17
18
19 @wvtest
20 def index_writer():
21     unlink('index.tmp')
22     ds = xstat.stat('.')
23     fs = xstat.stat('tindex.py')
24     w = index.Writer('index.tmp')
25     w.add('/var/tmp/sporky', fs)
26     w.add('/etc/passwd', fs)
27     w.add('/etc/', ds)
28     w.add('/', ds)
29     w.close()
30
31
32 def dump(m):
33     for e in list(m):
34         print '%s%s %s' % (e.is_valid() and ' ' or 'M',
35                            e.is_fake() and 'F' or ' ',
36                            e.name)
37
38 def fake_validate(*l):
39     for i in l:
40         for e in i:
41             e.validate(0100644, index.FAKE_SHA)
42             e.repack()
43
44 def eget(l, ename):
45     for e in l:
46         if e.name == ename:
47             return e
48
49 @wvtest
50 def index_negative_timestamps():
51     # Makes 'foo' exist
52     f = file('foo', 'wb')
53     f.close()
54
55     # Dec 31, 1969
56     os.utime("foo", (-86400, -86400))
57     e = index.BlankNewEntry("foo")
58     e.from_stat(xstat.stat("foo"), time.time())
59     assert len(e.packed())
60     WVPASS()
61
62     # Jun 10, 1893
63     os.utime("foo", (-0x90000000, -0x90000000))
64     e = index.BlankNewEntry("foo")
65     e.from_stat(xstat.stat("foo"), time.time())
66     assert len(e.packed())
67     WVPASS()
68
69
70 @wvtest
71 def index_dirty():
72     unlink('index.tmp')
73     unlink('index2.tmp')
74     ds = xstat.stat('.')
75     fs = xstat.stat('tindex.py')
76     
77     w1 = index.Writer('index.tmp')
78     w1.add('/a/b/x', fs)
79     w1.add('/a/b/c', fs)
80     w1.add('/a/b/', ds)
81     w1.add('/a/', ds)
82     #w1.close()
83     WVPASS()
84
85     w2 = index.Writer('index2.tmp')
86     w2.add('/a/b/n/2', fs)
87     #w2.close()
88     WVPASS()
89
90     w3 = index.Writer('index3.tmp')
91     w3.add('/a/c/n/3', fs)
92     #w3.close()
93     WVPASS()
94
95     r1 = w1.new_reader()
96     r2 = w2.new_reader()
97     r3 = w3.new_reader()
98     WVPASS()
99
100     r1all = [e.name for e in r1]
101     WVPASSEQ(r1all,
102              ['/a/b/x', '/a/b/c', '/a/b/', '/a/', '/'])
103     r2all = [e.name for e in r2]
104     WVPASSEQ(r2all,
105              ['/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
106     r3all = [e.name for e in r3]
107     WVPASSEQ(r3all,
108              ['/a/c/n/3', '/a/c/n/', '/a/c/', '/a/', '/'])
109     all = [e.name for e in index.merge(r2, r1, r3)]
110     WVPASSEQ(all,
111              ['/a/c/n/3', '/a/c/n/', '/a/c/',
112               '/a/b/x', '/a/b/n/2', '/a/b/n/', '/a/b/c',
113               '/a/b/', '/a/', '/'])
114     fake_validate(r1)
115     dump(r1)
116
117     print [hex(e.flags) for e in r1]
118     WVPASSEQ([e.name for e in r1 if e.is_valid()], r1all)
119     WVPASSEQ([e.name for e in r1 if not e.is_valid()], [])
120     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
121              ['/a/c/n/3', '/a/c/n/', '/a/c/',
122               '/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
123
124     expect_invalid = ['/'] + r2all + r3all
125     expect_real = (set(r1all) - set(r2all) - set(r3all)) \
126                     | set(['/a/b/n/2', '/a/c/n/3'])
127     dump(index.merge(r2, r1, r3))
128     for e in index.merge(r2, r1, r3):
129         print e.name, hex(e.flags), e.ctime
130         eiv = e.name in expect_invalid
131         er  = e.name in expect_real
132         WVPASSEQ(eiv, not e.is_valid())
133         WVPASSEQ(er, e.is_real())
134     fake_validate(r2, r3)
135     dump(index.merge(r2, r1, r3))
136     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()], [])
137     
138     e = eget(index.merge(r2, r1, r3), '/a/b/c')
139     e.invalidate()
140     e.repack()
141     dump(index.merge(r2, r1, r3))
142     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
143              ['/a/b/c', '/a/b/', '/a/', '/'])