418 lines
14 KiB
C++
418 lines
14 KiB
C++
/*
|
|
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#include "utils/file.hpp"
|
|
|
|
#include "utils/encryption.hpp"
|
|
#include "utils/error.hpp"
|
|
#include "utils/path.hpp"
|
|
#include "utils/string.hpp"
|
|
|
|
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,
|
|
¤t_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 i_fs_item::get_time(time_types type) const -> std::uint64_t {
|
|
static constexpr const std::string_view function_name{
|
|
static_cast<const char *>(__FUNCTION__),
|
|
};
|
|
|
|
try {
|
|
#if defined(_WIN32)
|
|
struct _stat64 st {};
|
|
_stat64(get_path().c_str(), &st);
|
|
#else // !defined(_WIN32)
|
|
struct stat st {};
|
|
stat(get_path().c_str(), &st);
|
|
#endif // defined(_WIN32)
|
|
|
|
switch (type) {
|
|
case time_types::access:
|
|
#if defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_atime);
|
|
#else // !defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_atim.tv_nsec +
|
|
st.st_atim.tv_sec *
|
|
utils::time::NANOS_PER_SECOND);
|
|
#endif // defined(_WIN32)
|
|
|
|
case time_types::creation:
|
|
#if defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_ctime);
|
|
#else // !defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_ctim.tv_nsec +
|
|
st.st_ctim.tv_sec *
|
|
utils::time::NANOS_PER_SECOND);
|
|
#endif // defined(_WIN32)
|
|
|
|
case time_types::modified:
|
|
#if defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_mtime);
|
|
#else // !defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
|
|
st.st_mtim.tv_sec *
|
|
utils::time::NANOS_PER_SECOND);
|
|
#endif // defined(_WIN32)
|
|
|
|
case time_types::write:
|
|
#if defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_mtime);
|
|
#else // !defined(_WIN32)
|
|
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
|
|
st.st_mtim.tv_sec *
|
|
utils::time::NANOS_PER_SECOND);
|
|
#endif // defined(_WIN32)
|
|
}
|
|
} catch (const std::exception &e) {
|
|
utils::error::handle_exception(function_name, e);
|
|
} catch (...) {
|
|
utils::error::handle_exception(function_name);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_JSON)
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto read_json_file(std::string_view path, nlohmann::json &data,
|
|
std::optional<std::string_view> password) -> bool {
|
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
static constexpr const std::string_view function_name{
|
|
static_cast<const char *>(__FUNCTION__),
|
|
};
|
|
|
|
try {
|
|
auto abs_path = utils::path::absolute(path);
|
|
auto file = file::open_file(abs_path);
|
|
if (not *file) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
data_buffer buffer{};
|
|
if (not file->read_all(buffer, 0U)) {
|
|
return false;
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
if (password.has_value()) {
|
|
data_buffer decrypted_data{};
|
|
if (not utils::encryption::decrypt_data(*password, buffer,
|
|
decrypted_data)) {
|
|
return false;
|
|
}
|
|
|
|
buffer = decrypted_data;
|
|
}
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
|
|
std::string json_str(buffer.begin(), buffer.end());
|
|
if (not json_str.empty()) {
|
|
data = nlohmann::json::parse(json_str);
|
|
}
|
|
} catch (const std::exception &e) {
|
|
utils::error::handle_exception(function_name, e);
|
|
return false;
|
|
} catch (...) {
|
|
utils::error::handle_exception(function_name);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} catch (const std::exception &e) {
|
|
utils::error::handle_exception(function_name, e);
|
|
} catch (...) {
|
|
utils::error::handle_exception(function_name);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto write_json_file(std::string_view path, const nlohmann::json &data,
|
|
std::optional<std::string_view> password) -> bool {
|
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto write_json_file(std::string_view path,
|
|
const nlohmann::json &data) -> bool {
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
static constexpr const std::string_view function_name{
|
|
static_cast<const char *>(__FUNCTION__),
|
|
};
|
|
|
|
try {
|
|
auto file = file::open_or_create_file(path);
|
|
if (not file->truncate()) {
|
|
throw std::runtime_error("failed to truncate file");
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
if (password.has_value()) {
|
|
const auto str_data = data.dump();
|
|
|
|
data_buffer encrypted_data{};
|
|
utils::encryption::encrypt_data(
|
|
*password, reinterpret_cast<const unsigned char *>(str_data.c_str()),
|
|
str_data.size(), encrypted_data);
|
|
return file->write(encrypted_data, 0U);
|
|
}
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
|
|
auto json_str = data.dump();
|
|
return file->write(
|
|
reinterpret_cast<const unsigned char *>(json_str.c_str()),
|
|
json_str.size(), 0U);
|
|
} catch (const std::exception &e) {
|
|
utils::error::handle_exception(function_name, e);
|
|
} catch (...) {
|
|
utils::error::handle_exception(function_name);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto read_json_file(std::wstring_view path, nlohmann::json &data,
|
|
std::optional<std::wstring_view> password) -> bool {
|
|
if (password.has_value()) {
|
|
auto password_a = utils::string::to_utf8(*password);
|
|
return read_json_file(utils::string::to_utf8(path), data, password_a);
|
|
}
|
|
|
|
return read_json_file(utils::string::to_utf8(path), data, std::nullopt);
|
|
}
|
|
|
|
auto write_json_file(std::wstring_view path, const nlohmann::json &data,
|
|
std::optional<std::wstring_view> password) -> bool {
|
|
if (password.has_value()) {
|
|
auto password_a = utils::string::to_utf8(*password);
|
|
return write_json_file(utils::string::to_utf8(path), data, password_a);
|
|
}
|
|
|
|
return write_json_file(utils::string::to_utf8(path), data, std::nullopt);
|
|
}
|
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool {
|
|
return read_json_file(utils::string::to_utf8(path), data);
|
|
}
|
|
|
|
auto write_json_file(std::wstring_view path,
|
|
const nlohmann::json &data) -> bool {
|
|
return write_json_file(utils::string::to_utf8(path), data);
|
|
}
|
|
#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
|