Skip to content

Commit

Permalink
use sparse files
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Jan 9, 2025
1 parent 5fe1bfa commit 0841310
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions system/loggerd/tests/loggerd_tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
from openpilot.system.loggerd.xattr_cache import setxattr


def create_random_file(file_path: Path, size_mb: float, lock: bool = False, upload_xattr: bytes = None) -> None:
def create_sparse_file(file_path: Path, size_mb: float, lock: bool = False, upload_xattr: bytes = None) -> None:
file_path.parent.mkdir(parents=True, exist_ok=True)

if lock:
lock_path = str(file_path) + ".lock"
os.close(os.open(lock_path, os.O_CREAT | os.O_EXCL))

chunks = 128
chunk_bytes = int(size_mb * 1024 * 1024 / chunks)
data = os.urandom(chunk_bytes)

with open(file_path, "wb") as f:
for _ in range(chunks):
f.write(data)
f.seek(int(size_mb * 1024 * 1024) - 1)
f.write(b"\0")

if upload_xattr is not None:
setxattr(str(file_path), uploader.UPLOAD_ATTR_NAME, upload_xattr)
Expand Down Expand Up @@ -82,7 +78,7 @@ def setup_method(self):
def make_file_with_data(self, f_dir: str, fn: str, size_mb: float = .1, lock: bool = False,
upload_xattr: bytes = None, preserve_xattr: bytes = None) -> Path:
file_path = Path(Paths.log_root()) / f_dir / fn
create_random_file(file_path, size_mb, lock, upload_xattr)
create_sparse_file(file_path, size_mb, lock, upload_xattr)

if preserve_xattr is not None:
setxattr(str(file_path.parent), deleter.PRESERVE_ATTR_NAME, preserve_xattr)
Expand Down

0 comments on commit 0841310

Please sign in to comment.