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