Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep tools working after opening a chat file in its app (#14390)

Nilay4962983b0a21
diff --git a/studio/backend/core/inference/os_sandbox.py b/studio/backend/core/inference/os_sandbox.py
index 810332a36cd..c98a60198f5 100644
--- a/studio/backend/core/inference/os_sandbox.py
+++ b/studio/backend/core/inference/os_sandbox.py
@@ -338,10 +338,23 @@ def _walk_for_host_channels(
return f"{unreadable[0]} cannot be fully inspected"
for found, total, path in links.values():
if found < total:
- return f"contains a file hard-linked from outside it: {path}"
+ return f"{_HARD_LINKED}{path}"
return None
+_HARD_LINKED = "contains a file hard-linked from outside it: "
+
+
+def _broke_staged_links() -> bool:
+ # An older backend on the same home can stage an "Open in default app" link after this one started.
+ try:
+ from utils.paths.path_utils import prune_open_staging
+ prune_open_staging()
+ return True
+ except Exception: # noqa: BLE001
+ return False
+
+
_scan_lock = threading.Lock()
# root -> (worker, answer, budget, deadline) of the walk in flight, so a concurrent launch shares it.
_scan_pending: "dict[str, tuple[threading.Thread, list, list, float]]" = {}
@@ -527,6 +540,12 @@ def scan_workdir_for_host_channels(workdir: str) -> tuple[str, ...]:
_overran_workdirs.add(key)
return (WORKDIR_SCAN_INCOMPLETE,)
_overran_workdirs.discard(key)
+ if hazard is not None and hazard.startswith(_HARD_LINKED) and _broke_staged_links():
+ # The hazard stands unless a full rescan proves it gone.
+ try:
+ hazard = _hazard_within_wall_clock(workdir, WORKDIR_SCAN_ENTRIES, WORKDIR_SCAN_SECONDS)
+ except _ScanBudgetExceeded:
+ pass
if hazard is not None:
raise WorkdirUnsafeError(f"the session workdir {hazard}")
return ()
diff --git a/studio/backend/main.py b/studio/backend/main.py
index f833c647100..08fc03a3db6 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -810,6 +810,18 @@ async def lifespan(app: FastAPI):
except Exception: # noqa: BLE001
pass
+ def _prune_open_staging_in_background() -> None:
+ try:
+ from utils.paths.path_utils import prune_open_staging
+ prune_open_staging()
+ except Exception: # noqa: BLE001
+ _lifespan_log.warning("could not prune open-staging", exc_info = True)
+
+ # Off the startup path: the cache can sit on a slow mount, and a tool launched first runs the same cleanup.
+ threading.Thread(
+ target = _prune_open_staging_in_background, name = "open-staging-prune", daemon = True
+ ).start()
+
# Warm the OS sandbox probe so the "off" gate has an answer.
try:
from core.inference.os_sandbox import start_tool_isolation_warmup
diff --git a/studio/backend/tests/test_sandbox_files_and_storage_roots.py b/studio/backend/tests/test_sandbox_files_and_storage_roots.py
index e9ef6a75186..f9e87f34065 100644
--- a/studio/backend/tests/test_sandbox_files_and_storage_roots.py
+++ b/studio/backend/tests/test_sandbox_files_and_storage_roots.py
@@ -10,8 +10,10 @@ behind. Verified on Windows, macOS and Linux.
"""
import asyncio
+import errno
import functools
import hashlib
+import io
import json
import os
import re
@@ -6546,11 +6548,230 @@ def test_opening_a_sandbox_document_hands_it_to_the_default_app(tmp_path, monkey
inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
(sandbox / "outputs" / "report.pdf").write_bytes(b"%PDF-1.4")
assert _open(inference, "outputs/report.pdf") == {"status": "ok"}
- # A private name for the same file, outside the sandbox.
(opened,) = [cmd[-1] for cmd in launched]
assert not opened.startswith(os.path.realpath(sandbox))
assert os.path.basename(opened) == "report.pdf"
- assert os.path.samefile(opened, sandbox / "outputs" / "report.pdf")
+ assert Path(opened).read_bytes() == b"%PDF-1.4"
+
+
+def test_opening_a_sandbox_file_keeps_the_chat_able_to_run_tools(tmp_path, monkeypatch):
+ from core.inference import os_sandbox
+
+ inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
+ (sandbox / "chart.png").write_bytes(b"\x89PNG")
+ assert _open(inference, "chart.png") == {"status": "ok"}
+ assert len(launched) == 1
+ assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()
+
+
+def _stage_like_an_older_build(
+ tmp_path,
+ sandbox,
+ name = "chart.png",
+):
+ linked = tmp_path / "cache" / "open-staging" / "linked"
+ linked.mkdir(parents = True)
+ (sandbox / name).write_bytes(b"\x89PNG")
+ os.link(sandbox / name, linked / name)
+ return linked
+
+
+def test_opening_a_file_clears_old_staged_files(tmp_path, monkeypatch):
+ inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
+ staging = tmp_path / "cache" / "open-staging"
+ old, recent = staging / "old", staging / "recent"
+ for entry in (old, recent):
+ entry.mkdir(parents = True)
+ (entry / "notes.txt").write_text("x", encoding = "utf-8")
+ day_ago = time.time() - 25 * 60 * 60
+ os.utime(old, (day_ago, day_ago))
+ (sandbox / "report.pdf").write_bytes(b"%PDF-1.4")
+ assert _open(inference, "report.pdf") == {"status": "ok"}
+ assert not old.exists()
+ assert recent.exists()
+
+
+# The longest name a filesystem allows, so the swap cannot lengthen it.
[email protected]("name", ["chart.png", "r" * 251 + ".pdf"])
+def test_startup_clears_a_hard_link_an_older_build_staged(tmp_path, monkeypatch, name):
+ import inspect
+
+ from core.inference import os_sandbox
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ linked = _stage_like_an_older_build(tmp_path, sandbox, name)
+ path_utils.prune_open_staging()
+ assert os.listdir(linked) == []
+ assert os.stat(sandbox / name).st_nlink == 1
+ assert (sandbox / name).read_bytes() == b"\x89PNG"
+ assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()
+
+ import main
+
+ source = inspect.getsource(main.lifespan)
+ assert "prune_open_staging()" in source
+ assert "threading.Thread(" in source and "target = _prune_open_staging_in_background" in source
+
+
+def test_an_app_saving_while_a_legacy_link_is_replaced_keeps_its_save(tmp_path, monkeypatch):
+ from core.inference import os_sandbox
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ linked = _stage_like_an_older_build(tmp_path, sandbox)
+ real_mkstemp = tempfile.mkstemp
+
+ def app_saves_meanwhile(*args, **kwargs):
+ saved = linked / "chart.png.saving"
+ saved.write_bytes(b"edited in the app")
+ os.replace(saved, linked / "chart.png")
+ return real_mkstemp(*args, **kwargs)
+
+ monkeypatch.setattr(tempfile, "mkstemp", app_saves_meanwhile)
+ path_utils.prune_open_staging()
+ assert (linked / "chart.png").read_bytes() == b"edited in the app"
+ assert os.listdir(linked) == ["chart.png"]
+ assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()
+
+
+def test_a_sibling_backend_breaking_legacy_links_is_not_raced(tmp_path, monkeypatch):
+ from utils.cache_cleanup import _try_lock, _unlock
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ _stage_like_an_older_build(tmp_path, sandbox)
+ sibling = os.open(tmp_path / "cache" / "open-staging.lock", os.O_CREAT | os.O_RDWR, 0o600)
+ _try_lock(sibling)
+ path_utils.prune_open_staging()
+ assert os.stat(sandbox / "chart.png").st_nlink == 2
+ _unlock(sibling)
+ path_utils.prune_open_staging()
+ assert os.stat(sandbox / "chart.png").st_nlink == 1
+
+
+def test_a_link_an_older_sibling_stages_after_startup_does_not_block_tools(tmp_path, monkeypatch):
+ from core.inference import os_sandbox
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ path_utils.prune_open_staging()
+ linked = _stage_like_an_older_build(tmp_path, sandbox)
+ assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()
+ assert os.listdir(linked) == []
+
+ (tmp_path / "elsewhere").mkdir()
+ os.link(sandbox / "chart.png", tmp_path / "elsewhere" / "chart.png")
+ with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
+ os_sandbox.scan_workdir_for_host_channels(str(sandbox))
+
+
+def test_a_rescan_that_runs_out_of_time_keeps_the_hard_link_refusal(tmp_path, monkeypatch):
+ from core.inference import os_sandbox
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ (sandbox / "chart.png").write_bytes(b"\x89PNG")
+ (tmp_path / "elsewhere").mkdir()
+ os.link(sandbox / "chart.png", tmp_path / "elsewhere" / "chart.png")
+ real_scan = os_sandbox._hazard_within_wall_clock
+ scans = []
+
+ def second_scan_overruns(*args, **kwargs):
+ scans.append(args)
+ if len(scans) > 1:
+ raise os_sandbox._ScanBudgetExceeded("over budget")
+ return real_scan(*args, **kwargs)
+
+ monkeypatch.setattr(os_sandbox, "_hazard_within_wall_clock", second_scan_overruns)
+ with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
+ os_sandbox.scan_workdir_for_host_channels(str(sandbox))
+ assert len(scans) == 2
+
+
[email protected](os.name == "nt", reason = "symlinked directories need privileges on Windows")
+def test_a_symlinked_staging_entry_is_not_walked(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (outside / "a.txt").write_bytes(b"shared")
+ os.link(outside / "a.txt", outside / "b.txt")
+ (tmp_path / "cache" / "open-staging").mkdir(parents = True)
+ (tmp_path / "cache" / "open-staging" / "redirect").symlink_to(outside, target_is_directory = True)
+ path_utils.prune_open_staging()
+ assert os.stat(outside / "a.txt").st_ino == os.stat(outside / "b.txt").st_ino
+ assert sorted(os.listdir(outside)) == ["a.txt", "b.txt"]
+
+
[email protected](os.name == "nt", reason = "symlinked directories need privileges on Windows")
+def test_a_symlinked_staging_root_is_left_alone(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ elsewhere = tmp_path / "elsewhere"
+ recent, old = elsewhere / "recent", elsewhere / "old"
+ for folder in (recent, old):
+ folder.mkdir(parents = True)
+ (recent / "a.txt").write_bytes(b"shared")
+ os.link(recent / "a.txt", recent / "b.txt")
+ day_ago = time.time() - 25 * 60 * 60
+ os.utime(old, (day_ago, day_ago))
+ (tmp_path / "cache").mkdir()
+ (tmp_path / "cache" / "open-staging").symlink_to(elsewhere, target_is_directory = True)
+ path_utils.prune_open_staging()
+ path_utils._prune_open_staging(tmp_path / "cache" / "open-staging")
+ assert sorted(os.listdir(recent)) == ["a.txt", "b.txt"]
+ assert old.exists()
+
+
+def test_legacy_links_wait_when_the_staging_lock_cannot_be_taken(tmp_path, monkeypatch):
+ from utils import cache_cleanup
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ sandbox = tmp_path / "sandbox" / "thread-1"
+ sandbox.mkdir(parents = True)
+ _stage_like_an_older_build(tmp_path, sandbox)
+
+ def no_locks_here(fd):
+ raise OSError(errno.ENOSYS, "locking not supported")
+
+ monkeypatch.setattr(cache_cleanup, "_try_lock", no_locks_here)
+ path_utils.prune_open_staging()
+ assert os.stat(sandbox / "chart.png").st_nlink == 2
+
+
+def test_a_failed_copy_leaves_nothing_staged(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ report = tmp_path / "sandbox" / "report.pdf"
+ report.parent.mkdir()
+ report.write_bytes(b"%PDF-1.4")
+
+ class DiskFull(io.BytesIO):
+ def read(self, *args):
+ raise OSError(28, "No space left on device")
+
+ def fdopen_then_fail(fd, *args, **kwargs):
+ os.close(fd)
+ return DiskFull()
+
+ monkeypatch.setattr(os, "fdopen", fdopen_then_fail)
+ with pytest.raises(OSError):
+ path_utils._stage_for_open(report, report.parent)
+ assert list((tmp_path / "cache" / "open-staging").iterdir()) == []
@pytest.mark.parametrize(
@@ -6612,19 +6833,102 @@ def test_a_file_swapped_for_a_link_after_the_check_opens_what_was_checked(tmp_pa
report.write_bytes(b"%PDF-1.4 checked")
app = tmp_path / "Evil.app"
app.write_text("x", encoding = "utf-8")
- real_link = os.link
+ real_fdopen = os.fdopen
- def swap_then_link(src, dst, **kwargs):
- os.unlink(src)
- os.symlink(app, src)
- return real_link(src, dst, **kwargs)
+ def swap_then_open(fd, *args, **kwargs):
+ os.unlink(report)
+ os.symlink(app, report)
+ return real_fdopen(fd, *args, **kwargs)
- monkeypatch.setattr(path_utils.os, "link", swap_then_link)
+ monkeypatch.setattr(os, "fdopen", swap_then_open)
staged = path_utils._stage_for_open(report, report.parent)
assert not staged.is_symlink()
assert staged.read_bytes() == b"%PDF-1.4 checked"
+def test_a_file_still_being_written_is_staged_as_it_was_opened(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ log = tmp_path / "sandbox" / "run.log"
+ log.parent.mkdir()
+ log.write_bytes(b"line 1\n")
+ real_fdopen = os.fdopen
+
+ def append_then_open(fd, *args, **kwargs):
+ with open(log, "ab") as handle:
+ handle.write(b"line 2\n")
+ return real_fdopen(fd, *args, **kwargs)
+
+ monkeypatch.setattr(os, "fdopen", append_then_open)
+ staged = path_utils._stage_for_open(log, log.parent)
+ assert staged.read_bytes() == b"line 1\n"
+
+
+def test_a_file_cut_short_while_staging_is_not_handed_over_half_copied(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ table = tmp_path / "sandbox" / "rows.csv"
+ table.parent.mkdir()
+ table.write_bytes(b"a,b\n1,2\n")
+ real_fdopen = os.fdopen
+
+ def truncate_then_open(fd, *args, **kwargs):
+ os.truncate(table, 4)
+ return real_fdopen(fd, *args, **kwargs)
+
+ monkeypatch.setattr(os, "fdopen", truncate_then_open)
+ with pytest.raises(OSError):
+ path_utils._stage_for_open(table, table.parent)
+ assert list((tmp_path / "cache" / "open-staging").iterdir()) == []
+
+
+def test_opening_a_file_again_reuses_its_copy_until_either_changes(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ video = tmp_path / "sandbox" / "clip.mp4"
+ video.parent.mkdir()
+ video.write_bytes(b"frames v1")
+ first = path_utils._stage_for_open(video, video.parent)
+ assert path_utils._stage_for_open(video, video.parent) == first
+ assert len(list((tmp_path / "cache" / "open-staging").iterdir())) == 1
+
+ first.write_bytes(b"frames v0")
+ edited = path_utils._stage_for_open(video, video.parent)
+ assert edited != first and edited.read_bytes() == b"frames v1"
+
+ # Same size and, as on a coarse-timestamp volume, the same mtime.
+ before = video.stat()
+ video.write_bytes(b"frames v2")
+ os.utime(video, ns = (before.st_atime_ns, before.st_mtime_ns))
+ changed = path_utils._stage_for_open(video, video.parent)
+ assert changed not in (first, edited) and changed.read_bytes() == b"frames v2"
+
+
+def test_reopening_after_many_edits_compares_one_earlier_copy(tmp_path, monkeypatch):
+ from utils.paths import path_utils
+
+ monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
+ table = tmp_path / "sandbox" / "rows.csv"
+ table.parent.mkdir()
+ for version in range(5):
+ table.write_bytes(b"a,b\n1,%d\n" % version)
+ path_utils._stage_for_open(table, table.parent)
+ compared = []
+ real_same_bytes = path_utils._same_bytes
+
+ def counting(*args):
+ compared.append(args)
+ return real_same_bytes(*args)
+
+ monkeypatch.setattr(path_utils, "_same_bytes", counting)
+ table.write_bytes(b"a,b\n1,9\n")
+ path_utils._stage_for_open(table, table.parent)
+ assert len(compared) == 1
+
+
@pytest.mark.skipif(os.name == "nt", reason = "symlinked directories need privileges on Windows")
def test_a_parent_swapped_for_a_link_is_refused(tmp_path, monkeypatch):
from utils.paths import path_utils
diff --git a/studio/backend/utils/paths/path_utils.py b/studio/backend/utils/paths/path_utils.py
index cb06122a2a9..a8f269552d3 100644
--- a/studio/backend/utils/paths/path_utils.py
+++ b/studio/backend/utils/paths/path_utils.py
@@ -463,23 +463,78 @@ DEFAULT_APP_OPEN_EXTENSIONS = frozenset(
_OPEN_STAGING_MAX_AGE_S = 24 * 60 * 60
-def _prune_open_staging(root: Path) -> None:
+# Older builds staged hard links the OS sandbox refuses; only the name goes, so an app holding the dir can still save.
+def _break_hard_links(entry: Path) -> None:
+ import stat as stat_module
+ import tempfile
+ for child in list(entry.iterdir()):
+ info = child.lstat()
+ if not (stat_module.S_ISREG(info.st_mode) and info.st_nlink > 1):
+ continue
+ handle, name = tempfile.mkstemp(dir = entry)
+ os.close(handle)
+ held, legacy = Path(name), False
+ try:
+ os.replace(child, held)
+ moved = held.lstat()
+ legacy = (moved.st_dev, moved.st_ino) == (info.st_dev, info.st_ino)
+ if not legacy:
+ try:
+ os.link(held, child)
+ except FileExistsError:
+ pass
+ finally:
+ if legacy or os.path.lexists(child):
+ held.unlink(missing_ok = True)
+
+
+def _prune_open_staging(root: Path, break_links: bool = False) -> None:
import shutil
+ import stat as stat_module
import time
+ def real_dir(info: os.stat_result) -> bool:
+ return stat_module.S_ISDIR(info.st_mode) and not getattr(info, "st_reparse_tag", 0)
+
cutoff = time.time() - _OPEN_STAGING_MAX_AGE_S
try:
+ # Never through a symlink or junction, which would reach outside the cache.
+ if not real_dir(root.lstat()):
+ return
entries = list(root.iterdir())
except OSError:
return
for entry in entries:
try:
- if entry.lstat().st_mtime < cutoff:
+ info = entry.lstat()
+ if info.st_mtime < cutoff:
shutil.rmtree(entry, ignore_errors = True)
+ elif break_links and real_dir(info):
+ _break_hard_links(entry)
except OSError:
continue
+# Only under the lock, so sibling backends sharing the home never swap at once; no lock = links wait for a later call.
+def prune_open_staging() -> None:
+ from utils.cache_cleanup import _try_lock, _unlock
+ from utils.paths.storage_roots import cache_root
+
+ try:
+ handle = os.open(cache_root() / "open-staging.lock", os.O_CREAT | os.O_RDWR, 0o600)
+ except OSError:
+ return
+ try:
+ _try_lock(handle)
+ except OSError:
+ os.close(handle)
+ return
+ try:
+ _prune_open_staging(cache_root() / "open-staging", break_links = True)
+ finally:
+ _unlock(handle)
+
+
def _opened_path(handle: int) -> Optional[str]:
"""Where an open file really is, or None where the OS can't say."""
if sys.platform == "darwin":
@@ -489,13 +544,25 @@ def _opened_path(handle: int) -> Optional[str]:
return os.readlink(proc) if os.path.islink(proc) else None
+def _same_bytes(handle: int, staged: Path, size: int) -> bool:
+ with os.fdopen(os.dup(handle), "rb") as source, open(staged, "rb") as copy:
+ source.seek(0)
+ while size:
+ chunk = source.read(min(size, 1 << 20))
+ if not chunk or copy.read(len(chunk)) != chunk:
+ return False
+ size -= len(chunk)
+ return True
+
+
def _stage_for_open(path: Path, root: Optional[Path] = None) -> Path:
"""A name for *path*'s current file in a private directory, for the OS opener.
Tool code runs in the sandbox and can swap *path* for a symlink (to an app or a script outside
it) between any check and the opener resolving the name. So the file is opened once without
- following links, and the inode that open returned is hard-linked (or, across filesystems,
- copied) into a fresh directory only Studio writes to. That name is what the OS opens.
+ following links, and what that open returned is copied into a fresh directory only Studio
+ writes to. That name is what the OS opens. Not a hard link: the OS sandbox refuses a workdir
+ file linked from outside it.
O_NOFOLLOW only covers the last component, so a swapped parent is caught by checking where the
opened file really is against *root*.
"""
@@ -528,17 +595,40 @@ def _stage_for_open(path: Path, root: Optional[Path] = None) -> Path:
staging = cache_root() / "open-staging"
staging.mkdir(parents = True, exist_ok = True)
_prune_open_staging(staging)
- target = Path(tempfile.mkdtemp(dir = staging)) / path.name
+
+ # Reuse the newest copy only if byte-identical: an app may have edited it, and coarse mtimes miss same-size rewrites.
+ def newest_first(entry: Path) -> int:
+ try:
+ return entry.lstat().st_mtime_ns
+ except OSError:
+ return 0
+
+ key = f"{info.st_dev}-{info.st_ino}-{info.st_size}-"
+ for entry in sorted(staging.glob(key + "*"), key = newest_first, reverse = True)[:1]:
+ try:
+ staged = os.lstat(entry / path.name)
+ if (
+ stat_module.S_ISREG(staged.st_mode)
+ and staged.st_nlink == 1
+ and staged.st_size == info.st_size
+ and _same_bytes(handle, entry / path.name, info.st_size)
+ ):
+ os.utime(entry)
+ return entry / path.name
+ except OSError:
+ continue
+ target = Path(tempfile.mkdtemp(prefix = key, dir = staging)) / path.name
try:
- os.link(path, target, follow_symlinks = False)
- linked = os.lstat(target)
- # Swapped since the open: copy what was opened.
- if (linked.st_dev, linked.st_ino) != (info.st_dev, info.st_ino):
- target.unlink()
- raise OSError("file changed")
- except (OSError, NotImplementedError):
with os.fdopen(os.dup(handle), "rb") as source, open(target, "xb") as copy:
- shutil.copyfileobj(source, copy)
+ source.seek(0)
+ remaining = info.st_size
+ while remaining and (chunk := source.read(min(remaining, 1 << 20))):
+ remaining -= copy.write(chunk)
+ if remaining:
+ raise OSError(f"{path.name} got shorter while it was being copied")
+ except OSError:
+ shutil.rmtree(target.parent, ignore_errors = True)
+ raise
return target
finally:
os.close(handle)