struggling a little with API exposure between modules and C++ types, needs a complete overhaul for checksums -- snapshot commit -- not functional

This commit is contained in:
Chris Punches
2025-03-31 02:03:18 -04:00
parent 045294aeb6
commit 81645c6d09
11 changed files with 573 additions and 223 deletions

View File

@@ -33,6 +33,7 @@
#include <string>
#include <sys/stat.h>
#include <dlfcn.h>
#include "ModuleOperations.hpp"
/**
* @brief Fatal log level constant
@@ -163,59 +164,6 @@ extern "C" {
* @return The module path
*/
const char* dpm_get_module_path(void);
/**
* @brief Checks if a module exists
*
* Verifies if a module exists at the configured module path.
*
* @param module_name Name of the module to check
* @return true if the module exists, false otherwise
*/
bool dpm_module_exists(const char* module_name);
/**
* @brief Loads a DPM module
*
* Attempts to load a module from the configured module path.
*
* @param module_name Name of the module to load
* @param module_handle Pointer to store the loaded module handle
* @return 0 on success, non-zero on failure
*/
int dpm_load_module(const char* module_name, void** module_handle);
/**
* @brief Checks if a symbol exists in a module
*
* Verifies if a specific symbol exists in a loaded module.
*
* @param module_handle Handle to a loaded module
* @param symbol_name Name of the symbol to check
* @return true if the symbol exists, false otherwise
*/
bool dpm_symbol_exists(void* module_handle, const char* symbol_name);
/**
* @brief Executes a symbol in a module
*
* Attempts to execute a function in a loaded module.
*
* @param module_handle Handle to a loaded module
* @param symbol_name Name of the symbol to execute
* @param args Arguments to pass to the function
* @return 0 on success, non-zero on failure
*/
int dpm_execute_symbol(void* module_handle, const char* symbol_name, void* args);
/**
* @brief Unloads a module
*
* Frees resources used by a loaded module.
*
* @param module_handle Handle to a loaded module
*/
void dpm_unload_module(void* module_handle);
}
/**

View File

@@ -0,0 +1,88 @@
/**
* @file ModuleOperations.hpp
* @brief C++ interface for module operations with direct passthrough
*
* @copyright Copyright (c) 2025 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
*
* Part of the Dark Horse Linux Package Manager (DPM)
*/
#pragma once
#include <string>
#include <filesystem>
#include <dlfcn.h>
#include <sys/stat.h>
#include <dpmdk/include/CommonModuleAPI.hpp>
/**
* @brief Checks if a module exists
*
* Verifies if a module exists at the configured module path.
*
* @param module_name Name of the module to check
* @return true if the module exists, false otherwise
*/
bool dpm_module_exists(const char* module_name);
/**
* @brief Loads a DPM module
*
* Attempts to load a module from the configured module path.
*
* @param module_name Name of the module to load
* @param module_handle Pointer to store the loaded module handle
* @return 0 on success, non-zero on failure
*/
int dpm_load_module(const char* module_name, void** module_handle);
/**
* @brief Checks if a symbol exists in a module
*
* Verifies if a specific symbol exists in a loaded module.
*
* @param module_handle Handle to a loaded module
* @param symbol_name Name of the symbol to check
* @return true if the symbol exists, false otherwise
*/
bool dpm_symbol_exists(void* module_handle, const char* symbol_name);
/**
* @brief Executes a symbol in a module with direct argument passthrough
*
* Template function that directly passes any number of arguments to the target function.
*
* @tparam Args Variable argument types to pass to the function
* @param module_handle Handle to a loaded module
* @param symbol_name Name of the symbol to execute
* @param args Arguments to pass to the function
* @return Return value from the executed function
*/
template<typename... Args>
int dpm_execute_symbol(void* module_handle, const char* symbol_name, Args&&... args) {
if (!module_handle || !symbol_name) return 1;
// Clear any previous error
dlerror();
// Look up the symbol with the correct function signature
typedef int (*FunctionPtr)(Args...);
FunctionPtr func = reinterpret_cast<FunctionPtr>(dlsym(module_handle, symbol_name));
// Check for errors
const char* error = dlerror();
if (error || !func) return 1;
// Call the function with the provided arguments - direct passthrough
return func(std::forward<Args>(args)...);
}
/**
* @brief Unloads a module
*
* Frees resources used by a loaded module.
*
* @param module_handle Handle to a loaded module
*/
void dpm_unload_module(void* module_handle);

View File

@@ -99,98 +99,6 @@ inline const char* dpm_get_module_path(void) {
return env_path ? env_path : "/usr/lib/dpm/modules/";
}
/**
* @brief Standalone implementation of dpm_module_exists
*/
inline bool dpm_module_exists(const char* module_name) {
if (!module_name) return false;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return false;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if file exists
struct stat buffer;
return (stat(module_file.c_str(), &buffer) == 0);
}
/**
* @brief Standalone implementation of dpm_load_module
*/
inline int dpm_load_module(const char* module_name, void** module_handle) {
if (!module_name || !module_handle) return 1;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return 1;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if the file exists
if (!dpm_module_exists(module_name)) return 1;
// Load the module
*module_handle = dlopen(module_file.c_str(), RTLD_LAZY);
if (!*module_handle) return 1;
return 0;
}
/**
* @brief Standalone implementation of dpm_symbol_exists
*/
inline bool dpm_symbol_exists(void* module_handle, const char* symbol_name) {
if (!module_handle || !symbol_name) return false;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error) return false;
return (symbol != NULL);
}
/**
* @brief Standalone implementation of dpm_execute_symbol
*/
inline int dpm_execute_symbol(void* module_handle, const char* symbol_name, void* args) {
if (!module_handle || !symbol_name) return 1;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error || !symbol) return 1;
// Cast to function pointer and call
typedef int (*FunctionPtr)(void*);
FunctionPtr func = (FunctionPtr)symbol;
return func(args);
}
/**
* @brief Standalone implementation of dpm_unload_module
*/
inline void dpm_unload_module(void* module_handle) {
if (module_handle) {
dlclose(module_handle);
}
}
/**
* @brief Standalone module main function
*

View File

@@ -31,81 +31,5 @@
// Only define these functions when not in standalone mode
#ifndef BUILD_STANDALONE
extern "C" bool dpm_module_exists(const char* module_name) {
if (!module_name) return false;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return false;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if file exists
struct stat buffer;
return (stat(module_file.c_str(), &buffer) == 0);
}
extern "C" int dpm_load_module(const char* module_name, void** module_handle) {
if (!module_name || !module_handle) return 1;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return 1;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if the file exists
if (!dpm_module_exists(module_name)) return 1;
// Load the module
*module_handle = dlopen(module_file.c_str(), RTLD_LAZY);
if (!*module_handle) return 1;
return 0;
}
extern "C" bool dpm_symbol_exists(void* module_handle, const char* symbol_name) {
if (!module_handle || !symbol_name) return false;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error) return false;
return (symbol != NULL);
}
extern "C" int dpm_execute_symbol(void* module_handle, const char* symbol_name, void* args) {
if (!module_handle || !symbol_name) return 1;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error || !symbol) return 1;
// Cast to function pointer and call
typedef int (*FunctionPtr)(void*);
FunctionPtr func = (FunctionPtr)symbol;
return func(args);
}
extern "C" void dpm_unload_module(void* module_handle) {
if (module_handle) {
dlclose(module_handle);
}
}
#endif // BUILD_STANDALONE

View File

@@ -0,0 +1,69 @@
/**
* @file ModuleOperations.cpp
* @brief Implementation of C++ interface for module operations
*
* @copyright Copyright (c) 2025 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
*
* Part of the Dark Horse Linux Package Manager (DPM)
*/
#include "ModuleOperations.hpp"
bool dpm_module_exists(const char* module_name) {
if (!module_name) return false;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return false;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if file exists
struct stat buffer;
return (stat(module_file.c_str(), &buffer) == 0);
}
int dpm_load_module(const char* module_name, void** module_handle) {
if (!module_name || !module_handle) return 1;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return 1;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if the file exists
if (!dpm_module_exists(module_name)) return 1;
// Load the module
*module_handle = dlopen(module_file.c_str(), RTLD_LAZY);
if (!*module_handle) return 1;
return 0;
}
bool dpm_symbol_exists(void* module_handle, const char* symbol_name) {
if (!module_handle || !symbol_name) return false;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error) return false;
return (symbol != nullptr);
}
void dpm_unload_module(void* module_handle) {
if (module_handle) {
dlclose(module_handle);
}
}