Skip to content

Commit

Permalink
config: streamlined absolutePath impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricekraus committed Feb 21, 2024
1 parent c3198d3 commit 434f631
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
23 changes: 15 additions & 8 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,37 @@ std::optional<std::string> CConfigManager::handleSource(const std::string& comma
std::unique_ptr<glob_t, void (*)(glob_t*)> glob_buf{new glob_t, [](glob_t* g) { globfree(g); }};
memset(glob_buf.get(), 0, sizeof(glob_t));

if (auto r = glob(absolutePath(rawpath, configCurrentPath).c_str(), GLOB_TILDE, nullptr, glob_buf.get()); r != 0) {

if (auto r = glob(absolutePath(rawpath, configCurrentPath)->c_str(), GLOB_TILDE, nullptr, glob_buf.get()); r != 0) {
std::string err = std::format("source= globbing error: {}", r == GLOB_NOMATCH ? "found no match" : GLOB_ABORTED ? "read error" : "out of memory");
Debug::log(ERR, "{}", err);
return err;
}

for (size_t i = 0; i < glob_buf->gl_pathc; i++) {
auto value = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);
auto pathValueOpt = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);
if (pathValueOpt->empty()) {
Debug::log(WARN, "source= skipping invalid path");
continue;
}

auto pathValue = pathValueOpt.value();

if (!std::filesystem::is_regular_file(value)) {
if (std::filesystem::exists(value)) {
Debug::log(WARN, "source= skipping non-file {}", value);
if (!std::filesystem::is_regular_file(pathValue)) {
if (std::filesystem::exists(pathValue)) {
Debug::log(WARN, "source= skipping non-file {}", pathValue);
continue;
}

Debug::log(ERR, "source= file doesnt exist");
return "source file " + value + " doesn't exist!";
return "source file " + pathValue + " doesn't exist!";
}

// allow for nested config parsing
auto backupConfigPath = configCurrentPath;
configCurrentPath = value;
configCurrentPath = pathValue;

m_config.parseFile(value.c_str());
m_config.parseFile(pathValue.c_str());

configCurrentPath = backupConfigPath;

Expand Down
34 changes: 16 additions & 18 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#include <filesystem>
#include "MiscFunctions.hpp"

std::string absolutePath(const std::string& rawpath, const std::string& currentPath) {
auto value = rawpath;
std::optional<std::string> absolutePath(const std::string& rawpath, const std::string& rawcurrentpath) {
std::filesystem::path path(rawpath);

if (value[0] == '~') {
// Handling the case where rawpath starts with '~'
if (!rawpath.empty() && rawpath[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
value.replace(0, 1, std::string(ENVHOME));
} else if (value[0] != '/') {
auto currentDir = currentPath.substr(0, currentPath.find_last_of('/'));

if (value[0] == '.') {
if (value[1] == '.' && value[2] == '/') {
auto parentDir = currentDir.substr(0, currentDir.find_last_of('/'));
value.replace(0, 2 + currentPath.empty(), parentDir);
} else if (value[1] == '/')
value.replace(0, 1 + currentPath.empty(), currentDir);
else
value = currentDir + '/' + value;
} else
value = currentDir + '/' + value;
return std::filesystem::path(ENVHOME) / path.relative_path().string().substr(2);
}
// Handling e.g. ./, ../
else if (path.is_relative()) {
const std::filesystem::path currentDir = std::filesystem::path(rawcurrentpath).parent_path();

return value;
auto finalPath = currentDir / path;
if (exists(finalPath))
return std::filesystem::canonical(currentDir / path);
return {};
} else {
return std::filesystem::canonical(path);
}
}
4 changes: 2 additions & 2 deletions src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <optional>

std::string absolutePath(const std::string&, const std::string&);

std::optional<std::string> absolutePath(const std::string&, const std::string&);

0 comments on commit 434f631

Please sign in to comment.