updated build system

This commit is contained in:
2024-08-20 08:48:29 -05:00
parent d83de7bb91
commit 734dab801d
26 changed files with 2440 additions and 277 deletions

View File

@ -77,10 +77,43 @@ namespace {
} // namespace
namespace repertory::utils::file {
auto i_file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
data_buffer buffer;
buffer.resize(get_read_buffer_size());
std::size_t current_read{};
while (read(reinterpret_cast<unsigned char *>(buffer.data()),
buffer.size() * sizeof(data_buffer::value_type), offset,
&current_read)) {
if (total_read != nullptr) {
*total_read += current_read;
}
if (current_read != 0U) {
offset += current_read;
data.insert(
data.end(), buffer.begin(),
std::next(buffer.begin(),
static_cast<std::int64_t>(
current_read / sizeof(data_buffer::value_type))));
continue;
}
return true;
}
return false;
}
auto create_directories(std::string_view path) -> bool {
if (is_directory(path)) {
return true;
}
#if defined(_WIN32)
return is_directory(path) ||
(::SHCreateDirectory(
return (::SHCreateDirectory(
nullptr,
utils::string::from_utf8(utils::path::absolute(path)).c_str()) ==
ERROR_SUCCESS);
@ -332,4 +365,170 @@ auto write_json_file(std::wstring_view path,
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // defined(PROJECT_ENABLE_JSON)
#if defined(PROJECT_ENABLE_LIBDSM)
static constexpr const auto validate_smb_path =
[](std::string_view path) -> bool {
return (not utils::string::begins_with(path, "///") &&
utils::string::begins_with(path, "//") &&
not utils::string::contains(path, " ") &&
std::count(path.begin(), path.end(), '/') >= 3U);
};
auto smb_create_smb_path(std::string_view smb_path,
std::string_view rel_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
std::string path{rel_path};
utils::path::format_path(path, "/", "\\");
utils::string::left_trim(path, '/');
auto old_parts =
repertory::utils::string::split(smb_path.substr(2U), '/', false);
old_parts.erase(std::next(old_parts.begin(), 2U), old_parts.end());
auto new_parts = repertory::utils::string::split(path, '/', false);
old_parts.insert(old_parts.end(), new_parts.begin(), new_parts.end());
path = utils::string::join(old_parts, '/');
path = "//" + utils::path::format_path(path, "/", "\\");
if (not validate_smb_path(path)) {
throw std::runtime_error("invalid smb path|" + std::string{path});
}
return path;
}
auto smb_create_and_validate_relative_path(
std::string_view smb_path, std::string_view path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
std::string dir_path;
if (utils::string::begins_with(path, "//")) {
if (not utils::file::smb_parent_is_same(smb_path, path)) {
throw std::runtime_error("failed to validate path|" +
std::string{smb_path} + '|' + std::string{path} +
"|parent paths are not the same");
}
return utils::file::smb_create_relative_path(path);
}
return utils::file::smb_create_relative_path(std::string{smb_path} + '/' +
std::string{path});
}
auto smb_create_relative_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
std::string path{smb_path};
utils::path::format_path(path, "\\", "/");
utils::string::left_trim(path, '\\');
auto parts = repertory::utils::string::split(path, '\\', false);
parts.erase(parts.begin(), std::next(parts.begin(), 2U));
return "\\" + utils::string::join(parts, '\\');
}
auto smb_create_search_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
std::string path{smb_path};
utils::string::left_trim(path, '/');
auto parts = repertory::utils::string::split(path, '/', false);
parts.erase(parts.begin(), std::next(parts.begin(), 2U));
auto search_path = repertory::utils::string::join(parts, '\\');
return search_path.empty() ? "\\*" : "\\" + search_path + "\\*";
}
auto smb_get_parent_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
auto parts = repertory::utils::string::split(smb_path.substr(2U), '/', false);
if (parts.size() > 2U) {
parts.erase(std::prev(parts.end()), parts.end());
}
auto parent_smb_path = "//" + utils::string::join(parts, '/');
if (not validate_smb_path(parent_smb_path)) {
throw std::runtime_error("invalid smb path|" + parent_smb_path);
}
return parent_smb_path;
}
auto smb_get_root_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
auto parts = repertory::utils::string::split(smb_path.substr(2U), '/', false);
if (parts.size() > 2U) {
parts.erase(std::next(parts.begin(), 2U), parts.end());
}
return "//" + utils::string::join(parts, '/');
}
auto smb_get_unc_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
std::string unc_path{smb_path};
utils::path::format_path(unc_path, "\\", "/");
return '\\' + unc_path;
}
auto smb_get_uri_path(std::string_view smb_path) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
return "smb:" + std::string{smb_path};
}
auto smb_get_uri_path(std::string_view smb_path, std::string_view user,
std::string_view password) -> std::string {
if (not validate_smb_path(smb_path)) {
throw std::runtime_error("invalid smb path|" + std::string{smb_path});
}
return "smb://" + std::string{user} + ':' + std::string{password} + '@' +
std::string{smb_path.substr(2U)};
}
auto smb_parent_is_same(std::string_view smb_path1,
std::string_view smb_path2) -> bool {
if (not(validate_smb_path(smb_path1) && validate_smb_path(smb_path2))) {
return false;
}
auto parts1 = utils::string::split(smb_path1.substr(2U), "/", false);
auto parts2 = utils::string::split(smb_path2.substr(2U), "/", false);
if (parts1.size() < 2U || parts2.size() < 2U) {
return false;
}
if (parts2.at(1U).empty() || parts1.at(1U).empty()) {
return false;
}
return std::equal(parts1.begin(), std::next(parts1.begin(), 2U),
parts2.begin());
}
#endif // defined(PROJECT_ENABLE_LIBDSM)
} // namespace repertory::utils::file