Skip to content

Commit eff1795

Browse files
committed
chore: remove rand dependency
1 parent aedc025 commit eff1795

4 files changed

Lines changed: 46 additions & 63 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ readme = "README.md"
1111
edition = "2024"
1212

1313
[dependencies]
14-
rand = "0.9.1"
1514
serde = { version = "1.0.219", features = ["derive"] }
1615
serde_json = "1.0.140"
1716
walkdir = { version = "2.5.0", optional = true }

src/hash.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub fn hash(input: &Vec<u8>) -> String {
55
xxh3_64(&input).to_string()
66
}
77

8+
// Converts the manifest to a string, and then hashes it
89
pub fn hash_manifest(input: &Vec<(String, String, bool)>) -> String {
910
// Not a true hash. Just a temporary place to dump data, which can then be hashed
1011
let mut current_hash = "".to_string();
@@ -28,4 +29,34 @@ mod tests {
2829
let result = hash(&vec![1, 2, 3]);
2930
assert_eq!(result, "16991689376074199867");
3031
}
32+
33+
#[test]
34+
fn hash_empty_vec() {
35+
let result = hash(&vec![]);
36+
assert_eq!(result, "3244421341483603138");
37+
}
38+
39+
#[test]
40+
fn hash_manifest_stable() {
41+
let manifest = vec![
42+
("file1.txt".to_string(), "hash1".to_string(), false),
43+
("file2.txt".to_string(), "hash2".to_string(), true),
44+
];
45+
let result = hash_manifest(&manifest);
46+
assert_eq!(result, "1259801786371591190");
47+
}
48+
49+
#[test]
50+
fn hash_manifest_empty() {
51+
let manifest: Vec<(String, String, bool)> = vec![];
52+
let result = hash_manifest(&manifest);
53+
assert_eq!(result, "3244421341483603138");
54+
}
55+
56+
#[test]
57+
fn hash_manifest_single_entry() {
58+
let manifest = vec![("main.rs".to_string(), "abc123".to_string(), true)];
59+
let result = hash_manifest(&manifest);
60+
assert_eq!(result, "9815591975043689442");
61+
}
3162
}

src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::{
2+
env::temp_dir,
23
fs::{self, create_dir_all, rename},
4+
option,
35
os::unix::fs::{PermissionsExt, symlink},
46
path::Path,
57
};
68

7-
use rand::RngCore;
8-
99
use crate::{artifacts::get_artifact, compression::decompress_file};
1010

1111
mod artifacts;
@@ -155,7 +155,7 @@ pub fn install_artifact(artifact_name: &String, store_path: &Path, repo_cache_pa
155155
}
156156

157157
// Create a temporary symlink for atomic update
158-
let tmp_file_name = format!(".tmp_{}", rand::rng().next_u64());
158+
let tmp_file_name = get_temp_file(None, &store_artifacts_path);
159159

160160
let tmp_symlink = store_artifacts_path.join(&tmp_file_name);
161161
let final_symlink = store_artifacts_path.join(artifact_name);
@@ -164,6 +164,18 @@ pub fn install_artifact(artifact_name: &String, store_path: &Path, repo_cache_pa
164164
rename(&tmp_symlink, &final_symlink).unwrap();
165165
}
166166

167+
fn get_temp_file(potential: Option<u8>, dir: &Path) -> String {
168+
let potential = potential.unwrap_or_default();
169+
170+
let file_name = format!(".tmp_{}", &potential);
171+
172+
return if dir.join(&file_name).exists() {
173+
get_temp_file(Some(potential + 1), dir)
174+
} else {
175+
file_name
176+
};
177+
}
178+
167179
fn make_chunk_executable(chunk_hash: &String, store_path: &Path) {
168180
let chunk_path = store_path.join("chunks").join(chunk_hash);
169181

0 commit comments

Comments
 (0)