Skip to content

Commit

Permalink
Merge pull request NixOS#10852 from siddhantk232/add-interrupts
Browse files Browse the repository at this point in the history
add call to `checkInterrupt` in a bunch of places
  • Loading branch information
edolstra authored Jun 4, 2024
2 parents b36aa04 + 8f1a266 commit ef140c2
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ StringSet NixRepl::completePrefix(const std::string & prefix)
auto dir = std::string(cur, 0, slash);
auto prefix2 = std::string(cur, slash + 1);
for (auto & entry : std::filesystem::directory_iterator{dir == "" ? "/" : dir}) {
checkInterrupt();
auto name = entry.path().filename().string();
if (name[0] != '.' && hasPrefix(name, prefix2))
completions.insert(prev + entry.path().string());
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/builtins/buildenv.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "buildenv.hh"
#include "derivations.hh"
#include "signals.hh"

#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -30,6 +31,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir,
}

for (const auto & ent : srcFiles) {
checkInterrupt();
auto name = ent.path().filename();
if (name.string()[0] == '.')
/* not matched by glob */
Expand Down
5 changes: 4 additions & 1 deletion src/libstore/gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
/* Read the `temproots' directory for per-process temporary root
files. */
for (auto & i : std::filesystem::directory_iterator{tempRootsDir}) {
checkInterrupt();
auto name = i.path().filename().string();
if (name[0] == '.') {
// Ignore hidden files. Some package managers (notably portage) create
Expand Down Expand Up @@ -228,8 +229,10 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
type = std::filesystem::symlink_status(path).type();

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);
}
}

else if (type == std::filesystem::file_type::symlink) {
Expand Down
6 changes: 5 additions & 1 deletion src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "args.hh"
#include "abstract-setting-to-json.hh"
#include "compute-levels.hh"
#include "signals.hh"

#include <algorithm>
#include <map>
Expand Down Expand Up @@ -346,14 +347,17 @@ void initPlugins()
std::vector<std::filesystem::path> pluginFiles;
try {
auto ents = std::filesystem::directory_iterator{pluginFile};
for (const auto & ent : ents)
for (const auto & ent : ents) {
checkInterrupt();
pluginFiles.emplace_back(ent.path());
}
} catch (std::filesystem::filesystem_error & e) {
if (e.code() != std::errc::not_a_directory)
throw;
pluginFiles.emplace_back(pluginFile);
}
for (const auto & file : pluginFiles) {
checkInterrupt();
/* handle is purposefully leaked as there may be state in the
DSO needed by the action of the plugin. */
#ifndef _WIN32 // TODO implement via DLL loading on Windows
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/local-binary-cache-store.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "binary-cache-store.hh"
#include "globals.hh"
#include "nar-info-disk-cache.hh"
#include "signals.hh"

#include <atomic>

Expand Down Expand Up @@ -88,6 +89,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
StorePathSet paths;

for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) {
checkInterrupt();
auto name = entry.path().filename().string();
if (name.size() != 40 ||
!hasSuffix(name, ".narinfo"))
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
printInfo("checking link hashes...");

for (auto & link : std::filesystem::directory_iterator{linksDir}) {
checkInterrupt();
auto name = link.path().filename();
printMsg(lvlTalkative, "checking contents of '%s'", name);
PosixSourceAccessor accessor;
Expand Down Expand Up @@ -1509,6 +1510,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair
invalid states.
*/
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) {
checkInterrupt();
try {
storePathsInStoreDir.insert({i.path().filename().string()});
} catch (BadStorePath &) { }
Expand Down
4 changes: 3 additions & 1 deletion src/libstore/posix-fs-canonicalise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ static void canonicalisePathMetaData_(
#endif

if (S_ISDIR(st.st_mode)) {
for (auto & i : std::filesystem::directory_iterator{path})
for (auto & i : std::filesystem::directory_iterator{path}) {
checkInterrupt();
canonicalisePathMetaData_(
i.path().string(),
#ifndef _WIN32
uidRange,
#endif
inodesSeen);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/profiles.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "profiles.hh"
#include "signals.hh"
#include "store-api.hh"
#include "local-fs-store.hh"
#include "users.hh"
Expand Down Expand Up @@ -38,6 +39,7 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro
auto profileName = std::string(baseNameOf(profile));

for (auto & i : std::filesystem::directory_iterator{profileDir}) {
checkInterrupt();
if (auto n = parseName(profileName, i.path().filename().string())) {
auto path = i.path().string();
gens.push_back({
Expand Down
2 changes: 2 additions & 0 deletions src/libutil/linux/cgroup.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cgroup.hh"
#include "signals.hh"
#include "util.hh"
#include "file-system.hh"
#include "finally.hh"
Expand Down Expand Up @@ -65,6 +66,7 @@ static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool retu
/* Otherwise, manually kill every process in the subcgroups and
this cgroup. */
for (auto & entry : std::filesystem::directory_iterator{cgroup}) {
checkInterrupt();
if (entry.symlink_status().type() != std::filesystem::file_type::directory) continue;
destroyCgroup(cgroup / entry.path().filename(), false);
}
Expand Down
1 change: 1 addition & 0 deletions src/libutil/posix-source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath &
assertNoSymlinks(path);
DirEntries res;
for (auto & entry : std::filesystem::directory_iterator{makeAbsPath(path)}) {
checkInterrupt();
auto type = [&]() -> std::optional<Type> {
std::filesystem::file_type nativeType;
try {
Expand Down
1 change: 1 addition & 0 deletions src/libutil/unix/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void closeMostFDs(const std::set<int> & exceptions)
#if __linux__
try {
for (auto & s : std::filesystem::directory_iterator{"/proc/self/fd"}) {
checkInterrupt();
auto fd = std::stoi(s.path().filename());
if (!exceptions.count(fd)) {
debug("closing leaked FD %d", fd);
Expand Down
2 changes: 2 additions & 0 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "eval-settings.hh"
#include "flake/flake.hh"
#include "get-drvs.hh"
#include "signals.hh"
#include "store-api.hh"
#include "derivations.hh"
#include "outputs-spec.hh"
Expand Down Expand Up @@ -867,6 +868,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
createDirs(to);

for (auto & entry : std::filesystem::directory_iterator{from}) {
checkInterrupt();
auto from2 = entry.path().string();
auto to2 = to + "/" + entry.path().filename().string();
auto st = lstat(from2);
Expand Down
2 changes: 2 additions & 0 deletions src/nix/run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "command-installable-value.hh"
#include "common-args.hh"
#include "shared.hh"
#include "signals.hh"
#include "store-api.hh"
#include "derivations.hh"
#include "local-fs-store.hh"
Expand Down Expand Up @@ -172,6 +173,7 @@ void chrootHelper(int argc, char * * argv)
throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir);

for (auto entry : std::filesystem::directory_iterator{"/"}) {
checkInterrupt();
auto src = entry.path().string();
Path dst = tmpDir + "/" + entry.path().filename().string();
if (pathExists(dst)) continue;
Expand Down

0 comments on commit ef140c2

Please sign in to comment.