Factor out the creation of the log file

This commit is contained in:
Théophane Hufschmitt 2022-03-21 10:42:44 +01:00
parent 5db8642224
commit 2f494b7834

View file

@ -175,6 +175,18 @@ StorePaths reverseTopoSortPaths(const std::map<StorePath, ValidPathInfo> & paths
return sorted; return sorted;
} }
std::pair<Path, AutoCloseFD> openLogFile(const std::string & logDir, const StorePath & drvPath)
{
string base(drvPath.to_string());
auto logFile = logDir + "/" + string(base, 0, 2) + "/" + string(base, 2);
createDirs(dirOf(logFile));
AutoCloseFD logFD = open(logFile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (!logFD) throw SysError("creating log file %s", logFile);
return {std::move(logFile), std::move(logFD)};
}
void State::buildRemote(ref<Store> destStore, void State::buildRemote(ref<Store> destStore,
Machine::ptr machine, Step::ptr step, Machine::ptr machine, Step::ptr step,
@ -185,14 +197,9 @@ void State::buildRemote(ref<Store> destStore,
{ {
assert(BuildResult::TimedOut == 8); assert(BuildResult::TimedOut == 8);
string base(step->drvPath.to_string()); auto [logFile, logFD] = openLogFile(logDir, step->drvPath);
result.logFile = logDir + "/" + string(base, 0, 2) + "/" + string(base, 2); AutoDelete logFileDel(logFile, false);
AutoDelete autoDelete(result.logFile, false); result.logFile = logFile;
createDirs(dirOf(result.logFile));
AutoCloseFD logFD = open(result.logFile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
if (!logFD) throw SysError("creating log file %s", result.logFile);
nix::Path tmpDir = createTempDir(); nix::Path tmpDir = createTempDir();
AutoDelete tmpDirDel(tmpDir, true); AutoDelete tmpDirDel(tmpDir, true);
@ -316,7 +323,7 @@ void State::buildRemote(ref<Store> destStore,
result.overhead += std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count(); result.overhead += std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
} }
autoDelete.cancel(); logFileDel.cancel();
/* Truncate the log to get rid of messages about substitutions /* Truncate the log to get rid of messages about substitutions
etc. on the remote system. */ etc. on the remote system. */