Skip to content

Commit e779e97

Browse files
Rollup merge of rust-lang#122723 - bjorn3:archive_writer_fixes, r=nnethercote
Use same file permissions for ar_archive_writer as the LLVM archive writer This is required to switch to ar_archive_writer in the future without regressions. In addition to this PR support for reading thin archives needs to be added (rust-lang#107407) to fix all known regressions. Fixes rust-lang#107495
2 parents 01b16b5 + 2a805f5 commit e779e97

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

compiler/rustc_codegen_ssa/src/back/archive.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use object::read::macho::FatArch;
1313
use tempfile::Builder as TempFileBuilder;
1414

1515
use std::error::Error;
16-
use std::fs::File;
16+
use std::fs::{self, File};
1717
use std::io::{self, Write};
1818
use std::path::{Path, PathBuf};
1919

@@ -276,29 +276,34 @@ impl<'a> ArArchiveBuilder<'a> {
276276
// This prevents programs (including rustc) from attempting to read a partial archive.
277277
// It also enables writing an archive with the same filename as a dependency on Windows as
278278
// required by a test.
279-
let mut archive_tmpfile = TempFileBuilder::new()
279+
// The tempfile crate currently uses 0o600 as mode for the temporary files and directories
280+
// it creates. We need it to be the default mode for back compat reasons however. (See
281+
// #107495) To handle this we are telling tempfile to create a temporary directory instead
282+
// and then inside this directory create a file using File::create.
283+
let archive_tmpdir = TempFileBuilder::new()
280284
.suffix(".temp-archive")
281-
.tempfile_in(output.parent().unwrap_or_else(|| Path::new("")))
282-
.map_err(|err| io_error_context("couldn't create a temp file", err))?;
283-
284-
write_archive_to_stream(
285-
archive_tmpfile.as_file_mut(),
286-
&entries,
287-
true,
288-
archive_kind,
289-
true,
290-
false,
291-
)?;
285+
.tempdir_in(output.parent().unwrap_or_else(|| Path::new("")))
286+
.map_err(|err| {
287+
io_error_context("couldn't create a directory for the temp file", err)
288+
})?;
289+
let archive_tmpfile_path = archive_tmpdir.path().join("tmp.a");
290+
let mut archive_tmpfile = File::create_new(&archive_tmpfile_path)
291+
.map_err(|err| io_error_context("couldn't create the temp file", err))?;
292+
293+
write_archive_to_stream(&mut archive_tmpfile, &entries, true, archive_kind, true, false)?;
294+
drop(archive_tmpfile);
292295

293296
let any_entries = !entries.is_empty();
294297
drop(entries);
295298
// Drop src_archives to unmap all input archives, which is necessary if we want to write the
296299
// output archive to the same location as an input archive on Windows.
297300
drop(self.src_archives);
298301

299-
archive_tmpfile
300-
.persist(output)
301-
.map_err(|err| io_error_context("failed to rename archive file", err.error))?;
302+
fs::rename(archive_tmpfile_path, output)
303+
.map_err(|err| io_error_context("failed to rename archive file", err))?;
304+
archive_tmpdir
305+
.close()
306+
.map_err(|err| io_error_context("failed to remove temporary directory", err))?;
302307

303308
Ok(any_entries)
304309
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Empty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{aux_build, out_dir};
4+
use std::fs;
5+
#[cfg(unix)]
6+
use std::os::unix::fs::PermissionsExt;
7+
use std::path::Path;
8+
9+
fn main() {
10+
aux_build().arg("foo.rs").run();
11+
verify(&out_dir().join("libfoo.rlib"));
12+
}
13+
14+
fn verify(path: &Path) {
15+
let perm = fs::metadata(path).unwrap().permissions();
16+
17+
assert!(!perm.readonly());
18+
19+
// Check that the file is readable for everyone
20+
#[cfg(unix)]
21+
assert_eq!(perm.mode(), 0o100664);
22+
}

0 commit comments

Comments
 (0)