Skip to content

Commit 78239cd

Browse files
committed
proper temp file handling
1 parent 8be5d06 commit 78239cd

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

otsserver/backup.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import threading
2424
import binascii
2525
import requests
26+
from tempfile import NamedTemporaryFile
2627
import time
2728
from urllib.parse import urlparse, urljoin
2829

@@ -139,10 +140,10 @@ def write_disk_cache(self, chunk, bytes):
139140
cache_path = self.cache_path + '/' + chunk_path
140141
os.makedirs(cache_path, exist_ok=True)
141142
cache_file = cache_path + '/' + chunk_str
142-
cache_file_tmp = cache_file + '.tmp'
143-
with open(cache_file_tmp, 'wb') as fd:
144-
fd.write(bytes)
145-
os.rename(cache_file_tmp, cache_file) # rename is atomic
143+
temp_file = NamedTemporaryFile(delete=False)
144+
temp_file.write(bytes)
145+
temp_file.close()
146+
os.rename(temp_file.name, cache_file) # rename is atomic
146147

147148
# The following is a shrinked version of the standard calendar http server, it only support the '/timestamp' endpoint
148149
# This way the backup server could serve request in place of the calendar serve which is backupping

0 commit comments

Comments
 (0)