]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
Merge branch 'meta'
[bup.git] / cmd / index-cmd.py
1 #!/usr/bin/env python
2 import sys, stat, time, os
3 from bup import options, git, index, drecurse
4 from bup.helpers import *
5 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
6
7 class IterHelper:
8     def __init__(self, l):
9         self.i = iter(l)
10         self.cur = None
11         self.next()
12
13     def next(self):
14         try:
15             self.cur = self.i.next()
16         except StopIteration:
17             self.cur = None
18         return self.cur
19
20
21 def check_index(reader):
22     try:
23         log('check: checking forward iteration...\n')
24         e = None
25         d = {}
26         for e in reader.forward_iter():
27             if e.children_n:
28                 if opt.verbose:
29                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
30                                             e.name))
31                 assert(e.children_ofs)
32                 assert(e.name.endswith('/'))
33                 assert(not d.get(e.children_ofs))
34                 d[e.children_ofs] = 1
35             if e.flags & index.IX_HASHVALID:
36                 assert(e.sha != index.EMPTY_SHA)
37                 assert(e.gitmode)
38         assert(not e or e.name == '/')  # last entry is *always* /
39         log('check: checking normal iteration...\n')
40         last = None
41         for e in reader:
42             if last:
43                 assert(last > e.name)
44             last = e.name
45     except:
46         log('index error! at %r\n' % e)
47         raise
48     log('check: passed.\n')
49
50
51 def update_index(top, excluded_paths):
52     tmax = time.time() - 1
53     ri = index.Reader(indexfile)
54     wi = index.Writer(indexfile, tmax)
55     rig = IterHelper(ri.iter(name=top))
56     tstart = int(time.time())
57
58     hashgen = None
59     if opt.fake_valid:
60         def hashgen(name):
61             return (GIT_MODE_FILE, index.FAKE_SHA)
62
63     total = 0
64     bup_dir = os.path.abspath(git.repo())
65     for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev,
66                                                  bup_dir=bup_dir,
67                                                  excluded_paths=excluded_paths):
68         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
69             sys.stdout.write('%s\n' % path)
70             sys.stdout.flush()
71             qprogress('Indexing: %d\r' % total)
72         elif not (total % 128):
73             qprogress('Indexing: %d\r' % total)
74         total += 1
75         while rig.cur and rig.cur.name > path:  # deleted paths
76             if rig.cur.exists():
77                 rig.cur.set_deleted()
78                 rig.cur.repack()
79             rig.next()
80         if rig.cur and rig.cur.name == path:    # paths that already existed
81             if pst:
82                 rig.cur.from_stat(pst, tstart)
83             if not (rig.cur.flags & index.IX_HASHVALID):
84                 if hashgen:
85                     (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
86                     rig.cur.flags |= index.IX_HASHVALID
87             if opt.fake_invalid:
88                 rig.cur.invalidate()
89             rig.cur.repack()
90             rig.next()
91         else:  # new paths
92             wi.add(path, pst, hashgen = hashgen)
93     progress('Indexing: %d, done.\n' % total)
94     
95     if ri.exists():
96         ri.save()
97         wi.flush()
98         if wi.count:
99             wr = wi.new_reader()
100             if opt.check:
101                 log('check: before merging: oldfile\n')
102                 check_index(ri)
103                 log('check: before merging: newfile\n')
104                 check_index(wr)
105             mi = index.Writer(indexfile, tmax)
106
107             for e in index.merge(ri, wr):
108                 # FIXME: shouldn't we remove deleted entries eventually?  When?
109                 mi.add_ixentry(e)
110
111             ri.close()
112             mi.close()
113             wr.close()
114         wi.abort()
115     else:
116         wi.close()
117
118
119 optspec = """
120 bup index <-p|m|s|u> [options...] <filenames...>
121 --
122  Modes:
123 p,print    print the index entries for the given names (also works with -u)
124 m,modified print only added/deleted/modified files (implies -p)
125 s,status   print each filename with a status char (A/M/D) (implies -p)
126 u,update   recursively update the index entries for the given file/dir names (default if no mode is specified)
127 check      carefully check index file integrity
128  Options:
129 H,hash     print the hash for each object next to its name
130 l,long     print more information about each file
131 fake-valid mark all index entries as up-to-date even if they aren't
132 fake-invalid mark all index entries as invalid
133 f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
134 exclude=   a path to exclude from the backup (can be used more than once)
135 exclude-from= a file that contains exclude paths (can be used more than once)
136 v,verbose  increase log output (can be used more than once)
137 x,xdev,one-file-system  don't cross filesystem boundaries
138 """
139 o = options.Options(optspec)
140 (opt, flags, extra) = o.parse(sys.argv[1:])
141
142 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
143     opt.update = 1
144 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
145     o.fatal('--fake-{in,}valid are meaningless without -u')
146 if opt.fake_valid and opt.fake_invalid:
147     o.fatal('--fake-valid is incompatible with --fake-invalid')
148
149 git.check_repo_or_die()
150 indexfile = opt.indexfile or git.repo('bupindex')
151
152 handle_ctrl_c()
153
154 if opt.check:
155     log('check: starting initial check.\n')
156     check_index(index.Reader(indexfile))
157
158 excluded_paths = drecurse.parse_excludes(flags)
159
160 paths = index.reduce_paths(extra)
161
162 if opt.update:
163     if not extra:
164         o.fatal('update mode (-u) requested but no paths given')
165     for (rp,path) in paths:
166         update_index(rp, excluded_paths)
167
168 if opt['print'] or opt.status or opt.modified:
169     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
170         if (opt.modified 
171             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
172             continue
173         line = ''
174         if opt.status:
175             if ent.is_deleted():
176                 line += 'D '
177             elif not ent.is_valid():
178                 if ent.sha == index.EMPTY_SHA:
179                     line += 'A '
180                 else:
181                     line += 'M '
182             else:
183                 line += '  '
184         if opt.hash:
185             line += ent.sha.encode('hex') + ' '
186         if opt.long:
187             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
188         print line + (name or './')
189
190 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
191     log('check: starting final check.\n')
192     check_index(index.Reader(indexfile))
193
194 if saved_errors:
195     log('WARNING: %d errors encountered.\n' % len(saved_errors))
196     sys.exit(1)