diff options
| author | David Sherret <dsherret@users.noreply.github.com> | 2024-05-14 14:26:48 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-14 14:26:48 -0400 |
| commit | f16b4d4df8bd03f2d0c5e5d6855e334fa768e828 (patch) | |
| tree | 6b99b3a79486cd73c32145bca981e13fc626549b /cli/util/path.rs | |
| parent | c0e3b6ed9d955bc59a8d88e177219b541881c63d (diff) | |
fix(npm): make tarball extraction more reliable (#23759)
1. Extracts to a directory beside the destination.
2. Renames to the destination with retries.
Diffstat (limited to 'cli/util/path.rs')
| -rw-r--r-- | cli/util/path.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/cli/util/path.rs b/cli/util/path.rs index a3109ad04..3c848edea 100644 --- a/cli/util/path.rs +++ b/cli/util/path.rs @@ -42,6 +42,32 @@ pub fn get_extension(file_path: &Path) -> Option<String> { .map(|e| e.to_lowercase()); } +pub fn get_atomic_dir_path(file_path: &Path) -> PathBuf { + let rand = gen_rand_path_component(); + let new_file_name = format!( + ".{}_{}", + file_path + .file_name() + .map(|f| f.to_string_lossy()) + .unwrap_or(Cow::Borrowed("")), + rand + ); + file_path.with_file_name(new_file_name) +} + +pub fn get_atomic_file_path(file_path: &Path) -> PathBuf { + let rand = gen_rand_path_component(); + let extension = format!("{rand}.tmp"); + file_path.with_extension(extension) +} + +fn gen_rand_path_component() -> String { + (0..4).fold(String::new(), |mut output, _| { + output.push_str(&format!("{:02x}", rand::random::<u8>())); + output + }) +} + /// TypeScript figures out the type of file based on the extension, but we take /// other factors into account like the file headers. The hack here is to map the /// specifier passed to TypeScript to a new specifier with the file extension. |
