Skip to content

Commit

Permalink
misc: path handling improvements (#163)
Browse files Browse the repository at this point in the history
* misc: use weakly_canonical for absolutePath

* misc: make absolutePath not optional

* misc: use currentDir as the argument to absolutePath

* config: avoid circular imports
  • Loading branch information
PaideiaDilemma authored Mar 10, 2024
1 parent 766d470 commit 0f44f76
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
23 changes: 12 additions & 11 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 3 additions & 8 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
@@ -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 '~'
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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&);
2 changes: 1 addition & 1 deletion src/renderer/AsyncResourceGatherer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 0f44f76

Please sign in to comment.