data_dir.cpp

src/config/data_dir.cpp

@file data_dir.cpp @brief Bundled data directory resolution.

Owners: @tvanfossen

resolve_data_dir

std::filesystem::path entropic::config::resolve_data_dir(const ParsedConfig &config)

Resolve the bundled data directory.

src/config/data_dir.cpp:81

Source · 43 lines
std::filesystem::path resolve_data_dir(const ParsedConfig& config)
{
    struct Candidate {
        std::filesystem::path path;
        const char* label;
    };

    std::vector<Candidate> candidates;

    // Priority 1: ENTROPIC_DATA_DIR env var (explicit operator override)
    if (const char* env = std::getenv("ENTROPIC_DATA_DIR"); env && *env) {
        candidates.push_back({env, "ENTROPIC_DATA_DIR env"});
    }

    // Priority 2: config.config_dir / "data"
    if (!config.config_dir.empty()) {
        candidates.push_back({config.config_dir / "data", "config.config_dir"});
    }

    // Priority 3: binary-relative discovery (portable across prefixes)
    if (auto from_lib = share_dir_from_library(); !from_lib.empty()) {
        candidates.push_back({from_lib, "binary-relative (dladdr)"});
    }

    // Priority 4-5: compile-time install path, then source-tree / CWD fallbacks
    candidates.push_back({CONFIG_ENTROPIC_DATA_DIR,        "compile-time install path"});
    candidates.push_back({CONFIG_ENTROPIC_SOURCE_DATA_DIR, "source tree (dev fallback)"});
    candidates.push_back({"data",                          "CWD-relative (dev fallback)"});

    std::filesystem::path result;
    for (const auto& [path, label] : candidates) {
        if (std::filesystem::is_directory(path)) {
            s_log->info("Data dir from {}: {}", label, path.string());
            result = path;
            break;
        }
    }

    if (result.empty()) {
        s_log->warn("No data directory found — bundled files unavailable");
    }
    return result;
}
Activity · 20 steps
lines 83–86struct Candidate { std::filesystem::path path; const char* label; }lines 88–88std::vector<Candidate> candidates;lines 91–91◇ if (const char* env = std::getenv("ENTROPIC_DATA_DIR"); env && *env)lines 92–92candidates.push_back({env, "ENTROPIC_DATA_DIR env"});lines 96–96◇ if (!config.config_dir.empty())lines 97–97candidates.push_back({config.config_dir / "data", "config.config_dir"});lines 101–101◇ if (auto from_lib = share_dir_from_library(); !from_lib.empty())lines 102–102candidates.push_back({from_lib, "binary-relative (dladdr)"});lines 106–106candidates.push_back({CONFIG_ENTROPIC_DATA_DIR, "compile-time install path"});lines 107–107candidates.push_back({CONFIG_ENTROPIC_SOURCE_DATA_DIR, "source tree (dev fallback)"});lines 108–108candidates.push_back({"data", "CWD-relative (dev fallback)"});lines 110–110std::filesystem::path result;lines 111–111◇ for (const auto& [path, label] : candidates)lines 112–112◇ if (std::filesystem::is_directory(path))lines 113–113s_log->info("Data dir from {}: {}", label, path.string());lines 114–114result = path;lines 115–115break;lines 119–119◇ if (result.empty())lines 120–120s_log->warn("No data directory found — bundled files unavailable");lines 122–122return result;

Requirements

REQ-CFG-008

share_dir_from_library

static std::filesystem::path entropic::config::share_dir_from_library()

Locate librentropic.so on disk and derive its sibling share dir.

src/config/data_dir.cpp:37

Source · 22 lines
static std::filesystem::path share_dir_from_library()
{
    // Take the address of resolve_data_dir itself — guaranteed to be
    // inside librentropic.so (we're in it). dladdr resolves this
    // pointer back to the .so's filesystem path.
    Dl_info info = {};
    if (dladdr(reinterpret_cast<void*>(&share_dir_from_library), &info) == 0
        || info.dli_fname == nullptr) {
        return {};
    }
    auto lib_path = std::filesystem::path(info.dli_fname);
    // dli_fname may be a relative path (e.g. just "librentropic.so")
    // when the loader resolved via LD_LIBRARY_PATH. Make it absolute
    // so ".." traversal is meaningful.
    std::error_code ec;
    auto abs_lib = std::filesystem::absolute(lib_path, ec);
    if (ec) {
        return {};
    }
    // <prefix>/lib/librentropic.so.2.0.5 → <prefix>/share/entropic
    return abs_lib.parent_path().parent_path() / "share" / "entropic";
}
Activity · 9 steps
lines 42–42Dl_info info = {};lines 43–44◇ if (dladdr(reinterpret_cast<void*>(&share_dir_from_library), &info) == 0 || info.dli_fname == nullptr)lines 45–45return {};lines 47–47auto lib_path = std::filesystem::path(info.dli_fname);lines 51–51std::error_code ec;lines 52–52auto abs_lib = std::filesystem::absolute(lib_path, ec);lines 53–53◇ if (ec)lines 54–54return {};lines 57–57return abs_lib.parent_path().parent_path() / "share" / "entropic";

Requirements

REQ-CFG-008