Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ fn safe_du(
seen_inodes: &mut HashSet<FileInfo>,
print_tx: &mpsc::Sender<UResult<StatPrintInfo>>,
parent_fd: Option<&DirFd>,
initial_stat: Option<std::io::Result<Stat>>,
) -> Result<Stat, Box<mpsc::SendError<UResult<StatPrintInfo>>>> {
// Get initial stat for this path - use DirFd if available to avoid path length issues
let mut my_stat = if let Some(parent_fd) = parent_fd {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing to read, my_stat is immediately shadowed at the beginning of the function. Maybe it would be better to change the naming of it to indicate the difference between the two?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it would be better to change the name. I thought about that too.
Renamed

Expand Down Expand Up @@ -354,7 +355,12 @@ fn safe_du(
}
} else {
// This is the initial directory - try regular Stat::new first, then fallback to DirFd
match Stat::new(path, None, options) {
let initial_stat = match initial_stat {
Some(s) => s,
None => Stat::new(path, None, options),
};

match initial_stat {
Ok(s) => s,
Err(_e) => {
// Try using our new DirFd method for the root directory
Expand Down Expand Up @@ -530,6 +536,7 @@ fn safe_du(
seen_inodes,
print_tx,
Some(&dir_fd),
None,
)?;

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

// Pre-populate seen_inodes with the starting directory to detect cycles
let stat = Stat::new(&path, None, &traversal_options);
if let Ok(stat) = stat.as_ref() {
if let Some(inode) = stat.inode {
if !traversal_options.count_links && seen_inodes.contains(&inode) {
continue 'loop_file;
}
seen_inodes.insert(inode);
}
}

if use_safe_traversal {
// Use safe traversal (Unix except Redox, when not using -L)
#[cfg(all(unix, not(target_os = "redox")))]
{
// Pre-populate seen_inodes with the starting directory to detect cycles
if let Ok(stat) = Stat::new(&path, None, &traversal_options) {
if let Some(inode) = stat.inode {
if !traversal_options.count_links && seen_inodes.contains(&inode) {
continue 'loop_file;
}
seen_inodes.insert(inode);
}
}

match safe_du(
&path,
&traversal_options,
0,
&mut seen_inodes,
&print_tx,
None,
Some(stat),
) {
Ok(stat) => {
print_tx
Expand All @@ -1148,13 +1157,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
} else {
// Use regular traversal (non-Linux or when -L is used)
if let Ok(stat) = Stat::new(&path, None, &traversal_options) {
if let Some(inode) = stat.inode {
if !traversal_options.count_links && seen_inodes.contains(&inode) {
continue 'loop_file;
}
seen_inodes.insert(inode);
}
if let Ok(stat) = stat {
let stat = du_regular(
stat,
&traversal_options,
Expand Down
Loading