##// END OF EJS Templates
Attempt to recover journal automatically
mpm@selenic.com -
r43:42177b56 default
parent child Browse files
Show More
@@ -1,62 +1,65 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.map = {}
21 21 self.journal = journal
22 22
23 23 # abort here if the journal already exists
24 24 if os.path.exists(self.journal):
25 raise "Journal already exists!"
25 print "journal already exists, recovering"
26 self.recover()
27
26 28 self.file = open(self.journal, "w")
27 29
28 30 def __del__(self):
29 31 if self.entries: self.abort()
30 32 try: os.unlink(self.journal)
31 33 except: pass
32 34
33 35 def add(self, file, offset):
34 36 if file in self.map: return
35 37 self.entries.append((file, offset))
36 38 self.map[file] = 1
37 39 # add enough data to the journal to do the truncate
38 40 self.file.write("%s\0%d\n" % (file, offset))
39 41 self.file.flush()
40 42
41 43 def close(self):
42 44 self.file.close()
43 45 self.entries = []
44 46 os.unlink(self.journal)
45 47
46 48 def abort(self):
47 49 if not self.entries: return
48 50
49 51 print "transaction abort!"
50 52
51 53 for f, o in self.entries:
52 54 self.opener(f, "a").truncate(o)
53 55
54 56 self.entries = []
55 57
56 58 print "rollback completed"
57 59
58 60 def recover(self):
59 61 for l in open(self.journal).readlines():
60 62 f, o = l.split('\0')
61 63 self.opener(f, "a").truncate(int(o))
64 os.unlink(self.journal)
62 65
General Comments 0
You need to be logged in to leave comments. Login now