Show More
@@ -1,76 +1,77 | |||
|
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 | import util |
|
16 | 16 | |
|
17 | 17 | class transaction: |
|
18 | 18 | def __init__(self, opener, journal, after = None): |
|
19 | 19 | self.journal = None |
|
20 | 20 | |
|
21 | 21 | # abort here if the journal already exists |
|
22 | 22 | if os.path.exists(journal): |
|
23 | 23 | raise "journal already exists - run hg recover" |
|
24 | 24 | |
|
25 | 25 | self.opener = opener |
|
26 | 26 | self.after = after |
|
27 | 27 | self.entries = [] |
|
28 | 28 | self.map = {} |
|
29 | 29 | self.journal = journal |
|
30 | 30 | |
|
31 | 31 | self.file = open(self.journal, "w") |
|
32 | 32 | |
|
33 | 33 | def __del__(self): |
|
34 | 34 | if self.journal: |
|
35 | 35 | if self.entries: self.abort() |
|
36 | 36 | self.file.close() |
|
37 | 37 | try: os.unlink(self.journal) |
|
38 | 38 | except: pass |
|
39 | 39 | |
|
40 | 40 | def add(self, file, offset): |
|
41 | 41 | if file in self.map: return |
|
42 | 42 | self.entries.append((file, offset)) |
|
43 | 43 | self.map[file] = 1 |
|
44 | 44 | # add enough data to the journal to do the truncate |
|
45 | 45 | self.file.write("%s\0%d\n" % (file, offset)) |
|
46 | 46 | self.file.flush() |
|
47 | 47 | |
|
48 | 48 | def close(self): |
|
49 | 49 | self.file.close() |
|
50 | 50 | self.entries = [] |
|
51 | 51 | if self.after: |
|
52 | 52 | util.rename(self.journal, self.after) |
|
53 | 53 | else: |
|
54 | 54 | os.unlink(self.journal) |
|
55 | self.journal = None | |
|
55 | 56 | |
|
56 | 57 | def abort(self): |
|
57 | 58 | if not self.entries: return |
|
58 | 59 | |
|
59 | 60 | print "transaction abort!" |
|
60 | 61 | |
|
61 | 62 | for f, o in self.entries: |
|
62 | 63 | try: |
|
63 | 64 | self.opener(f, "a").truncate(o) |
|
64 | 65 | except: |
|
65 | 66 | print "failed to truncate", f |
|
66 | 67 | |
|
67 | 68 | self.entries = [] |
|
68 | 69 | |
|
69 | 70 | print "rollback completed" |
|
70 | 71 | |
|
71 | 72 | def rollback(opener, file): |
|
72 | 73 | for l in open(file).readlines(): |
|
73 | 74 | f, o = l.split('\0') |
|
74 | 75 | opener(f, "a").truncate(int(o)) |
|
75 | 76 | os.unlink(file) |
|
76 | 77 |
General Comments 0
You need to be logged in to leave comments.
Login now