Skip to content

Commit

Permalink
Fix #1635.
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Dec 4, 2017
1 parent cd74a55 commit f1efb97
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/nix/search.cc
Expand Up @@ -214,13 +214,28 @@ struct CmdSearch : SourceExprCommand, MixJSON
}

else {
createDirs(dirOf(jsonCacheFileName));

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)
throw SysError("cannot rename '%s' to '%s'", tmpFile, jsonCacheFileName);
Expand Down

1 comment on commit f1efb97

@Ma27
Copy link
Member

@Ma27 Ma27 commented on f1efb97 Dec 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Please sign in to comment.