Skip to content

Commit

Permalink
Always hash the file prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Jan 23, 2024
1 parent 1316b84 commit cbe0137
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
34 changes: 15 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3820,25 +3820,21 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(),
fn objects_from_files(files: &[Arc<Path>], dst: &Path) -> Result<Vec<Object>, 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)?,
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit cbe0137

Please sign in to comment.