MCPcopy Create free account
hub / github.com/1jehuang/jcode / write_file_atomically

Function write_file_atomically

crates/jcode-base/src/browser.rs:517–545  ·  view source on GitHub ↗
(path: &PathBuf, bytes: &[u8], _executable: bool)

Source from the content-addressed store, hash-verified

515}
516
517fn write_file_atomically(path: &PathBuf, bytes: &[u8], _executable: bool) -> Result<()> {
518 let parent = path
519 .parent()
520 .context("Target file has no parent directory")?;
521 std::fs::create_dir_all(parent)?;
522
523 let ts = chrono::Utc::now().timestamp_nanos_opt().unwrap_or_default();
524 let pid = std::process::id();
525 let tmp_path = parent.join(format!(
526 ".{}.tmp-{}-{}",
527 path.file_name()
528 .and_then(|s| s.to_str())
529 .unwrap_or("download"),
530 pid,
531 ts
532 ));
533
534 std::fs::write(&tmp_path, bytes)?;
535
536 #[cfg(unix)]
537 {
538 use std::os::unix::fs::PermissionsExt;
539 let mode = if _executable { 0o755 } else { 0o644 };
540 std::fs::set_permissions(&tmp_path, std::fs::Permissions::from_mode(mode))?;
541 }
542
543 std::fs::rename(&tmp_path, path)?;
544 Ok(())
545}
546
547fn get_platform_asset_name() -> String {
548 #[cfg(all(target_os = "linux", target_arch = "x86_64"))]

Callers 1

download_browser_binaryFunction · 0.85

Calls 1

writeFunction · 0.85

Tested by

no test coverage detected