##// 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 # 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
15
16 class transaction:
16 class transaction:
17 def __init__(self, opener, journal):
17 def __init__(self, opener, journal):
18 self.opener = opener
18 self.opener = opener
19 self.entries = []
19 self.entries = []
20 self.map = {}
20 self.map = {}
21 self.journal = journal
21 self.journal = journal
22
22
23 # abort here if the journal already exists
23 # abort here if the journal already exists
24 if os.path.exists(self.journal):
24 if os.path.exists(self.journal):
25 raise "Journal already exists!"
25 print "journal already exists, recovering"
26 self.recover()
27
26 self.file = open(self.journal, "w")
28 self.file = open(self.journal, "w")
27
29
28 def __del__(self):
30 def __del__(self):
29 if self.entries: self.abort()
31 if self.entries: self.abort()
30 try: os.unlink(self.journal)
32 try: os.unlink(self.journal)
31 except: pass
33 except: pass
32
34
33 def add(self, file, offset):
35 def add(self, file, offset):
34 if file in self.map: return
36 if file in self.map: return
35 self.entries.append((file, offset))
37 self.entries.append((file, offset))
36 self.map[file] = 1
38 self.map[file] = 1
37 # add enough data to the journal to do the truncate
39 # add enough data to the journal to do the truncate
38 self.file.write("%s\0%d\n" % (file, offset))
40 self.file.write("%s\0%d\n" % (file, offset))
39 self.file.flush()
41 self.file.flush()
40
42
41 def close(self):
43 def close(self):
42 self.file.close()
44 self.file.close()
43 self.entries = []
45 self.entries = []
44 os.unlink(self.journal)
46 os.unlink(self.journal)
45
47
46 def abort(self):
48 def abort(self):
47 if not self.entries: return
49 if not self.entries: return
48
50
49 print "transaction abort!"
51 print "transaction abort!"
50
52
51 for f, o in self.entries:
53 for f, o in self.entries:
52 self.opener(f, "a").truncate(o)
54 self.opener(f, "a").truncate(o)
53
55
54 self.entries = []
56 self.entries = []
55
57
56 print "rollback completed"
58 print "rollback completed"
57
59
58 def recover(self):
60 def recover(self):
59 for l in open(self.journal).readlines():
61 for l in open(self.journal).readlines():
60 f, o = l.split('\0')
62 f, o = l.split('\0')
61 self.opener(f, "a").truncate(int(o))
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