From cbe01376379aaf3e305e25b61d8ca3e2e8c2d6d0 Mon Sep 17 00:00:00 2001 From: roblabla Date: Tue, 23 Jan 2024 16:53:12 +0100 Subject: [PATCH] Always hash the file prefix --- src/lib.rs | 34 +++++++++++++++------------------- tests/test.rs | 4 ++-- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0187ec7d0..60c8e5359 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3820,25 +3820,21 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(), fn objects_from_files(files: &[Arc], dst: &Path) -> Result, Error> { let mut objects = Vec::with_capacity(files.len()); for file in files { - let obj = if file.has_root() || file.components().any(|x| x == Component::ParentDir) { - // If `file` is an absolute path or might not be usable directly as a suffix due to - // using "..", use the `basename` prefixed with the `dirname`'s hash to ensure name - // uniqueness. - let basename = file - .file_name() - .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "file_name() failure"))? - .to_string_lossy(); - let dirname = file - .parent() - .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "parent() failure"))? - .to_string_lossy(); - let mut hasher = hash_map::DefaultHasher::new(); - hasher.write(dirname.to_string().as_bytes()); - dst.join(format!("{:016x}-{}", hasher.finish(), basename)) - .with_extension("o") - } else { - dst.join(file).with_extension("o") - }; + let basename = file + .file_name() + .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "file_name() failure"))? + .to_string_lossy(); + let dirname = file + .parent() + .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "parent() failure"))? + .to_string_lossy(); + + // Hash the dirname. This should prevent conflicts if we have multiple + // object files with the same filename in different subfolders. + let mut hasher = hash_map::DefaultHasher::new(); + hasher.write(dirname.to_string().as_bytes()); + let obj = dst.join(format!("{:016x}-{}", hasher.finish(), basename)) + .with_extension("o"); match obj.parent() { Some(s) => fs::create_dir_all(s)?, diff --git a/tests/test.rs b/tests/test.rs index f588a6cae..4ca939c4e 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -24,7 +24,7 @@ fn gnu_smoke() { .must_have("-c") .must_have("-ffunction-sections") .must_have("-fdata-sections"); - test.cmd(1).must_have(test.td.path().join("foo.o")); + test.cmd(1).must_have(test.td.path().join("d1fba762150c532c-foo.o")); } #[test] @@ -399,7 +399,7 @@ fn msvc_smoke() { .must_not_have("-Z7") .must_have("-c") .must_have("-MD"); - test.cmd(1).must_have(test.td.path().join("foo.o")); + test.cmd(1).must_have(test.td.path().join("d1fba762150c532c-foo.o")); } #[test]