Files
dpm-core-ng/include/internal/context.hpp
Christopher M. Punches 50d71ca55f Modules can report why they failed
A module had one channel back to its caller: the int returned from
dpm_module_execute, handed through by dpm_execute. Any detail behind
that number could only reach a log, so a caller wanting the reason had
to read output rather than ask for it.

dpm_set_last_error joins the exported API. A module records its reason
on the context handed to its entry point, which belongs to the caller,
and the caller reads it back with dpm_get_last_error.

The two accessors on the context now say what they do to it:
dpm_module_path becomes dpm_get_resolved_module_path, naming the value
it reports rather than the setting it came from, and dpm_last_error
becomes dpm_get_last_error, pairing with the setter.

Path normalization moves to sanitizers.cpp, which holds the conversions
that put a value written by a person into the single form the library
stores it in.
2026-08-19 00:50:18 -04:00

94 lines
3.1 KiB
C++

/**
* @file context.hpp
* @brief The libdpm-core.so context: configuration, logging, module registry
*
* @copyright Copyright (c) 2026 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
*
* Part of the Dark Horse Linux Package Manager (DPM)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <dpm/core.h>
#include <map>
#include <memory>
#include <string>
#include "internal/modules.hpp"
/** @brief A libdpm-core.so context: configuration, logging, module registry */
struct dpm_ctx {
/** Directory the .conf files were read from. */
std::string config_dir;
/** Directory modules are loaded from. */
std::string module_path;
/** Target root for package operations. */
std::string root;
/** Messages above this level are dropped. */
int log_level = DPM_LOG_INFO;
/** Whether to append messages to log_file. */
bool write_to_log = false;
/** Path of the log file. */
std::string log_file;
/** Configuration store, addressed as config[module][section][key]. */
std::map<std::string,
std::map<std::string,
std::map<std::string, std::string>>> config;
/** Validated modules keyed by name; each is loaded at most once. */
std::map<std::string, std::unique_ptr<dpm_module>> modules;
/** Reason for the most recent failure. */
std::string last_error;
};
/**
* @namespace dpm_core
* @brief Internal implementation of libdpm-core.so
*
* Holds what the library's translation units share with each other and
* with nothing else: the loader, the configuration reader, the error
* recorder, and the version parser. It is declared in include/internal/,
* which is never installed, so a consumer or a module cannot name any of
* it.
*
* Nothing in here reaches the export table. The library is compiled with
* hidden default symbol visibility and linked against a version script
* whose `local: *` covers everything the public declarations do not
* claim, so these keep external linkage across the library's own files
* while staying invisible outside it.
*
* Helpers used by a single translation unit belong in that file's
* unnamed namespace instead, which gives them internal linkage and keeps
* them from colliding across files.
*/
namespace dpm_core {
/**
* @brief Records a failure reason on the context
*
* @param ctx The libdpm-core.so context; NULL is a no-op
* @param msg The failure description
*/
void set_last_error(dpm_ctx* ctx, const std::string& msg);
} // namespace dpm_core