From 8456beeb0529b8dde7f4eea9a949c1c4f91a6120 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 21 Nov 2017 11:39:48 +0200 Subject: Diagnose failure to open depdb The cause is often a missing fsdir{} if the user tries to stash the target in a subdirectory. --- build2/depdb.cxx | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'build2/depdb.cxx') diff --git a/build2/depdb.cxx b/build2/depdb.cxx index ee96efa..c041a0a 100644 --- a/build2/depdb.cxx +++ b/build2/depdb.cxx @@ -6,6 +6,8 @@ #include // file_mtime() +#include + using namespace std; using namespace butl; @@ -15,31 +17,46 @@ namespace build2 depdb (const path& f) : mtime_ (file_mtime (f)), touch_ (false) { - fs_.exceptions (fstream::failbit | fstream::badbit); + fstream::openmode om (fstream::out | fstream::binary); + fstream::iostate em (fstream::badbit); - if (mtime_ != timestamp_nonexistent) + if (mtime_ == timestamp_nonexistent) + { + mtime_ = timestamp_unknown; + state_ = state::write; + em |= fstream::failbit; + } + else { - // Open an existing file. - // - fs_.open (f.string (), fstream::in | fstream::out | fstream::binary); state_ = state::read; - fs_.exceptions (fstream::badbit); + om |= fstream::in; + } - // Read the database format version. - // + fs_.open (f.string (), om); + if (!fs_.is_open ()) + { + bool c (state_ == state::write); + + diag_record dr (fail); + dr << "unable to " << (c ? "create" : "open") << ' ' << f; + + if (c) + dr << info << "did you forget to add fsdir{} prerequisite for " + << "output directory?"; + } + + fs_.exceptions (em); + + // Read/write the database format version. + // + if (state_ == state::read) + { string* l (read ()); if (l == nullptr || *l != "1") write ('1'); } else - { - fs_.open (f.string (), fstream::out | fstream::binary); - - state_ = state::write; - mtime_ = timestamp_unknown; - write ('1'); - } } void depdb:: -- cgit v1.1