Skip to content

Commit 39f5e72

Browse files
committed
refactor(sort): replace unsafe libc fcntl with nix and add constants for salt generation
- Remove libc dependency from Cargo.toml - Use nix::fcntl instead of unsafe libc::fcntl in current_open_fd_count - Add constants for salt length, buffer sizes, and domain separation tag - Improve documentation and code clarity in salt_from_random_source
1 parent e8c1533 commit 39f5e72

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/uu/sort/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ compare = { workspace = true }
2727
ctrlc = { workspace = true }
2828
fnv = { workspace = true }
2929
itertools = { workspace = true }
30-
libc = { workspace = true }
3130
memchr = { workspace = true }
3231
rand = { workspace = true }
3332
rayon = { workspace = true }

src/uu/sort/src/sort.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ pub(crate) fn fd_soft_limit() -> Option<usize> {
11091109

11101110
#[cfg(unix)]
11111111
pub(crate) fn current_open_fd_count() -> Option<usize> {
1112+
use nix::fcntl::{FcntlArg, fcntl};
1113+
11121114
fn count_dir(path: &str) -> Option<usize> {
11131115
let entries = std::fs::read_dir(path).ok()?;
11141116
let mut count = 0usize;
@@ -1133,7 +1135,7 @@ pub(crate) fn current_open_fd_count() -> Option<usize> {
11331135

11341136
let mut count = 0usize;
11351137
for fd in 0..limit {
1136-
if unsafe { libc::fcntl(fd as i32, libc::F_GETFD) } != -1 {
1138+
if fcntl(fd as i32, FcntlArg::F_GETFD).is_ok() {
11371139
count = count.saturating_add(1);
11381140
}
11391141
}
@@ -2198,14 +2200,21 @@ fn general_numeric_compare(
21982200
a.partial_cmp(b).unwrap()
21992201
}
22002202

2201-
fn get_rand_string() -> [u8; 16] {
2203+
/// Generate a 128-bit salt from a uniform RNG distribution.
2204+
fn get_rand_string() -> [u8; SALT_LEN] {
22022205
rng().sample(rand::distr::StandardUniform)
22032206
}
22042207

2205-
fn salt_from_random_source(path: &Path) -> UResult<[u8; 16]> {
2206-
const MAX_BYTES: usize = 1024 * 1024;
2208+
const SALT_LEN: usize = 16; // 128-bit salt
2209+
const MAX_BYTES: usize = 1024 * 1024; // Read cap: 1 MiB
2210+
const BUF_LEN: usize = 8192; // 8 KiB read buffer
2211+
const U64_LEN: usize = 8;
2212+
const RANDOM_SOURCE_TAG: &[u8] = b"uutils-sort-random-source"; // Domain separation tag
2213+
2214+
/// Create a 128-bit salt by hashing up to 1 MiB from the given file.
2215+
fn salt_from_random_source(path: &Path) -> UResult<[u8; SALT_LEN]> {
22072216
let mut reader = open_with_open_failed_error(path)?;
2208-
let mut buf = [0u8; 8192];
2217+
let mut buf = [0u8; BUF_LEN];
22092218
let mut total = 0usize;
22102219
let mut hasher = FnvHasher::default();
22112220

@@ -2233,13 +2242,13 @@ fn salt_from_random_source(path: &Path) -> UResult<[u8; 16]> {
22332242

22342243
let first = hasher.finish();
22352244
let mut second_hasher = FnvHasher::default();
2236-
second_hasher.write(b"uutils-sort-random-source");
2245+
second_hasher.write(RANDOM_SOURCE_TAG);
22372246
second_hasher.write_u64(first);
22382247
let second = second_hasher.finish();
22392248

2240-
let mut out = [0u8; 16];
2241-
out[..8].copy_from_slice(&first.to_le_bytes());
2242-
out[8..].copy_from_slice(&second.to_le_bytes());
2249+
let mut out = [0u8; SALT_LEN];
2250+
out[..U64_LEN].copy_from_slice(&first.to_le_bytes());
2251+
out[U64_LEN..].copy_from_slice(&second.to_le_bytes());
22432252
Ok(out)
22442253
}
22452254

0 commit comments

Comments
 (0)