From 29d0b4f177d9da6be203ab29261ef8a7962d2b93 Mon Sep 17 00:00:00 2001 From: binhvq Date: Wed, 11 Nov 2026 15:07:52 +0700 Subject: [PATCH 1/2] diff: only report a recursive directory loop when both sides loop One list of ancestors was shared by both sides, so -r stopped with 'recursive directory loop' as soon as either directory was a link back to a parent. Check each side against its own ancestors, and give up only when both loop or the other side is missing: a real directory on the other side ends the recursion. --- src/dir_diff.rs | 49 ++++++++++++++++++++---------------- tests/by-util/test_diff.rs | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/dir_diff.rs b/src/dir_diff.rs index 9d82331c..b59e2b20 100644 --- a/src/dir_diff.rs +++ b/src/dir_diff.rs @@ -37,29 +37,37 @@ pub fn compare(params: &Params, option_args: &[OsString]) -> Outcome { struct DirectoryWalker<'a> { params: &'a Params, option_args: &'a [OsString], - /// Canonical paths of the directories being compared, from the top, to - /// detect symbolic links looping back to one of them. - ancestors: Vec, + /// Canonical paths of the pairs of directories being compared, from the + /// top, to detect symbolic links looping back to one of them. A directory + /// missing on purpose (-N) has none. + ancestors: Vec<[Option; 2]>, } impl DirectoryWalker<'_> { fn compare_directories(&mut self, from_dir: &OsStr, to_dir: &OsStr) -> Outcome { let mut outcome = Outcome::Identical; - let mut canonical_dirs = Vec::new(); - for dir in [from_dir, to_dir] { - let Ok(canonical) = fs::canonicalize(dir) else { - // A directory missing on purpose (-N) has no entries. - continue; - }; - if self.ancestors.contains(&canonical) { - eprintln!( - "{}: {}: recursive directory loop", - self.params.executable.to_string_lossy(), - dir.to_string_lossy() - ); - return Outcome::Trouble; - } - canonical_dirs.push(canonical); + let dirs = [from_dir, to_dir]; + let canonical_dirs = dirs.map(|dir| fs::canonicalize(dir).ok()); + // A side is checked against its own ancestors only. As long as the + // other side is a directory that is not one of its ancestors, that + // side's finite tree ends the recursion. + let loops = |side: usize| { + canonical_dirs[side].as_ref().is_none_or(|dir| { + let mut ancestors = self.ancestors.iter().map(|pair| pair[side].as_ref()); + ancestors.any(|ancestor| ancestor == Some(dir)) + }) + }; + if let (true, true, Some(side)) = ( + loops(0), + loops(1), + canonical_dirs.iter().position(Option::is_some), + ) { + eprintln!( + "{}: {}: recursive directory loop", + self.params.executable.to_string_lossy(), + dirs[side].to_string_lossy() + ); + return Outcome::Trouble; } let (Some(mut from_names), Some(mut to_names)) = (self.list(from_dir), self.list(to_dir)) else { @@ -71,8 +79,7 @@ impl DirectoryWalker<'_> { from_names.retain(|name| *self.name_key(name) >= *start); to_names.retain(|name| *self.name_key(name) >= *start); } - let ancestors_len = self.ancestors.len(); - self.ancestors.extend(canonical_dirs); + self.ancestors.push(canonical_dirs); let entries = pair_entries(&from_names, &to_names, |from, to| { self.name_key(from).cmp(&self.name_key(to)) @@ -95,7 +102,7 @@ impl DirectoryWalker<'_> { }; outcome = outcome.max(entry_outcome); } - self.ancestors.truncate(ancestors_len); + self.ancestors.pop(); outcome } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 10fc0a10..f7a2fb9f 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -748,6 +748,57 @@ fn compare_directories_special_entries() -> Result<(), Box Result<(), Box> { + use std::os::unix::fs::symlink; + + let tmp_dir = tempdir()?; + let dir = tmp_dir.path(); + std::fs::create_dir_all(dir.join("one/sub"))?; + std::fs::create_dir_all(dir.join("two/sub/loop"))?; + symlink("..", dir.join("one/sub/loop"))?; + + for (from, to) in [("one", "two"), ("two", "one")] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.current_dir(dir).arg("diff").arg("-r").arg(from).arg(to); + cmd.assert() + .code(predicate::eq(1)) + .stdout("Only in one/sub/loop: sub\n") + .stderr(""); + } + + // With -N a missing directory is compared as an empty one, which would + // never end the recursion. + std::fs::remove_dir(dir.join("two/sub/loop"))?; + for (from, to) in [("one", "two"), ("two", "one")] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.current_dir(dir).arg("diff").arg("-r").arg("-N"); + cmd.arg(from).arg(to); + cmd.assert() + .code(predicate::eq(2)) + .stderr("diff: one/sub/loop: recursive directory loop\n"); + } + + // Each side is checked against its own ancestors: here the links lead to + // the top of the other side, and only loop one level further down. + std::fs::remove_file(dir.join("one/sub/loop"))?; + symlink("../../two", dir.join("one/sub/loop"))?; + symlink("../../one", dir.join("two/sub/loop"))?; + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.current_dir(dir) + .arg("diff") + .arg("-r") + .arg("one") + .arg("two"); + cmd.assert() + .code(predicate::eq(2)) + .stderr("diff: one/sub/loop/sub/loop: recursive directory loop\n"); + Ok(()) +} + #[test] fn directory_name_matching() -> Result<(), Box> { let tmp_dir = tempdir()?; From f4fc09fba078c44c29393edc8e235a677e1d20e2 Mon Sep 17 00:00:00 2001 From: binhvq Date: Wed, 11 Nov 2026 23:03:56 +0700 Subject: [PATCH 2/2] diff: check for a directory loop with a plain if --- src/dir_diff.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/dir_diff.rs b/src/dir_diff.rs index b59e2b20..96837831 100644 --- a/src/dir_diff.rs +++ b/src/dir_diff.rs @@ -57,17 +57,16 @@ impl DirectoryWalker<'_> { ancestors.any(|ancestor| ancestor == Some(dir)) }) }; - if let (true, true, Some(side)) = ( - loops(0), - loops(1), - canonical_dirs.iter().position(Option::is_some), - ) { - eprintln!( - "{}: {}: recursive directory loop", - self.params.executable.to_string_lossy(), - dirs[side].to_string_lossy() - ); - return Outcome::Trouble; + if loops(0) && loops(1) { + // Named after the first side that exists, as one may be missing (-N) + if let Some(side) = canonical_dirs.iter().position(Option::is_some) { + eprintln!( + "{}: {}: recursive directory loop", + self.params.executable.to_string_lossy(), + dirs[side].to_string_lossy() + ); + return Outcome::Trouble; + } } let (Some(mut from_names), Some(mut to_names)) = (self.list(from_dir), self.list(to_dir)) else {