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