]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
git.list_refs: change arg from refnames to patterns
[bup.git] / cmd / index-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 import sys, stat, time, os, errno, re
9
10 from bup import metadata, options, git, index, drecurse, hlinkdb
11 from bup.drecurse import recursive_dirlist
12 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
13 from bup.helpers import (add_error, handle_ctrl_c, log, parse_excludes, parse_rx_excludes,
14                          progress, qprogress, saved_errors)
15
16
17 class IterHelper:
18     def __init__(self, l):
19         self.i = iter(l)
20         self.cur = None
21         self.next()
22
23     def next(self):
24         try:
25             self.cur = self.i.next()
26         except StopIteration:
27             self.cur = None
28         return self.cur
29
30
31 def check_index(reader):
32     try:
33         log('check: checking forward iteration...\n')
34         e = None
35         d = {}
36         for e in reader.forward_iter():
37             if e.children_n:
38                 if opt.verbose:
39                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
40                                             e.name))
41                 assert(e.children_ofs)
42                 assert(e.name.endswith('/'))
43                 assert(not d.get(e.children_ofs))
44                 d[e.children_ofs] = 1
45             if e.flags & index.IX_HASHVALID:
46                 assert(e.sha != index.EMPTY_SHA)
47                 assert(e.gitmode)
48         assert(not e or e.name == '/')  # last entry is *always* /
49         log('check: checking normal iteration...\n')
50         last = None
51         for e in reader:
52             if last:
53                 assert(last > e.name)
54             last = e.name
55     except:
56         log('index error! at %r\n' % e)
57         raise
58     log('check: passed.\n')
59
60
61 def clear_index(indexfile):
62     indexfiles = [indexfile, indexfile + '.meta', indexfile + '.hlink']
63     for indexfile in indexfiles:
64         path = git.repo(indexfile)
65         try:
66             os.remove(path)
67             if opt.verbose:
68                 log('clear: removed %s\n' % path)
69         except OSError as e:
70             if e.errno != errno.ENOENT:
71                 raise
72
73
74 def update_index(top, excluded_paths, exclude_rxs, xdev_exceptions):
75     # tmax and start must be epoch nanoseconds.
76     tmax = (time.time() - 1) * 10**9
77     ri = index.Reader(indexfile)
78     msw = index.MetaStoreWriter(indexfile + '.meta')
79     wi = index.Writer(indexfile, msw, tmax)
80     rig = IterHelper(ri.iter(name=top))
81     tstart = int(time.time()) * 10**9
82
83     hlinks = hlinkdb.HLinkDB(indexfile + '.hlink')
84
85     fake_hash = None
86     if opt.fake_valid:
87         def fake_hash(name):
88             return (GIT_MODE_FILE, index.FAKE_SHA)
89
90     total = 0
91     bup_dir = os.path.abspath(git.repo())
92     index_start = time.time()
93     for path, pst in recursive_dirlist([top],
94                                        xdev=opt.xdev,
95                                        bup_dir=bup_dir,
96                                        excluded_paths=excluded_paths,
97                                        exclude_rxs=exclude_rxs,
98                                        xdev_exceptions=xdev_exceptions):
99         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
100             sys.stdout.write('%s\n' % path)
101             sys.stdout.flush()
102             elapsed = time.time() - index_start
103             paths_per_sec = total / elapsed if elapsed else 0
104             qprogress('Indexing: %d (%d paths/s)\r' % (total, paths_per_sec))
105         elif not (total % 128):
106             elapsed = time.time() - index_start
107             paths_per_sec = total / elapsed if elapsed else 0
108             qprogress('Indexing: %d (%d paths/s)\r' % (total, paths_per_sec))
109         total += 1
110
111         while rig.cur and rig.cur.name > path:  # deleted paths
112             if rig.cur.exists():
113                 rig.cur.set_deleted()
114                 rig.cur.repack()
115                 if rig.cur.nlink > 1 and not stat.S_ISDIR(rig.cur.mode):
116                     hlinks.del_path(rig.cur.name)
117             rig.next()
118
119         if rig.cur and rig.cur.name == path:    # paths that already existed
120             need_repack = False
121             if(rig.cur.stale(pst, tstart, check_device=opt.check_device)):
122                 try:
123                     meta = metadata.from_path(path, statinfo=pst)
124                 except (OSError, IOError) as e:
125                     add_error(e)
126                     rig.next()
127                     continue
128                 if not stat.S_ISDIR(rig.cur.mode) and rig.cur.nlink > 1:
129                     hlinks.del_path(rig.cur.name)
130                 if not stat.S_ISDIR(pst.st_mode) and pst.st_nlink > 1:
131                     hlinks.add_path(path, pst.st_dev, pst.st_ino)
132                 # Clear these so they don't bloat the store -- they're
133                 # already in the index (since they vary a lot and they're
134                 # fixed length).  If you've noticed "tmax", you might
135                 # wonder why it's OK to do this, since that code may
136                 # adjust (mangle) the index mtime and ctime -- producing
137                 # fake values which must not end up in a .bupm.  However,
138                 # it looks like that shouldn't be possible:  (1) When
139                 # "save" validates the index entry, it always reads the
140                 # metadata from the filesytem. (2) Metadata is only
141                 # read/used from the index if hashvalid is true. (3)
142                 # "faked" entries will be stale(), and so we'll invalidate
143                 # them below.
144                 meta.ctime = meta.mtime = meta.atime = 0
145                 meta_ofs = msw.store(meta)
146                 rig.cur.update_from_stat(pst, meta_ofs)
147                 rig.cur.invalidate()
148                 need_repack = True
149             if not (rig.cur.flags & index.IX_HASHVALID):
150                 if fake_hash:
151                     rig.cur.gitmode, rig.cur.sha = fake_hash(path)
152                     rig.cur.flags |= index.IX_HASHVALID
153                     need_repack = True
154             if opt.fake_invalid:
155                 rig.cur.invalidate()
156                 need_repack = True
157             if need_repack:
158                 rig.cur.repack()
159             rig.next()
160         else:  # new paths
161             try:
162                 meta = metadata.from_path(path, statinfo=pst)
163             except (OSError, IOError) as e:
164                 add_error(e)
165                 continue
166             # See same assignment to 0, above, for rationale.
167             meta.atime = meta.mtime = meta.ctime = 0
168             meta_ofs = msw.store(meta)
169             wi.add(path, pst, meta_ofs, hashgen=fake_hash)
170             if not stat.S_ISDIR(pst.st_mode) and pst.st_nlink > 1:
171                 hlinks.add_path(path, pst.st_dev, pst.st_ino)
172
173     elapsed = time.time() - index_start
174     paths_per_sec = total / elapsed if elapsed else 0
175     progress('Indexing: %d, done (%d paths/s).\n' % (total, paths_per_sec))
176
177     hlinks.prepare_save()
178
179     if ri.exists():
180         ri.save()
181         wi.flush()
182         if wi.count:
183             wr = wi.new_reader()
184             if opt.check:
185                 log('check: before merging: oldfile\n')
186                 check_index(ri)
187                 log('check: before merging: newfile\n')
188                 check_index(wr)
189             mi = index.Writer(indexfile, msw, tmax)
190
191             for e in index.merge(ri, wr):
192                 # FIXME: shouldn't we remove deleted entries eventually?  When?
193                 mi.add_ixentry(e)
194
195             ri.close()
196             mi.close()
197             wr.close()
198         wi.abort()
199     else:
200         wi.close()
201
202     msw.close()
203     hlinks.commit_save()
204
205
206 optspec = """
207 bup index <-p|-m|-s|-u|--clear|--check> [options...] <filenames...>
208 --
209  Modes:
210 p,print    print the index entries for the given names (also works with -u)
211 m,modified print only added/deleted/modified files (implies -p)
212 s,status   print each filename with a status char (A/M/D) (implies -p)
213 u,update   recursively update the index entries for the given file/dir names (default if no mode is specified)
214 check      carefully check index file integrity
215 clear      clear the default index
216  Options:
217 H,hash     print the hash for each object next to its name
218 l,long     print more information about each file
219 no-check-device don't invalidate an entry if the containing device changes
220 fake-valid mark all index entries as up-to-date even if they aren't
221 fake-invalid mark all index entries as invalid
222 f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
223 exclude= a path to exclude from the backup (may be repeated)
224 exclude-from= skip --exclude paths in file (may be repeated)
225 exclude-rx= skip paths matching the unanchored regex (may be repeated)
226 exclude-rx-from= skip --exclude-rx patterns in file (may be repeated)
227 v,verbose  increase log output (can be used more than once)
228 x,xdev,one-file-system  don't cross filesystem boundaries
229 """
230 o = options.Options(optspec)
231 (opt, flags, extra) = o.parse(sys.argv[1:])
232
233 if not (opt.modified or \
234         opt['print'] or \
235         opt.status or \
236         opt.update or \
237         opt.check or \
238         opt.clear):
239     opt.update = 1
240 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
241     o.fatal('--fake-{in,}valid are meaningless without -u')
242 if opt.fake_valid and opt.fake_invalid:
243     o.fatal('--fake-valid is incompatible with --fake-invalid')
244 if opt.clear and opt.indexfile:
245     o.fatal('cannot clear an external index (via -f)')
246
247 # FIXME: remove this once we account for timestamp races, i.e. index;
248 # touch new-file; index.  It's possible for this to happen quickly
249 # enough that new-file ends up with the same timestamp as the first
250 # index, and then bup will ignore it.
251 tick_start = time.time()
252 time.sleep(1 - (tick_start - int(tick_start)))
253
254 git.check_repo_or_die()
255 indexfile = opt.indexfile or git.repo('bupindex')
256
257 handle_ctrl_c()
258
259 if opt.check:
260     log('check: starting initial check.\n')
261     check_index(index.Reader(indexfile))
262
263 if opt.clear:
264     log('clear: clearing index.\n')
265     clear_index(indexfile)
266
267 if opt.update:
268     if not extra:
269         o.fatal('update mode (-u) requested but no paths given')
270     excluded_paths = parse_excludes(flags, o.fatal)
271     exclude_rxs = parse_rx_excludes(flags, o.fatal)
272     xexcept = index.unique_resolved_paths(extra)
273     for rp, path in index.reduce_paths(extra):
274         update_index(rp, excluded_paths, exclude_rxs, xdev_exceptions=xexcept)
275
276 if opt['print'] or opt.status or opt.modified:
277     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
278         if (opt.modified 
279             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
280             continue
281         line = ''
282         if opt.status:
283             if ent.is_deleted():
284                 line += 'D '
285             elif not ent.is_valid():
286                 if ent.sha == index.EMPTY_SHA:
287                     line += 'A '
288                 else:
289                     line += 'M '
290             else:
291                 line += '  '
292         if opt.hash:
293             line += ent.sha.encode('hex') + ' '
294         if opt.long:
295             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
296         print line + (name or './')
297
298 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
299     log('check: starting final check.\n')
300     check_index(index.Reader(indexfile))
301
302 if saved_errors:
303     log('WARNING: %d errors encountered.\n' % len(saved_errors))
304     sys.exit(1)