From 895330e0097b67e101f41c7e841a0eb946bc9b52 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Wed, 4 Mar 2020 20:21:00 +0200 Subject: [PATCH] Use filesystem.replace for atomic move operations --- certbot-apache/certbot_apache/_internal/prefetch_ocsp.py | 6 ++---- certbot-apache/tests/ocsp_prefetch_test.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/certbot-apache/certbot_apache/_internal/prefetch_ocsp.py b/certbot-apache/certbot_apache/_internal/prefetch_ocsp.py index 678d4d88e..afc2f398c 100644 --- a/certbot-apache/certbot_apache/_internal/prefetch_ocsp.py +++ b/certbot-apache/certbot_apache/_internal/prefetch_ocsp.py @@ -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") diff --git a/certbot-apache/tests/ocsp_prefetch_test.py b/certbot-apache/tests/ocsp_prefetch_test.py index 3bc35eeee..a687d047c 100644 --- a/certbot-apache/tests/ocsp_prefetch_test.py +++ b/certbot-apache/tests/ocsp_prefetch_test.py @@ -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)