From 6e898936d6e0f44da0992ad09139f0e8e6d141af Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Mon, 10 Jun 2019 13:40:37 +0100 Subject: Add a method for getting a list of files in a directory. --- src/fileutils.cpp | 34 ++++++++++++++++++++++++++++++++++ src/modulemanager.cpp | 42 +++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 731e4ea01..d5e1e5839 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -86,6 +86,40 @@ bool FileSystem::FileExists(const std::string& file) return !access(file.c_str(), F_OK); } +bool FileSystem::GetFileList(const std::string& directory, std::vector& entries, const std::string& match) +{ +#ifdef _WIN32 + const std::string search_path = directory + "\\" + match; + + WIN32_FIND_DATAA wfd; + HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd); + if (fh == INVALID_HANDLE_VALUE) + return false; + + do + { + entries.push_back(wfd.cFileName); + } while (FindNextFile(fh, &wfd) != 0); + + FindClose(fh); + return true; +#else + DIR* library = opendir(directory.c_str()); + if (!library) + return false; + + dirent* entry = NULL; + while ((entry = readdir(library))) + { + if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map)) + entries.push_back(entry->d_name); + } + closedir(library); + return true; +#endif +} + + std::string FileSystem::GetFileName(const std::string& name) { #ifdef _WIN32 diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index 5ed7d12d5..8cb7a0401 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -21,10 +21,6 @@ #include "exitcodes.h" #include -#ifndef _WIN32 -#include -#endif - bool ModuleManager::Load(const std::string& modname, bool defer) { /* Don't allow people to specify paths for modules, it doesn't work as expected */ @@ -126,29 +122,29 @@ bool ModuleManager::Load(const std::string& modname, bool defer) /* We must load the modules AFTER initializing the socket engine, now */ void ModuleManager::LoadCoreModules(std::map& servicemap) { - std::cout << std::endl << "Loading core commands" << std::flush; + std::cout << "Loading core modules " << std::flush; - DIR* library = opendir(ServerInstance->Config->Paths.Module.c_str()); - if (library) + std::vector files; + if (!FileSystem::GetFileList(ServerInstance->Config->Paths.Module, files, "core_*.so")) { - dirent* entry = NULL; - while (0 != (entry = readdir(library))) - { - if (InspIRCd::Match(entry->d_name, "core_*.so", ascii_case_insensitive_map)) - { - std::cout << "." << std::flush; + std::cout << "failed!" << std::endl; + ServerInstance->Exit(EXIT_STATUS_MODULE); + } - this->NewServices = &servicemap[entry->d_name]; + for (std::vector::const_iterator iter = files.begin(); iter != files.end(); ++iter) + { + std::cout << "." << std::flush; - if (!Load(entry->d_name, true)) - { - ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError()); - std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl; - ServerInstance->Exit(EXIT_STATUS_MODULE); - } - } + const std::string& name = *iter; + this->NewServices = &servicemap[name]; + + if (!Load(name, true)) + { + ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError()); + std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl; + ServerInstance->Exit(EXIT_STATUS_MODULE); } - closedir(library); - std::cout << std::endl; } + + std::cout << std::endl; } -- cgit v1.2.3