Merge pull request #724 from dadav/feature/ensure_write

add ensure_write
This commit is contained in:
evilsocket 2019-12-16 11:59:40 +02:00 committed by GitHub
commit 1f91c6f09e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,8 @@ import yaml
import json import json
import shutil import shutil
import gzip import gzip
import contextlib
import tempfile
import pwnagotchi import pwnagotchi
@ -345,6 +347,17 @@ def extract_from_pcap(path, fields):
return results return results
@contextlib.contextmanager
def ensure_write(filename, mode='w'):
path = os.path.dirname(filename)
fd, tmp = tempfile.mkstemp(dir=path)
with os.fdopen(fd, mode) as f:
yield f
f.flush()
os.fsync(f.fileno())
os.replace(tmp, filename)
class StatusFile(object): class StatusFile(object):
def __init__(self, path, data_format='raw'): def __init__(self, path, data_format='raw'):
@ -378,7 +391,7 @@ class StatusFile(object):
def update(self, data=None): def update(self, data=None):
self._updated = datetime.now() self._updated = datetime.now()
self.data = data self.data = data
with open(self._path, 'w') as fp: with ensure_write(self._path, 'w') as fp:
if data is None: if data is None:
fp.write(str(self._updated)) fp.write(str(self._updated))