MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / fs_write_atomic

Function fs_write_atomic

crates/cache/src/lib.rs:324–333  ·  view source on GitHub ↗

Assumption: path inside cache directory. Then, we don't have to use sound OS-specific exclusive file access. Note: there's no need to remove temporary file here - cleanup task will do it later.

(path: &Path, reason: &str, contents: &[u8])

Source from the content-addressed store, hash-verified

322// Then, we don't have to use sound OS-specific exclusive file access.
323// Note: there's no need to remove temporary file here - cleanup task will do it later.
324fn fs_write_atomic(path: &Path, reason: &str, contents: &[u8]) -> io::Result<()> {
325 let lock_path = path.with_extension(format!("wip-atomic-write-{reason}"));
326 fs::OpenOptions::new()
327 .create_new(true) // atomic file creation (assumption: no one will open it without this flag)
328 .write(true)
329 .open(&lock_path)
330 .and_then(|mut file| file.write_all(contents))
331 // file should go out of scope and be closed at this point
332 .and_then(|()| fs::rename(&lock_path, &path)) // atomic file rename
333}
334
335#[cfg(test)]
336mod tests;

Callers 2

update_dataMethod · 0.85
write_stats_fileFunction · 0.85

Calls 5

renameFunction · 0.85
write_allMethod · 0.80
newFunction · 0.50
openMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected