WriteLock writeLock(&s_state_lock);
setArgPrefix("geoip" + suffix);
if (!getArg("dnssec-keydir").empty()) {
- DIR* dir = opendir(getArg("dnssec-keydir").c_str());
- if (dir == nullptr) {
+ auto dirHandle = std::unique_ptr<DIR, decltype(&closedir)>(opendir(getArg("dnssec-keydir").c_str()), closedir);
+ if (!dirHandle) {
throw PDNSException("dnssec-keydir " + getArg("dnssec-keydir") + " does not exist");
}
d_dnssec = true;
- closedir(dir);
+ dirHandle.reset();
}
if (s_rc == 0) { // first instance gets to open everything
initialize();
void BackendMakerClass::load_all()
{
// TODO: Implement this?
- DIR *dir=opendir(arg()["module-dir"].c_str());
- if(!dir) {
+ auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(arg()["module-dir"].c_str()), closedir);
+ if (!dir) {
g_log<<Logger::Error<<"Unable to open module directory '"<<arg()["module-dir"]<<"'"<<endl;
return;
}
- struct dirent *entry;
- while((entry=readdir(dir))) {
- if(!strncmp(entry->d_name,"lib",3) &&
- strlen(entry->d_name)>13 &&
- !strcmp(entry->d_name+strlen(entry->d_name)-10,"backend.so"))
+ struct dirent* entry = nullptr;
+ while ((entry=readdir(dir.get()))) {
+ if (strncmp(entry->d_name, "lib", 3) == 0 &&
+ strlen(entry->d_name) > 13 &&
+ strcmp(entry->d_name + strlen(entry->d_name)-10, "backend.so") == 0)
load(entry->d_name);
}
- closedir(dir);
}
void BackendMakerClass::load(const string &module)
return;
}
- DIR* dirp;
- struct dirent* ent;
+ auto dirp = std::unique_ptr<DIR, decltype(&closedir)>(opendir(dirname.c_str()), closedir);
std::vector<std::string> files;
- if (!(dirp = opendir(dirname.c_str()))) {
+ if (!dirp) {
errlog("Error opening the included directory %s!", dirname.c_str());
g_outputBuffer = "Error opening the included directory " + dirname + "!";
return;
}
- while ((ent = readdir(dirp)) != NULL) {
+ struct dirent* ent = nullptr;
+ while ((ent = readdir(dirp.get())) != NULL) {
if (ent->d_name[0] == '.') {
continue;
}
}
}
- closedir(dirp);
+ dirp.reset();
std::sort(files.begin(), files.end());
g_included = true;
static void cleanUpDomain(const DNSName& domain, const uint16_t& keep, const string& workdir) {
string dir = workdir + "/" + domain.toString();
- DIR *dp;
- dp = opendir(dir.c_str());
- if (dp == nullptr) {
+ auto dirHandle = std::unique_ptr<DIR, decltype(&closedir)>(opendir(dir.c_str()), closedir);
+ if (!dirHandle) {
return;
}
vector<uint32_t> zoneVersions;
- struct dirent *d;
- while ((d = readdir(dp)) != nullptr) {
- if(!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
+ struct dirent* entry = nullptr;
+ while ((entry = readdir(dirHandle.get())) != nullptr) {
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
- zoneVersions.push_back(std::stoi(d->d_name));
+ zoneVersions.push_back(std::stoi(entry->d_name));
}
- closedir(dp);
+ dirHandle.reset();
g_log<<Logger::Info<<"Found "<<zoneVersions.size()<<" versions of "<<domain<<", asked to keep "<<keep<<", ";
if (zoneVersions.size() <= keep) {
g_log<<Logger::Info<<"not cleaning up"<<endl;
uint32_t getSerialFromDir(const std::string& dir)
{
- uint32_t ret=0;
- DIR* dirhdl=opendir(dir.c_str());
- if(!dirhdl)
+ uint32_t ret = 0;
+ auto dirhdl = std::unique_ptr<DIR, decltype(&closedir)>(opendir(dir.c_str()), closedir);
+ if (!dirhdl) {
throw runtime_error("Could not open IXFR directory '" + dir + "': " + stringerror());
- struct dirent *entry;
+ }
- while((entry = readdir(dirhdl))) {
+ struct dirent* entry = nullptr;
+ while ((entry = readdir(dirhdl.get()))) {
uint32_t num = atoi(entry->d_name);
- if(std::to_string(num) == entry->d_name)
+ if (std::to_string(num) == entry->d_name) {
ret = max(num, ret);
+ }
}
- closedir(dirhdl);
+
return ret;
}
uint64_t getOpenFileDescriptors(const std::string&)
{
#ifdef __linux__
- DIR* dirhdl=opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str());
- if(!dirhdl)
+ auto dirhdl = std::unique_ptr<DIR, decltype(&closedir)>(opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str()), closedir);
+ if (!dirhdl) {
return 0;
+ }
- struct dirent *entry;
- int ret=0;
- while((entry = readdir(dirhdl))) {
+ int ret = 0;
+ struct dirent* entry = nullptr;
+ while ((entry = readdir(dirhdl.get()))) {
uint32_t num;
try {
pdns::checked_stoi_into(num, entry->d_name);
} catch (...) {
continue; // was not a number.
}
- if(std::to_string(num) == entry->d_name)
+ if (std::to_string(num) == entry->d_name) {
ret++;
+ }
}
- closedir(dirhdl);
return ret;
#elif defined(__OpenBSD__)