Show More
@@ -71,11 +71,18 b' else:' | |||
|
71 | 71 | class OperationalError(Exception): |
|
72 | 72 | "Dummy exception when sqlite could not be imported. Should never occur." |
|
73 | 73 | |
|
74 | # use 16kB as threshold for whether a corrupt history db should be saved | |
|
75 | # that should be at least 100 entries or so | |
|
76 | _SAVE_DB_SIZE = 16384 | |
|
77 | ||
|
74 | 78 | @decorator |
|
75 | 79 | def catch_corrupt_db(f, self, *a, **kw): |
|
76 | 80 | """A decorator which wraps HistoryAccessor method calls to catch errors from |
|
77 | 81 | a corrupt SQLite database, move the old database out of the way, and create |
|
78 | 82 | a new one. |
|
83 | ||
|
84 | We avoid clobbering larger databases because this may be triggered due to filesystem issues, | |
|
85 | not just a corrupt file. | |
|
79 | 86 | """ |
|
80 | 87 | try: |
|
81 | 88 | return f(self, *a, **kw) |
@@ -87,16 +94,23 b' def catch_corrupt_db(f, self, *a, **kw):' | |||
|
87 | 94 | self.hist_file = ':memory:' |
|
88 | 95 | self.log.error("Failed to load history too many times, history will not be saved.") |
|
89 | 96 | elif os.path.isfile(self.hist_file): |
|
90 |
# |
|
|
91 | base,ext = os.path.splitext(self.hist_file) | |
|
92 | now = datetime.datetime.now().isoformat().replace(':', '.') | |
|
93 | newpath = base + '-corrupt-' + now + ext | |
|
94 | # don't clobber previous corrupt backups | |
|
95 | for i in range(100): | |
|
96 | if not os.path.isfile(newpath): | |
|
97 |
b |
|
|
98 |
|
|
|
99 | newpath = base + '-corrupt-' + now + (u'-%i' % i) + ext | |
|
97 | # move the file out of the way | |
|
98 | base, ext = os.path.splitext(self.hist_file) | |
|
99 | size = os.stat(self.hist_file).st_size | |
|
100 | if size >= _SAVE_DB_SIZE: | |
|
101 | # if there's significant content, avoid clobbering | |
|
102 | now = datetime.datetime.now().isoformat().replace(':', '.') | |
|
103 | newpath = base + '-corrupt-' + now + ext | |
|
104 | # don't clobber previous corrupt backups | |
|
105 | for i in range(100): | |
|
106 | if not os.path.isfile(newpath): | |
|
107 | break | |
|
108 | else: | |
|
109 | newpath = base + '-corrupt-' + now + (u'-%i' % i) + ext | |
|
110 | else: | |
|
111 | # not much content, possibly empty; don't worry about clobbering | |
|
112 | # maybe we should just delete it? | |
|
113 | newpath = base + '-corrupt' + ext | |
|
100 | 114 | os.rename(self.hist_file, newpath) |
|
101 | 115 | self.log.error("History file was moved to %s and a new file created.", newpath) |
|
102 | 116 | self.init_db() |
General Comments 0
You need to be logged in to leave comments.
Login now