Use filesystem.replace for atomic move operations

This commit is contained in:
Joona Hoikkala 2020-03-04 20:21:00 +02:00
parent 56cb226bd4
commit 895330e009
No known key found for this signature in database
GPG key ID: D5AA86BBF9B29A5C
2 changed files with 3 additions and 5 deletions

View file

@ -32,7 +32,6 @@ restart.
from datetime import datetime
import logging
import shutil
import time
from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module
@ -183,8 +182,7 @@ class OCSPPrefetchMixin(object):
with DBMHandler(tmp_file, 'w') as db:
db[key] = value
shutil.copy2(tmp_file, filename)
os.remove(tmp_file)
filesystem.replace(tmp_file, filename)
def _ocsp_ttl(self, next_update):
"""Calculates Apache internal TTL for the next OCSP staple
@ -284,7 +282,7 @@ class OCSPPrefetchMixin(object):
cache_path = os.path.join(self.config.work_dir, "ocsp", "ocsp_cache.db")
work_file_path = os.path.join(self.config.work_dir, "ocsp_work", "ocsp_cache.db")
try:
shutil.copy2(work_file_path, cache_path)
filesystem.replace(work_file_path, cache_path)
except IOError:
logger.debug("Encountered an issue when trying to restore OCSP dbm file")

View file

@ -256,7 +256,7 @@ class OCSPPrefetchTest(util.ApacheTest):
log_string = "Encountered an issue while trying to backup OCSP dbm file"
log_string2 = "Encountered an issue when trying to restore OCSP dbm file"
self.config._ocsp_prefetch = {"mock": "value"}
with mock.patch("shutil.copy2", side_effect=IOError):
with mock.patch("certbot.compat.filesystem.replace", side_effect=IOError):
with mock.patch(log_path) as mock_log:
self.config.restart()
self.assertTrue(mock_log.called)