new_build_system (#18)
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
Reviewed-on: #18
This commit is contained in:
@@ -23,18 +23,11 @@
|
||||
|
||||
#include "utils/common.hpp"
|
||||
#include "utils/error.hpp"
|
||||
#include "utils/file.hpp"
|
||||
#include "utils/string.hpp"
|
||||
#include "utils/unix.hpp"
|
||||
|
||||
namespace {
|
||||
static const std::string directory_seperator_str{
|
||||
repertory::utils::path::directory_seperator,
|
||||
};
|
||||
|
||||
static const std::wstring directory_seperator_str_w{
|
||||
repertory::utils::path::directory_seperator_w,
|
||||
};
|
||||
|
||||
[[nodiscard]] auto resolve(std::string path) -> std::string {
|
||||
#if defined(_WIN32)
|
||||
if (repertory::utils::string::contains(path, "~") ||
|
||||
@@ -52,12 +45,17 @@ static const std::wstring directory_seperator_str_w{
|
||||
#else // !defined (_WIN32)
|
||||
if (repertory::utils::string::contains(path, "~")) {
|
||||
std::string home{};
|
||||
repertory::utils::use_getpwuid(getuid(), [&home](struct passwd *pw) {
|
||||
home = (pw->pw_dir ? pw->pw_dir : "");
|
||||
if (home.empty() || ((home == "/") && (getuid() != 0))) {
|
||||
home = repertory::utils::path::combine("/home", {pw->pw_name});
|
||||
}
|
||||
});
|
||||
auto res =
|
||||
repertory::utils::use_getpwuid(getuid(), [&home](struct passwd *pw) {
|
||||
home = (pw->pw_dir ? pw->pw_dir : "");
|
||||
if (home.empty() ||
|
||||
((home == repertory::utils::path::slash) && (getuid() != 0))) {
|
||||
home = repertory::utils::path::combine("/home", {pw->pw_name});
|
||||
}
|
||||
});
|
||||
if (res) {
|
||||
throw std::runtime_error("failed to getpwuid: " + res.reason);
|
||||
}
|
||||
|
||||
return repertory::utils::string::replace(path, "~", home);
|
||||
}
|
||||
@@ -70,42 +68,59 @@ static const std::wstring directory_seperator_str_w{
|
||||
namespace repertory::utils::path {
|
||||
auto absolute(std::string_view path) -> std::string {
|
||||
std::string abs_path{path};
|
||||
abs_path = resolve(abs_path);
|
||||
format_path(abs_path, directory_seperator, not_directory_seperator);
|
||||
if (abs_path.empty()) {
|
||||
return abs_path;
|
||||
}
|
||||
|
||||
abs_path = finalize(resolve(abs_path));
|
||||
#if defined(_WIN32)
|
||||
if (not abs_path.empty() && ::PathIsRelativeA(abs_path.c_str())) {
|
||||
std::string temp;
|
||||
temp.resize(MAX_PATH + 1);
|
||||
abs_path = _fullpath(temp.data(), abs_path.c_str(), MAX_PATH);
|
||||
if (not utils::string::contains(abs_path, dot)) {
|
||||
return abs_path;
|
||||
}
|
||||
|
||||
std::string temp;
|
||||
temp.resize(repertory::max_path_length + 1);
|
||||
::GetFullPathNameA(abs_path.c_str(), static_cast<DWORD>(temp.size()),
|
||||
temp.data(), nullptr);
|
||||
#else // !defined(_WIN32)
|
||||
if (not abs_path.empty() && (abs_path.at(0U) != '/')) {
|
||||
auto found{false};
|
||||
std::string tmp{abs_path};
|
||||
do {
|
||||
auto *res = realpath(tmp.c_str(), nullptr);
|
||||
if (res != nullptr) {
|
||||
abs_path = res + std::string{directory_seperator} +
|
||||
abs_path.substr(tmp.size());
|
||||
free(res);
|
||||
found = true;
|
||||
} else if (tmp == ".") {
|
||||
found = true;
|
||||
} else {
|
||||
tmp = dirname(tmp.data());
|
||||
}
|
||||
} while (not found);
|
||||
if (not utils::string::contains(abs_path, dot) ||
|
||||
utils::string::begins_with(abs_path, slash)) {
|
||||
return abs_path;
|
||||
}
|
||||
|
||||
auto found{false};
|
||||
std::string tmp{abs_path};
|
||||
do {
|
||||
auto *res = realpath(tmp.c_str(), nullptr);
|
||||
if (res != nullptr) {
|
||||
abs_path =
|
||||
res + std::string{directory_seperator} + abs_path.substr(tmp.size());
|
||||
free(res);
|
||||
found = true;
|
||||
} else if (tmp == dot) {
|
||||
found = true;
|
||||
} else {
|
||||
tmp = dirname(tmp.data());
|
||||
}
|
||||
} while (not found);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return format_path(abs_path, directory_seperator, not_directory_seperator);
|
||||
return finalize(abs_path);
|
||||
}
|
||||
|
||||
auto absolute(std::wstring_view path) -> std::wstring {
|
||||
return utils::string::from_utf8(absolute(utils::string::to_utf8(path)));
|
||||
}
|
||||
|
||||
auto exists(std::string_view path) -> bool {
|
||||
return utils::file::file{path}.exists() ||
|
||||
utils::file::directory{path}.exists();
|
||||
}
|
||||
|
||||
auto exists(std::wstring_view path) -> bool {
|
||||
return exists(utils::string::to_utf8(path));
|
||||
}
|
||||
|
||||
auto find_program_in_path(const std::string &name_without_extension)
|
||||
-> std::string {
|
||||
static std::mutex mtx{};
|
||||
@@ -137,12 +152,12 @@ auto find_program_in_path(const std::string &name_without_extension)
|
||||
static constexpr const auto split_char = ':';
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
const auto search_path_list = utils::string::split(path, split_char, false);
|
||||
auto search_path_list = utils::string::split(path, split_char, false);
|
||||
for (auto &&search_path : search_path_list) {
|
||||
for (auto &&extension : extension_list) {
|
||||
auto exec_path = combine(
|
||||
search_path, {name_without_extension + std::string{extension}});
|
||||
if (std::filesystem::exists(exec_path)) {
|
||||
if (utils::file::file(exec_path).exists()) {
|
||||
found_items[name_without_extension] = exec_path;
|
||||
return exec_path;
|
||||
}
|
||||
@@ -158,41 +173,66 @@ find_program_in_path(std::wstring_view name_without_extension) -> std::wstring {
|
||||
find_program_in_path(utils::string::to_utf8(name_without_extension)));
|
||||
}
|
||||
|
||||
auto get_parent_directory(std::string_view path) -> std::string {
|
||||
auto ret = std::filesystem::path{path}.parent_path().string();
|
||||
#if !defined(_WIN32)
|
||||
if (ret == ".") {
|
||||
ret = "/";
|
||||
}
|
||||
#endif // !defined(_WIN32)
|
||||
auto get_parent_path(std::string_view path) -> std::string {
|
||||
auto abs_path = absolute(path);
|
||||
|
||||
return absolute(ret);
|
||||
#if defined(_WIN32)
|
||||
::PathRemoveFileSpecA(abs_path.data());
|
||||
abs_path = abs_path.c_str();
|
||||
#else // !defined(_WIN32)
|
||||
abs_path = std::filesystem::path{abs_path}.parent_path().string();
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return finalize(abs_path);
|
||||
}
|
||||
|
||||
auto get_parent_directory(std::wstring_view path) -> std::wstring {
|
||||
auto get_parent_path(std::wstring_view path) -> std::wstring {
|
||||
return utils::string::from_utf8(
|
||||
get_parent_directory(utils::string::to_utf8(path)));
|
||||
get_parent_path(utils::string::to_utf8(path)));
|
||||
}
|
||||
|
||||
auto is_trash_directory(std::string_view path) -> bool {
|
||||
auto trash_path = utils::string::to_lower(absolute(path));
|
||||
return utils::string::begins_with(trash_path,
|
||||
directory_seperator_str + ".trash-") ||
|
||||
utils::string::begins_with(trash_path,
|
||||
directory_seperator_str + ".trashes") ||
|
||||
utils::string::begins_with(trash_path,
|
||||
directory_seperator_str + "$recycle.bin");
|
||||
auto get_relative_path(std::string_view path,
|
||||
std::string_view root_path) -> std::string {
|
||||
auto abs_path = absolute(path);
|
||||
auto abs_root_path =
|
||||
absolute(root_path) + std::string{get_directory_seperator<char>()};
|
||||
#if defined(_WIN32)
|
||||
if (utils::string::to_lower(abs_path).starts_with(
|
||||
utils::string::to_lower(abs_root_path))) {
|
||||
#else // !defined(_WIN32)
|
||||
if (abs_path.starts_with(abs_root_path)) {
|
||||
#endif // defined(_WIN32)
|
||||
return abs_path.substr(abs_root_path.size());
|
||||
}
|
||||
|
||||
return abs_path;
|
||||
}
|
||||
|
||||
auto is_trash_directory(std::wstring_view path) -> bool {
|
||||
return is_trash_directory(utils::string::to_utf8(path));
|
||||
auto get_relative_path(std::wstring_view path,
|
||||
std::wstring_view root_path) -> std::wstring {
|
||||
return utils::string::from_utf8(get_relative_path(
|
||||
utils::string::to_utf8(path), utils::string::to_utf8(root_path)));
|
||||
}
|
||||
|
||||
auto contains_trash_directory(std::string_view path) -> bool {
|
||||
auto parts = utils::string::split(utils::string::to_lower(absolute(path)),
|
||||
get_directory_seperator<char>(), false);
|
||||
|
||||
return std::find_if(parts.begin(), parts.end(), [](auto &&part) -> bool {
|
||||
return utils::string::begins_with(part, ".trash-") ||
|
||||
part == ".trashes" || part == "$recycle.bin";
|
||||
}) != parts.end();
|
||||
}
|
||||
|
||||
auto contains_trash_directory(std::wstring_view path) -> bool {
|
||||
return contains_trash_directory(utils::string::to_utf8(path));
|
||||
}
|
||||
|
||||
auto make_file_uri(std::string_view path) -> std::string {
|
||||
auto abs_path = absolute(path);
|
||||
#if defined(_WIN32)
|
||||
utils::string::replace(abs_path, '\\', '/');
|
||||
abs_path = '/' + abs_path;
|
||||
utils::string::replace(abs_path, backslash, slash);
|
||||
abs_path = std::string{slash} + abs_path;
|
||||
#endif // defined(_WIN32)
|
||||
return "file://" + abs_path;
|
||||
}
|
||||
@@ -201,31 +241,11 @@ auto make_file_uri(std::wstring_view path) -> std::wstring {
|
||||
return utils::string::from_utf8(make_file_uri(utils::string::to_utf8(path)));
|
||||
}
|
||||
|
||||
auto remove_file_name(std::string_view path) -> std::string {
|
||||
auto abs_path = absolute(path);
|
||||
|
||||
#if defined(_WIN32)
|
||||
::PathRemoveFileSpecA(abs_path.data());
|
||||
abs_path = abs_path.c_str();
|
||||
#else // !defined(_WIN32)
|
||||
if (abs_path != "/") {
|
||||
auto idx{abs_path.size() - 1U};
|
||||
while ((idx != 0U) && (abs_path.at(idx) != '/')) {
|
||||
idx--;
|
||||
}
|
||||
|
||||
abs_path = (idx > 0U) ? absolute(abs_path.substr(0U, idx)) : "/";
|
||||
}
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return abs_path;
|
||||
}
|
||||
|
||||
auto strip_to_file_name(std::string path) -> std::string {
|
||||
#if defined(_WIN32)
|
||||
return ::PathFindFileNameA(path.c_str());
|
||||
#else // !defined(_WIN32)
|
||||
return utils::string::contains(path, "/") ? basename(path.data()) : path;
|
||||
return utils::string::contains(path, slash) ? basename(path.data()) : path;
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user