diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index d2f2a9648..8a9155ab6 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -260,8 +260,9 @@ StringSet NixRepl::completePrefix(const std::string & prefix) auto dir = std::string(cur, 0, slash); auto prefix2 = std::string(cur, slash + 1); for (auto & entry : readDirectory(dir == "" ? "/" : dir)) { - if (entry.name[0] != '.' && hasPrefix(entry.name, prefix2)) - completions.insert(prev + dir + "/" + entry.name); + 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 &) { diff --git a/src/libstore/builtins/buildenv.cc b/src/libstore/builtins/buildenv.cc index ebd8f1348..5fcdf6f15 100644 --- a/src/libstore/builtins/buildenv.cc +++ b/src/libstore/builtins/buildenv.cc @@ -17,7 +17,7 @@ struct State /* For each activated package, create symlinks */ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, int priority) { - DirEntries srcFiles; + std::vector srcFiles; try { srcFiles = readDirectory(srcDir); @@ -30,11 +30,12 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, } for (const auto & ent : srcFiles) { - if (ent.name[0] == '.') + auto name = ent.path().filename(); + if (name.string()[0] == '.') /* not matched by glob */ continue; - auto srcFile = srcDir + "/" + ent.name; - auto dstFile = dstDir + "/" + ent.name; + auto srcFile = (std::filesystem::path{srcDir} / name).string(); + auto dstFile = (std::filesystem::path{dstDir} / name).string(); struct stat srcSt; try { diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index f0096b981..4df2880e6 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -343,11 +343,11 @@ void initPlugins() { assert(!settings.pluginFiles.pluginsLoaded); for (const auto & pluginFile : settings.pluginFiles.get()) { - Paths pluginFiles; + std::vector pluginFiles; try { auto ents = readDirectory(pluginFile); for (const auto & ent : ents) - pluginFiles.emplace_back(pluginFile + "/" + ent.name); + pluginFiles.emplace_back(ent.path()); } catch (std::filesystem::filesystem_error & e) { if (e.code() != std::errc::not_a_directory) throw; diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 5481dd762..3a48f4480 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -84,11 +84,12 @@ protected: StorePathSet paths; for (auto & entry : readDirectory(binaryCacheDir)) { - if (entry.name.size() != 40 || - !hasSuffix(entry.name, ".narinfo")) + auto name = entry.path().filename().string(); + if (name.size() != 40 || + !hasSuffix(name, ".narinfo")) continue; paths.insert(parseStorePath( - storeDir + "/" + entry.name.substr(0, entry.name.size() - 8) + storeDir + "/" + name.substr(0, name.size() - 8) + "-" + MissingName)); } diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index a0bb60410..fa8026703 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -34,12 +34,12 @@ std::pair> findGenerations(Path pro { Generations gens; - Path profileDir = dirOf(profile); + std::filesystem::path profileDir = dirOf(profile); auto profileName = std::string(baseNameOf(profile)); - for (auto & i : readDirectory(profileDir)) { - if (auto n = parseName(profileName, i.name)) { - auto path = profileDir + "/" + i.name; + for (auto & i : readDirectory(profileDir.string())) { + if (auto n = parseName(profileName, i.path().filename().string())) { + auto path = i.path().string(); gens.push_back({ .number = *n, .path = path, diff --git a/src/libstore/unix/builtins/unpack-channel.cc b/src/libstore/unix/builtins/unpack-channel.cc index 6f68d4c0b..47bf5d49c 100644 --- a/src/libstore/unix/builtins/unpack-channel.cc +++ b/src/libstore/unix/builtins/unpack-channel.cc @@ -24,7 +24,7 @@ void builtinUnpackChannel( auto entries = readDirectory(out); if (entries.size() != 1) throw Error("channel tarball '%s' contains more than one file", src); - renameFile((out + "/" + entries[0].name), (out + "/" + channelName)); + renameFile(entries[0].path().string(), (out + "/" + channelName)); } } diff --git a/src/libstore/unix/gc.cc b/src/libstore/unix/gc.cc index 6677946aa..c10ed5564 100644 --- a/src/libstore/unix/gc.cc +++ b/src/libstore/unix/gc.cc @@ -160,14 +160,15 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor) /* Read the `temproots' directory for per-process temporary root files. */ for (auto & i : readDirectory(tempRootsDir)) { - if (i.name[0] == '.') { + auto name = i.path().filename().string(); + if (name[0] == '.') { // Ignore hidden files. Some package managers (notably portage) create // those to keep the directory alive. continue; } - Path path = tempRootsDir + "/" + i.name; + Path path = i.path(); - pid_t pid = std::stoi(i.name); + pid_t pid = std::stoi(name); debug("reading temporary root file '%1%'", path); AutoCloseFD fd(open(path.c_str(), O_CLOEXEC | O_RDWR, 0666)); @@ -222,7 +223,7 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R if (type == std::filesystem::file_type::directory) { for (auto & i : readDirectory(path)) - findRoots(path + "/" + i.name, i.type, roots); + findRoots(i.path().string(), i.symlink_status().type(), roots); } else if (type == std::filesystem::file_type::symlink) { diff --git a/src/libstore/unix/local-store.cc b/src/libstore/unix/local-store.cc index a3de523a3..7a47d1524 100644 --- a/src/libstore/unix/local-store.cc +++ b/src/libstore/unix/local-store.cc @@ -1388,15 +1388,16 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair) printInfo("checking link hashes..."); for (auto & link : readDirectory(linksDir)) { - printMsg(lvlTalkative, "checking contents of '%s'", link.name); - Path linkPath = linksDir + "/" + link.name; + auto name = link.path().filename(); + printMsg(lvlTalkative, "checking contents of '%s'", name); + Path linkPath = linksDir / name; PosixSourceAccessor accessor; std::string hash = hashPath( {getFSSourceAccessor(), CanonPath(linkPath)}, FileIngestionMethod::Recursive, HashAlgorithm::SHA256).to_string(HashFormat::Nix32, false); - if (hash != link.name) { + if (hash != name.string()) { printError("link '%s' was modified! expected hash '%s', got '%s'", - linkPath, link.name, hash); + linkPath, name, hash); if (repair) { if (unlink(linkPath.c_str()) == 0) printInfo("removed link '%s'", linkPath); @@ -1483,7 +1484,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair */ for (auto & i : readDirectory(realStoreDir)) { try { - storePathsInStoreDir.insert({i.name}); + storePathsInStoreDir.insert({i.path().filename().string()}); } catch (BadStorePath &) { } } diff --git a/src/libstore/unix/posix-fs-canonicalise.cc b/src/libstore/unix/posix-fs-canonicalise.cc index 8b29e90d4..916db49ac 100644 --- a/src/libstore/unix/posix-fs-canonicalise.cc +++ b/src/libstore/unix/posix-fs-canonicalise.cc @@ -136,9 +136,9 @@ static void canonicalisePathMetaData_( } if (S_ISDIR(st.st_mode)) { - DirEntries entries = readDirectory(path); + std::vector entries = readDirectory(path); for (auto & i : entries) - canonicalisePathMetaData_(path + "/" + i.name, uidRange, inodesSeen); + canonicalisePathMetaData_(i.path().string(), uidRange, inodesSeen); } } diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 0e68241fc..b9abcade9 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -228,16 +228,14 @@ bool isLink(const Path & path) } -DirEntries readDirectory(const Path & path) +std::vector readDirectory(const Path & path) { - DirEntries entries; + std::vector entries; entries.reserve(64); for (auto & entry : fs::directory_iterator{path}) { checkInterrupt(); - entries.emplace_back( - entry.path().filename().string(), - entry.symlink_status().type()); + entries.push_back(std::move(entry)); } return entries; diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 2ecf2881c..2bf703276 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -122,17 +122,7 @@ bool isLink(const Path & path); * Read the contents of a directory. The entries `.` and `..` are * removed. */ -struct DirEntry -{ - std::string name; - std::filesystem::file_type type; - DirEntry(std::string name, std::filesystem::file_type type) - : name(std::move(name)), type(type) { } -}; - -typedef std::vector DirEntries; - -DirEntries readDirectory(const Path & path); +std::vector readDirectory(const Path & path); std::filesystem::file_type getFileType(const Path & path); diff --git a/src/libutil/linux/cgroup.cc b/src/libutil/linux/cgroup.cc index 9234a4b2e..619ef7764 100644 --- a/src/libutil/linux/cgroup.cc +++ b/src/libutil/linux/cgroup.cc @@ -47,26 +47,26 @@ std::map getCgroups(const Path & cgroupFile) return cgroups; } -static CgroupStats destroyCgroup(const Path & cgroup, bool returnStats) +static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool returnStats) { if (!pathExists(cgroup)) return {}; - auto procsFile = cgroup + "/cgroup.procs"; + auto procsFile = cgroup / "cgroup.procs"; if (!pathExists(procsFile)) throw Error("'%s' is not a cgroup", cgroup); /* Use the fast way to kill every process in a cgroup, if available. */ - auto killFile = cgroup + "/cgroup.kill"; + auto killFile = cgroup / "cgroup.kill"; if (pathExists(killFile)) writeFile(killFile, "1"); /* Otherwise, manually kill every process in the subcgroups and this cgroup. */ for (auto & entry : readDirectory(cgroup)) { - if (entry.type != std::filesystem::file_type::directory) continue; - destroyCgroup(cgroup + "/" + entry.name, false); + if (entry.symlink_status().type() != std::filesystem::file_type::directory) continue; + destroyCgroup(cgroup / entry.path().filename(), false); } int round = 1; @@ -111,7 +111,7 @@ static CgroupStats destroyCgroup(const Path & cgroup, bool returnStats) CgroupStats stats; if (returnStats) { - auto cpustatPath = cgroup + "/cpu.stat"; + auto cpustatPath = cgroup / "cpu.stat"; if (pathExists(cpustatPath)) { for (auto & line : tokenizeString>(readFile(cpustatPath), "\n")) { diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index a2559a0fb..4ff61f6bb 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -138,14 +138,14 @@ SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath & // additional file types are allowed. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wswitch-enum" - switch (entry.type) { + switch (entry.symlink_status().type()) { case std::filesystem::file_type::regular: type = Type::tRegular; break; case std::filesystem::file_type::symlink: type = Type::tSymlink; break; case std::filesystem::file_type::directory: type = Type::tDirectory; break; default: type = tMisc; } #pragma GCC diagnostic pop - res.emplace(entry.name, type); + res.emplace(entry.path().filename().string(), type); } return res; } diff --git a/src/libutil/unix/file-descriptor.cc b/src/libutil/unix/file-descriptor.cc index 068b37843..222d077e5 100644 --- a/src/libutil/unix/file-descriptor.cc +++ b/src/libutil/unix/file-descriptor.cc @@ -125,7 +125,7 @@ void closeMostFDs(const std::set & exceptions) #if __linux__ try { for (auto & s : readDirectory("/proc/self/fd")) { - auto fd = std::stoi(s.name); + auto fd = std::stoi(s.path().filename()); if (!exceptions.count(fd)) { debug("closing leaked FD %d", fd); close(fd); diff --git a/src/nix-collect-garbage/nix-collect-garbage.cc b/src/nix-collect-garbage/nix-collect-garbage.cc index 9dfbea3f1..02a3a4b83 100644 --- a/src/nix-collect-garbage/nix-collect-garbage.cc +++ b/src/nix-collect-garbage/nix-collect-garbage.cc @@ -30,8 +30,8 @@ void removeOldGenerations(std::string dir) for (auto & i : readDirectory(dir)) { checkInterrupt(); - auto path = dir + "/" + i.name; - auto type = i.type == std::filesystem::file_type::unknown ? getFileType(path) : i.type; + auto path = i.path().string(); + auto type = i.symlink_status().type(); if (type == std::filesystem::file_type::symlink && canWrite) { std::string link; diff --git a/src/nix/flake.cc b/src/nix/flake.cc index d5987237f..9c1888aa0 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -867,8 +867,8 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand createDirs(to); for (auto & entry : readDirectory(from)) { - auto from2 = from + "/" + entry.name; - auto to2 = to + "/" + entry.name; + auto from2 = entry.path().string(); + auto to2 = to + "/" + entry.path().filename().string(); auto st = lstat(from2); if (S_ISDIR(st.st_mode)) copyDir(from2, to2); diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 6c2eb5aaf..cff1c7988 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -117,7 +117,7 @@ std::tuple prefetchFile( that as the top-level. */ auto entries = readDirectory(unpacked); if (entries.size() == 1) - tmpFile = unpacked + "/" + entries[0].name; + tmpFile = entries[0].path().string(); else tmpFile = unpacked; } diff --git a/src/nix/run.cc b/src/nix/run.cc index 88821710d..9c559bdf6 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -249,8 +249,8 @@ void chrootHelper(int argc, char * * argv) throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir); for (auto entry : readDirectory("/")) { - auto src = "/" + entry.name; - Path dst = tmpDir + "/" + entry.name; + auto src = entry.path().string(); + Path dst = tmpDir + "/" + entry.path().filename().string(); if (pathExists(dst)) continue; auto st = lstat(src); if (S_ISDIR(st.st_mode)) {