Skip to content

Quickly sandbox your AI agent.

Python

image build: tolerate Windows-unstattable rootfs entries and temp-dir cleanup failures

Jace Medlin0f607b7dce83
diff --git a/packages/dev/quicksand-image-tools/quicksand_image_tools/build.py b/packages/dev/quicksand-image-tools/quicksand_image_tools/build.py
index c2d27a8..3eab1b0 100644
--- a/packages/dev/quicksand-image-tools/quicksand_image_tools/build.py
+++ b/packages/dev/quicksand-image-tools/quicksand_image_tools/build.py
@@ -106,7 +106,9 @@ def build_image(
try:
log.info("[1/5] Building Docker image...")
- with tempfile.TemporaryDirectory(prefix="quicksand-build-") as tmpdir:
+ with tempfile.TemporaryDirectory(
+ prefix="quicksand-build-", ignore_cleanup_errors=True
+ ) as tmpdir:
tmpdir_path = Path(tmpdir)
# Build Docker image
@@ -228,7 +230,12 @@ def _tar_to_qcow2(tar_path: Path, qcow2_path: Path, log: logging.Logger = logger
" Windows: https://www.qemu.org/download/#windows"
)
- with tempfile.TemporaryDirectory(prefix="quicksand-convert-") as tmpdir:
+ # ignore_cleanup_errors: the extracted Linux rootfs contains files whose
+ # permissions Windows cannot remove, so tolerate rmtree failures on exit
+ # (the OS reclaims the temp dir eventually).
+ with tempfile.TemporaryDirectory(
+ prefix="quicksand-convert-", ignore_cleanup_errors=True
+ ) as tmpdir:
tmpdir_path = Path(tmpdir)
# Extract tar on host to get kernel/initrd
@@ -415,9 +422,19 @@ def _find_initrd(rootfs: Path) -> Path | None:
def _get_dir_size(path: Path) -> int:
- """Get the total size of a directory in bytes."""
+ """Get the total size of a directory in bytes.
+
+ Entries that cannot be stat-ed are skipped. On Windows the rootfs is
+ extracted from a Linux tar, so some entries (device nodes, restricted
+ directories) raise OSError on stat; they are irrelevant to the size
+ estimate, which only needs a rough lower bound (the caller pads it and
+ rounds up when allocating the ext4 image).
+ """
total = 0
for entry in path.rglob("*"):
- if entry.is_file() and not entry.is_symlink():
- total += entry.stat().st_size
+ try:
+ if entry.is_file() and not entry.is_symlink():
+ total += entry.stat().st_size
+ except OSError:
+ continue
return total
diff --git a/tests/unit/test_dev_build.py b/tests/unit/test_dev_build.py
index 6247b96..742d896 100644
--- a/tests/unit/test_dev_build.py
+++ b/tests/unit/test_dev_build.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from pathlib import Path
from unittest.mock import patch
from quicksand_image_tools.build import (
@@ -119,6 +120,29 @@ class TestGetDirSize:
size = _get_dir_size(tmp_dir)
assert size == 100
+ def test_skips_unstattable_entries(self, tmp_dir):
+ """Entries that raise OSError on stat are skipped, not fatal.
+
+ On Windows the rootfs is extracted from a Linux tar and some entries
+ (device nodes, restricted dirs) cannot be stat-ed; the walk must
+ tolerate that instead of aborting the build.
+ """
+ (tmp_dir / "good").write_bytes(b"a" * 100)
+ (tmp_dir / "bad").write_bytes(b"b" * 999)
+
+ real_stat = Path.stat
+
+ def flaky_stat(self, *args, **kwargs):
+ if self.name == "bad":
+ raise PermissionError("cannot stat")
+ return real_stat(self, *args, **kwargs)
+
+ with patch.object(Path, "stat", flaky_stat):
+ size = _get_dir_size(tmp_dir)
+
+ # Only the stat-able file counts; no exception is raised.
+ assert size == 100
+
class TestBuildImage:
"""Tests for build_image function."""