From 61c35a810be8d658a3a950b32ac87e29c9d94ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Mon, 11 Apr 2022 10:20:36 +0200 Subject: [PATCH] gc: Use the trace helper --- src/libstore/gc.cc | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 024da66c1..adb554836 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -295,9 +295,42 @@ void LocalStore::findRootsNoTemp(Roots & roots, bool censor) Roots LocalStore::findRoots(bool censor) { Roots roots; - findRootsNoTemp(roots, censor); - findTempRoots(roots, censor); + Pipe fromHelper; + fromHelper.create(); + Pid helperPid = startProcess([&]() { + if (dup2(fromHelper.writeSide.get(), STDOUT_FILENO) == -1) + throw SysError("cannot pipe standard output into log file"); + if (chdir("/") == -1) throw SysError("changing into /"); + auto helperProgram = settings.nixLibexecDir + "/nix/nix-find-roots"; + Strings args = {std::string(baseNameOf(helperProgram))}; + execv( + helperProgram.c_str(), + stringsToCharPtrs(args).data() + ); + + throw SysError("executing '%s'", helperProgram); + }); + + try { + while (true) { + auto line = readLine(fromHelper.readSide.get()); + if (line.empty()) break; // TODO: Handle the broken symlinks + auto parsedLine = tokenizeString>(line, "\t"); + if (parsedLine.size() != 2) + throw Error("Invalid result from the gc helper"); + auto rawDestPath = parsedLine[0]; + if (!isInStore(rawDestPath)) continue; + auto destPath = toStorePath(rawDestPath).first; + if (!isValidPath(destPath)) continue; + roots[destPath].insert(parsedLine[1]); + } + } catch (EndOfFile &) { + } + + int res = helperPid.wait(); + if (res != 0) + throw Error("unable to start the gc helper process"); return roots; }