@@ -13,7 +13,7 @@ use object::read::macho::FatArch;
13
13
use tempfile:: Builder as TempFileBuilder ;
14
14
15
15
use std:: error:: Error ;
16
- use std:: fs:: File ;
16
+ use std:: fs:: { self , File } ;
17
17
use std:: io:: { self , Write } ;
18
18
use std:: path:: { Path , PathBuf } ;
19
19
@@ -280,22 +280,33 @@ impl<'a> ArArchiveBuilder<'a> {
280
280
// This prevents programs (including rustc) from attempting to read a partial archive.
281
281
// It also enables writing an archive with the same filename as a dependency on Windows as
282
282
// required by a test.
283
- let mut archive_tmpfile = TempFileBuilder :: new ( )
283
+ // The tempfile crate currently uses 0o600 as mode for the temporary files and directories
284
+ // it creates. We need it to be the default mode for back compat reasons however. (See
285
+ // #107495) To handle this we are telling tempfile to create a temporary directory instead
286
+ // and then inside this directory create a file using File::create.
287
+ let archive_tmpdir = TempFileBuilder :: new ( )
284
288
. suffix ( ".temp-archive" )
285
- . tempfile_in ( output. parent ( ) . unwrap_or_else ( || Path :: new ( "" ) ) )
286
- . map_err ( |err| io_error_context ( "couldn't create a temp file" , err) ) ?;
289
+ . tempdir_in ( output. parent ( ) . unwrap_or_else ( || Path :: new ( "" ) ) )
290
+ . map_err ( |err| {
291
+ io_error_context ( "couldn't create a directory for the temp file" , err)
292
+ } ) ?;
293
+ let archive_tmpfile_path = archive_tmpdir. path ( ) . join ( "tmp.a" ) ;
294
+ let mut archive_tmpfile = File :: create_new ( & archive_tmpfile_path)
295
+ . map_err ( |err| io_error_context ( "couldn't create the temp file" , err) ) ?;
287
296
288
- write_archive_to_stream ( archive_tmpfile. as_file_mut ( ) , & entries, archive_kind, false ) ?;
297
+ write_archive_to_stream ( & mut archive_tmpfile, & entries, archive_kind, false ) ?;
289
298
290
299
let any_entries = !entries. is_empty ( ) ;
291
300
drop ( entries) ;
292
301
// Drop src_archives to unmap all input archives, which is necessary if we want to write the
293
302
// output archive to the same location as an input archive on Windows.
294
303
drop ( self . src_archives ) ;
295
304
296
- archive_tmpfile
297
- . persist ( output)
298
- . map_err ( |err| io_error_context ( "failed to rename archive file" , err. error ) ) ?;
305
+ fs:: rename ( archive_tmpfile_path, output)
306
+ . map_err ( |err| io_error_context ( "failed to rename archive file" , err) ) ?;
307
+ archive_tmpdir
308
+ . close ( )
309
+ . map_err ( |err| io_error_context ( "failed to remove temporary directory" , err) ) ?;
299
310
300
311
Ok ( any_entries)
301
312
}
0 commit comments