This commit is contained in:
Eelco Dolstra 2017-12-04 13:18:25 +01:00
parent cd74a55afc
commit f1efb97075
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -214,14 +214,29 @@ struct CmdSearch : SourceExprCommand, MixJSON
} }
else { else {
createDirs(dirOf(jsonCacheFileName));
Path tmpFile = fmt("%s.tmp.%d", jsonCacheFileName, getpid()); Path tmpFile = fmt("%s.tmp.%d", jsonCacheFileName, getpid());
std::ofstream jsonCacheFile(tmpFile); std::ofstream jsonCacheFile;
try {
// iostream considered harmful
jsonCacheFile.exceptions(std::ofstream::failbit);
jsonCacheFile.open(tmpFile);
auto cache = writeCache ? std::make_unique<JSONObject>(jsonCacheFile, false) : nullptr; auto cache = writeCache ? std::make_unique<JSONObject>(jsonCacheFile, false) : nullptr;
doExpr(getSourceExpr(*state), "", true, cache.get()); doExpr(getSourceExpr(*state), "", true, cache.get());
} catch (std::exception &) {
/* Fun fact: catching std::ios::failure does not work
due to C++11 ABI shenanigans.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66145 */
if (!jsonCacheFile)
throw Error("error writing to %s", tmpFile);
}
if (rename(tmpFile.c_str(), jsonCacheFileName.c_str()) == -1) if (rename(tmpFile.c_str(), jsonCacheFileName.c_str()) == -1)
throw SysError("cannot rename '%s' to '%s'", tmpFile, jsonCacheFileName); throw SysError("cannot rename '%s' to '%s'", tmpFile, jsonCacheFileName);
} }