diff options
| author | xXJsonDeruloXx <danielhimebauch@gmail.com> | 2026-08-01 14:47:18 -0400 |
|---|---|---|
| committer | xXJsonDeruloXx <danielhimebauch@gmail.com> | 2026-08-01 14:47:18 -0400 |
| commit | 02521a797e195b331af1778cd7bc854d3a396ead (patch) | |
| tree | 63c48b246d9db2f6ab0140259d7fb8b15f2e085b /backend/files.py | |
| parent | 96eb17b0a9f2cfd2b00ad082bef893f4efc229f7 (diff) | |
| download | Decky-Framegen-02521a797e195b331af1778cd7bc854d3a396ead.tar.gz Decky-Framegen-02521a797e195b331af1778cd7bc854d3a396ead.zip | |
refactor patch management and add ownership safeguards
Diffstat (limited to 'backend/files.py')
| -rw-r--r-- | backend/files.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/backend/files.py b/backend/files.py new file mode 100644 index 0000000..c5ec839 --- /dev/null +++ b/backend/files.py @@ -0,0 +1,36 @@ +"""Small filesystem helpers shared by backend services.""" + +import filecmp +import hashlib +import json +from pathlib import Path + + +def file_sha256(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def files_match(file_a: Path, file_b: Path) -> bool: + try: + return file_a.is_file() and file_b.is_file() and filecmp.cmp(file_a, file_b, shallow=False) + except OSError: + return False + + +def read_json(path: Path) -> dict: + try: + with path.open("r", encoding="utf-8") as stream: + payload = json.load(stream) + return payload if isinstance(payload, dict) else {} + except (OSError, ValueError, TypeError): + return {} + + +def write_json(path: Path, payload: dict) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("w", encoding="utf-8") as stream: + json.dump(payload, stream, indent=2) |
