misc: path handling improvements (#163)

* misc: use weakly_canonical for absolutePath

* misc: make absolutePath not optional

* misc: use currentDir as the argument to absolutePath

* config: avoid circular imports
This commit is contained in:
Maximilian Seidler 2024-03-10 15:37:25 +01:00 committed by GitHub
parent 766d470308
commit 0f44f76368
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 21 deletions

View file

@ -208,36 +208,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) {
const auto CURRENTDIR = std::filesystem::path(configCurrentPath).parent_path().string();
if (auto r = glob(absolutePath(rawpath, CURRENTDIR).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 pathValueOpt = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);
if (pathValueOpt->empty()) {
const auto PATH = absolutePath(glob_buf->gl_pathv[i], CURRENTDIR);
if (PATH.empty() || PATH == configCurrentPath) {
Debug::log(WARN, "source= skipping invalid path");
continue;
}
auto pathValue = pathValueOpt.value();
if (!std::filesystem::is_regular_file(pathValue)) {
if (std::filesystem::exists(pathValue)) {
Debug::log(WARN, "source= skipping non-file {}", pathValue);
if (!std::filesystem::is_regular_file(PATH)) {
if (std::filesystem::exists(PATH)) {
Debug::log(WARN, "source= skipping non-file {}", PATH);
continue;
}
Debug::log(ERR, "source= file doesnt exist");
return "source file " + pathValue + " doesn't exist!";
return "source file " + PATH + " doesn't exist!";
}
// allow for nested config parsing
auto backupConfigPath = configCurrentPath;
configCurrentPath = pathValue;
configCurrentPath = PATH;
m_config.parseFile(pathValue.c_str());
m_config.parseFile(PATH.c_str());
configCurrentPath = backupConfigPath;
}

View file

@ -1,7 +1,7 @@
#include <filesystem>
#include "MiscFunctions.hpp"
std::optional<std::string> absolutePath(const std::string& rawpath, const std::string& rawcurrentpath) {
std::string absolutePath(const std::string& rawpath, const std::string& currentDir) {
std::filesystem::path path(rawpath);
// Handling where rawpath starts with '~'
@ -11,13 +11,8 @@ std::optional<std::string> absolutePath(const std::string& rawpath, const std::s
}
// Handling e.g. ./, ../
else if (path.is_relative()) {
const std::filesystem::path currentDir = std::filesystem::path(rawcurrentpath).parent_path();
auto finalPath = currentDir / path;
if (exists(finalPath))
return std::filesystem::canonical(currentDir / path);
return {};
return std::filesystem::weakly_canonical(std::filesystem::path(currentDir) / path);
} else {
return std::filesystem::canonical(path);
return std::filesystem::weakly_canonical(path);
}
}

View file

@ -3,4 +3,4 @@
#include <string>
#include <optional>
std::optional<std::string> absolutePath(const std::string&, const std::string&);
std::string absolutePath(const std::string&, const std::string&);

View file

@ -132,7 +132,7 @@ void CAsyncResourceGatherer::gather() {
continue;
std::string id = std::string{"background:"} + path;
const auto ABSOLUTEPATH = absolutePath(path, "").value_or(path);
const auto ABSOLUTEPATH = absolutePath(path, "");
// preload bg img
const auto CAIROISURFACE = cairo_image_surface_create_from_png(ABSOLUTEPATH.c_str());