Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark new db as fresh #71

Closed
wants to merge 1 commit into from
Closed
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: 4 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,20 @@ impl<'a> Reader<'a> {
}

/// Opens or creates an on-disk database, loading its state into the provided Graph.
pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<Writer> {
/// Returns (fresh, Writer) where "fresh" is true when this is the first n2 run.
pub fn open(path: &str, graph: &mut Graph, hashes: &mut Hashes) -> anyhow::Result<(bool, Writer)> {
match std::fs::OpenOptions::new()
.read(true)
.append(true)
.open(path)
{
Ok(mut f) => {
let ids = Reader::read(&mut f, graph, hashes)?;
Ok(Writer::from_opened(ids, f))
Ok((false, Writer::from_opened(ids, f)))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
let w = Writer::create(path)?;
Ok(w)
Ok((true, w))
}
Err(err) => Err(anyhow!(err)),
}
Expand Down
5 changes: 4 additions & 1 deletion src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ impl Loader {
/// State loaded by read().
pub struct State {
pub graph: graph::Graph,
/// True if we think this is the first time n2 has been run for this build directory.
pub fresh: bool,
pub db: db::Writer,
pub hashes: graph::Hashes,
pub default: Vec<FileId>,
Expand All @@ -199,12 +201,13 @@ pub fn read(build_filename: &str) -> anyhow::Result<State> {
loader.read_file(id)
})?;
let mut hashes = graph::Hashes::default();
let db = trace::scope("db::open", || {
let (fresh, db) = trace::scope("db::open", || {
db::open(".n2_db", &mut loader.graph, &mut hashes)
})
.map_err(|err| anyhow!("load .n2_db: {}", err))?;
Ok(State {
graph: loader.graph,
fresh,
db,
hashes,
default: loader.default,
Expand Down
5 changes: 4 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ fn build(

let mut tasks_finished = 0;

// Attempt to rebuild build.ninja.
let build_file_target = work.lookup(&build_filename);
// Attempt to rebuild build.ninja.
if let Some(target) = build_file_target {
if state.fresh {
work.adopt = true;
}
work.want_file(target)?;
match trace::scope("work.run", || work.run())? {
None => return Ok(None),
Expand Down