##// END OF EJS Templates
transaction: nullify journal after close()...
mpm@selenic.com -
r573:fbfbd4e5 default
parent child Browse files
Show More
@@ -1,76 +1,77 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.journal:
34 if self.journal:
35 if self.entries: self.abort()
35 if self.entries: self.abort()
36 self.file.close()
36 self.file.close()
37 try: os.unlink(self.journal)
37 try: os.unlink(self.journal)
38 except: pass
38 except: pass
39
39
40 def add(self, file, offset):
40 def add(self, file, offset):
41 if file in self.map: return
41 if file in self.map: return
42 self.entries.append((file, offset))
42 self.entries.append((file, offset))
43 self.map[file] = 1
43 self.map[file] = 1
44 # add enough data to the journal to do the truncate
44 # add enough data to the journal to do the truncate
45 self.file.write("%s\0%d\n" % (file, offset))
45 self.file.write("%s\0%d\n" % (file, offset))
46 self.file.flush()
46 self.file.flush()
47
47
48 def close(self):
48 def close(self):
49 self.file.close()
49 self.file.close()
50 self.entries = []
50 self.entries = []
51 if self.after:
51 if self.after:
52 util.rename(self.journal, self.after)
52 util.rename(self.journal, self.after)
53 else:
53 else:
54 os.unlink(self.journal)
54 os.unlink(self.journal)
55 self.journal = None
55
56
56 def abort(self):
57 def abort(self):
57 if not self.entries: return
58 if not self.entries: return
58
59
59 print "transaction abort!"
60 print "transaction abort!"
60
61
61 for f, o in self.entries:
62 for f, o in self.entries:
62 try:
63 try:
63 self.opener(f, "a").truncate(o)
64 self.opener(f, "a").truncate(o)
64 except:
65 except:
65 print "failed to truncate", f
66 print "failed to truncate", f
66
67
67 self.entries = []
68 self.entries = []
68
69
69 print "rollback completed"
70 print "rollback completed"
70
71
71 def rollback(opener, file):
72 def rollback(opener, file):
72 for l in open(file).readlines():
73 for l in open(file).readlines():
73 f, o = l.split('\0')
74 f, o = l.split('\0')
74 opener(f, "a").truncate(int(o))
75 opener(f, "a").truncate(int(o))
75 os.unlink(file)
76 os.unlink(file)
76
77
General Comments 0
You need to be logged in to leave comments. Login now