]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
65604f80053f3240c84179551ffe9d2d3fac9f86
[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, hlinkdb
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     hlinks = hlinkdb.HLinkDB(indexfile + '.hlink')
59
60     hashgen = None
61     if opt.fake_valid:
62         def hashgen(name):
63             return (GIT_MODE_FILE, index.FAKE_SHA)
64
65     total = 0
66     bup_dir = os.path.abspath(git.repo())
67     for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev,
68                                                  bup_dir=bup_dir,
69                                                  excluded_paths=excluded_paths):
70         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
71             sys.stdout.write('%s\n' % path)
72             sys.stdout.flush()
73             qprogress('Indexing: %d\r' % total)
74         elif not (total % 128):
75             qprogress('Indexing: %d\r' % total)
76         total += 1
77         while rig.cur and rig.cur.name > path:  # deleted paths
78             if rig.cur.exists():
79                 rig.cur.set_deleted()
80                 rig.cur.repack()
81                 if rig.cur.nlink > 1 and not stat.S_ISDIR(rig.cur.mode):
82                     hlinks.del_path(rig.cur.name)
83             rig.next()
84         if rig.cur and rig.cur.name == path:    # paths that already existed
85             if not stat.S_ISDIR(rig.cur.mode) and rig.cur.nlink > 1:
86                 hlinks.del_path(rig.cur.name)
87             if not stat.S_ISDIR(pst.st_mode) and pst.st_nlink > 1:
88                 hlinks.add_path(path, pst.st_dev, pst.st_ino)
89             rig.cur.from_stat(pst, tstart)
90             if not (rig.cur.flags & index.IX_HASHVALID):
91                 if hashgen:
92                     (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
93                     rig.cur.flags |= index.IX_HASHVALID
94             if opt.fake_invalid:
95                 rig.cur.invalidate()
96             rig.cur.repack()
97             rig.next()
98         else:  # new paths
99             wi.add(path, pst, hashgen = hashgen)
100             if not stat.S_ISDIR(pst.st_mode) and pst.st_nlink > 1:
101                 hlinks.add_path(path, pst.st_dev, pst.st_ino)
102
103     progress('Indexing: %d, done.\n' % total)
104     
105     hlinks.prepare_save()
106
107     if ri.exists():
108         ri.save()
109         wi.flush()
110         if wi.count:
111             wr = wi.new_reader()
112             if opt.check:
113                 log('check: before merging: oldfile\n')
114                 check_index(ri)
115                 log('check: before merging: newfile\n')
116                 check_index(wr)
117             mi = index.Writer(indexfile, tmax)
118
119             for e in index.merge(ri, wr):
120                 # FIXME: shouldn't we remove deleted entries eventually?  When?
121                 mi.add_ixentry(e)
122
123             ri.close()
124             mi.close()
125             wr.close()
126         wi.abort()
127     else:
128         wi.close()
129
130     hlinks.commit_save()
131
132
133 optspec = """
134 bup index <-p|m|s|u> [options...] <filenames...>
135 --
136  Modes:
137 p,print    print the index entries for the given names (also works with -u)
138 m,modified print only added/deleted/modified files (implies -p)
139 s,status   print each filename with a status char (A/M/D) (implies -p)
140 u,update   recursively update the index entries for the given file/dir names (default if no mode is specified)
141 check      carefully check index file integrity
142  Options:
143 H,hash     print the hash for each object next to its name
144 l,long     print more information about each file
145 fake-valid mark all index entries as up-to-date even if they aren't
146 fake-invalid mark all index entries as invalid
147 f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
148 exclude=   a path to exclude from the backup (can be used more than once)
149 exclude-from= a file that contains exclude paths (can be used more than once)
150 v,verbose  increase log output (can be used more than once)
151 x,xdev,one-file-system  don't cross filesystem boundaries
152 """
153 o = options.Options(optspec)
154 (opt, flags, extra) = o.parse(sys.argv[1:])
155
156 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
157     opt.update = 1
158 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
159     o.fatal('--fake-{in,}valid are meaningless without -u')
160 if opt.fake_valid and opt.fake_invalid:
161     o.fatal('--fake-valid is incompatible with --fake-invalid')
162
163 # FIXME: remove this once we account for timestamp races, i.e. index;
164 # touch new-file; index.  It's possible for this to happen quickly
165 # enough that new-file ends up with the same timestamp as the first
166 # index, and then bup will ignore it.
167 tick_start = time.time()
168 time.sleep(1 - (tick_start - int(tick_start)))
169
170 git.check_repo_or_die()
171 indexfile = opt.indexfile or git.repo('bupindex')
172
173 handle_ctrl_c()
174
175 if opt.check:
176     log('check: starting initial check.\n')
177     check_index(index.Reader(indexfile))
178
179 excluded_paths = drecurse.parse_excludes(flags)
180
181 paths = index.reduce_paths(extra)
182
183 if opt.update:
184     if not extra:
185         o.fatal('update mode (-u) requested but no paths given')
186     for (rp,path) in paths:
187         update_index(rp, excluded_paths)
188
189 if opt['print'] or opt.status or opt.modified:
190     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
191         if (opt.modified 
192             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
193             continue
194         line = ''
195         if opt.status:
196             if ent.is_deleted():
197                 line += 'D '
198             elif not ent.is_valid():
199                 if ent.sha == index.EMPTY_SHA:
200                     line += 'A '
201                 else:
202                     line += 'M '
203             else:
204                 line += '  '
205         if opt.hash:
206             line += ent.sha.encode('hex') + ' '
207         if opt.long:
208             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
209         print line + (name or './')
210
211 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
212     log('check: starting final check.\n')
213     check_index(index.Reader(indexfile))
214
215 if saved_errors:
216     log('WARNING: %d errors encountered.\n' % len(saved_errors))
217     sys.exit(1)