1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41: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 {
auto dir = std::string(cur, 0, slash);
auto prefix2 = std::string(cur, slash + 1);
for (auto & entry : std::filesystem::directory_iterator{dir == "" ? "/" : dir}) {
for (auto & entry : DirectoryIterator{dir == "" ? "/" : dir}) {
checkInterrupt();
auto name = entry.path().filename().string();
if (name[0] != '.' && hasPrefix(name, prefix2))
completions.insert(prev + entry.path().string());
}
} catch (Error &) {
} catch (std::filesystem::filesystem_error &) {
}
} else if ((dot = cur.rfind('.')) == std::string::npos) {
/* 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/signals.hh"
#include "nix/util/file-system.hh"
namespace nix {
@ -77,13 +78,13 @@ void initPlugins()
for (const auto & pluginFile : pluginSettings.pluginFiles.get()) {
std::vector<std::filesystem::path> pluginFiles;
try {
auto ents = std::filesystem::directory_iterator{pluginFile};
auto ents = DirectoryIterator{pluginFile};
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)
} catch (SysError & e) {
if (e.errNo != ENOTDIR)
throw;
pluginFiles.emplace_back(pluginFile);
}

View file

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

View file

@ -29,13 +29,9 @@ void builtinUnpackChannel(
size_t fileCount;
std::string fileName;
try {
auto entries = fs::directory_iterator{out};
auto entries = DirectoryIterator{out};
fileName = entries->path().string();
fileCount = std::distance(fs::begin(entries), fs::end(entries));
} catch (fs::filesystem_error &) {
throw SysError("failed to read directory %1%", out.string());
}
fileCount = std::distance(entries.begin(), entries.end());
if (fileCount != 1)
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
files. */
for (auto & i : std::filesystem::directory_iterator{tempRootsDir}) {
for (auto & i : DirectoryIterator{tempRootsDir}) {
checkInterrupt();
auto name = i.path().filename().string();
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();
if (type == std::filesystem::file_type::directory) {
for (auto & i : std::filesystem::directory_iterator{path}) {
for (auto & i : DirectoryIterator{path}) {
checkInterrupt();
findRoots(i.path().string(), i.symlink_status().type(), roots);
}

View file

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

View file

@ -1382,7 +1382,7 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
printInfo("checking link hashes...");
for (auto & link : std::filesystem::directory_iterator{linksDir}) {
for (auto & link : DirectoryIterator{linksDir}) {
checkInterrupt();
auto name = link.path().filename();
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
invalid states.
*/
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) {
for (auto & i : DirectoryIterator{realStoreDir.to_string()}) {
checkInterrupt();
try {
storePathsInStoreDir.insert({i.path().filename().string()});

View file

@ -136,7 +136,7 @@ static void canonicalisePathMetaData_(
#endif
if (S_ISDIR(st.st_mode)) {
for (auto & i : std::filesystem::directory_iterator{path}) {
for (auto & i : DirectoryIterator{path}) {
checkInterrupt();
canonicalisePathMetaData_(
i.path().string(),

View file

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

View file

@ -387,7 +387,7 @@ void recursiveSync(const Path & path)
while (!dirsToEnumerate.empty()) {
auto currentDir = dirsToEnumerate.back();
dirsToEnumerate.pop_back();
for (auto & entry : std::filesystem::directory_iterator(currentDir)) {
for (auto & entry : DirectoryIterator(currentDir)) {
auto st = entry.symlink_status();
if (fs::is_directory(st)) {
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);
} else if (fs::is_directory(fromStatus)) {
fs::create_directory(to);
for (auto & entry : fs::directory_iterator(from)) {
for (auto & entry : DirectoryIterator(from)) {
copyFile(entry, to / entry.path().filename(), andDelete);
}
} 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
this cgroup. */
for (auto & entry : std::filesystem::directory_iterator{cgroup}) {
for (auto & entry : DirectoryIterator{cgroup}) {
checkInterrupt();
if (entry.symlink_status().type() != std::filesystem::file_type::directory) continue;
destroyCgroup(cgroup / entry.path().filename(), false);

View file

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

View file

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

View file

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

View file

@ -116,11 +116,11 @@ std::tuple<StorePath, Hash> prefetchFile(
createDirs(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
that as the top-level. */
tmpFile = entries->path();
auto fileCount = std::distance(entries, std::filesystem::directory_iterator{});
auto fileCount = std::distance(entries, DirectoryIterator{});
if (fileCount != 1) {
/* otherwise, use the directory itself */
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)
throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir);
for (const auto & entry : fs::directory_iterator{"/"}) {
for (const auto & entry : DirectoryIterator{"/"}) {
checkInterrupt();
const auto & src = entry.path();
fs::path dst = tmpDir / entry.path().filename();