Don't panic on io errors

Fixes #920
This commit is contained in:
bjorn3 2020-08-08 16:14:11 +02:00
parent 5afffb823c
commit 240e178532
4 changed files with 35 additions and 23 deletions

View File

@ -149,6 +149,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
Gnu(ar::GnuBuilder<File>), Gnu(ar::GnuBuilder<File>),
} }
let sess = self.config.sess;
let mut symbol_table = BTreeMap::new(); let mut symbol_table = BTreeMap::new();
let mut entries = Vec::new(); let mut entries = Vec::new();
@ -169,10 +171,11 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
let mut data = Vec::new(); let mut data = Vec::new();
entry.read_to_end(&mut data).unwrap(); entry.read_to_end(&mut data).unwrap();
data data
} }
ArchiveEntry::File(file) => { ArchiveEntry::File(file) => {
std::fs::read(file).unwrap() std::fs::read(file).unwrap_or_else(|err| {
sess.fatal(&format!("error while reading object file during archive building: {}", err));
})
} }
}; };
@ -192,7 +195,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
if err == "Unknown file magic" { if err == "Unknown file magic" {
// Not an object file; skip it. // Not an object file; skip it.
} else { } else {
self.config.sess.fatal(&format!("Error parsing `{}` during archive creation: {}", entry_name, err)); sess.fatal(&format!("error parsing `{}` during archive creation: {}", entry_name, err));
} }
} }
} }
@ -203,7 +206,9 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
let mut builder = if self.config.use_gnu_style_archive { let mut builder = if self.config.use_gnu_style_archive {
BuilderKind::Gnu(ar::GnuBuilder::new( BuilderKind::Gnu(ar::GnuBuilder::new(
File::create(&self.config.dst).unwrap(), File::create(&self.config.dst).unwrap_or_else(|err| {
sess.fatal(&format!("error opening destination during archive building: {}", err));
}),
entries entries
.iter() .iter()
.map(|(name, _)| name.as_bytes().to_vec()) .map(|(name, _)| name.as_bytes().to_vec())
@ -213,7 +218,9 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
).unwrap()) ).unwrap())
} else { } else {
BuilderKind::Bsd(ar::Builder::new( BuilderKind::Bsd(ar::Builder::new(
File::create(&self.config.dst).unwrap(), File::create(&self.config.dst).unwrap_or_else(|err| {
sess.fatal(&format!("error opening destination during archive building: {}", err));
}),
symbol_table, symbol_table,
).unwrap()) ).unwrap())
}; };
@ -260,8 +267,10 @@ impl<'a> ArArchiveBuilder<'a> {
let mut i = 0; let mut i = 0;
while let Some(entry) = archive.next_entry() { while let Some(entry) = archive.next_entry() {
let entry = entry.unwrap(); let entry = entry?;
let file_name = String::from_utf8(entry.header().identifier().to_vec()).unwrap(); let file_name = String::from_utf8(entry.header().identifier().to_vec()).map_err(|err| {
std::io::Error::new(std::io::ErrorKind::InvalidData, err)
})?;
if !skip(&file_name) { if !skip(&file_name) {
self.entries.push(( self.entries.push((
file_name, file_name,

View File

@ -54,7 +54,9 @@ fn emit_module<B: Backend>(
.output_filenames(LOCAL_CRATE) .output_filenames(LOCAL_CRATE)
.temp_path(OutputType::Object, Some(&name)); .temp_path(OutputType::Object, Some(&name));
let obj = product.emit(); let obj = product.emit();
std::fs::write(&tmp_file, obj).unwrap(); if let Err(err) = std::fs::write(&tmp_file, obj) {
tcx.sess.fatal(&format!("error writing object file: {}", err));
}
let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() { let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() {
None None
@ -254,7 +256,9 @@ pub(super) fn run_aot(
crate::metadata::write_metadata(tcx, object); crate::metadata::write_metadata(tcx, object);
}); });
std::fs::write(&tmp_file, obj).unwrap(); if let Err(err) = std::fs::write(&tmp_file, obj) {
tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
}
(metadata_cgu_name, tmp_file) (metadata_cgu_name, tmp_file)
}); });

View File

@ -1,4 +1,4 @@
#![feature(rustc_private, decl_macro, type_alias_impl_trait, associated_type_bounds, never_type)] #![feature(rustc_private, decl_macro, type_alias_impl_trait, associated_type_bounds, never_type, try_blocks)]
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
#![warn(unused_lifetimes)] #![warn(unused_lifetimes)]

View File

@ -243,19 +243,18 @@ pub(crate) fn write_clif_file<'tcx>(
) )
.unwrap(); .unwrap();
match ::std::fs::File::create(clif_file_name) { let res: std::io::Result<()> = try {
Ok(mut file) => { let mut file = std::fs::File::create(clif_file_name)?;
let target_triple = crate::target_triple(tcx.sess); let target_triple = crate::target_triple(tcx.sess);
writeln!(file, "test compile").unwrap(); writeln!(file, "test compile")?;
writeln!(file, "set is_pic").unwrap(); writeln!(file, "set is_pic")?;
writeln!(file, "set enable_simd").unwrap(); writeln!(file, "set enable_simd")?;
writeln!(file, "target {} haswell", target_triple).unwrap(); writeln!(file, "target {} haswell", target_triple)?;
writeln!(file, "").unwrap(); writeln!(file, "")?;
file.write(clif.as_bytes()).unwrap(); file.write(clif.as_bytes())?;
} };
Err(e) => { if let Err(err) = res {
tcx.sess.warn(&format!("err opening clif file: {:?}", e)); tcx.sess.warn(&format!("err writing clif file: {}", err));
}
} }
} }