build: recover store path when replacing fails

This shouldn't happen in normal circumstances, but just in case
attempt to move the temporary path back if possible.
This commit is contained in:
Daiderd Jordan 2020-01-04 17:47:52 +01:00
parent 0de33cc81b
commit b33fefcb92
No known key found for this signature in database
GPG key ID: D02435D05B810C96

View file

@ -1513,8 +1513,10 @@ void replaceValidPath(const Path & storePath, const Path tmpPath)
Path oldPath = (format("%1%.old-%2%-%3%") % storePath % getpid() % random()).str(); Path oldPath = (format("%1%.old-%2%-%3%") % storePath % getpid() % random()).str();
if (pathExists(storePath)) if (pathExists(storePath))
rename(storePath.c_str(), oldPath.c_str()); rename(storePath.c_str(), oldPath.c_str());
if (rename(tmpPath.c_str(), storePath.c_str()) == -1) if (rename(tmpPath.c_str(), storePath.c_str()) == -1) {
rename(oldPath.c_str(), storePath.c_str()); // attempt to recover
throw SysError("moving '%s' to '%s'", tmpPath, storePath); throw SysError("moving '%s' to '%s'", tmpPath, storePath);
}
deletePath(oldPath); deletePath(oldPath);
} }