comparison mercurial/revlog.py @ 47217:8f6165c90163

revlog: open files in 'r+' instead of 'a+' The code doing actual writing is already doing the necessary seeking, so we could safely use 'r+'. This make the file objecs usable in more situation, like updating the sidedata information during pull. revlog: forcibly move the file cursor at the right location before writing This is a paranoid change in case the changelog computation moved the cursors under our feets. This is not known to happens right now. Differential Revision: https://phab.mercurial-scm.org/D10608
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 03 May 2021 12:28:15 +0200
parents 2bd4b5218918
children b3acefded601
comparison
equal deleted inserted replaced
47216:2bd4b5218918 47217:8f6165c90163
2014 nodemaputil.setup_persistent_nodemap(tr, self) 2014 nodemaputil.setup_persistent_nodemap(tr, self)
2015 self._chunkclear() 2015 self._chunkclear()
2016 2016
2017 if existing_handles: 2017 if existing_handles:
2018 # switched from inline to conventional reopen the index 2018 # switched from inline to conventional reopen the index
2019 ifh = self._indexfp(b"a+") 2019 ifh = self._indexfp(b"r+")
2020 self._writinghandles = (ifh, new_dfh) 2020 self._writinghandles = (ifh, new_dfh)
2021 new_dfh = None 2021 new_dfh = None
2022 finally: 2022 finally:
2023 if new_dfh is not None: 2023 if new_dfh is not None:
2024 new_dfh.close() 2024 new_dfh.close()
2035 dsize = 0 2035 dsize = 0
2036 if r: 2036 if r:
2037 dsize = self.end(r - 1) 2037 dsize = self.end(r - 1)
2038 dfh = None 2038 dfh = None
2039 if not self._inline: 2039 if not self._inline:
2040 dfh = self._datafp(b"a+") 2040 try:
2041 dfh = self._datafp(b"r+")
2042 dfh.seek(0, os.SEEK_END)
2043 except IOError as inst:
2044 if inst.errno != errno.ENOENT:
2045 raise
2046 dfh = self._datafp(b"w+")
2041 transaction.add(self._datafile, dsize) 2047 transaction.add(self._datafile, dsize)
2042 try: 2048 try:
2043 isize = r * self.index.entry_size 2049 isize = r * self.index.entry_size
2044 ifh = self._indexfp(b"a+") 2050 try:
2051 ifh = self._indexfp(b"r+")
2052 ifh.seek(0, os.SEEK_END)
2053 except IOError as inst:
2054 if inst.errno != errno.ENOENT:
2055 raise
2056 ifh = self._indexfp(b"w+")
2045 if self._inline: 2057 if self._inline:
2046 transaction.add(self._indexfile, dsize + isize) 2058 transaction.add(self._indexfile, dsize + isize)
2047 else: 2059 else:
2048 transaction.add(self._indexfile, isize) 2060 transaction.add(self._indexfile, isize)
2049 try: 2061 try:
3172 # the sidedata helpers 3184 # the sidedata helpers
3173 new_offset_flags = entry[0] | flags[0] & ~flags[1] 3185 new_offset_flags = entry[0] | flags[0] & ~flags[1]
3174 entry = (new_offset_flags,) + entry[1:8] 3186 entry = (new_offset_flags,) + entry[1:8]
3175 entry += (current_offset, len(serialized_sidedata)) 3187 entry += (current_offset, len(serialized_sidedata))
3176 3188
3189 # the sidedata computation might have move the file cursors around
3190 dfh.seek(current_offset, os.SEEK_SET)
3177 dfh.write(serialized_sidedata) 3191 dfh.write(serialized_sidedata)
3178 new_entries.append(entry) 3192 new_entries.append(entry)
3179 current_offset += len(serialized_sidedata) 3193 current_offset += len(serialized_sidedata)
3180 3194
3181 # rewrite the new index entries 3195 # rewrite the new index entries