Skip to content

Commit

Permalink
umu_bspatch: prefer using a temporary file
Browse files Browse the repository at this point in the history
  • Loading branch information
R1kaB3rN committed Dec 10, 2024
1 parent 1b4ff93 commit 2c0cc5d
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions umu/umu_bspatch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from base64 import b64encode
from concurrent.futures import Future, ThreadPoolExecutor
from enum import StrEnum
from pathlib import Path
from shutil import move, rmtree
from tempfile import NamedTemporaryFile
from typing import TypedDict

from umu.umu_log import log
Expand Down Expand Up @@ -110,7 +110,6 @@ def add_binaries(self) -> None: # noqa: D102
self._thread_pool.submit(
self._write_proton_file,
build_file,
Path(item["name"]).name,
self._cache,
item["data"],
item["cksum"],
Expand Down Expand Up @@ -306,16 +305,13 @@ def _patch_proton_file(
def _write_proton_file(
self,
path: Path,
segment: str,
tmp: Path,
data: bytes,
digest: int,
mode: int,
time: float,
) -> None:
token: str = b64encode(os.getrandom(8, os.GRND_NONBLOCK)).hex()
parts: Path = tmp.joinpath(f"{segment}.{token}.parts")
with parts.open("ab+") as fp:
with NamedTemporaryFile(dir=tmp) as fp:
stats: os.stat_result
cksum: int = 0

Expand All @@ -336,12 +332,12 @@ def _write_proton_file(
"Expected %s, received %s for file '%s'",
digest,
cksum,
parts,
fp.name,
)
err: str = "Digest mismatch when creating file"
raise ValueError(err)

# Update our metadata
os.fchmod(fp.fileno(), mode)
os.utime(fp.fileno(), (stats.st_atime, time))
move(parts, path)
move(fp.name, path)

0 comments on commit 2c0cc5d

Please sign in to comment.