updated build system
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
This commit is contained in:
@ -25,50 +25,28 @@
|
|||||||
#include "utils/config.hpp"
|
#include "utils/config.hpp"
|
||||||
|
|
||||||
#include "utils/path.hpp"
|
#include "utils/path.hpp"
|
||||||
|
#include "utils/types/file/i_directory.hpp"
|
||||||
|
#include "utils/types/file/i_file.hpp"
|
||||||
|
#include "utils/types/file/i_fs_item.hpp"
|
||||||
|
|
||||||
namespace repertory::utils::file {
|
namespace repertory::utils::file {
|
||||||
enum class time_type {
|
|
||||||
accessed,
|
|
||||||
created,
|
|
||||||
modified,
|
|
||||||
written,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct file_times final {
|
|
||||||
std::uint64_t accessed{};
|
|
||||||
std::uint64_t created{};
|
|
||||||
std::uint64_t modified{};
|
|
||||||
std::uint64_t written{};
|
|
||||||
|
|
||||||
[[nodiscard]] auto get(time_type type) const -> std::uint64_t {
|
|
||||||
switch (type) {
|
|
||||||
case time_type::accessed:
|
|
||||||
return accessed;
|
|
||||||
case time_type::created:
|
|
||||||
return created;
|
|
||||||
case time_type::modified:
|
|
||||||
return modified;
|
|
||||||
case time_type::written:
|
|
||||||
return written;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("type_type not supported");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[[nodiscard]] auto change_to_process_directory() -> bool;
|
[[nodiscard]] auto change_to_process_directory() -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] inline auto
|
[[nodiscard]] inline auto
|
||||||
directory_exists_in_path(std::string_view path,
|
directory_exists_in_path(std::string_view path,
|
||||||
std::string_view sub_directory) -> bool;
|
std::string_view sub_directory) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] inline auto
|
[[nodiscard]] inline auto
|
||||||
directory_exists_in_path(std::wstring_view path,
|
directory_exists_in_path(std::wstring_view path,
|
||||||
std::wstring_view sub_directory) -> bool;
|
std::wstring_view sub_directory) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] inline auto
|
[[nodiscard]] inline auto
|
||||||
file_exists_in_path(std::string_view path, std::string_view file_name) -> bool;
|
file_exists_in_path(std::string_view path, std::string_view file_name) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] inline auto
|
[[nodiscard]] inline auto
|
||||||
file_exists_in_path(std::wstring_view path,
|
file_exists_in_path(std::wstring_view path,
|
||||||
std::wstring_view file_name) -> bool;
|
std::wstring_view file_name) -> bool;
|
||||||
@ -102,12 +80,15 @@ get_total_drive_space(std::wstring_view path) -> std::optional<std::uint64_t>;
|
|||||||
smb_create_and_validate_relative_path(std::string_view smb_path,
|
smb_create_and_validate_relative_path(std::string_view smb_path,
|
||||||
std::string_view rel_path) -> std::string;
|
std::string_view rel_path) -> std::string;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto
|
[[nodiscard]] auto
|
||||||
smb_create_relative_path(std::string_view smb_path) -> std::string;
|
smb_create_relative_path(std::string_view smb_path) -> std::string;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto
|
[[nodiscard]] auto
|
||||||
smb_create_search_path(std::string_view smb_path) -> std::string;
|
smb_create_search_path(std::string_view smb_path) -> std::string;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto
|
[[nodiscard]] auto
|
||||||
smb_create_smb_path(std::string_view smb_path,
|
smb_create_smb_path(std::string_view smb_path,
|
||||||
std::string_view rel_path) -> std::string;
|
std::string_view rel_path) -> std::string;
|
||||||
@ -125,129 +106,18 @@ smb_get_parent_path(std::string_view smb_path) -> std::string;
|
|||||||
std::string_view user,
|
std::string_view user,
|
||||||
std::string_view password) -> std::string;
|
std::string_view password) -> std::string;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto smb_parent_is_same(std::string_view smb_path1,
|
[[nodiscard]] auto smb_parent_is_same(std::string_view smb_path1,
|
||||||
std::string_view smb_path2) -> bool;
|
std::string_view smb_path2) -> bool;
|
||||||
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
||||||
|
|
||||||
struct i_fs_item {
|
|
||||||
using fs_item_t = std::unique_ptr<i_fs_item>;
|
|
||||||
|
|
||||||
virtual ~i_fs_item() = default;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto copy_to(std::string_view to_path,
|
|
||||||
bool overwrite) const -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto copy_to(std::wstring_view new_path,
|
|
||||||
bool overwrite) -> bool {
|
|
||||||
return copy_to(utils::string::to_utf8(new_path), overwrite);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto exists() const -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
get_time(time_type type) const -> std::optional<std::uint64_t>;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto is_directory_item() const -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] auto is_file_item() const -> bool {
|
|
||||||
return not is_directory_item();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto is_symlink() const -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto move_to(std::wstring_view new_path) -> bool {
|
|
||||||
return move_to(utils::string::to_utf8(new_path));
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto remove() -> bool = 0;
|
|
||||||
|
|
||||||
public:
|
|
||||||
[[nodiscard]] virtual operator bool() const = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
i_fs_item() noexcept = default;
|
|
||||||
|
|
||||||
i_fs_item(const i_fs_item &) noexcept = default;
|
|
||||||
|
|
||||||
i_fs_item(i_fs_item &&) noexcept = default;
|
|
||||||
|
|
||||||
auto operator=(i_fs_item &&) noexcept -> i_fs_item & = default;
|
|
||||||
|
|
||||||
auto operator=(const i_fs_item &) noexcept -> i_fs_item & = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct i_file : public i_fs_item {
|
|
||||||
using fs_file_t = std::unique_ptr<i_file>;
|
|
||||||
|
|
||||||
virtual ~i_file() = default;
|
|
||||||
|
|
||||||
virtual void close() = 0;
|
|
||||||
|
|
||||||
virtual void flush() const = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto get_handle() const -> native_handle = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] auto is_directory_item() const -> bool override {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto is_read_only() const -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto read(data_buffer &data, std::uint64_t offset,
|
|
||||||
std::size_t *total_read = nullptr) -> bool {
|
|
||||||
return read(data.data(), data.size(), offset, total_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
|
||||||
std::size_t *total_read = nullptr) -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
read_all(data_buffer &data, std::uint64_t offset,
|
|
||||||
std::size_t *total_read = nullptr) -> bool;
|
|
||||||
|
|
||||||
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto size() const -> std::optional<std::uint64_t> = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); }
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto truncate(std::size_t size) -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
write(const data_buffer &data, std::uint64_t offset,
|
|
||||||
std::size_t *total_written = nullptr) -> bool {
|
|
||||||
return write(data.data(), data.size(), offset, total_written);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
|
|
||||||
std::size_t *total_written = nullptr) -> bool = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
i_file() noexcept = default;
|
|
||||||
|
|
||||||
i_file(const i_file &) noexcept = default;
|
|
||||||
|
|
||||||
i_file(i_file &&) noexcept = default;
|
|
||||||
|
|
||||||
auto operator=(i_file &&) noexcept -> i_file & = default;
|
|
||||||
|
|
||||||
auto operator=(const i_file &) noexcept -> i_file & = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
class file final : public i_file {
|
class file final : public i_file {
|
||||||
public:
|
public:
|
||||||
// [[nodiscard]] static auto
|
// [[nodiscard]] static auto
|
||||||
// attach_file(native_handle handle,
|
// attach_file(native_handle handle,
|
||||||
// bool read_only = false) -> fs_file_t;
|
// bool read_only = false) -> fs_file_t;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] static auto open_file(std::string_view path,
|
[[nodiscard]] static auto open_file(std::string_view path,
|
||||||
bool read_only = false) -> fs_file_t;
|
bool read_only = false) -> fs_file_t;
|
||||||
|
|
||||||
@ -256,6 +126,7 @@ public:
|
|||||||
return open_file(utils::string::to_utf8(path), read_only);
|
return open_file(utils::string::to_utf8(path), read_only);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] static auto
|
[[nodiscard]] static auto
|
||||||
open_or_create_file(std::string_view path,
|
open_or_create_file(std::string_view path,
|
||||||
bool read_only = false) -> fs_file_t;
|
bool read_only = false) -> fs_file_t;
|
||||||
@ -578,53 +449,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct i_directory : public i_fs_item {
|
|
||||||
using fs_directory_t = std::unique_ptr<i_directory>;
|
|
||||||
using fs_file_t = i_file::fs_file_t;
|
|
||||||
|
|
||||||
virtual ~i_directory() = default;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
count(bool recursive = false) const -> std::uint64_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
create_directory(std::string_view path = "") const -> fs_directory_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto create_file(std::string_view file_name,
|
|
||||||
bool read_only) const -> fs_file_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] auto is_directory_item() const -> bool override { return true; }
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
get_directory(std::string_view path) const -> fs_directory_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
get_directories() const -> std::vector<fs_directory_t> = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
get_file(std::string_view path) const -> fs_file_t = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto get_files() const -> std::vector<fs_file_t> = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto get_items() const -> std::vector<fs_item_t> = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto remove_recursively() -> bool = 0;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual auto
|
|
||||||
size(bool recursive = false) const -> std::uint64_t = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
i_directory() noexcept = default;
|
|
||||||
|
|
||||||
i_directory(const i_directory &) noexcept = default;
|
|
||||||
|
|
||||||
i_directory(i_directory &&) noexcept = default;
|
|
||||||
|
|
||||||
auto operator=(i_directory &&) noexcept -> i_directory & = default;
|
|
||||||
|
|
||||||
auto operator=(const i_directory &) noexcept -> i_directory & = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
class directory final : public i_directory {
|
class directory final : public i_directory {
|
||||||
public:
|
public:
|
||||||
using directory_t = std::unique_ptr<directory>;
|
using directory_t = std::unique_ptr<directory>;
|
||||||
@ -936,6 +760,7 @@ public:
|
|||||||
read_json_file(std::string_view path, nlohmann::json &data,
|
read_json_file(std::string_view path, nlohmann::json &data,
|
||||||
std::optional<std::string_view> password = std::nullopt) -> bool;
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto read_json_file(
|
[[nodiscard]] auto read_json_file(
|
||||||
std::wstring_view path, nlohmann::json &data,
|
std::wstring_view path, nlohmann::json &data,
|
||||||
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
||||||
@ -944,16 +769,19 @@ read_json_file(std::string_view path, nlohmann::json &data,
|
|||||||
std::string_view path, const nlohmann::json &data,
|
std::string_view path, const nlohmann::json &data,
|
||||||
std::optional<std::string_view> password = std::nullopt) -> bool;
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto write_json_file(
|
[[nodiscard]] auto write_json_file(
|
||||||
std::wstring_view path, const nlohmann::json &data,
|
std::wstring_view path, const nlohmann::json &data,
|
||||||
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
||||||
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto read_json_file(std::string_view path,
|
[[nodiscard]] auto read_json_file(std::string_view path,
|
||||||
nlohmann::json &data) -> bool;
|
nlohmann::json &data) -> bool;
|
||||||
|
|
||||||
[[nodiscard]] auto read_json_file(std::wstring_view path,
|
[[nodiscard]] auto read_json_file(std::wstring_view path,
|
||||||
nlohmann::json &data) -> bool;
|
nlohmann::json &data) -> bool;
|
||||||
|
|
||||||
|
// INFO: has test
|
||||||
[[nodiscard]] auto write_json_file(std::string_view path,
|
[[nodiscard]] auto write_json_file(std::string_view path,
|
||||||
const nlohmann::json &data) -> bool;
|
const nlohmann::json &data) -> bool;
|
||||||
|
|
||||||
|
79
support/include/utils/types/file/i_directory.hpp
Normal file
79
support/include/utils/types/file/i_directory.hpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
#ifndef FIFTHGRID_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_
|
||||||
|
#define FIFTHGRID_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_
|
||||||
|
|
||||||
|
#include "utils/config.hpp"
|
||||||
|
|
||||||
|
#include "utils/types/file/i_file.hpp"
|
||||||
|
#include "utils/types/file/i_fs_item.hpp"
|
||||||
|
|
||||||
|
namespace fifthgrid::utils::file {
|
||||||
|
struct i_directory : public i_fs_item {
|
||||||
|
using fs_directory_t = std::unique_ptr<i_directory>;
|
||||||
|
using fs_file_t = i_file::fs_file_t;
|
||||||
|
|
||||||
|
virtual ~i_directory() = default;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
count(bool recursive = false) const -> std::uint64_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
create_directory(std::string_view path = "") const -> fs_directory_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto create_file(std::string_view file_name,
|
||||||
|
bool read_only) const -> fs_file_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] auto is_directory_item() const -> bool override { return true; }
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
get_directory(std::string_view path) const -> fs_directory_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
get_directories() const -> std::vector<fs_directory_t> = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
get_file(std::string_view path) const -> fs_file_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto get_files() const -> std::vector<fs_file_t> = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto get_items() const -> std::vector<fs_item_t> = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto remove_recursively() -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
size(bool recursive = false) const -> std::uint64_t = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
i_directory() noexcept = default;
|
||||||
|
|
||||||
|
i_directory(const i_directory &) noexcept = default;
|
||||||
|
|
||||||
|
i_directory(i_directory &&) noexcept = default;
|
||||||
|
|
||||||
|
auto operator=(i_directory &&) noexcept -> i_directory & = default;
|
||||||
|
|
||||||
|
auto operator=(const i_directory &) noexcept -> i_directory & = default;
|
||||||
|
};
|
||||||
|
} // namespace fifthgrid::utils::file
|
||||||
|
|
||||||
|
#endif // FIFTHGRID_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_
|
93
support/include/utils/types/file/i_file.hpp
Normal file
93
support/include/utils/types/file/i_file.hpp
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
#ifndef FIFTHGRID_INCLUDE_TYPES_FILE_I_FILE_HPP_
|
||||||
|
#define FIFTHGRID_INCLUDE_TYPES_FILE_I_FILE_HPP_
|
||||||
|
|
||||||
|
#include "utils/config.hpp"
|
||||||
|
|
||||||
|
#include "utils/types/file/i_fs_item.hpp"
|
||||||
|
|
||||||
|
namespace fifthgrid::utils::file {
|
||||||
|
struct i_file : public i_fs_item {
|
||||||
|
using fs_file_t = std::unique_ptr<i_file>;
|
||||||
|
|
||||||
|
virtual ~i_file() = default;
|
||||||
|
|
||||||
|
virtual void close() = 0;
|
||||||
|
|
||||||
|
virtual void flush() const = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto get_handle() const -> native_handle = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] auto is_directory_item() const -> bool override {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto is_read_only() const -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto read(data_buffer &data, std::uint64_t offset,
|
||||||
|
std::size_t *total_read = nullptr) -> bool {
|
||||||
|
return read(data.data(), data.size(), offset, total_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
||||||
|
std::size_t *total_read = nullptr) -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
read_all(data_buffer &data, std::uint64_t offset,
|
||||||
|
std::size_t *total_read = nullptr) -> bool;
|
||||||
|
|
||||||
|
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto size() const -> std::optional<std::uint64_t> = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); }
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto truncate(std::size_t size) -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
write(const data_buffer &data, std::uint64_t offset,
|
||||||
|
std::size_t *total_written = nullptr) -> bool {
|
||||||
|
return write(data.data(), data.size(), offset, total_written);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
|
||||||
|
std::size_t *total_written = nullptr) -> bool = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
i_file() noexcept = default;
|
||||||
|
|
||||||
|
i_file(const i_file &) noexcept = default;
|
||||||
|
|
||||||
|
i_file(i_file &&) noexcept = default;
|
||||||
|
|
||||||
|
auto operator=(i_file &&) noexcept -> i_file & = default;
|
||||||
|
|
||||||
|
auto operator=(const i_file &) noexcept -> i_file & = default;
|
||||||
|
};
|
||||||
|
} // namespace fifthgrid::utils::file
|
||||||
|
|
||||||
|
#endif // FIFTHGRID_INCLUDE_TYPES_FILE_I_FILE_HPP_
|
111
support/include/utils/types/file/i_fs_item.hpp
Normal file
111
support/include/utils/types/file/i_fs_item.hpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
#ifndef FIFTHGRID_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_
|
||||||
|
#define FIFTHGRID_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_
|
||||||
|
|
||||||
|
#include "utils/config.hpp"
|
||||||
|
|
||||||
|
#include "utils/string.hpp"
|
||||||
|
|
||||||
|
namespace fifthgrid::utils::file {
|
||||||
|
enum class time_type {
|
||||||
|
accessed,
|
||||||
|
created,
|
||||||
|
modified,
|
||||||
|
written,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct file_times final {
|
||||||
|
std::uint64_t accessed{};
|
||||||
|
std::uint64_t created{};
|
||||||
|
std::uint64_t modified{};
|
||||||
|
std::uint64_t written{};
|
||||||
|
|
||||||
|
[[nodiscard]] auto get(time_type type) const -> std::uint64_t {
|
||||||
|
switch (type) {
|
||||||
|
case time_type::accessed:
|
||||||
|
return accessed;
|
||||||
|
case time_type::created:
|
||||||
|
return created;
|
||||||
|
case time_type::modified:
|
||||||
|
return modified;
|
||||||
|
case time_type::written:
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("type_type not supported");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct i_fs_item {
|
||||||
|
using fs_item_t = std::unique_ptr<i_fs_item>;
|
||||||
|
|
||||||
|
virtual ~i_fs_item() = default;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto copy_to(std::string_view to_path,
|
||||||
|
bool overwrite) const -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto copy_to(std::wstring_view new_path,
|
||||||
|
bool overwrite) -> bool {
|
||||||
|
return copy_to(utils::string::to_utf8(new_path), overwrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto exists() const -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto
|
||||||
|
get_time(time_type type) const -> std::optional<std::uint64_t>;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto is_directory_item() const -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] auto is_file_item() const -> bool {
|
||||||
|
return not is_directory_item();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto is_symlink() const -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto move_to(std::wstring_view new_path) -> bool {
|
||||||
|
return move_to(utils::string::to_utf8(new_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual auto remove() -> bool = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
[[nodiscard]] virtual operator bool() const = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
i_fs_item() noexcept = default;
|
||||||
|
|
||||||
|
i_fs_item(const i_fs_item &) noexcept = default;
|
||||||
|
|
||||||
|
i_fs_item(i_fs_item &&) noexcept = default;
|
||||||
|
|
||||||
|
auto operator=(i_fs_item &&) noexcept -> i_fs_item & = default;
|
||||||
|
|
||||||
|
auto operator=(const i_fs_item &) noexcept -> i_fs_item & = default;
|
||||||
|
};
|
||||||
|
} // namespace fifthgrid::utils::file
|
||||||
|
|
||||||
|
#endif // FIFTHGRID_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_
|
@ -83,7 +83,8 @@ auto get_free_drive_space(std::string_view path)
|
|||||||
try {
|
try {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
ULARGE_INTEGER li{};
|
ULARGE_INTEGER li{};
|
||||||
if (not ::GetDiskFreeSpaceEx(path.c_str(), &li, nullptr, nullptr)) {
|
if (not ::GetDiskFreeSpaceEx(std::string{path}.c_str(), &li, nullptr,
|
||||||
|
nullptr)) {
|
||||||
throw std::runtime_error("failed to get free disk space|" +
|
throw std::runtime_error("failed to get free disk space|" +
|
||||||
std::string{path} + '|' +
|
std::string{path} + '|' +
|
||||||
std::to_string(utils::get_last_error_code()));
|
std::to_string(utils::get_last_error_code()));
|
||||||
|
@ -53,6 +53,8 @@ static void decrypt_and_verify(const buffer_t &buffer, std::string_view token,
|
|||||||
}
|
}
|
||||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||||
|
|
||||||
|
auto generate_test_directory() -> utils::file::i_directory &;
|
||||||
|
|
||||||
[[nodiscard]] auto get_test_input_dir() -> std::string;
|
[[nodiscard]] auto get_test_input_dir() -> std::string;
|
||||||
|
|
||||||
[[nodiscard]] auto get_test_output_dir() -> std::string;
|
[[nodiscard]] auto get_test_output_dir() -> std::string;
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
namespace {
|
namespace {
|
||||||
static std::recursive_mutex file_mtx{};
|
static std::recursive_mutex file_mtx{};
|
||||||
|
|
||||||
static std::vector<std::unique_ptr<repertory::utils::file::i_file>>
|
static std::vector<std::unique_ptr<repertory::utils::file::i_fs_item>>
|
||||||
generated_files{};
|
generated_files{};
|
||||||
|
|
||||||
static void delete_generated_files() {
|
static void delete_generated_files() {
|
||||||
@ -32,8 +32,7 @@ static void delete_generated_files() {
|
|||||||
std::optional<std::string> parent_path;
|
std::optional<std::string> parent_path;
|
||||||
for (auto &&path : generated_files) {
|
for (auto &&path : generated_files) {
|
||||||
if (parent_path->empty()) {
|
if (parent_path->empty()) {
|
||||||
parent_path =
|
parent_path = repertory::utils::path::get_parent_path(path->get_path());
|
||||||
repertory::utils::path::get_parent_path(path->get_path());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[maybe_unused]] auto removed = path->remove();
|
[[maybe_unused]] auto removed = path->remove();
|
||||||
@ -73,10 +72,25 @@ auto create_random_file(std::size_t size) -> utils::file::i_file & {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generated_files.emplace_back(std::move(file));
|
generated_files.emplace_back(std::move(file));
|
||||||
return *generated_files.back();
|
return *dynamic_cast<utils::file::i_file *>(generated_files.back().get());
|
||||||
}
|
}
|
||||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
||||||
|
|
||||||
|
auto generate_test_directory() -> utils::file::i_directory & {
|
||||||
|
recur_mutex_lock lock{file_mtx};
|
||||||
|
|
||||||
|
auto path = utils::path::combine(
|
||||||
|
get_test_output_dir(),
|
||||||
|
{
|
||||||
|
std::string{"test_dir"} + std::to_string(generated_files.size()),
|
||||||
|
});
|
||||||
|
generated_files.emplace_back(std::unique_ptr<utils::file::i_fs_item>(
|
||||||
|
new utils::file::directory{path}));
|
||||||
|
|
||||||
|
return *dynamic_cast<utils::file::i_directory *>(
|
||||||
|
generated_files.back().get());
|
||||||
|
}
|
||||||
|
|
||||||
auto generate_test_file_name(std::string_view file_name_no_extension)
|
auto generate_test_file_name(std::string_view file_name_no_extension)
|
||||||
-> std::string {
|
-> std::string {
|
||||||
recur_mutex_lock lock{file_mtx};
|
recur_mutex_lock lock{file_mtx};
|
||||||
|
@ -262,4 +262,42 @@ TEST(utils_file, smb_parent_is_not_same) {
|
|||||||
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
|
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
|
||||||
}
|
}
|
||||||
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
||||||
|
|
||||||
|
TEST(util_file, directory_exists_in_path) {
|
||||||
|
auto &test_dir = test::generate_test_directory();
|
||||||
|
auto sub_dir = test_dir.create_directory("moose");
|
||||||
|
EXPECT_TRUE(sub_dir != nullptr);
|
||||||
|
if (sub_dir) {
|
||||||
|
EXPECT_TRUE(
|
||||||
|
utils::file::directory_exists_in_path(test_dir.get_path(), "moose"));
|
||||||
|
|
||||||
|
EXPECT_FALSE(
|
||||||
|
utils::file::file_exists_in_path(test_dir.get_path(), "moose"));
|
||||||
|
|
||||||
|
EXPECT_TRUE(utils::file::directory_exists_in_path(
|
||||||
|
utils::string::from_utf8(test_dir.get_path()), L"moose"));
|
||||||
|
|
||||||
|
EXPECT_FALSE(utils::file::file_exists_in_path(
|
||||||
|
utils::string::from_utf8(test_dir.get_path()), L"moose"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(util_file, file_exists_in_path) {
|
||||||
|
auto &test_dir = test::generate_test_directory();
|
||||||
|
auto sub_file = test_dir.create_file("moose.txt", false);
|
||||||
|
EXPECT_TRUE(sub_file != nullptr);
|
||||||
|
if (sub_file) {
|
||||||
|
EXPECT_TRUE(
|
||||||
|
utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt"));
|
||||||
|
|
||||||
|
EXPECT_FALSE(utils::file::directory_exists_in_path(test_dir.get_path(),
|
||||||
|
"moose.txt"));
|
||||||
|
|
||||||
|
EXPECT_TRUE(utils::file::file_exists_in_path(
|
||||||
|
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
|
||||||
|
|
||||||
|
EXPECT_FALSE(utils::file::directory_exists_in_path(
|
||||||
|
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
|
||||||
|
}
|
||||||
|
}
|
||||||
} // namespace repertory
|
} // namespace repertory
|
||||||
|
Reference in New Issue
Block a user