| 110 | # file (if any) is renamed with a .bak extension first. If a .bak |
| 111 | # file currently exists, it's deleted. |
| 112 | def _commit(self): |
| 113 | # CAUTION: It's vital that _commit() succeed, and _commit() can |
| 114 | # be called from __del__(). Therefore we must never reference a |
| 115 | # global in this routine. |
| 116 | if self._index is None or not self._modified: |
| 117 | return # nothing to do |
| 118 | |
| 119 | try: |
| 120 | self._os.unlink(self._bakfile) |
| 121 | except OSError: |
| 122 | pass |
| 123 | |
| 124 | try: |
| 125 | self._os.rename(self._dirfile, self._bakfile) |
| 126 | except OSError: |
| 127 | pass |
| 128 | |
| 129 | with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f: |
| 130 | self._chmod(self._dirfile) |
| 131 | for key, pos_and_siz_pair in self._index.items(): |
| 132 | # Use Latin-1 since it has no qualms with any value in any |
| 133 | # position; UTF-8, though, does care sometimes. |
| 134 | entry = "%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair) |
| 135 | f.write(entry) |
| 136 | self._modified = False |
| 137 | |
| 138 | sync = _commit |
| 139 | |