Private temporary workspaces in Laravel Rust
Scope, source, and alternatives
Study Spatie's MIT-licensed spatie/temporary-directory 2.4.0 at 32cbb9645b28839cf4f476708e99a2c70e6802c9: src/TemporaryDirectory.php and its tests. Preserve the upstream license notice if adapting source. Framework baseline: Laravel Rust 0.1.3 source 7837d40d8508957fd0e48a25d0087c851095fcc9. Confirm your lockfile before implementing.
Check get-recipe's current port catalog for Rust ports, their URLs and compatibility; a listing is not an audit or an endorsement. Installing a compatible port is a valid option. For the narrow workspace lifecycle, Rust's tempfile crate is usually less code than porting a PHP wrapper. It is a small native dependency, not a claim of dependency-free operation.
This guidance has not been executed in your app. Scope: one private local workspace per export/job, fixed filenames, explicit cleanup. Exclude arbitrary named locations, force-overwrite, generic recursive file management, and untrusted archive extraction.
Translate the contract, not the fluent API
The PHP source generates a child directory, rejects collisions unless forced, can create nested parents while resolving path(), and optionally deletes in its destructor. Its deletion returns false on errors and does not follow symlinks. A direct translation of deleteWhenDestroyed() into a global/static workspace is wrong: ownership must follow the operation's lifetime.
Use a tempfile::TempDir owned by the job/function. Verify and lock a maintained compatible tempfile release in the target app. Prefer a trusted, provisioned root outside public storage. This illustrative fragment assumes the dependency exists and the surrounding function returns an appropriate Result:
let workspace = tempfile::Builder::new()
.prefix("export-")
.tempdir_in(&trusted_root)?;
let output_path = workspace.path().join("result.json");
let generated = generate_and_read_export(&output_path).await;
let cleaned = workspace.close();
let bytes = generated?;
cleaned?;
generate_and_read_export represents your existing export operation, not a framework function. Read/close the output before cleanup. Computing cleaned before propagating generated ensures an ordinary operation error does not skip explicit cleanup; decide how to report both errors without leaking private filesystem paths. Drop is a fallback, not a source of observable cleanup errors.
Adaptation steps
- Inventory the PHP caller's used methods and output lifetime. Keep only the needed behavior; do not expose
force()or raw locations to request input. - Provision the root with private permissions and trusted ancestors. Review directory permissions for the target OS and selected crate version. Object-store disks do not support this API.
- Keep the
TempDiralive across all.awaitpoints that access the files. Do not return a borrowed path after dropping its owner. Do not hand only the path to a spawned task and let the owner disappear. - For blocking conversion tools or large synchronous filesystem operations, use the application's blocking-task facility; do not block an async request executor. Await completion of all readers before deleting. For queued work, create the directory inside the worker.
- Write only fixed filenames or validated single components. Joining an absolute path can discard the workspace prefix. Reject traversal/separators instead of trying to sanitize them after joining. Ensure subprocesses receive structured arguments, not a shell command constructed from filenames.
- When output must survive the task, copy/move it to durable private storage before cleanup. Process termination may leave artifacts; an optional janitor must operate only within the dedicated root and an application-defined safe retention window.
Acceptance checks
Exercise the real public export operation using a real temporary filesystem:
- Parallel operations produce independent artifacts and clean only their own workspaces.
- Success, ordinary errors, task cancellation, and panic unwinding release owned resources; document that abort/process kill cannot guarantee cleanup.
- A sentinel outside the workspace survives both a traversal attempt and an internal symlink cleanup. Run this on the production OS; platform-specific symlink tests may need privileges.
- Missing/read-only root and full-disk/write failures return errors rather than silently succeeding.
- A delayed reader/task cannot observe premature deletion. Stream lifetime is covered explicitly if the application streams output.
Run cargo fmt --all --check, cargo clippy --all-targets -- -D warnings, and the target app's workspace/export tests with cargo test. This prose's snippets are integration sketches; compile the actual adaptation and record the crate version, OS, and decisive test output with the change.