diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2021-03-17 18:31:21 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2021-08-28 21:45:02 +0200 |
| commit | 6b47e1ece87f8cb96709b772dbea1a2a979c1cbd (patch) | |
| tree | a82baf6abd1bdba516be309b68dfd06ae47433e0 /compiler/rustc_incremental/src | |
| parent | 4afdeaaabd021bf5ac03d74c7577747ccbb926d0 (diff) | |
| download | rust-6b47e1ece87f8cb96709b772dbea1a2a979c1cbd.tar.gz rust-6b47e1ece87f8cb96709b772dbea1a2a979c1cbd.zip | |
Move save_in to file_format.
Diffstat (limited to 'compiler/rustc_incremental/src')
| -rw-r--r-- | compiler/rustc_incremental/src/persist/file_format.rs | 57 | ||||
| -rw-r--r-- | compiler/rustc_incremental/src/persist/save.rs | 62 |
2 files changed, 61 insertions, 58 deletions
diff --git a/compiler/rustc_incremental/src/persist/file_format.rs b/compiler/rustc_incremental/src/persist/file_format.rs index 501f6bdb9cf..2da72bfc292 100644 --- a/compiler/rustc_incremental/src/persist/file_format.rs +++ b/compiler/rustc_incremental/src/persist/file_format.rs @@ -12,11 +12,12 @@ use std::env; use std::fs; use std::io::{self, Read}; -use std::path::Path; +use std::path::{Path, PathBuf}; use rustc_data_structures::memmap::Mmap; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_serialize::Encoder; +use rustc_session::Session; /// The first few bytes of files generated by incremental compilation. const FILE_MAGIC: &[u8] = b"RSIC"; @@ -29,7 +30,7 @@ const HEADER_FORMAT_VERSION: u16 = 0; /// the Git commit hash. const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); -pub fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult { +pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult { stream.emit_raw_bytes(FILE_MAGIC)?; stream.emit_raw_bytes(&[ (HEADER_FORMAT_VERSION >> 0) as u8, @@ -42,6 +43,58 @@ pub fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileE stream.emit_raw_bytes(rustc_version.as_bytes()) } +pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F) +where + F: FnOnce(&mut FileEncoder) -> FileEncodeResult, +{ + debug!("save: storing data in {}", path_buf.display()); + + // Delete the old file, if any. + // Note: It's important that we actually delete the old file and not just + // truncate and overwrite it, since it might be a shared hard-link, the + // underlying data of which we don't want to modify + match fs::remove_file(&path_buf) { + Ok(()) => { + debug!("save: remove old file"); + } + Err(err) if err.kind() == io::ErrorKind::NotFound => (), + Err(err) => { + sess.err(&format!( + "unable to delete old {} at `{}`: {}", + name, + path_buf.display(), + err + )); + return; + } + } + + let mut encoder = match FileEncoder::new(&path_buf) { + Ok(encoder) => encoder, + Err(err) => { + sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err)); + return; + } + }; + + if let Err(err) = write_file_header(&mut encoder, sess.is_nightly_build()) { + sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err)); + return; + } + + if let Err(err) = encode(&mut encoder) { + sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err)); + return; + } + + if let Err(err) = encoder.flush() { + sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err)); + return; + } + + debug!("save: data written to disk successfully"); +} + /// Reads the contents of a file with a file header as defined in this module. /// /// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index a8455854ebb..2feba71e010 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -6,8 +6,6 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_serialize::Encodable as RustcEncodable; use rustc_session::Session; use std::fs; -use std::io; -use std::path::PathBuf; use super::data::*; use super::dirty_clean; @@ -44,7 +42,9 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) { join( move || { sess.time("incr_comp_persist_result_cache", || { - save_in(sess, query_cache_path, "query cache", |e| encode_query_cache(tcx, e)); + file_format::save_in(sess, query_cache_path, "query cache", |e| { + encode_query_cache(tcx, e) + }); }); }, move || { @@ -86,7 +86,9 @@ pub fn save_work_product_index( debug!("save_work_product_index()"); dep_graph.assert_ignored(); let path = work_products_path(sess); - save_in(sess, path, "work product index", |e| encode_work_product_index(&new_work_products, e)); + file_format::save_in(sess, path, "work product index", |e| { + encode_work_product_index(&new_work_products, e) + }); // We also need to clean out old work-products, as not all of them are // deleted during invalidation. Some object files don't change their @@ -113,58 +115,6 @@ pub fn save_work_product_index( }); } -pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F) -where - F: FnOnce(&mut FileEncoder) -> FileEncodeResult, -{ - debug!("save: storing data in {}", path_buf.display()); - - // Delete the old file, if any. - // Note: It's important that we actually delete the old file and not just - // truncate and overwrite it, since it might be a shared hard-link, the - // underlying data of which we don't want to modify - match fs::remove_file(&path_buf) { - Ok(()) => { - debug!("save: remove old file"); - } - Err(err) if err.kind() == io::ErrorKind::NotFound => (), - Err(err) => { - sess.err(&format!( - "unable to delete old {} at `{}`: {}", - name, - path_buf.display(), - err - )); - return; - } - } - - let mut encoder = match FileEncoder::new(&path_buf) { - Ok(encoder) => encoder, - Err(err) => { - sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err)); - return; - } - }; - - if let Err(err) = file_format::write_file_header(&mut encoder, sess.is_nightly_build()) { - sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err)); - return; - } - - if let Err(err) = encode(&mut encoder) { - sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err)); - return; - } - - if let Err(err) = encoder.flush() { - sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err)); - return; - } - - debug!("save: data written to disk successfully"); -} - fn encode_work_product_index( work_products: &FxHashMap<WorkProductId, WorkProduct>, encoder: &mut FileEncoder, |
