Skip to content

Commit cb0ae79

Browse files
authored
du: deduplicate Stat::new call (#10584)
* du: deduplicate Stat::new call * du: pass Stat::new result into the safe_du func * du: apply stylistic changes * du: rename the my_stat parameter for clarity
1 parent daf8fb5 commit cb0ae79

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/uu/du/src/du.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ fn safe_du(
303303
seen_inodes: &mut HashSet<FileInfo>,
304304
print_tx: &mpsc::Sender<UResult<StatPrintInfo>>,
305305
parent_fd: Option<&DirFd>,
306+
initial_stat: Option<std::io::Result<Stat>>,
306307
) -> Result<Stat, Box<mpsc::SendError<UResult<StatPrintInfo>>>> {
307308
// Get initial stat for this path - use DirFd if available to avoid path length issues
308309
let mut my_stat = if let Some(parent_fd) = parent_fd {
@@ -354,7 +355,12 @@ fn safe_du(
354355
}
355356
} else {
356357
// This is the initial directory - try regular Stat::new first, then fallback to DirFd
357-
match Stat::new(path, None, options) {
358+
let initial_stat = match initial_stat {
359+
Some(s) => s,
360+
None => Stat::new(path, None, options),
361+
};
362+
363+
match initial_stat {
358364
Ok(s) => s,
359365
Err(_e) => {
360366
// Try using our new DirFd method for the root directory
@@ -530,6 +536,7 @@ fn safe_du(
530536
seen_inodes,
531537
print_tx,
532538
Some(&dir_fd),
539+
None,
533540
)?;
534541

535542
if !options.separate_dirs {
@@ -1107,27 +1114,29 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
11071114
#[cfg(not(all(unix, not(target_os = "redox"))))]
11081115
let use_safe_traversal = false;
11091116

1117+
// Pre-populate seen_inodes with the starting directory to detect cycles
1118+
let stat = Stat::new(&path, None, &traversal_options);
1119+
if let Ok(stat) = stat.as_ref() {
1120+
if let Some(inode) = stat.inode {
1121+
if !traversal_options.count_links && seen_inodes.contains(&inode) {
1122+
continue 'loop_file;
1123+
}
1124+
seen_inodes.insert(inode);
1125+
}
1126+
}
1127+
11101128
if use_safe_traversal {
11111129
// Use safe traversal (Unix except Redox, when not using -L)
11121130
#[cfg(all(unix, not(target_os = "redox")))]
11131131
{
1114-
// Pre-populate seen_inodes with the starting directory to detect cycles
1115-
if let Ok(stat) = Stat::new(&path, None, &traversal_options) {
1116-
if let Some(inode) = stat.inode {
1117-
if !traversal_options.count_links && seen_inodes.contains(&inode) {
1118-
continue 'loop_file;
1119-
}
1120-
seen_inodes.insert(inode);
1121-
}
1122-
}
1123-
11241132
match safe_du(
11251133
&path,
11261134
&traversal_options,
11271135
0,
11281136
&mut seen_inodes,
11291137
&print_tx,
11301138
None,
1139+
Some(stat),
11311140
) {
11321141
Ok(stat) => {
11331142
print_tx
@@ -1148,13 +1157,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
11481157
}
11491158
} else {
11501159
// Use regular traversal (non-Linux or when -L is used)
1151-
if let Ok(stat) = Stat::new(&path, None, &traversal_options) {
1152-
if let Some(inode) = stat.inode {
1153-
if !traversal_options.count_links && seen_inodes.contains(&inode) {
1154-
continue 'loop_file;
1155-
}
1156-
seen_inodes.insert(inode);
1157-
}
1160+
if let Ok(stat) = stat {
11581161
let stat = du_regular(
11591162
stat,
11601163
&traversal_options,

0 commit comments

Comments
 (0)