1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 14:51:16 +02:00

replace all instances of std::filesystem::directory_iterator with DirectoryIterator

This commit is contained in:
Jörg Thalheim 2025-05-01 09:54:14 +02:00
parent 7ccc0d591f
commit 1c4496f4e5
16 changed files with 55 additions and 64 deletions

View file

@ -244,14 +244,13 @@ StringSet NixRepl::completePrefix(const std::string & prefix)
try { try {
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 : DirectoryIterator{dir == "" ? "/" : dir}) {
checkInterrupt(); 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());
} }
} catch (Error &) { } catch (Error &) {
} catch (std::filesystem::filesystem_error &) {
} }
} else if ((dot = cur.rfind('.')) == std::string::npos) { } else if ((dot = cur.rfind('.')) == std::string::npos) {
/* This is a variable name; look it up in the current scope. */ /* This is a variable name; look it up in the current scope. */

View file

@ -6,6 +6,7 @@
#include "nix/util/config-global.hh" #include "nix/util/config-global.hh"
#include "nix/util/signals.hh" #include "nix/util/signals.hh"
#include "nix/util/file-system.hh"
namespace nix { namespace nix {
@ -77,13 +78,13 @@ void initPlugins()
for (const auto & pluginFile : pluginSettings.pluginFiles.get()) { for (const auto & pluginFile : pluginSettings.pluginFiles.get()) {
std::vector<std::filesystem::path> pluginFiles; std::vector<std::filesystem::path> pluginFiles;
try { try {
auto ents = std::filesystem::directory_iterator{pluginFile}; auto ents = DirectoryIterator{pluginFile};
for (const auto & ent : ents) { for (const auto & ent : ents) {
checkInterrupt(); checkInterrupt();
pluginFiles.emplace_back(ent.path()); pluginFiles.emplace_back(ent.path());
} }
} catch (std::filesystem::filesystem_error & e) { } catch (SysError & e) {
if (e.code() != std::errc::not_a_directory) if (e.errNo != ENOTDIR)
throw; throw;
pluginFiles.emplace_back(pluginFile); pluginFiles.emplace_back(pluginFile);
} }

View file

@ -18,12 +18,12 @@ struct State
/* For each activated package, create symlinks */ /* For each activated package, create symlinks */
static void createLinks(State & state, const Path & srcDir, const Path & dstDir, int priority) static void createLinks(State & state, const Path & srcDir, const Path & dstDir, int priority)
{ {
std::filesystem::directory_iterator srcFiles; DirectoryIterator srcFiles;
try { try {
srcFiles = std::filesystem::directory_iterator{srcDir}; srcFiles = DirectoryIterator{srcDir};
} catch (std::filesystem::filesystem_error & e) { } catch (SysError & e) {
if (e.code() == std::errc::not_a_directory) { if (e.errNo == ENOTDIR) {
warn("not including '%s' in the user environment because it's not a directory", srcDir); warn("not including '%s' in the user environment because it's not a directory", srcDir);
return; return;
} }

View file

@ -29,13 +29,9 @@ void builtinUnpackChannel(
size_t fileCount; size_t fileCount;
std::string fileName; std::string fileName;
try { auto entries = DirectoryIterator{out};
auto entries = fs::directory_iterator{out};
fileName = entries->path().string(); fileName = entries->path().string();
fileCount = std::distance(fs::begin(entries), fs::end(entries)); fileCount = std::distance(entries.begin(), entries.end());
} catch (fs::filesystem_error &) {
throw SysError("failed to read directory %1%", out.string());
}
if (fileCount != 1) if (fileCount != 1)
throw Error("channel tarball '%s' contains more than one file", src); throw Error("channel tarball '%s' contains more than one file", src);

View file

@ -164,7 +164,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 : DirectoryIterator{tempRootsDir}) {
checkInterrupt(); checkInterrupt();
auto name = i.path().filename().string(); auto name = i.path().filename().string();
if (name[0] == '.') { if (name[0] == '.') {
@ -232,7 +232,7 @@ 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 : DirectoryIterator{path}) {
checkInterrupt(); checkInterrupt();
findRoots(i.path().string(), i.symlink_status().type(), roots); findRoots(i.path().string(), i.symlink_status().type(), roots);
} }

View file

@ -84,7 +84,7 @@ protected:
{ {
StorePathSet paths; StorePathSet paths;
for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) { for (auto & entry : DirectoryIterator{binaryCacheDir}) {
checkInterrupt(); checkInterrupt();
auto name = entry.path().filename().string(); auto name = entry.path().filename().string();
if (name.size() != 40 || if (name.size() != 40 ||

View file

@ -1382,7 +1382,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 : DirectoryIterator{linksDir}) {
checkInterrupt(); 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);
@ -1475,7 +1475,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair
database and the filesystem) in the loop below, in order to catch database and the filesystem) in the loop below, in order to catch
invalid states. invalid states.
*/ */
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) { for (auto & i : DirectoryIterator{realStoreDir.to_string()}) {
checkInterrupt(); checkInterrupt();
try { try {
storePathsInStoreDir.insert({i.path().filename().string()}); storePathsInStoreDir.insert({i.path().filename().string()});

View file

@ -136,7 +136,7 @@ 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 : DirectoryIterator{path}) {
checkInterrupt(); checkInterrupt();
canonicalisePathMetaData_( canonicalisePathMetaData_(
i.path().string(), i.path().string(),

View file

@ -38,7 +38,7 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro
std::filesystem::path profileDir = dirOf(profile); std::filesystem::path profileDir = dirOf(profile);
auto profileName = std::string(baseNameOf(profile)); auto profileName = std::string(baseNameOf(profile));
for (auto & i : std::filesystem::directory_iterator{profileDir}) { for (auto & i : DirectoryIterator{profileDir}) {
checkInterrupt(); 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();

View file

@ -387,7 +387,7 @@ void recursiveSync(const Path & path)
while (!dirsToEnumerate.empty()) { while (!dirsToEnumerate.empty()) {
auto currentDir = dirsToEnumerate.back(); auto currentDir = dirsToEnumerate.back();
dirsToEnumerate.pop_back(); dirsToEnumerate.pop_back();
for (auto & entry : std::filesystem::directory_iterator(currentDir)) { for (auto & entry : DirectoryIterator(currentDir)) {
auto st = entry.symlink_status(); auto st = entry.symlink_status();
if (fs::is_directory(st)) { if (fs::is_directory(st)) {
dirsToEnumerate.emplace_back(entry.path()); dirsToEnumerate.emplace_back(entry.path());
@ -691,7 +691,7 @@ void copyFile(const fs::path & from, const fs::path & to, bool andDelete)
fs::copy(from, to, fs::copy_options::copy_symlinks | fs::copy_options::overwrite_existing); fs::copy(from, to, fs::copy_options::copy_symlinks | fs::copy_options::overwrite_existing);
} else if (fs::is_directory(fromStatus)) { } else if (fs::is_directory(fromStatus)) {
fs::create_directory(to); fs::create_directory(to);
for (auto & entry : fs::directory_iterator(from)) { for (auto & entry : DirectoryIterator(from)) {
copyFile(entry, to / entry.path().filename(), andDelete); copyFile(entry, to / entry.path().filename(), andDelete);
} }
} else { } else {

View file

@ -65,7 +65,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 : DirectoryIterator{cgroup}) {
checkInterrupt(); 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

@ -138,8 +138,7 @@ SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath &
{ {
assertNoSymlinks(path); assertNoSymlinks(path);
DirEntries res; DirEntries res;
try { for (auto & entry : DirectoryIterator{makeAbsPath(path)}) {
for (auto & entry : std::filesystem::directory_iterator{makeAbsPath(path)}) {
checkInterrupt(); checkInterrupt();
auto type = [&]() -> std::optional<Type> { auto type = [&]() -> std::optional<Type> {
std::filesystem::file_type nativeType; std::filesystem::file_type nativeType;
@ -172,9 +171,6 @@ SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath &
}(); }();
res.emplace(entry.path().filename().string(), type); res.emplace(entry.path().filename().string(), type);
} }
} catch (std::filesystem::filesystem_error & e) {
throw SysError("reading directory %1%", showPath(path));
}
return res; return res;
} }

View file

@ -191,7 +191,7 @@ void unix::closeExtraFDs()
#ifdef __linux__ #ifdef __linux__
try { try {
for (auto & s : std::filesystem::directory_iterator{"/proc/self/fd"}) { for (auto & s : DirectoryIterator{"/proc/self/fd"}) {
checkInterrupt(); checkInterrupt();
auto fd = std::stoi(s.path().filename()); auto fd = std::stoi(s.path().filename());
if (fd > MAX_KEPT_FD) { if (fd > MAX_KEPT_FD) {
@ -201,7 +201,6 @@ void unix::closeExtraFDs()
} }
return; return;
} catch (SysError &) { } catch (SysError &) {
} catch (std::filesystem::filesystem_error &) {
} }
#endif #endif

View file

@ -30,7 +30,7 @@ void removeOldGenerations(fs::path dir)
bool canWrite = access(dir.string().c_str(), W_OK) == 0; bool canWrite = access(dir.string().c_str(), W_OK) == 0;
for (auto & i : fs::directory_iterator{dir}) { for (auto & i : DirectoryIterator{dir}) {
checkInterrupt(); checkInterrupt();
auto path = i.path().string(); auto path = i.path().string();

View file

@ -116,11 +116,11 @@ std::tuple<StorePath, Hash> prefetchFile(
createDirs(unpacked); createDirs(unpacked);
unpackTarfile(tmpFile.string(), unpacked); unpackTarfile(tmpFile.string(), unpacked);
auto entries = std::filesystem::directory_iterator{unpacked}; auto entries = DirectoryIterator{unpacked};
/* If the archive unpacks to a single file/directory, then use /* If the archive unpacks to a single file/directory, then use
that as the top-level. */ that as the top-level. */
tmpFile = entries->path(); tmpFile = entries->path();
auto fileCount = std::distance(entries, std::filesystem::directory_iterator{}); auto fileCount = std::distance(entries, DirectoryIterator{});
if (fileCount != 1) { if (fileCount != 1) {
/* otherwise, use the directory itself */ /* otherwise, use the directory itself */
tmpFile = unpacked; tmpFile = unpacked;

View file

@ -179,7 +179,7 @@ void chrootHelper(int argc, char * * argv)
if (mount(realStoreDir.c_str(), (tmpDir + storeDir).c_str(), "", MS_BIND, 0) == -1) if (mount(realStoreDir.c_str(), (tmpDir + storeDir).c_str(), "", MS_BIND, 0) == -1)
throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir); throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir);
for (const auto & entry : fs::directory_iterator{"/"}) { for (const auto & entry : DirectoryIterator{"/"}) {
checkInterrupt(); checkInterrupt();
const auto & src = entry.path(); const auto & src = entry.path();
fs::path dst = tmpDir / entry.path().filename(); fs::path dst = tmpDir / entry.path().filename();