diff --git a/cli/cache/disk_cache.rs b/cli/cache/disk_cache.rs index 3aeebbc6d9..2fee1efe09 100644 --- a/cli/cache/disk_cache.rs +++ b/cli/cache/disk_cache.rs @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use super::CACHE_PERM; -use crate::util::fs::atomic_write_file; +use crate::util::fs::atomic_write_file_with_retries; use deno_cache_dir::url_to_filename; use deno_core::url::Host; @@ -120,7 +120,7 @@ impl DiskCache { pub fn set(&self, filename: &Path, data: &[u8]) -> std::io::Result<()> { let path = self.location.join(filename); - atomic_write_file(&path, data, CACHE_PERM) + atomic_write_file_with_retries(&path, data, CACHE_PERM) } } diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index a8c60e97a2..64d046c153 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -7,7 +7,7 @@ use crate::file_fetcher::FetchOptions; use crate::file_fetcher::FileFetcher; use crate::file_fetcher::FileOrRedirect; use crate::npm::CliNpmResolver; -use crate::util::fs::atomic_write_file; +use crate::util::fs::atomic_write_file_with_retries; use deno_ast::MediaType; use deno_core::futures; @@ -74,7 +74,7 @@ impl deno_cache_dir::DenoCacheEnv for RealDenoCacheEnv { path: &Path, bytes: &[u8], ) -> std::io::Result<()> { - atomic_write_file(path, bytes, CACHE_PERM) + atomic_write_file_with_retries(path, bytes, CACHE_PERM) } fn modified(&self, path: &Path) -> std::io::Result> { diff --git a/cli/npm/managed/resolvers/local.rs b/cli/npm/managed/resolvers/local.rs index 4243d04f2a..e93d400e16 100644 --- a/cli/npm/managed/resolvers/local.rs +++ b/cli/npm/managed/resolvers/local.rs @@ -17,7 +17,7 @@ use std::sync::Arc; use crate::cache::CACHE_PERM; use crate::npm::cache_dir::mixed_case_package_name_decode; -use crate::util::fs::atomic_write_file; +use crate::util::fs::atomic_write_file_with_retries; use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs; use crate::util::fs::clone_dir_recursive; use crate::util::fs::symlink_dir; @@ -550,7 +550,7 @@ impl SetupCache { } bincode::serialize(&self.current).ok().and_then(|data| { - atomic_write_file(&self.file_path, data, CACHE_PERM).ok() + atomic_write_file_with_retries(&self.file_path, data, CACHE_PERM).ok() }); true } diff --git a/cli/util/fs.rs b/cli/util/fs.rs index 5adb777b03..f33368d1a0 100644 --- a/cli/util/fs.rs +++ b/cli/util/fs.rs @@ -32,6 +32,12 @@ use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBarStyle; use crate::util::progress_bar::ProgressMessagePrompt; +/// Writes the file to the file system at a temporary path, then +/// renames it to the destination in a single sys call in order +/// to never leave the file system in a corrupted state. +/// +/// This also handles creating the directory if a NotFound error +/// occurs. pub fn atomic_write_file_with_retries>( file_path: &Path, data: T, @@ -60,7 +66,7 @@ pub fn atomic_write_file_with_retries>( /// /// This also handles creating the directory if a NotFound error /// occurs. -pub fn atomic_write_file>( +fn atomic_write_file>( file_path: &Path, data: T, mode: u32,