add call to checkInterrupt in a bunch of places

This brings back the old behaviour. We check for interrupts in places
that may iterate over wide directories.
This commit is contained in:
siddhantCodes 2024-06-04 19:35:40 +05:30
parent 1623249745
commit 8f1a26667e
13 changed files with 29 additions and 3 deletions

View file

@ -260,6 +260,7 @@ StringSet NixRepl::completePrefix(const std::string & prefix)
auto dir = std::string(cur, 0, slash); auto dir = std::string(cur, 0, slash);
auto prefix2 = std::string(cur, slash + 1); auto prefix2 = std::string(cur, slash + 1);
for (auto & entry : std::filesystem::directory_iterator{dir == "" ? "/" : dir}) { for (auto & entry : std::filesystem::directory_iterator{dir == "" ? "/" : dir}) {
checkInterrupt();
auto name = entry.path().filename().string(); auto name = entry.path().filename().string();
if (name[0] != '.' && hasPrefix(name, prefix2)) if (name[0] != '.' && hasPrefix(name, prefix2))
completions.insert(prev + entry.path().string()); completions.insert(prev + entry.path().string());

View file

@ -1,5 +1,6 @@
#include "buildenv.hh" #include "buildenv.hh"
#include "derivations.hh" #include "derivations.hh"
#include "signals.hh"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -30,6 +31,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir,
} }
for (const auto & ent : srcFiles) { for (const auto & ent : srcFiles) {
checkInterrupt();
auto name = ent.path().filename(); auto name = ent.path().filename();
if (name.string()[0] == '.') if (name.string()[0] == '.')
/* not matched by glob */ /* not matched by glob */

View file

@ -162,6 +162,7 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
/* Read the `temproots' directory for per-process temporary root /* Read the `temproots' directory for per-process temporary root
files. */ files. */
for (auto & i : std::filesystem::directory_iterator{tempRootsDir}) { for (auto & i : std::filesystem::directory_iterator{tempRootsDir}) {
checkInterrupt();
auto name = i.path().filename().string(); auto name = i.path().filename().string();
if (name[0] == '.') { if (name[0] == '.') {
// Ignore hidden files. Some package managers (notably portage) create // Ignore hidden files. Some package managers (notably portage) create
@ -228,8 +229,10 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
type = std::filesystem::symlink_status(path).type(); type = std::filesystem::symlink_status(path).type();
if (type == std::filesystem::file_type::directory) { if (type == std::filesystem::file_type::directory) {
for (auto & i : std::filesystem::directory_iterator{path}) for (auto & i : std::filesystem::directory_iterator{path}) {
checkInterrupt();
findRoots(i.path().string(), i.symlink_status().type(), roots); findRoots(i.path().string(), i.symlink_status().type(), roots);
}
} }
else if (type == std::filesystem::file_type::symlink) { else if (type == std::filesystem::file_type::symlink) {

View file

@ -4,6 +4,7 @@
#include "args.hh" #include "args.hh"
#include "abstract-setting-to-json.hh" #include "abstract-setting-to-json.hh"
#include "compute-levels.hh" #include "compute-levels.hh"
#include "signals.hh"
#include <algorithm> #include <algorithm>
#include <map> #include <map>
@ -346,14 +347,17 @@ void initPlugins()
std::vector<std::filesystem::path> pluginFiles; std::vector<std::filesystem::path> pluginFiles;
try { try {
auto ents = std::filesystem::directory_iterator{pluginFile}; auto ents = std::filesystem::directory_iterator{pluginFile};
for (const auto & ent : ents) for (const auto & ent : ents) {
checkInterrupt();
pluginFiles.emplace_back(ent.path()); pluginFiles.emplace_back(ent.path());
}
} catch (std::filesystem::filesystem_error & e) { } catch (std::filesystem::filesystem_error & e) {
if (e.code() != std::errc::not_a_directory) if (e.code() != std::errc::not_a_directory)
throw; throw;
pluginFiles.emplace_back(pluginFile); pluginFiles.emplace_back(pluginFile);
} }
for (const auto & file : pluginFiles) { for (const auto & file : pluginFiles) {
checkInterrupt();
/* handle is purposefully leaked as there may be state in the /* handle is purposefully leaked as there may be state in the
DSO needed by the action of the plugin. */ DSO needed by the action of the plugin. */
#ifndef _WIN32 // TODO implement via DLL loading on Windows #ifndef _WIN32 // TODO implement via DLL loading on Windows

View file

@ -1,6 +1,7 @@
#include "binary-cache-store.hh" #include "binary-cache-store.hh"
#include "globals.hh" #include "globals.hh"
#include "nar-info-disk-cache.hh" #include "nar-info-disk-cache.hh"
#include "signals.hh"
#include <atomic> #include <atomic>
@ -84,6 +85,7 @@ protected:
StorePathSet paths; StorePathSet paths;
for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) { for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) {
checkInterrupt();
auto name = entry.path().filename().string(); auto name = entry.path().filename().string();
if (name.size() != 40 || if (name.size() != 40 ||
!hasSuffix(name, ".narinfo")) !hasSuffix(name, ".narinfo"))

View file

@ -1407,6 +1407,7 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
printInfo("checking link hashes..."); printInfo("checking link hashes...");
for (auto & link : std::filesystem::directory_iterator{linksDir}) { for (auto & link : std::filesystem::directory_iterator{linksDir}) {
checkInterrupt();
auto name = link.path().filename(); auto name = link.path().filename();
printMsg(lvlTalkative, "checking contents of '%s'", name); printMsg(lvlTalkative, "checking contents of '%s'", name);
PosixSourceAccessor accessor; PosixSourceAccessor accessor;
@ -1499,6 +1500,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair
invalid states. invalid states.
*/ */
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) { for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) {
checkInterrupt();
try { try {
storePathsInStoreDir.insert({i.path().filename().string()}); storePathsInStoreDir.insert({i.path().filename().string()});
} catch (BadStorePath &) { } } catch (BadStorePath &) { }

View file

@ -144,13 +144,15 @@ static void canonicalisePathMetaData_(
#endif #endif
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
for (auto & i : std::filesystem::directory_iterator{path}) for (auto & i : std::filesystem::directory_iterator{path}) {
checkInterrupt();
canonicalisePathMetaData_( canonicalisePathMetaData_(
i.path().string(), i.path().string(),
#ifndef _WIN32 #ifndef _WIN32
uidRange, uidRange,
#endif #endif
inodesSeen); inodesSeen);
}
} }
} }

View file

@ -1,4 +1,5 @@
#include "profiles.hh" #include "profiles.hh"
#include "signals.hh"
#include "store-api.hh" #include "store-api.hh"
#include "local-fs-store.hh" #include "local-fs-store.hh"
#include "users.hh" #include "users.hh"
@ -38,6 +39,7 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro
auto profileName = std::string(baseNameOf(profile)); auto profileName = std::string(baseNameOf(profile));
for (auto & i : std::filesystem::directory_iterator{profileDir}) { for (auto & i : std::filesystem::directory_iterator{profileDir}) {
checkInterrupt();
if (auto n = parseName(profileName, i.path().filename().string())) { if (auto n = parseName(profileName, i.path().filename().string())) {
auto path = i.path().string(); auto path = i.path().string();
gens.push_back({ gens.push_back({

View file

@ -1,4 +1,5 @@
#include "cgroup.hh" #include "cgroup.hh"
#include "signals.hh"
#include "util.hh" #include "util.hh"
#include "file-system.hh" #include "file-system.hh"
#include "finally.hh" #include "finally.hh"
@ -65,6 +66,7 @@ static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool retu
/* Otherwise, manually kill every process in the subcgroups and /* Otherwise, manually kill every process in the subcgroups and
this cgroup. */ this cgroup. */
for (auto & entry : std::filesystem::directory_iterator{cgroup}) { for (auto & entry : std::filesystem::directory_iterator{cgroup}) {
checkInterrupt();
if (entry.symlink_status().type() != std::filesystem::file_type::directory) continue; if (entry.symlink_status().type() != std::filesystem::file_type::directory) continue;
destroyCgroup(cgroup / entry.path().filename(), false); destroyCgroup(cgroup / entry.path().filename(), false);
} }

View file

@ -133,6 +133,7 @@ SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath &
assertNoSymlinks(path); assertNoSymlinks(path);
DirEntries res; DirEntries res;
for (auto & entry : std::filesystem::directory_iterator{makeAbsPath(path)}) { for (auto & entry : std::filesystem::directory_iterator{makeAbsPath(path)}) {
checkInterrupt();
auto type = [&]() -> std::optional<Type> { auto type = [&]() -> std::optional<Type> {
std::filesystem::file_type nativeType; std::filesystem::file_type nativeType;
try { try {

View file

@ -125,6 +125,7 @@ void closeMostFDs(const std::set<int> & exceptions)
#if __linux__ #if __linux__
try { try {
for (auto & s : std::filesystem::directory_iterator{"/proc/self/fd"}) { for (auto & s : std::filesystem::directory_iterator{"/proc/self/fd"}) {
checkInterrupt();
auto fd = std::stoi(s.path().filename()); auto fd = std::stoi(s.path().filename());
if (!exceptions.count(fd)) { if (!exceptions.count(fd)) {
debug("closing leaked FD %d", fd); debug("closing leaked FD %d", fd);

View file

@ -7,6 +7,7 @@
#include "eval-settings.hh" #include "eval-settings.hh"
#include "flake/flake.hh" #include "flake/flake.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
#include "signals.hh"
#include "store-api.hh" #include "store-api.hh"
#include "derivations.hh" #include "derivations.hh"
#include "outputs-spec.hh" #include "outputs-spec.hh"
@ -867,6 +868,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
createDirs(to); createDirs(to);
for (auto & entry : std::filesystem::directory_iterator{from}) { for (auto & entry : std::filesystem::directory_iterator{from}) {
checkInterrupt();
auto from2 = entry.path().string(); auto from2 = entry.path().string();
auto to2 = to + "/" + entry.path().filename().string(); auto to2 = to + "/" + entry.path().filename().string();
auto st = lstat(from2); auto st = lstat(from2);

View file

@ -3,6 +3,7 @@
#include "command-installable-value.hh" #include "command-installable-value.hh"
#include "common-args.hh" #include "common-args.hh"
#include "shared.hh" #include "shared.hh"
#include "signals.hh"
#include "store-api.hh" #include "store-api.hh"
#include "derivations.hh" #include "derivations.hh"
#include "local-fs-store.hh" #include "local-fs-store.hh"
@ -249,6 +250,7 @@ void chrootHelper(int argc, char * * argv)
throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir); throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir);
for (auto entry : std::filesystem::directory_iterator{"/"}) { for (auto entry : std::filesystem::directory_iterator{"/"}) {
checkInterrupt();
auto src = entry.path().string(); auto src = entry.path().string();
Path dst = tmpDir + "/" + entry.path().filename().string(); Path dst = tmpDir + "/" + entry.path().filename().string();
if (pathExists(dst)) continue; if (pathExists(dst)) continue;