[dl:http] fix potential FileExistsError when '.part' file moved (#5385)

when the downloader detects a '.part' file and attempts to resume its
download, a FileExistsError was raised while trying to create an assumed
missing directory path when said '.part' file was (re)moved during the
time it was detected and the downloader attempted to open it in 'a+b' mode.
This commit is contained in:
Mike Fährmann
2025-06-12 16:44:41 +02:00
parent 5b60c66e0f
commit e0a961047c
2 changed files with 6 additions and 0 deletions

View File

@@ -308,6 +308,9 @@ class HttpDownloader(DownloaderBase):
# download content
self.downloading = True
with pathfmt.open(mode) as fp:
if fp is None:
# '.part' file no longer exists
break
if file_header:
fp.write(file_header)
offset += len(file_header)

View File

@@ -171,6 +171,9 @@ class PathFormat():
try:
return open(self.temppath, mode)
except FileNotFoundError:
if "r" in mode:
# '.part' file no longer exists
return util.NullContext()
os.makedirs(self.realdirectory)
return open(self.temppath, mode)