Skip to content

Commit 143fb88

Browse files
authored
Merge pull request #13122 from Mic92/directory-iterator
Replace all instances of std::filesystem::directory_iterator with DirectoryIterator
2 parents b5dc818 + 1c4496f commit 143fb88

File tree

18 files changed

+165
-65
lines changed

18 files changed

+165
-65
lines changed

src/libcmd/repl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,13 @@ StringSet NixRepl::completePrefix(const std::string & prefix)
244244
try {
245245
auto dir = std::string(cur, 0, slash);
246246
auto prefix2 = std::string(cur, slash + 1);
247-
for (auto & entry : std::filesystem::directory_iterator{dir == "" ? "/" : dir}) {
247+
for (auto & entry : DirectoryIterator{dir == "" ? "/" : dir}) {
248248
checkInterrupt();
249249
auto name = entry.path().filename().string();
250250
if (name[0] != '.' && hasPrefix(name, prefix2))
251251
completions.insert(prev + entry.path().string());
252252
}
253253
} catch (Error &) {
254-
} catch (std::filesystem::filesystem_error &) {
255254
}
256255
} else if ((dot = cur.rfind('.')) == std::string::npos) {
257256
/* This is a variable name; look it up in the current scope. */

src/libmain/plugin.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "nix/util/config-global.hh"
88
#include "nix/util/signals.hh"
9+
#include "nix/util/file-system.hh"
910

1011
namespace nix {
1112

@@ -77,13 +78,13 @@ void initPlugins()
7778
for (const auto & pluginFile : pluginSettings.pluginFiles.get()) {
7879
std::vector<std::filesystem::path> pluginFiles;
7980
try {
80-
auto ents = std::filesystem::directory_iterator{pluginFile};
81+
auto ents = DirectoryIterator{pluginFile};
8182
for (const auto & ent : ents) {
8283
checkInterrupt();
8384
pluginFiles.emplace_back(ent.path());
8485
}
85-
} catch (std::filesystem::filesystem_error & e) {
86-
if (e.code() != std::errc::not_a_directory)
86+
} catch (SysError & e) {
87+
if (e.errNo != ENOTDIR)
8788
throw;
8889
pluginFiles.emplace_back(pluginFile);
8990
}

src/libstore/builtins/buildenv.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ struct State
1818
/* For each activated package, create symlinks */
1919
static void createLinks(State & state, const Path & srcDir, const Path & dstDir, int priority)
2020
{
21-
std::filesystem::directory_iterator srcFiles;
21+
DirectoryIterator srcFiles;
2222

2323
try {
24-
srcFiles = std::filesystem::directory_iterator{srcDir};
25-
} catch (std::filesystem::filesystem_error & e) {
26-
if (e.code() == std::errc::not_a_directory) {
24+
srcFiles = DirectoryIterator{srcDir};
25+
} catch (SysError & e) {
26+
if (e.errNo == ENOTDIR) {
2727
warn("not including '%s' in the user environment because it's not a directory", srcDir);
2828
return;
2929
}

src/libstore/builtins/unpack-channel.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ void builtinUnpackChannel(
2929

3030
size_t fileCount;
3131
std::string fileName;
32-
try {
33-
auto entries = fs::directory_iterator{out};
34-
fileName = entries->path().string();
35-
fileCount = std::distance(fs::begin(entries), fs::end(entries));
36-
} catch (fs::filesystem_error &) {
37-
throw SysError("failed to read directory %1%", out.string());
38-
}
32+
auto entries = DirectoryIterator{out};
33+
fileName = entries->path().string();
34+
fileCount = std::distance(entries.begin(), entries.end());
3935

4036
if (fileCount != 1)
4137
throw Error("channel tarball '%s' contains more than one file", src);

src/libstore/gc.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
164164
{
165165
/* Read the `temproots' directory for per-process temporary root
166166
files. */
167-
for (auto & i : std::filesystem::directory_iterator{tempRootsDir}) {
167+
for (auto & i : DirectoryIterator{tempRootsDir}) {
168168
checkInterrupt();
169169
auto name = i.path().filename().string();
170170
if (name[0] == '.') {
@@ -232,7 +232,7 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
232232
type = std::filesystem::symlink_status(path).type();
233233

234234
if (type == std::filesystem::file_type::directory) {
235-
for (auto & i : std::filesystem::directory_iterator{path}) {
235+
for (auto & i : DirectoryIterator{path}) {
236236
checkInterrupt();
237237
findRoots(i.path().string(), i.symlink_status().type(), roots);
238238
}

src/libstore/local-binary-cache-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct LocalBinaryCacheStore : virtual LocalBinaryCacheStoreConfig, virtual Bina
8484
{
8585
StorePathSet paths;
8686

87-
for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) {
87+
for (auto & entry : DirectoryIterator{binaryCacheDir}) {
8888
checkInterrupt();
8989
auto name = entry.path().filename().string();
9090
if (name.size() != 40 ||

src/libstore/local-store.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
13821382

13831383
printInfo("checking link hashes...");
13841384

1385-
for (auto & link : std::filesystem::directory_iterator{linksDir}) {
1385+
for (auto & link : DirectoryIterator{linksDir}) {
13861386
checkInterrupt();
13871387
auto name = link.path().filename();
13881388
printMsg(lvlTalkative, "checking contents of '%s'", name);
@@ -1475,7 +1475,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair
14751475
database and the filesystem) in the loop below, in order to catch
14761476
invalid states.
14771477
*/
1478-
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) {
1478+
for (auto & i : DirectoryIterator{realStoreDir.to_string()}) {
14791479
checkInterrupt();
14801480
try {
14811481
storePathsInStoreDir.insert({i.path().filename().string()});

src/libstore/posix-fs-canonicalise.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void canonicalisePathMetaData_(
136136
#endif
137137

138138
if (S_ISDIR(st.st_mode)) {
139-
for (auto & i : std::filesystem::directory_iterator{path}) {
139+
for (auto & i : DirectoryIterator{path}) {
140140
checkInterrupt();
141141
canonicalisePathMetaData_(
142142
i.path().string(),

src/libstore/profiles.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro
3838
std::filesystem::path profileDir = dirOf(profile);
3939
auto profileName = std::string(baseNameOf(profile));
4040

41-
for (auto & i : std::filesystem::directory_iterator{profileDir}) {
41+
for (auto & i : DirectoryIterator{profileDir}) {
4242
checkInterrupt();
4343
if (auto n = parseName(profileName, i.path().filename().string())) {
4444
auto path = i.path().string();

src/libutil-tests/file-system.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,25 @@ TEST(chmodIfNeeded, nonexistent)
297297
ASSERT_THROW(chmodIfNeeded("/schnitzel/darmstadt/pommes", 0755), SysError);
298298
}
299299

300+
/* ----------------------------------------------------------------------------
301+
* DirectoryIterator
302+
* --------------------------------------------------------------------------*/
303+
304+
TEST(DirectoryIterator, works)
305+
{
306+
auto tmpDir = nix::createTempDir();
307+
nix::AutoDelete delTmpDir(tmpDir, true);
308+
309+
nix::writeFile(tmpDir + "/somefile", "");
310+
311+
for (auto path : DirectoryIterator(tmpDir)) {
312+
ASSERT_EQ(path.path().string(), tmpDir + "/somefile");
313+
}
314+
}
315+
316+
TEST(DirectoryIterator, nonexistent)
317+
{
318+
ASSERT_THROW(DirectoryIterator("/schnitzel/darmstadt/pommes"), SysError);
319+
}
320+
300321
}

0 commit comments

Comments
 (0)