First Commit
This commit is contained in:
106
src/ModuleLoader.cpp
Normal file
106
src/ModuleLoader.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "ModuleLoader.hpp"
|
||||
#include "dpm_interface.hpp"
|
||||
#include <filesystem>
|
||||
#include <dlfcn.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
ModuleLoader::ModuleLoader(std::string module_path) : module_path_(std::move(module_path))
|
||||
{
|
||||
if (!module_path_.empty() && module_path_.back() != '/') {
|
||||
module_path_ += '/';
|
||||
}
|
||||
}
|
||||
|
||||
DPMError ModuleLoader::check_module_path() const
|
||||
{
|
||||
if (!fs::exists(module_path_)) {
|
||||
return DPMError::PATH_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (!fs::is_directory(module_path_)) {
|
||||
return DPMError::PATH_NOT_DIRECTORY;
|
||||
}
|
||||
|
||||
try {
|
||||
fs::directory_iterator(module_path_);
|
||||
} catch (const fs::filesystem_error&) {
|
||||
return DPMError::PERMISSION_DENIED;
|
||||
}
|
||||
|
||||
return DPMError::SUCCESS;
|
||||
}
|
||||
|
||||
std::string ModuleLoader::get_absolute_module_path() const
|
||||
{
|
||||
try {
|
||||
return fs::absolute(module_path_).string();
|
||||
} catch (const fs::filesystem_error&) {
|
||||
return module_path_; // Return relative path if conversion fails
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<std::vector<std::string>, DPMError> ModuleLoader::list_available_modules() const
|
||||
{
|
||||
std::vector<std::string> modules;
|
||||
|
||||
try {
|
||||
fs::path absolute_path = fs::absolute(module_path_);
|
||||
for (const auto& entry : fs::directory_iterator(absolute_path)) {
|
||||
if (entry.is_regular_file()) {
|
||||
std::string filename = entry.path().filename().string();
|
||||
// Check if it's a .so file
|
||||
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") {
|
||||
// Remove the .so extension
|
||||
modules.push_back(filename.substr(0, filename.size() - 3));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const fs::filesystem_error&) {
|
||||
return {modules, DPMError::PERMISSION_DENIED};
|
||||
}
|
||||
|
||||
return {modules, DPMError::SUCCESS};
|
||||
}
|
||||
|
||||
void* ModuleLoader::load_module(const std::string& module_name) const
|
||||
{
|
||||
// Construct path to module shared object
|
||||
std::string module_so_path = module_path_ + module_name + ".so";
|
||||
|
||||
// Load the module
|
||||
void* module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY);
|
||||
if (!module_handle) {
|
||||
std::cerr << "Failed to load module: " << dlerror() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Clear any existing errors
|
||||
dlerror();
|
||||
|
||||
return module_handle;
|
||||
}
|
||||
|
||||
int ModuleLoader::execute_module(void* module_handle, const std::string& command) const
|
||||
{
|
||||
if (!module_handle) {
|
||||
std::cerr << "Invalid module handle" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Find the execution entry point
|
||||
using ExecuteFn = int (*)(const char*, int, char**);
|
||||
ExecuteFn execute_fn = (ExecuteFn)dlsym(module_handle, "dpm_module_execute");
|
||||
|
||||
const char* error = dlerror();
|
||||
if (error != nullptr) {
|
||||
std::cerr << "Failed to find module entry point: " << error << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Execute the module with just the provided command string
|
||||
int result = execute_fn(command.c_str(), 0, nullptr);
|
||||
|
||||
return result;
|
||||
}
|
||||
50
src/dpm.cpp
Normal file
50
src/dpm.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "ModuleLoader.hpp"
|
||||
#include "dpm_interface.hpp"
|
||||
|
||||
/*
|
||||
* DPM serves three functions:
|
||||
* 1. Find and load modules.
|
||||
* 2. Route commands to modules.
|
||||
* 3. Provide a module-agnostic unified interface for modules.
|
||||
*/
|
||||
|
||||
// the default behaviour if dpm is executed without being told to do anything
|
||||
int default_behavior(const ModuleLoader& loader)
|
||||
{
|
||||
return main_list_modules(loader);
|
||||
}
|
||||
|
||||
// entry point for the DPM utility
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto args = parse_args(argc, argv);
|
||||
ModuleLoader loader(args.module_path);
|
||||
|
||||
// check if the modules path even exists and return an error if not since we can't do anything
|
||||
if (auto result = main_check_module_path(loader); result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// if no modules are supplied, execute the default behaviour and exit
|
||||
if (args.module_name.empty()) {
|
||||
return default_behavior(loader);
|
||||
}
|
||||
|
||||
// load the module specified
|
||||
void* module_handle = loader.load_module(args.module_name);
|
||||
if (!module_handle) {
|
||||
std::cerr << "Failed to load module: " << args.module_name << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Execute the module with the command string
|
||||
int result = loader.execute_module(module_handle, args.command);
|
||||
|
||||
// Cleanup
|
||||
dlclose(module_handle);
|
||||
|
||||
return result;
|
||||
}
|
||||
106
src/dpm_interface.cpp
Normal file
106
src/dpm_interface.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "dpm_interface.hpp"
|
||||
|
||||
/*
|
||||
*
|
||||
* DPM Interface methods. These are wrappers of DPM functionality that are meant to handle user view, turning
|
||||
* error codes into human-presentable information, etc. Features are defined internally, these will only ever be
|
||||
* wrappers of existing features to provide the human/cli interface.
|
||||
*
|
||||
*/
|
||||
|
||||
// check if the module path exists
|
||||
int main_check_module_path(const ModuleLoader& loader)
|
||||
{
|
||||
if (auto result = loader.check_module_path(); result != DPMError::SUCCESS) {
|
||||
switch (result) {
|
||||
case DPMError::PATH_NOT_FOUND:
|
||||
std::cerr << "Module path not found: " << loader.get_absolute_module_path() << std::endl;
|
||||
break;
|
||||
case DPMError::PATH_NOT_DIRECTORY:
|
||||
std::cerr << "Not a directory: " << loader.get_absolute_module_path() << std::endl;
|
||||
break;
|
||||
case DPMError::PERMISSION_DENIED:
|
||||
std::cerr << "Permission denied: " << loader.get_absolute_module_path() << std::endl;
|
||||
break;
|
||||
default:
|
||||
std::cerr << "Failed checking module path: " << loader.get_absolute_module_path() << std::endl;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// list the modules
|
||||
int main_list_modules(const ModuleLoader& loader)
|
||||
{
|
||||
auto [modules, list_error] = loader.list_available_modules();
|
||||
if (list_error != DPMError::SUCCESS) {
|
||||
switch (list_error) {
|
||||
case DPMError::PERMISSION_DENIED:
|
||||
std::cerr << "Permission denied reading modules from: " << loader.get_absolute_module_path() << std::endl;
|
||||
break;
|
||||
default:
|
||||
std::cerr << "Failed listing modules from: " << loader.get_absolute_module_path() << std::endl;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Available modules in " << loader.get_absolute_module_path() << ":\n";
|
||||
for (const auto& module : modules) {
|
||||
std::cout << " " << module << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// parser for populating data structure for supplied arguments
|
||||
CommandArgs parse_args(int argc, char* argv[])
|
||||
{
|
||||
CommandArgs args;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"module-path", required_argument, 0, 'm'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int opt;
|
||||
int option_index = 0;
|
||||
while ((opt = getopt_long(argc, argv, "m:h", long_options, &option_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'm':
|
||||
args.module_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
std::cout << "Usage: dpm [options] [module-name] [module args...]\n"
|
||||
<< "Options:\n"
|
||||
<< " -m, --module-path PATH Path to DPM modules\n"
|
||||
<< " -h, --help Show this help message\n"
|
||||
<< "\nIf no module is specified, available modules will be listed.\n";
|
||||
exit(0);
|
||||
case '?':
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// If there are remaining args, the first one is the module name
|
||||
if (optind < argc) {
|
||||
args.module_name = argv[optind++];
|
||||
|
||||
// Collect all remaining arguments and combine them into a single command string
|
||||
for (int i = optind; i < argc; i++) {
|
||||
if (!args.command.empty()) {
|
||||
args.command += " ";
|
||||
}
|
||||
|
||||
// Handle arguments with spaces by quoting them
|
||||
std::string arg = argv[i];
|
||||
if (arg.find(' ') != std::string::npos) {
|
||||
args.command += "\"" + arg + "\"";
|
||||
} else {
|
||||
args.command += arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
Reference in New Issue
Block a user