Show More
@@ -1,58 +1,59 b'' | |||
|
1 | 1 | # transaction.py - simple journalling scheme for mercurial |
|
2 | 2 | # |
|
3 | 3 | # This transaction scheme is intended to gracefully handle program |
|
4 | 4 | # errors and interruptions. More serious failures like system crashes |
|
5 | 5 | # can be recovered with an fsck-like tool. As the whole repository is |
|
6 | 6 | # effectively log-structured, this should amount to simply truncating |
|
7 | 7 | # anything that isn't referenced in the changelog. |
|
8 | 8 | # |
|
9 | 9 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
10 | 10 | # |
|
11 | 11 | # This software may be used and distributed according to the terms |
|
12 | 12 | # of the GNU General Public License, incorporated herein by reference. |
|
13 | 13 | |
|
14 | 14 | import os |
|
15 | 15 | |
|
16 | 16 | class transaction: |
|
17 | 17 | def __init__(self, opener, journal): |
|
18 | 18 | self.opener = opener |
|
19 | 19 | self.entries = [] |
|
20 | 20 | self.journal = journal |
|
21 | 21 | |
|
22 | 22 | # abort here if the journal already exists |
|
23 | 23 | if os.path.exists(self.journal): |
|
24 | 24 | raise "Journal already exists!" |
|
25 | 25 | self.file = open(self.journal, "w") |
|
26 | 26 | |
|
27 | 27 | def __del__(self): |
|
28 | 28 | if self.entries: self.abort() |
|
29 | self.close() | |
|
29 | try: os.unlink(self.journal) | |
|
30 | except: pass | |
|
30 | 31 | |
|
31 | 32 | def add(self, file, offset): |
|
32 | 33 | self.entries.append((file, offset)) |
|
33 | 34 | # add enough data to the journal to do the truncate |
|
34 | 35 | self.file.write("%s\0%d\n" % (file, offset)) |
|
35 | 36 | self.file.flush() |
|
36 | 37 | |
|
37 | 38 | def close(self): |
|
38 | 39 | self.file.close() |
|
39 | 40 | self.entries = [] |
|
40 | 41 | os.unlink(self.journal) |
|
41 | 42 | |
|
42 | 43 | def abort(self): |
|
43 | 44 | if not self.entries: return |
|
44 | 45 | |
|
45 | 46 | print "transaction abort!" |
|
46 | 47 | |
|
47 | 48 | for f, o in self.entries: |
|
48 | 49 | self.opener(f, "a").truncate(o) |
|
49 | 50 | |
|
50 | 51 | self.entries = [] |
|
51 | 52 | |
|
52 | 53 | print "rollback completed" |
|
53 | 54 | |
|
54 | 55 | def recover(self): |
|
55 | 56 | for l in open(self.journal).readlines(): |
|
56 | 57 | f, o = l.split('\0') |
|
57 | 58 | self.opener(f, "a").truncate(int(o)) |
|
58 | 59 |
General Comments 0
You need to be logged in to leave comments.
Login now