1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 20:41:47 +02:00

Replace our DirEntry with std::filesystem's

This commit is contained in:
John Ericson 2024-05-07 11:29:33 -04:00
parent c371070580
commit a3c573950b
18 changed files with 52 additions and 59 deletions

View file

@ -228,16 +228,14 @@ bool isLink(const Path & path)
}
DirEntries readDirectory(const Path & path)
std::vector<std::filesystem::directory_entry> readDirectory(const Path & path)
{
DirEntries entries;
std::vector<std::filesystem::directory_entry> 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;

View file

@ -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<DirEntry> DirEntries;
DirEntries readDirectory(const Path & path);
std::vector<std::filesystem::directory_entry> readDirectory(const Path & path);
std::filesystem::file_type getFileType(const Path & path);

View file

@ -47,26 +47,26 @@ std::map<std::string, std::string> 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<std::vector<std::string>>(readFile(cpustatPath), "\n")) {

View file

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

View file

@ -125,7 +125,7 @@ void closeMostFDs(const std::set<int> & 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);