Use atomic writes to make it more stable again full disk (#2267)

This commit is contained in:
Pascal Vizeli 2020-11-18 17:08:57 +01:00 committed by GitHub
parent 7aa039d162
commit ecdf4e53b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View File

@ -1,5 +1,6 @@
aiohttp==3.7.2
async_timeout==3.0.1
atomicwrites==1.4.0
attrs==20.3.0
brotlipy==0.7.0
cchardet==2.1.7

View File

@ -4,6 +4,7 @@ import logging
from pathlib import Path
from typing import Any, Dict
from atomicwrites import atomic_write
import voluptuous as vol
from voluptuous.humanize import humanize_error
@ -17,7 +18,8 @@ _DEFAULT: Dict[str, Any] = {}
def write_json_file(jsonfile: Path, data: Any) -> None:
"""Write a JSON file."""
try:
jsonfile.write_text(json.dumps(data, indent=2))
with atomic_write(jsonfile, overwrite=True) as fp:
fp.write(json.dumps(data, indent=2))
jsonfile.chmod(0o600)
except (OSError, ValueError, TypeError) as err:
_LOGGER.error("Can't write %s: %s", jsonfile, err)