mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Merge pull request #2003 from dhavalgshah/kceu22_bugbash/issue1999
fix: ngclient: temp_file could be undefined #1999
This commit is contained in:
commit
e78b1aaa7d
1 changed files with 4 additions and 2 deletions
|
|
@ -275,6 +275,7 @@ def _load_local_metadata(self, rolename: str) -> bytes:
|
|||
|
||||
def _persist_metadata(self, rolename: str, data: bytes) -> None:
|
||||
"""Write metadata to disk atomically to avoid data loss."""
|
||||
temp_file_name: Optional[str] = None
|
||||
try:
|
||||
# encode the rolename to avoid issues with e.g. path separators
|
||||
encoded_name = parse.quote(rolename, "")
|
||||
|
|
@ -282,14 +283,15 @@ def _persist_metadata(self, rolename: str, data: bytes) -> None:
|
|||
with tempfile.NamedTemporaryFile(
|
||||
dir=self._dir, delete=False
|
||||
) as temp_file:
|
||||
temp_file_name = temp_file.name
|
||||
temp_file.write(data)
|
||||
os.replace(temp_file.name, filename)
|
||||
except OSError as e:
|
||||
# remove tempfile if we managed to create one,
|
||||
# then let the exception happen
|
||||
if temp_file:
|
||||
if temp_file_name is not None:
|
||||
try:
|
||||
os.remove(temp_file.name)
|
||||
os.remove(temp_file_name)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
raise e
|
||||
|
|
|
|||
Loading…
Reference in a new issue