Skip to content

Commit 6b47e1e

Browse files
committed
Move save_in to file_format.
1 parent 4afdeaa commit 6b47e1e

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
use std::env;
1313
use std::fs;
1414
use std::io::{self, Read};
15-
use std::path::Path;
15+
use std::path::{Path, PathBuf};
1616

1717
use rustc_data_structures::memmap::Mmap;
1818
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
1919
use rustc_serialize::Encoder;
20+
use rustc_session::Session;
2021

2122
/// The first few bytes of files generated by incremental compilation.
2223
const FILE_MAGIC: &[u8] = b"RSIC";
@@ -29,7 +30,7 @@ const HEADER_FORMAT_VERSION: u16 = 0;
2930
/// the Git commit hash.
3031
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
3132

32-
pub fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult {
33+
pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult {
3334
stream.emit_raw_bytes(FILE_MAGIC)?;
3435
stream.emit_raw_bytes(&[
3536
(HEADER_FORMAT_VERSION >> 0) as u8,
@@ -42,6 +43,58 @@ pub fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileE
4243
stream.emit_raw_bytes(rustc_version.as_bytes())
4344
}
4445

46+
pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
47+
where
48+
F: FnOnce(&mut FileEncoder) -> FileEncodeResult,
49+
{
50+
debug!("save: storing data in {}", path_buf.display());
51+
52+
// Delete the old file, if any.
53+
// Note: It's important that we actually delete the old file and not just
54+
// truncate and overwrite it, since it might be a shared hard-link, the
55+
// underlying data of which we don't want to modify
56+
match fs::remove_file(&path_buf) {
57+
Ok(()) => {
58+
debug!("save: remove old file");
59+
}
60+
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
61+
Err(err) => {
62+
sess.err(&format!(
63+
"unable to delete old {} at `{}`: {}",
64+
name,
65+
path_buf.display(),
66+
err
67+
));
68+
return;
69+
}
70+
}
71+
72+
let mut encoder = match FileEncoder::new(&path_buf) {
73+
Ok(encoder) => encoder,
74+
Err(err) => {
75+
sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err));
76+
return;
77+
}
78+
};
79+
80+
if let Err(err) = write_file_header(&mut encoder, sess.is_nightly_build()) {
81+
sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err));
82+
return;
83+
}
84+
85+
if let Err(err) = encode(&mut encoder) {
86+
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
87+
return;
88+
}
89+
90+
if let Err(err) = encoder.flush() {
91+
sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err));
92+
return;
93+
}
94+
95+
debug!("save: data written to disk successfully");
96+
}
97+
4598
/// Reads the contents of a file with a file header as defined in this module.
4699
///
47100
/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a

compiler/rustc_incremental/src/persist/save.rs

+6-56
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
66
use rustc_serialize::Encodable as RustcEncodable;
77
use rustc_session::Session;
88
use std::fs;
9-
use std::io;
10-
use std::path::PathBuf;
119

1210
use super::data::*;
1311
use super::dirty_clean;
@@ -44,7 +42,9 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
4442
join(
4543
move || {
4644
sess.time("incr_comp_persist_result_cache", || {
47-
save_in(sess, query_cache_path, "query cache", |e| encode_query_cache(tcx, e));
45+
file_format::save_in(sess, query_cache_path, "query cache", |e| {
46+
encode_query_cache(tcx, e)
47+
});
4848
});
4949
},
5050
move || {
@@ -86,7 +86,9 @@ pub fn save_work_product_index(
8686
debug!("save_work_product_index()");
8787
dep_graph.assert_ignored();
8888
let path = work_products_path(sess);
89-
save_in(sess, path, "work product index", |e| encode_work_product_index(&new_work_products, e));
89+
file_format::save_in(sess, path, "work product index", |e| {
90+
encode_work_product_index(&new_work_products, e)
91+
});
9092

9193
// We also need to clean out old work-products, as not all of them are
9294
// deleted during invalidation. Some object files don't change their
@@ -113,58 +115,6 @@ pub fn save_work_product_index(
113115
});
114116
}
115117

116-
pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
117-
where
118-
F: FnOnce(&mut FileEncoder) -> FileEncodeResult,
119-
{
120-
debug!("save: storing data in {}", path_buf.display());
121-
122-
// Delete the old file, if any.
123-
// Note: It's important that we actually delete the old file and not just
124-
// truncate and overwrite it, since it might be a shared hard-link, the
125-
// underlying data of which we don't want to modify
126-
match fs::remove_file(&path_buf) {
127-
Ok(()) => {
128-
debug!("save: remove old file");
129-
}
130-
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
131-
Err(err) => {
132-
sess.err(&format!(
133-
"unable to delete old {} at `{}`: {}",
134-
name,
135-
path_buf.display(),
136-
err
137-
));
138-
return;
139-
}
140-
}
141-
142-
let mut encoder = match FileEncoder::new(&path_buf) {
143-
Ok(encoder) => encoder,
144-
Err(err) => {
145-
sess.err(&format!("failed to create {} at `{}`: {}", name, path_buf.display(), err));
146-
return;
147-
}
148-
};
149-
150-
if let Err(err) = file_format::write_file_header(&mut encoder, sess.is_nightly_build()) {
151-
sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err));
152-
return;
153-
}
154-
155-
if let Err(err) = encode(&mut encoder) {
156-
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
157-
return;
158-
}
159-
160-
if let Err(err) = encoder.flush() {
161-
sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err));
162-
return;
163-
}
164-
165-
debug!("save: data written to disk successfully");
166-
}
167-
168118
fn encode_work_product_index(
169119
work_products: &FxHashMap<WorkProductId, WorkProduct>,
170120
encoder: &mut FileEncoder,

0 commit comments

Comments
 (0)