summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2022-12-17 15:44:44 +0000
committerSadie Powell <sadie@witchery.services>2022-12-17 16:03:28 +0000
commit3f3062a0773d0a45941f6db2534989a07256f267 (patch)
tree3e9080e751e5a26a2d49ef4412833ba6456edb18
parent8a8fb7725b4433b725866cc8842edf40ec8b7025 (diff)
Use <filesystem> instead of opendir/readdir/closedir.
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/modulemanager.cpp26
-rw-r--r--src/win32/anope_windows.h1
-rw-r--r--src/win32/dir/dir.cpp48
-rw-r--r--src/win32/dir/dir.h27
5 files changed, 10 insertions, 93 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d722dd86c..80207756c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,7 +2,6 @@
file(GLOB SRC_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp")
if(WIN32)
- list(APPEND SRC_SRCS win32/dir/dir.cpp)
list(APPEND SRC_SRCS win32/socket.cpp)
list(APPEND SRC_SRCS win32/windows.cpp)
list(APPEND SRC_SRCS win32/dl/dl.cpp)
diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp
index 3317a6f09..0ef2421e6 100644
--- a/src/modulemanager.cpp
+++ b/src/modulemanager.cpp
@@ -15,11 +15,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
-#include <dirent.h>
#include <sys/types.h>
#include <dlfcn.h>
#endif
+#include <filesystem>
+
std::list<Module *> ModuleManager::Modules;
std::vector<Module *> ModuleManager::EventHandlers[I_SIZE];
@@ -29,25 +30,18 @@ void ModuleManager::CleanupRuntimeDirectory()
Anope::string dirbuf = Anope::DataDir + "/runtime";
Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment, please wait";
-
- DIR *dirp = opendir(dirbuf.c_str());
- if (!dirp)
+ try
{
- Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << ")";
- return;
+ for (const auto &entry : std::filesystem::directory_iterator(dirbuf.str()))
+ {
+ if (entry.is_regular_file())
+ std::filesystem::remove(entry);
+ }
}
-
- for (dirent *dp; (dp = readdir(dirp));)
+ catch (const std::filesystem::filesystem_error &err)
{
- if (!dp->d_ino)
- continue;
- if (Anope::string(dp->d_name).equals_cs(".") || Anope::string(dp->d_name).equals_cs(".."))
- continue;
- Anope::string filebuf = dirbuf + "/" + dp->d_name;
- unlink(filebuf.c_str());
+ Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << "): " << err.what();
}
-
- closedir(dirp);
}
/**
diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h
index 7417b0f23..3ccb34198 100644
--- a/src/win32/anope_windows.h
+++ b/src/win32/anope_windows.h
@@ -51,7 +51,6 @@
#define EINPROGRESS WSAEWOULDBLOCK
#include "socket.h"
-#include "dir/dir.h"
#include "dl/dl.h"
#include "pipe/pipe.h"
#include "pthread/pthread.h"
diff --git a/src/win32/dir/dir.cpp b/src/win32/dir/dir.cpp
deleted file mode 100644
index 8c8cdf907..000000000
--- a/src/win32/dir/dir.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-/* POSIX emulation layer for Windows.
- *
- * (C) 2008-2022 Anope Team
- * Contact us at team@anope.org
- *
- * Please read COPYING and README for further details.
- */
-
-#include "dir.h"
-#include <stdio.h>
-
-DIR *opendir(const char *path)
-{
- char real_path[MAX_PATH];
- _snprintf(real_path, sizeof(real_path), "%s/*", path);
-
- DIR *d = new DIR();
- d->handle = FindFirstFile(real_path, &d->data);
- d->read_first = false;
-
- if (d->handle == INVALID_HANDLE_VALUE)
- {
- delete d;
- return NULL;
- }
-
- return d;
-}
-
-dirent *readdir(DIR *d)
-{
- if (d->read_first == false)
- d->read_first = true;
- else if (!FindNextFile(d->handle, &d->data))
- return NULL;
-
- d->ent.d_ino = 1;
- d->ent.d_name = d->data.cFileName;
-
- return &d->ent;
-}
-
-int closedir(DIR *d)
-{
- FindClose(d->handle);
- delete d;
- return 0;
-}
diff --git a/src/win32/dir/dir.h b/src/win32/dir/dir.h
deleted file mode 100644
index bfacdd456..000000000
--- a/src/win32/dir/dir.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/* POSIX emulation layer for Windows.
- *
- * (C) 2008-2022 Anope Team
- * Contact us at team@anope.org
- *
- * Please read COPYING and README for further details.
- */
-
-#include <windows.h>
-
-struct dirent
-{
- int d_ino;
- char *d_name;
-};
-
-struct DIR
-{
- dirent ent;
- HANDLE handle;
- WIN32_FIND_DATA data;
- bool read_first;
-};
-
-DIR *opendir(const char *);
-dirent *readdir(DIR *);
-int closedir(DIR *);