AutoCloseDir: Use std::unique_ptr

This commit is contained in:
Eelco Dolstra 2017-01-16 22:39:27 +01:00
parent 2b9d0a99cb
commit 8079ab87a2
4 changed files with 20 additions and 65 deletions

View file

@ -379,7 +379,7 @@ void LocalStore::findRuntimeRoots(PathSet & roots)
auto digitsRegex = std::regex(R"(^\d+$)"); auto digitsRegex = std::regex(R"(^\d+$)");
auto mapRegex = std::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)"); auto mapRegex = std::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)");
auto storePathRegex = std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)"); auto storePathRegex = std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
while (errno = 0, ent = readdir(procDir)) { while (errno = 0, ent = readdir(procDir.get())) {
checkInterrupt(); checkInterrupt();
if (std::regex_match(ent->d_name, digitsRegex)) { if (std::regex_match(ent->d_name, digitsRegex)) {
readProcLink((format("/proc/%1%/exe") % ent->d_name).str(), paths); readProcLink((format("/proc/%1%/exe") % ent->d_name).str(), paths);
@ -393,14 +393,14 @@ void LocalStore::findRuntimeRoots(PathSet & roots)
throw SysError(format("opening %1%") % fdStr); throw SysError(format("opening %1%") % fdStr);
} }
struct dirent * fd_ent; struct dirent * fd_ent;
while (errno = 0, fd_ent = readdir(fdDir)) { while (errno = 0, fd_ent = readdir(fdDir.get())) {
if (fd_ent->d_name[0] != '.') { if (fd_ent->d_name[0] != '.') {
readProcLink((format("%1%/%2%") % fdStr % fd_ent->d_name).str(), paths); readProcLink((format("%1%/%2%") % fdStr % fd_ent->d_name).str(), paths);
} }
} }
if (errno) if (errno)
throw SysError(format("iterating /proc/%1%/fd") % ent->d_name); throw SysError(format("iterating /proc/%1%/fd") % ent->d_name);
fdDir.close(); fdDir.reset();
auto mapLines = auto mapLines =
tokenizeString<std::vector<string>>(readFile((format("/proc/%1%/maps") % ent->d_name).str(), true), "\n"); tokenizeString<std::vector<string>>(readFile((format("/proc/%1%/maps") % ent->d_name).str(), true), "\n");
@ -651,13 +651,13 @@ void LocalStore::tryToDelete(GCState & state, const Path & path)
the link count. */ the link count. */
void LocalStore::removeUnusedLinks(const GCState & state) void LocalStore::removeUnusedLinks(const GCState & state)
{ {
AutoCloseDir dir = opendir(linksDir.c_str()); AutoCloseDir dir(opendir(linksDir.c_str()));
if (!dir) throw SysError(format("opening directory %1%") % linksDir); if (!dir) throw SysError(format("opening directory %1%") % linksDir);
long long actualSize = 0, unsharedSize = 0; long long actualSize = 0, unsharedSize = 0;
struct dirent * dirent; struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { while (errno = 0, dirent = readdir(dir.get())) {
checkInterrupt(); checkInterrupt();
string name = dirent->d_name; string name = dirent->d_name;
if (name == "." || name == "..") continue; if (name == "." || name == "..") continue;
@ -776,7 +776,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
try { try {
AutoCloseDir dir = opendir(realStoreDir.c_str()); AutoCloseDir dir(opendir(realStoreDir.c_str()));
if (!dir) throw SysError(format("opening directory %1%") % realStoreDir); if (!dir) throw SysError(format("opening directory %1%") % realStoreDir);
/* Read the store and immediately delete all paths that /* Read the store and immediately delete all paths that
@ -787,7 +787,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
can start faster. */ can start faster. */
Paths entries; Paths entries;
struct dirent * dirent; struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { while (errno = 0, dirent = readdir(dir.get())) {
checkInterrupt(); checkInterrupt();
string name = dirent->d_name; string name = dirent->d_name;
if (name == "." || name == "..") continue; if (name == "." || name == "..") continue;
@ -798,7 +798,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
tryToDelete(state, path); tryToDelete(state, path);
} }
dir.close(); dir.reset();
/* Now delete the unreachable valid paths. Randomise the /* Now delete the unreachable valid paths. Randomise the
order in which we delete entries to make the collector order in which we delete entries to make the collector

View file

@ -47,11 +47,11 @@ LocalStore::InodeHash LocalStore::loadInodeHash()
debug("loading hash inodes in memory"); debug("loading hash inodes in memory");
InodeHash inodeHash; InodeHash inodeHash;
AutoCloseDir dir = opendir(linksDir.c_str()); AutoCloseDir dir(opendir(linksDir.c_str()));
if (!dir) throw SysError(format("opening directory %1%") % linksDir); if (!dir) throw SysError(format("opening directory %1%") % linksDir);
struct dirent * dirent; struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { /* sic */ while (errno = 0, dirent = readdir(dir.get())) { /* sic */
checkInterrupt(); checkInterrupt();
// We don't care if we hit non-hash files, anything goes // We don't care if we hit non-hash files, anything goes
inodeHash.insert(dirent->d_ino); inodeHash.insert(dirent->d_ino);
@ -68,11 +68,11 @@ Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHa
{ {
Strings names; Strings names;
AutoCloseDir dir = opendir(path.c_str()); AutoCloseDir dir(opendir(path.c_str()));
if (!dir) throw SysError(format("opening directory %1%") % path); if (!dir) throw SysError(format("opening directory %1%") % path);
struct dirent * dirent; struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { /* sic */ while (errno = 0, dirent = readdir(dir.get())) { /* sic */
checkInterrupt(); checkInterrupt();
if (inodeHash.count(dirent->d_ino)) { if (inodeHash.count(dirent->d_ino)) {

View file

@ -234,11 +234,11 @@ DirEntries readDirectory(const Path & path)
DirEntries entries; DirEntries entries;
entries.reserve(64); entries.reserve(64);
AutoCloseDir dir = opendir(path.c_str()); AutoCloseDir dir(opendir(path.c_str()));
if (!dir) throw SysError(format("opening directory %1%") % path); if (!dir) throw SysError(format("opening directory %1%") % path);
struct dirent * dirent; struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { /* sic */ while (errno = 0, dirent = readdir(dir.get())) { /* sic */
checkInterrupt(); checkInterrupt();
string name = dirent->d_name; string name = dirent->d_name;
if (name == "." || name == "..") continue; if (name == "." || name == "..") continue;
@ -642,48 +642,6 @@ void Pipe::create()
//////////////////////////////////////////////////////////////////////
AutoCloseDir::AutoCloseDir()
{
dir = 0;
}
AutoCloseDir::AutoCloseDir(DIR * dir)
{
this->dir = dir;
}
AutoCloseDir::~AutoCloseDir()
{
close();
}
void AutoCloseDir::operator =(DIR * dir)
{
this->dir = dir;
}
AutoCloseDir::operator DIR *()
{
return dir;
}
void AutoCloseDir::close()
{
if (dir) {
closedir(dir);
dir = 0;
}
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View file

@ -180,18 +180,15 @@ public:
}; };
class AutoCloseDir struct DIRDeleter
{ {
DIR * dir; void operator()(DIR * dir) const {
public: closedir(dir);
AutoCloseDir(); }
AutoCloseDir(DIR * dir);
~AutoCloseDir();
void operator =(DIR * dir);
operator DIR *();
void close();
}; };
typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
class Pid class Pid
{ {