Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/coverage_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ impl CoveragePrinter {
}
}
}
// Output to a file is buffered, so the last writes only reach the disk
// here; a failure (e.g. a full disk) must not leave a truncated file
// behind a successful exit.
if let Err(e) = print_stream.flush() {
error!("Failed to write output: {e}");
process::exit(1);
}
}

pub fn print_headers(
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ impl FlagFilter {
}

pub struct OutputWriter {
pub output_file: Option<Arc<std::sync::Mutex<std::fs::File>>>,
// Buffered: output is written in many small pieces (one per field), and an
// unbuffered File turns each into a write syscall. Flushed, with errors
// checked, by CoveragePrinter::finalise_printing.
pub output_file: Option<Arc<std::sync::Mutex<std::io::BufWriter<std::fs::File>>>>,
}

impl OutputWriter {
Expand All @@ -95,9 +98,9 @@ impl OutputWriter {
info!("Writing output to file: {file_path}");
OutputWriter {
output_file: Some(Arc::new(std::sync::Mutex::new(
std::fs::File::create(path).unwrap_or_else(|_| {
panic!("Failed to create output file: {}", file_path)
}),
std::io::BufWriter::new(std::fs::File::create(path).unwrap_or_else(
|_| panic!("Failed to create output file: {}", file_path),
)),
))),
}
}
Expand Down
Loading