Use os.replace where applicable (#793)

When using 
```py
os.remove(encodeFilename(filename))
os.rename(encodeFilename(temp_filename), encodeFilename(filename))
```
the `os.remove` need not be atomic and so can be executed arbitrarily compared to the immediately following rename call. It is better to use `os.replace` instead

Authored by: paulwrubel
This commit is contained in:
Paul Wrubel
2021-08-26 21:27:20 -05:00
committed by GitHub
parent 691d5823d6
commit d75201a873
5 changed files with 10 additions and 18 deletions

View File

@@ -207,12 +207,9 @@ class FileDownloader(object):
if old_filename == new_filename:
return
try:
if self.params.get('overwrites', False):
if os.path.isfile(encodeFilename(new_filename)):
os.remove(encodeFilename(new_filename))
os.rename(encodeFilename(old_filename), encodeFilename(new_filename))
os.replace(old_filename, new_filename)
except (IOError, OSError) as err:
self.report_error('unable to rename file: %s' % error_to_compat_str(err))
self.report_error(f'unable to rename file: {err}')
def try_utime(self, filename, last_modified_hdr):
"""Try to set the last-modified time of the given file."""