Show More
@@ -1,84 +1,97 b'' | |||
|
1 | 1 | # dirstateguard.py - class to allow restoring dirstate after failure |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | import os | |
|
10 | 11 | from .i18n import _ |
|
11 | 12 | |
|
12 | 13 | from . import ( |
|
13 | 14 | error, |
|
14 | 15 | narrowspec, |
|
16 | requirements, | |
|
15 | 17 | util, |
|
16 | 18 | ) |
|
17 | 19 | |
|
18 | 20 | |
|
19 | 21 | class dirstateguard(util.transactional): |
|
20 | 22 | """Restore dirstate at unexpected failure. |
|
21 | 23 | |
|
22 | 24 | At the construction, this class does: |
|
23 | 25 | |
|
24 | 26 | - write current ``repo.dirstate`` out, and |
|
25 | 27 | - save ``.hg/dirstate`` into the backup file |
|
26 | 28 | |
|
27 | 29 | This restores ``.hg/dirstate`` from backup file, if ``release()`` |
|
28 | 30 | is invoked before ``close()``. |
|
29 | 31 | |
|
30 | 32 | This just removes the backup file at ``close()`` before ``release()``. |
|
31 | 33 | """ |
|
32 | 34 | |
|
33 | 35 | def __init__(self, repo, name): |
|
34 | 36 | self._repo = repo |
|
35 | 37 | self._active = False |
|
36 | 38 | self._closed = False |
|
37 | self._backupname = b'dirstate.backup.%s.%d' % (name, id(self)) | |
|
38 | self._narrowspecbackupname = b'narrowspec.backup.%s.%d' % ( | |
|
39 | name, | |
|
40 |
|
|
|
39 | ||
|
40 | def getname(prefix): | |
|
41 | fd, fname = repo.vfs.mkstemp(prefix=prefix) | |
|
42 | os.close(fd) | |
|
43 | return fname | |
|
44 | ||
|
45 | self._backupname = getname(b'dirstate.backup.%s.' % name) | |
|
46 | repo.dirstate.savebackup(repo.currenttransaction(), self._backupname) | |
|
47 | # Don't make this the empty string, things may join it with stuff and | |
|
48 | # blindly try to unlink it, which could be bad. | |
|
49 | self._narrowspecbackupname = None | |
|
50 | if requirements.NARROW_REQUIREMENT in repo.requirements: | |
|
51 | self._narrowspecbackupname = getname( | |
|
52 | b'narrowspec.backup.%s.' % name | |
|
41 | 53 | ) |
|
42 | repo.dirstate.savebackup(repo.currenttransaction(), self._backupname) | |
|
43 | 54 | narrowspec.savewcbackup(repo, self._narrowspecbackupname) |
|
44 | 55 | self._active = True |
|
45 | 56 | |
|
46 | 57 | def __del__(self): |
|
47 | 58 | if self._active: # still active |
|
48 | 59 | # this may occur, even if this class is used correctly: |
|
49 | 60 | # for example, releasing other resources like transaction |
|
50 | 61 | # may raise exception before ``dirstateguard.release`` in |
|
51 | 62 | # ``release(tr, ....)``. |
|
52 | 63 | self._abort() |
|
53 | 64 | |
|
54 | 65 | def close(self): |
|
55 | 66 | if not self._active: # already inactivated |
|
56 | 67 | msg = ( |
|
57 | 68 | _(b"can't close already inactivated backup: %s") |
|
58 | 69 | % self._backupname |
|
59 | 70 | ) |
|
60 | 71 | raise error.Abort(msg) |
|
61 | 72 | |
|
62 | 73 | self._repo.dirstate.clearbackup( |
|
63 | 74 | self._repo.currenttransaction(), self._backupname |
|
64 | 75 | ) |
|
76 | if self._narrowspecbackupname: | |
|
65 | 77 | narrowspec.clearwcbackup(self._repo, self._narrowspecbackupname) |
|
66 | 78 | self._active = False |
|
67 | 79 | self._closed = True |
|
68 | 80 | |
|
69 | 81 | def _abort(self): |
|
82 | if self._narrowspecbackupname: | |
|
70 | 83 | narrowspec.restorewcbackup(self._repo, self._narrowspecbackupname) |
|
71 | 84 | self._repo.dirstate.restorebackup( |
|
72 | 85 | self._repo.currenttransaction(), self._backupname |
|
73 | 86 | ) |
|
74 | 87 | self._active = False |
|
75 | 88 | |
|
76 | 89 | def release(self): |
|
77 | 90 | if not self._closed: |
|
78 | 91 | if not self._active: # already inactivated |
|
79 | 92 | msg = ( |
|
80 | 93 | _(b"can't release already inactivated backup: %s") |
|
81 | 94 | % self._backupname |
|
82 | 95 | ) |
|
83 | 96 | raise error.Abort(msg) |
|
84 | 97 | self._abort() |
General Comments 0
You need to be logged in to leave comments.
Login now