updated build system
This commit is contained in:
@ -27,6 +27,21 @@
|
||||
#include "utils/path.hpp"
|
||||
|
||||
namespace repertory::utils::file {
|
||||
[[nodiscard]] inline auto
|
||||
directory_exists_in_path(std::string_view path,
|
||||
std::string_view sub_directory) -> bool;
|
||||
|
||||
[[nodiscard]] inline auto
|
||||
directory_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view sub_directory) -> bool;
|
||||
|
||||
[[nodiscard]] inline auto
|
||||
file_exists_in_path(std::string_view path, std::string_view file_name) -> bool;
|
||||
|
||||
[[nodiscard]] inline auto
|
||||
file_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view file_name) -> bool;
|
||||
|
||||
#if defined(PROJECT_ENABLE_LIBDSM)
|
||||
[[nodiscard]] auto
|
||||
smb_create_and_validate_relative_path(std::string_view smb_path,
|
||||
@ -61,6 +76,7 @@ smb_get_parent_path(std::string_view smb_path) -> std::string;
|
||||
|
||||
struct i_fs_item {
|
||||
using fs_item_t = std::unique_ptr<i_fs_item>;
|
||||
|
||||
enum class time_types {
|
||||
access,
|
||||
creation,
|
||||
@ -74,8 +90,7 @@ struct i_fs_item {
|
||||
|
||||
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
get_time(time_types type) const -> std::uint64_t = 0;
|
||||
[[nodiscard]] virtual auto get_time(time_types type) const -> std::uint64_t;
|
||||
|
||||
[[nodiscard]] virtual auto is_directory_item() const -> bool = 0;
|
||||
|
||||
@ -136,7 +151,7 @@ struct i_file : public i_fs_item {
|
||||
|
||||
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
|
||||
|
||||
[[nodiscard]] virtual auto size() const -> std::uint64_t = 0;
|
||||
[[nodiscard]] virtual auto size() const -> std::optional<std::uint64_t> = 0;
|
||||
|
||||
[[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); }
|
||||
|
||||
@ -268,7 +283,7 @@ public:
|
||||
return read_buffer_size;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto size() const -> std::uint64_t override;
|
||||
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
|
||||
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
||||
|
||||
@ -335,7 +350,9 @@ public:
|
||||
return file_->get_read_buffer_size();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override;
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override {
|
||||
return file_->get_time(type);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto is_read_only() const -> bool override {
|
||||
return file_->is_read_only();
|
||||
@ -353,7 +370,7 @@ public:
|
||||
return file_->set_read_buffer_size(size);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto size() const -> std::uint64_t override;
|
||||
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
|
||||
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
||||
|
||||
@ -445,7 +462,9 @@ public:
|
||||
return file_->get_read_buffer_size();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override;
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override {
|
||||
return file_->get_time(type);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto is_read_only() const -> bool override {
|
||||
return file_->is_read_only();
|
||||
@ -463,7 +482,7 @@ public:
|
||||
return file_->set_read_buffer_size(size);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto size() const -> std::uint64_t override;
|
||||
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
|
||||
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
||||
|
||||
@ -497,7 +516,7 @@ struct i_directory : public i_fs_item {
|
||||
count(bool recursive = false) const -> std::uint64_t = 0;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
create_directory(std::string_view path) const -> fs_directory_t = 0;
|
||||
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;
|
||||
@ -559,7 +578,7 @@ public:
|
||||
count(bool recursive = false) const -> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto
|
||||
create_directory(std::string_view path) const -> fs_directory_t override;
|
||||
create_directory(std::string_view path = "") const -> fs_directory_t override;
|
||||
|
||||
[[nodiscard]] auto create_file(std::string_view file_name,
|
||||
bool read_only) const -> fs_file_t override;
|
||||
@ -581,8 +600,6 @@ public:
|
||||
|
||||
[[nodiscard]] auto get_path() const -> std::string override { return path_; }
|
||||
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
|
||||
|
||||
[[nodiscard]] auto remove() -> bool override;
|
||||
@ -609,52 +626,35 @@ class smb_file final : public i_file {
|
||||
public:
|
||||
smb_file() = default;
|
||||
|
||||
smb_file(std::uint64_t access_time, std::uint64_t creation_time,
|
||||
std::optional<smb_fd> fd, std::uint64_t modified_time,
|
||||
std::string path, smb_session_t session, std::string_view share_name,
|
||||
smb_tid tid, std::uint64_t size, std::uint64_t write_time)
|
||||
: access_time_(access_time),
|
||||
creation_time_(creation_time),
|
||||
fd_(std::move(fd)),
|
||||
modified_time_(modified_time),
|
||||
smb_file(std::optional<smb_fd> fd, std::string path, smb_session_t session,
|
||||
std::string_view share_name, smb_tid tid)
|
||||
: fd_(std::move(fd)),
|
||||
path_(std::move(path)),
|
||||
session_(std::move(session)),
|
||||
share_name_(share_name),
|
||||
size_(size),
|
||||
tid_(tid),
|
||||
write_time_(write_time) {}
|
||||
tid_(tid) {}
|
||||
|
||||
smb_file(const smb_file &) = delete;
|
||||
|
||||
smb_file(smb_file &&f) noexcept
|
||||
: access_time_(f.access_time_),
|
||||
creation_time_(f.creation_time_),
|
||||
fd_(std::move(f.fd_)),
|
||||
modified_time_(f.modified_time_),
|
||||
: fd_(std::move(f.fd_)),
|
||||
path_(std::move(f.path_)),
|
||||
read_buffer_size(f.get_read_buffer_size()),
|
||||
read_only_(f.read_only_),
|
||||
session_(std::move(f.session_)),
|
||||
share_name_(std::move(f.share_name_)),
|
||||
size_(f.size_),
|
||||
tid_(f.tid_),
|
||||
write_time_(f.write_time_) {}
|
||||
tid_(f.tid_) {}
|
||||
|
||||
~smb_file() override { close(); }
|
||||
|
||||
private:
|
||||
std::uint64_t access_time_;
|
||||
std::uint64_t creation_time_;
|
||||
std::optional<smb_fd> fd_;
|
||||
std::uint64_t modified_time_;
|
||||
std::string path_;
|
||||
std::atomic_uint32_t read_buffer_size{65536U};
|
||||
bool read_only_;
|
||||
smb_session_t session_;
|
||||
std::string share_name_;
|
||||
std::uint64_t size_;
|
||||
smb_tid tid_;
|
||||
std::uint64_t write_time_;
|
||||
|
||||
public:
|
||||
void close() override;
|
||||
@ -673,20 +673,12 @@ public:
|
||||
return read_buffer_size;
|
||||
}
|
||||
|
||||
[[nodiscard]] static auto get_time(smb_session *session, smb_tid tid,
|
||||
std::string path,
|
||||
time_types type) -> std::uint64_t;
|
||||
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override {
|
||||
switch (type) {
|
||||
case time_types::access:
|
||||
return access_time_;
|
||||
|
||||
case time_types::creation:
|
||||
return creation_time_;
|
||||
|
||||
case time_types::modified:
|
||||
return modified_time_;
|
||||
|
||||
case time_types::write:
|
||||
return write_time_;
|
||||
}
|
||||
return get_time(session_.get(), tid_, path_, type);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_unc_path() const -> std::string {
|
||||
@ -722,7 +714,7 @@ public:
|
||||
return read_buffer_size;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto size() const -> std::uint64_t override { return size_; }
|
||||
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> override;
|
||||
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
||||
|
||||
@ -735,18 +727,13 @@ public:
|
||||
|
||||
auto operator=(smb_file &&move_file) noexcept -> smb_file & {
|
||||
if (this != &move_file) {
|
||||
access_time_ = move_file.access_time_;
|
||||
creation_time_ = move_file.creation_time_;
|
||||
fd_ = std::move(move_file.fd_);
|
||||
modified_time_ = move_file.modified_time_;
|
||||
path_ = std::move(move_file.path_);
|
||||
read_buffer_size = move_file.get_read_buffer_size();
|
||||
read_only_ = move_file.read_only_;
|
||||
session_ = std::move(move_file.session_);
|
||||
share_name_ = std::move(move_file.share_name_);
|
||||
size_ = move_file.size_;
|
||||
tid_ = move_file.tid_;
|
||||
write_time_ = move_file.write_time_;
|
||||
}
|
||||
|
||||
return *this;
|
||||
@ -796,7 +783,7 @@ public:
|
||||
count(bool recursive = false) const -> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto
|
||||
create_directory(std::string_view path) const -> fs_directory_t override;
|
||||
create_directory(std::string_view path = "") const -> fs_directory_t override;
|
||||
|
||||
[[nodiscard]] auto create_file(std::string_view file_name,
|
||||
bool read_only) const -> fs_file_t override;
|
||||
@ -818,7 +805,9 @@ public:
|
||||
|
||||
[[nodiscard]] auto get_path() const -> std::string override { return path_; }
|
||||
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override;
|
||||
[[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override {
|
||||
return smb_file::get_time(session_.get(), tid_, path_, type);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_unc_path() const -> std::string {
|
||||
return smb_get_unc_path(path_);
|
||||
@ -853,44 +842,6 @@ public:
|
||||
};
|
||||
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
||||
|
||||
[[nodiscard]] auto create_directories(std::string_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto create_directories(std::wstring_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto
|
||||
directory_exists_in_path(std::string_view path,
|
||||
std::string_view sub_directory) -> bool;
|
||||
|
||||
[[nodiscard]] auto
|
||||
directory_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view sub_directory) -> bool;
|
||||
|
||||
[[nodiscard]] auto get_file_size(std::string_view path,
|
||||
std::uint64_t &file_size) -> bool;
|
||||
|
||||
[[nodiscard]] auto get_file_size(std::wstring_view path,
|
||||
std::uint64_t &file_size) -> bool;
|
||||
|
||||
[[nodiscard]] auto file_exists_in_path(std::string_view path,
|
||||
std::string_view file_name) -> bool;
|
||||
|
||||
[[nodiscard]] auto file_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view file_name) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_directory(std::string_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_file(std::string_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto remove_directory(std::string_view path,
|
||||
bool recursive = false) -> bool;
|
||||
|
||||
[[nodiscard]] auto remove_directory(std::wstring_view path,
|
||||
bool recursive = false) -> bool;
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
[[nodiscard]] auto
|
||||
@ -924,18 +875,38 @@ read_json_file(std::string_view path, nlohmann::json &data,
|
||||
#endif // defined(PROJECT_ENABLE_JSON)
|
||||
|
||||
template <typename string_t>
|
||||
inline auto directory_exists_in_path_t(
|
||||
[[nodiscard]] inline auto directory_exists_in_path_t(
|
||||
std::basic_string_view<typename string_t::value_type> path,
|
||||
std::basic_string_view<typename string_t::value_type> sub_directory)
|
||||
-> bool {
|
||||
return is_directory(utils::path::combine(path, {sub_directory}));
|
||||
return directory(utils::path::combine(path, {sub_directory})).exists();
|
||||
}
|
||||
|
||||
template <typename string_t>
|
||||
inline auto file_exists_in_path_t(
|
||||
[[nodiscard]] inline auto file_exists_in_path_t(
|
||||
std::basic_string_view<typename string_t::value_type> path,
|
||||
std::basic_string_view<typename string_t::value_type> file_name) -> bool {
|
||||
return is_file(utils::path::combine(path, {file_name}));
|
||||
return file(utils::path::combine(path, {file_name})).exists();
|
||||
}
|
||||
|
||||
inline auto directory_exists_in_path(std::string_view path,
|
||||
std::string_view sub_directory) -> bool {
|
||||
return directory_exists_in_path_t<std::string>(path, sub_directory);
|
||||
}
|
||||
|
||||
inline auto directory_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view sub_directory) -> bool {
|
||||
return directory_exists_in_path_t<std::wstring>(path, sub_directory);
|
||||
}
|
||||
|
||||
inline auto file_exists_in_path(std::string_view path,
|
||||
std::string_view file_name) -> bool {
|
||||
return file_exists_in_path_t<std::string>(path, file_name);
|
||||
}
|
||||
|
||||
inline auto file_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view file_name) -> bool {
|
||||
return file_exists_in_path_t<std::wstring>(path, file_name);
|
||||
}
|
||||
} // namespace repertory::utils::file
|
||||
|
||||
|
@ -127,10 +127,11 @@ protected:
|
||||
|
||||
reader_.set_read_position(reinterpret_cast<std::uintptr_t>(gptr()));
|
||||
|
||||
const auto res = encrypting_reader::reader_function(
|
||||
auto res = encrypting_reader::reader_function(
|
||||
ptr, 1U, static_cast<std::size_t>(count), &reader_);
|
||||
if ((res == reader_.get_error_return()) ||
|
||||
(reader_.get_stop_requested() && (res == CURL_READFUNC_ABORT))) {
|
||||
(reader_.get_stop_requested() &&
|
||||
(res == static_cast<std::size_t>(CURL_READFUNC_ABORT)))) {
|
||||
return traits_type::eof();
|
||||
}
|
||||
|
||||
@ -200,7 +201,13 @@ encrypting_reader::encrypting_reader(
|
||||
encrypted_file_path_ += '/' + encrypted_file_name_;
|
||||
}
|
||||
|
||||
auto file_size = source_file_->size();
|
||||
auto opt_size = source_file_->size();
|
||||
if (not opt_size.has_value()) {
|
||||
throw std::runtime_error("failed to get file size|" +
|
||||
source_file_->get_path());
|
||||
}
|
||||
|
||||
auto file_size{opt_size.value()};
|
||||
|
||||
const auto total_chunks = utils::divide_with_ceiling(
|
||||
file_size, static_cast<std::uint64_t>(data_chunk_size_));
|
||||
@ -234,7 +241,13 @@ encrypting_reader::encrypting_reader(std::string_view encrypted_file_path,
|
||||
encrypted_file_path_ = encrypted_file_path;
|
||||
encrypted_file_name_ = utils::path::strip_to_file_name(encrypted_file_path_);
|
||||
|
||||
auto file_size = source_file_->size();
|
||||
auto opt_size = source_file_->size();
|
||||
if (not opt_size.has_value()) {
|
||||
throw std::runtime_error("failed to get file size|" +
|
||||
source_file_->get_path());
|
||||
}
|
||||
|
||||
auto file_size{opt_size.value()};
|
||||
|
||||
const auto total_chunks = utils::divide_with_ceiling(
|
||||
file_size, static_cast<std::uint64_t>(data_chunk_size_));
|
||||
@ -270,7 +283,14 @@ encrypting_reader::encrypting_reader(
|
||||
encrypted_file_path_ = encrypted_file_path;
|
||||
encrypted_file_name_ = utils::path::strip_to_file_name(encrypted_file_path_);
|
||||
|
||||
auto file_size = source_file_->size();
|
||||
auto opt_size = source_file_->size();
|
||||
if (not opt_size.has_value()) {
|
||||
throw std::runtime_error("get file size failed|src|" +
|
||||
source_file_->get_path() + '|' +
|
||||
std::to_string(utils::get_last_error_code()));
|
||||
}
|
||||
|
||||
auto file_size{opt_size.value()};
|
||||
|
||||
const auto total_chunks = utils::divide_with_ceiling(
|
||||
file_size, static_cast<std::uint64_t>(data_chunk_size_));
|
||||
@ -313,13 +333,15 @@ auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size)
|
||||
|
||||
auto encrypting_reader::calculate_encrypted_size(std::string_view source_path)
|
||||
-> std::uint64_t {
|
||||
std::uint64_t file_size{};
|
||||
if (not utils::file::get_file_size(source_path, file_size)) {
|
||||
auto opt_size = utils::file::file{source_path}.size();
|
||||
if (not opt_size.has_value()) {
|
||||
throw std::runtime_error("get file size failed|src|" +
|
||||
std::string{source_path} + '|' +
|
||||
std::to_string(utils::get_last_error_code()));
|
||||
}
|
||||
|
||||
auto file_size{opt_size.value()};
|
||||
|
||||
const auto total_chunks = utils::divide_with_ceiling(
|
||||
file_size, static_cast<std::uint64_t>(data_chunk_size_));
|
||||
return file_size + (total_chunks * encryption_header_size);
|
||||
@ -387,7 +409,7 @@ auto encrypting_reader::reader_function(char *buffer, size_t size,
|
||||
}
|
||||
}
|
||||
|
||||
return stop_requested_ ? CURL_READFUNC_ABORT
|
||||
return stop_requested_ ? static_cast<std::size_t>(CURL_READFUNC_ABORT)
|
||||
: ret ? total_read
|
||||
: error_return_;
|
||||
}
|
||||
|
@ -26,56 +26,6 @@
|
||||
#include "utils/path.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace {
|
||||
[[nodiscard]] auto remove_directory_recursively(std::string_view path) -> bool {
|
||||
#if defined(_WIN32)
|
||||
WIN32_FIND_DATAA fd{};
|
||||
auto search = repertory::utils::path::combine(path, {"*.*"});
|
||||
auto find = ::FindFirstFileA(search.c_str(), &fd);
|
||||
if (find != INVALID_HANDLE_VALUE) {
|
||||
auto res{true};
|
||||
do {
|
||||
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
if ((std::string(fd.cFileName) != ".") &&
|
||||
(std::string(fd.cFileName) != "..")) {
|
||||
res = remove_directory_recursively(
|
||||
repertory::utils::path::combine(path, {fd.cFileName}));
|
||||
}
|
||||
} else {
|
||||
res = repertory::utils::file::file(
|
||||
repertory::utils::path::combine(path, {fd.cFileName}))
|
||||
.remove();
|
||||
}
|
||||
} while (res && (::FindNextFileA(find, &fd) != 0));
|
||||
|
||||
::FindClose(find);
|
||||
}
|
||||
#else
|
||||
auto *root = opendir(std::string{path}.c_str());
|
||||
if (root != nullptr) {
|
||||
auto res{true};
|
||||
struct dirent *de{};
|
||||
while (res && (de = readdir(root))) {
|
||||
if (de->d_type == DT_DIR) {
|
||||
if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
|
||||
res = remove_directory_recursively(
|
||||
repertory::utils::path::combine(path, {de->d_name}));
|
||||
}
|
||||
} else {
|
||||
res = repertory::utils::file::file(
|
||||
repertory::utils::path::combine(path, {de->d_name}))
|
||||
.remove();
|
||||
}
|
||||
}
|
||||
|
||||
closedir(root);
|
||||
}
|
||||
#endif
|
||||
|
||||
return repertory::utils::file::remove_directory(path, false);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace repertory::utils::file {
|
||||
auto i_file::read_all(data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool {
|
||||
@ -107,131 +57,64 @@ auto i_file::read_all(data_buffer &data, std::uint64_t offset,
|
||||
return false;
|
||||
}
|
||||
|
||||
auto create_directories(std::string_view path) -> bool {
|
||||
if (is_directory(path)) {
|
||||
return true;
|
||||
}
|
||||
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)
|
||||
return (::SHCreateDirectory(
|
||||
nullptr,
|
||||
utils::string::from_utf8(utils::path::absolute(path)).c_str()) ==
|
||||
ERROR_SUCCESS);
|
||||
#else // !defined(_WIN32)
|
||||
auto ret{true};
|
||||
auto paths = utils::string::split(utils::path::absolute(path),
|
||||
utils::path::directory_seperator, false);
|
||||
struct _stat64 st {};
|
||||
_stat64(get_path().c_str(), &st);
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
stat(get_path().c_str(), &st);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
std::string current_path;
|
||||
for (std::size_t idx = 0U; ret && (idx < paths.size()); idx++) {
|
||||
if (paths.at(idx).empty()) {
|
||||
current_path = utils::path::directory_seperator;
|
||||
continue;
|
||||
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)
|
||||
}
|
||||
|
||||
current_path = utils::path::combine(current_path, {paths.at(idx)});
|
||||
auto status = mkdir(current_path.c_str(), S_IRWXU);
|
||||
ret = ((status == 0) || (errno == EEXIST));
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
auto create_directories(std::wstring_view path) -> bool {
|
||||
return create_directories(utils::string::to_utf8(path));
|
||||
}
|
||||
|
||||
auto directory_exists_in_path(std::string_view path,
|
||||
std::string_view sub_directory) -> bool {
|
||||
return directory_exists_in_path_t<std::string>(path, sub_directory);
|
||||
}
|
||||
|
||||
auto directory_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view sub_directory) -> bool {
|
||||
return directory_exists_in_path_t<std::wstring>(path, sub_directory);
|
||||
}
|
||||
|
||||
auto file_exists_in_path(std::string_view path,
|
||||
std::string_view file_name) -> bool {
|
||||
return file_exists_in_path_t<std::string>(path, file_name);
|
||||
}
|
||||
|
||||
auto file_exists_in_path(std::wstring_view path,
|
||||
std::wstring_view file_name) -> bool {
|
||||
return file_exists_in_path_t<std::wstring>(path, file_name);
|
||||
}
|
||||
|
||||
auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
file_size = 0U;
|
||||
|
||||
#if defined(_WIN32)
|
||||
struct _stat64 st {};
|
||||
auto res = _stat64(std::string{path}.c_str(), &st);
|
||||
if (res != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
file_size = static_cast<std::uint64_t>(st.st_size);
|
||||
return true;
|
||||
#else // !defined(_WIN32)
|
||||
std::error_code ec{};
|
||||
file_size = std::filesystem::file_size(abs_path, ec);
|
||||
return (ec.value() == 0);
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool {
|
||||
return get_file_size(utils::string::to_utf8(path), file_size);
|
||||
}
|
||||
|
||||
auto is_directory(std::string_view path) -> bool {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
|
||||
#if defined(_WIN32)
|
||||
return ::PathIsDirectoryA(abs_path.c_str()) != 0;
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
return (stat(abs_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto is_directory(std::wstring_view path) -> bool {
|
||||
return is_directory(utils::string::to_utf8(path));
|
||||
}
|
||||
|
||||
auto is_file(std::string_view path) -> bool {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
|
||||
#if defined(_WIN32)
|
||||
return (::PathFileExistsA(abs_path.c_str()) &&
|
||||
not ::PathIsDirectoryA(abs_path.c_str()));
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
return (stat(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode));
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto is_file(std::wstring_view path) -> bool {
|
||||
return is_file(utils::string::to_utf8(path));
|
||||
}
|
||||
|
||||
auto remove_directory(std::string_view path, bool recursive) -> bool {
|
||||
auto abs_path = utils::path::absolute(path);
|
||||
if (recursive) {
|
||||
return remove_directory_recursively(abs_path);
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
return (not is_directory(abs_path) || ::RemoveDirectoryA(abs_path.c_str()));
|
||||
#else // !defined(_WIN32)
|
||||
return not is_directory(abs_path) || (rmdir(abs_path.c_str()) == 0);
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
auto remove_directory(std::wstring_view path, bool recursive) -> bool {
|
||||
return remove_directory(utils::string::to_utf8(path), recursive);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
|
@ -21,35 +21,384 @@
|
||||
*/
|
||||
#include "utils/file.hpp"
|
||||
|
||||
#include "utils/error.hpp"
|
||||
#include "utils/unix.hpp"
|
||||
#include "utils/windows.hpp"
|
||||
|
||||
namespace {
|
||||
auto traverse_directory(
|
||||
std::string_view path,
|
||||
std::function<bool(repertory::utils::file::directory)> directory_action,
|
||||
std::function<bool(repertory::utils::file::file)> file_action) -> bool {
|
||||
auto res{true};
|
||||
|
||||
#if defined(_WIN32)
|
||||
WIN32_FIND_DATAA fd{};
|
||||
auto search = repertory::utils::path::combine(path, {"*.*"});
|
||||
auto find = ::FindFirstFileA(search.c_str(), &fd);
|
||||
if (find == INVALID_HANDLE_VALUE) {
|
||||
throw std::runtime_error(
|
||||
"failed to open directory|" + std::string{path} + '|' +
|
||||
std::to_string(repertory::utils::get_last_error_code()));
|
||||
}
|
||||
|
||||
do {
|
||||
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
if ((std::string_view(fd.cFileName) != ".") &&
|
||||
(std::string_view(fd.cFileName) != "..")) {
|
||||
res = directory_action(repertory::utils::file::directory{
|
||||
repertory::utils::path::combine(path, {fd.cFileName})});
|
||||
}
|
||||
} else {
|
||||
res = file_action(repertory::utils::file::file(
|
||||
repertory::utils::path::combine(path, {fd.cFileName})));
|
||||
}
|
||||
} while (res && (::FindNextFileA(find, &fd) != 0));
|
||||
|
||||
::FindClose(find);
|
||||
#else // !defined(_WIN32)
|
||||
auto *root = opendir(path.c_str());
|
||||
if (root == nullptr) {
|
||||
throw std::runtime_error("failed to open directory|" + std::string{path} +
|
||||
'|' +
|
||||
std::to_string(utils::get_last_error_code()));
|
||||
}
|
||||
|
||||
struct dirent *de{};
|
||||
while (res && (de = readdir(root))) {
|
||||
if (de->d_type == DT_DIR) {
|
||||
if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
|
||||
res = directory_action(repertory::utils::file::directory(
|
||||
repertory::utils::path::combine(path, {de->d_name})));
|
||||
}
|
||||
} else {
|
||||
res = file_action(repertory::utils::file::file(
|
||||
repertory::utils::path::combine(path, {de->d_name})));
|
||||
}
|
||||
}
|
||||
|
||||
closedir(root);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return res;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace repertory::utils::file {
|
||||
auto directory::count(bool recursive) const -> std::uint64_t { return 0U; }
|
||||
auto directory::count(bool recursive) const -> std::uint64_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::uint64_t ret{0U};
|
||||
|
||||
traverse_directory(
|
||||
path_,
|
||||
[&ret, &recursive](auto dir_item) -> bool {
|
||||
if (recursive) {
|
||||
ret += dir_item.count(true);
|
||||
}
|
||||
|
||||
++ret;
|
||||
return true;
|
||||
},
|
||||
[&ret](auto /* file_item */) -> bool {
|
||||
++ret;
|
||||
return true;
|
||||
});
|
||||
|
||||
return ret;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto directory::create_directory(std::string_view path) const
|
||||
-> fs_directory_t {}
|
||||
-> fs_directory_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
auto directory::exists() const -> bool { return false; }
|
||||
try {
|
||||
auto abs_path = utils::path::combine(path_, {path});
|
||||
if (directory{abs_path}.exists()) {
|
||||
return std::make_unique<directory>(abs_path);
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto res = ::SHCreateDirectory(nullptr,
|
||||
utils::string::from_utf8(abs_path).c_str());
|
||||
if (res != ERROR_SUCCESS) {
|
||||
throw std::runtime_error("failed to create directory|" +
|
||||
std::string{abs_path} + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
#else // !defined(_WIN32)
|
||||
auto ret{true};
|
||||
auto paths =
|
||||
utils::string::split(abs_path, utils::path::directory_seperator, false);
|
||||
|
||||
std::string current_path;
|
||||
for (std::size_t idx = 0U; ret && (idx < paths.size()); ++idx) {
|
||||
if (paths.at(idx).empty()) {
|
||||
current_path = utils::path::directory_seperator;
|
||||
continue;
|
||||
}
|
||||
|
||||
current_path = utils::path::combine(current_path, {paths.at(idx)});
|
||||
auto status = mkdir(current_path.c_str(), S_IRWXU);
|
||||
ret = ((status == 0) || (errno == EEXIST));
|
||||
}
|
||||
#endif
|
||||
|
||||
return std::make_unique<directory>(abs_path);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto directory::exists() const -> bool {
|
||||
#if defined(_WIN32)
|
||||
return ::PathIsDirectoryA(path_.c_str()) != 0;
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
return (stat(path_.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto directory::get_directory(std::string_view path) const -> fs_directory_t {
|
||||
return {};
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
auto dir_path = utils::path::combine(path_, {path});
|
||||
return fs_directory_t{
|
||||
directory{dir_path}.exists() ? new directory{dir_path} : nullptr,
|
||||
};
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto directory::get_directories() const -> std::vector<fs_directory_t> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::vector<fs_directory_t> ret{};
|
||||
|
||||
traverse_directory(
|
||||
path_,
|
||||
[&ret](auto dir_item) -> bool {
|
||||
ret.emplace_back(fs_directory_t{
|
||||
new directory(dir_item.get_path()),
|
||||
});
|
||||
|
||||
return true;
|
||||
},
|
||||
[](auto /* file_item */) -> bool { return true; });
|
||||
|
||||
return ret;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
auto directory::get_time(time_types type) const -> std::uint64_t {}
|
||||
auto directory::create_file(std::string_view file_name,
|
||||
bool read_only) const -> fs_file_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
auto directory::get_file(std::string_view path) const -> fs_file_t {}
|
||||
try {
|
||||
auto file_path = utils::path::combine(path_, {file_name});
|
||||
return file::open_or_create_file(file_path, read_only);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
auto directory::get_files() const -> std::vector<fs_file_t> { return {}; }
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto directory::get_items() const -> std::vector<fs_item_t> { return {}; }
|
||||
auto directory::get_file(std::string_view path) const -> fs_file_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
auto file_path = utils::path::combine(path_, {path});
|
||||
return fs_file_t{
|
||||
file{file_path}.exists() ? new file(file_path) : nullptr,
|
||||
};
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto directory::get_files() const -> std::vector<fs_file_t> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::vector<fs_file_t> ret{};
|
||||
|
||||
traverse_directory(
|
||||
path_, [](auto /* dir_item */) -> bool { return true; },
|
||||
[&ret](auto file_item) -> bool {
|
||||
ret.emplace_back(fs_file_t{
|
||||
new file(file_item.get_path()),
|
||||
});
|
||||
return true;
|
||||
});
|
||||
|
||||
return ret;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
auto directory::get_items() const -> std::vector<fs_item_t> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::vector<fs_item_t> ret{};
|
||||
|
||||
traverse_directory(
|
||||
path_,
|
||||
[&ret](auto dir_item) -> bool {
|
||||
ret.emplace_back(fs_item_t{
|
||||
new directory(dir_item.get_path()),
|
||||
});
|
||||
return true;
|
||||
},
|
||||
[&ret](auto file_item) -> bool {
|
||||
ret.emplace_back(fs_item_t{
|
||||
new file(file_item.get_path()),
|
||||
});
|
||||
return true;
|
||||
});
|
||||
|
||||
return ret;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
auto directory::move_to(std::string_view new_path) -> bool { return false; }
|
||||
|
||||
auto directory::remove() -> bool { return false; }
|
||||
auto directory::remove() -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
auto directory::remove_recursively() -> bool { return false; }
|
||||
try {
|
||||
#if defined(_WIN32)
|
||||
return (not exists() || ::RemoveDirectoryA(path_.c_str()));
|
||||
#else // !defined(_WIN32)
|
||||
return not exists() || (rmdir(path_.c_str()) == 0);
|
||||
#endif // defined(_WIN32)
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
auto directory::size(bool recursive) const -> std::uint64_t { return 0U; }
|
||||
return false;
|
||||
}
|
||||
|
||||
auto directory::remove_recursively() -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not traverse_directory(
|
||||
path_,
|
||||
[](auto dir_item) -> bool { return dir_item.remove_recursively(); },
|
||||
[](auto file_item) -> bool { return file_item.remove(); })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return remove();
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto directory::size(bool recursive) const -> std::uint64_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
std::uint64_t ret{0U};
|
||||
|
||||
traverse_directory(
|
||||
path_,
|
||||
[&ret, &recursive](auto dir_item) -> bool {
|
||||
if (recursive) {
|
||||
ret += dir_item.size(true);
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
[&ret](auto file_item) -> bool {
|
||||
auto cur_size = file_item.size();
|
||||
if (cur_size.has_value()) {
|
||||
ret += cur_size.value();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
return ret;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
} // namespace repertory::utils::file
|
||||
|
@ -32,8 +32,6 @@ void enc_file::close() {}
|
||||
|
||||
void enc_file::flush() const {}
|
||||
|
||||
auto enc_file::get_time(time_types type) const -> std::uint64_t {}
|
||||
|
||||
auto enc_file::move_to(std::string_view path) -> bool {}
|
||||
|
||||
auto enc_file::read(unsigned char *data, std::size_t to_read,
|
||||
@ -46,7 +44,7 @@ auto enc_file::truncate(std::size_t size) -> bool {}
|
||||
auto enc_file::write(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset, std::size_t *total_written) -> bool {}
|
||||
|
||||
auto enc_file::size() const -> std::uint64_t {}
|
||||
auto enc_file::size() const -> std::optional<std::uint64_t> {}
|
||||
} // namespace repertory::utils::file
|
||||
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
|
@ -27,6 +27,41 @@
|
||||
#include "utils/string.hpp"
|
||||
#include "utils/time.hpp"
|
||||
|
||||
namespace {
|
||||
[[nodiscard]] auto get_file_size(std::string_view path,
|
||||
std::uint64_t &file_size) -> bool {
|
||||
auto abs_path = repertory::utils::path::absolute(path);
|
||||
file_size = 0U;
|
||||
|
||||
#if defined(_WIN32)
|
||||
struct _stat64 st {};
|
||||
auto res = _stat64(std::string{path}.c_str(), &st);
|
||||
if (res != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
file_size = static_cast<std::uint64_t>(st.st_size);
|
||||
return true;
|
||||
#else // !defined(_WIN32)
|
||||
std::error_code ec{};
|
||||
file_size = std::filesystem::file_size(abs_path, ec);
|
||||
return (ec.value() == 0);
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
[[nodiscard]] auto is_file(std::string_view path) -> bool {
|
||||
auto abs_path = repertory::utils::path::absolute(path);
|
||||
|
||||
#if defined(_WIN32)
|
||||
return (::PathFileExistsA(abs_path.c_str()) &&
|
||||
not ::PathIsDirectoryA(abs_path.c_str()));
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
return (stat(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode));
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace repertory::utils::file {
|
||||
// auto file::attach_file(native_handle handle,
|
||||
// bool read_only) -> fs_file_t {
|
||||
@ -189,65 +224,11 @@ auto file::get_handle() const -> native_handle {
|
||||
}
|
||||
|
||||
auto file::get_time(time_types type) const -> std::uint64_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
recur_mutex_lock lock{*mtx_};
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
try {
|
||||
#if defined(_WIN32)
|
||||
#else // !defined(_WIN32)
|
||||
struct stat st {};
|
||||
stat(path_.c_str(), &st);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
switch (type) {
|
||||
case time_types::access:
|
||||
#if defined(_WIN32)
|
||||
break;
|
||||
#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)
|
||||
break;
|
||||
#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)
|
||||
break;
|
||||
#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)
|
||||
break;
|
||||
#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;
|
||||
return i_fs_item::get_time(type);
|
||||
}
|
||||
|
||||
auto file::move_to(std::string_view path) -> bool {
|
||||
@ -456,7 +437,7 @@ auto file::write(const unsigned char *data, std::size_t to_write,
|
||||
return false;
|
||||
}
|
||||
|
||||
auto file::size() const -> std::uint64_t {
|
||||
auto file::size() const -> std::optional<std::uint64_t> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
@ -491,6 +472,6 @@ auto file::size() const -> std::uint64_t {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return 0U;
|
||||
return std::nullopt;
|
||||
}
|
||||
} // namespace repertory::utils::file
|
||||
|
@ -201,19 +201,9 @@ auto smb_directory::create_file(std::string_view file_name,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())};
|
||||
if (not st) {
|
||||
smb_fclose(session_.get(), fd);
|
||||
throw std::runtime_error("failed to stat file|" + rel_path);
|
||||
}
|
||||
|
||||
return std::make_unique<smb_file>(
|
||||
smb_stat_get(st.get(), SMB_STAT_ATIME),
|
||||
smb_stat_get(st.get(), SMB_STAT_CTIME), fd,
|
||||
smb_stat_get(st.get(), SMB_STAT_MTIME),
|
||||
smb_create_smb_path(path_, std::string{rel_path}), session_,
|
||||
share_name_, smb_stat_get(st.get(), SMB_STAT_SIZE), tid_,
|
||||
smb_stat_get(st.get(), SMB_STAT_WTIME));
|
||||
fd, smb_create_smb_path(path_, std::string{rel_path}), session_,
|
||||
share_name_, tid_);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
@ -361,12 +351,8 @@ auto smb_directory::get_file(std::string_view path) const -> fs_file_t {
|
||||
}
|
||||
|
||||
return std::make_unique<smb_file>(
|
||||
smb_stat_get(st.get(), SMB_STAT_ATIME),
|
||||
smb_stat_get(st.get(), SMB_STAT_CTIME), std::nullopt,
|
||||
smb_stat_get(st.get(), SMB_STAT_MTIME),
|
||||
smb_create_smb_path(path_, std::string{rel_path}), session_,
|
||||
share_name_, smb_stat_get(st.get(), SMB_STAT_SIZE), tid_,
|
||||
smb_stat_get(st.get(), SMB_STAT_WTIME));
|
||||
std::nullopt, smb_create_smb_path(path_, std::string{rel_path}),
|
||||
session_, share_name_, tid_);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
@ -404,11 +390,8 @@ auto smb_directory::get_files() const -> std::vector<fs_file_t> {
|
||||
|
||||
std::string name{smb_stat_name(st)};
|
||||
ret.emplace_back(std::make_unique<smb_file>(
|
||||
smb_stat_get(st, SMB_STAT_ATIME), smb_stat_get(st, SMB_STAT_CTIME),
|
||||
std::nullopt, smb_stat_get(st, SMB_STAT_MTIME),
|
||||
smb_create_smb_path(path_, name), session_, share_name_,
|
||||
smb_stat_get(st, SMB_STAT_SIZE), tid_,
|
||||
smb_stat_get(st, SMB_STAT_WTIME)));
|
||||
std::nullopt, smb_create_smb_path(path_, name), session_, share_name_,
|
||||
tid_));
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -462,10 +445,8 @@ auto smb_directory::get_items() const -> std::vector<fs_item_t> {
|
||||
}
|
||||
|
||||
ret.emplace_back(std::make_unique<smb_file>(
|
||||
smb_stat_get(st, SMB_STAT_ATIME), smb_stat_get(st, SMB_STAT_CTIME),
|
||||
std::nullopt, smb_stat_get(st, SMB_STAT_MTIME),
|
||||
smb_create_smb_path(path_, name), session_, share_name_, tid_,
|
||||
smb_stat_get(st, SMB_STAT_SIZE), smb_stat_get(st, SMB_STAT_WTIME)));
|
||||
std::nullopt, smb_create_smb_path(path_, name), session_, share_name_,
|
||||
tid_));
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -478,44 +459,6 @@ auto smb_directory::get_items() const -> std::vector<fs_item_t> {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto smb_directory::get_time(time_types type) const -> std::uint64_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not session_) {
|
||||
throw std::runtime_error("session not found|" + path_);
|
||||
}
|
||||
|
||||
auto rel_path = smb_create_relative_path(path_);
|
||||
smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())};
|
||||
if (not st) {
|
||||
throw std::runtime_error("failed to stat directory|" + rel_path);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case time_types::access:
|
||||
return smb_stat_get(st.get(), SMB_STAT_ATIME);
|
||||
|
||||
case time_types::creation:
|
||||
return smb_stat_get(st.get(), SMB_STAT_CTIME);
|
||||
|
||||
case time_types::modified:
|
||||
return smb_stat_get(st.get(), SMB_STAT_MTIME);
|
||||
|
||||
case time_types::write:
|
||||
return smb_stat_get(st.get(), SMB_STAT_WTIME);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto smb_directory::move_to(std::string_view new_path) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
|
@ -75,6 +75,45 @@ void smb_file::flush() const {
|
||||
}
|
||||
}
|
||||
|
||||
auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
|
||||
time_types type) -> std::uint64_t {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (session == nullptr) {
|
||||
throw std::runtime_error("session not found|" + path);
|
||||
}
|
||||
|
||||
auto rel_path = smb_create_relative_path(path);
|
||||
smb_stat_t st{smb_fstat(session, tid, rel_path.c_str())};
|
||||
if (not st) {
|
||||
throw std::runtime_error("failed to stat directory|" + rel_path);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case time_types::access:
|
||||
return smb_stat_get(st.get(), SMB_STAT_ATIME);
|
||||
|
||||
case time_types::creation:
|
||||
return smb_stat_get(st.get(), SMB_STAT_CTIME);
|
||||
|
||||
case time_types::modified:
|
||||
return smb_stat_get(st.get(), SMB_STAT_MTIME);
|
||||
|
||||
case time_types::write:
|
||||
return smb_stat_get(st.get(), SMB_STAT_WTIME);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
auto smb_file::move_to(std::string_view new_path) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
@ -259,6 +298,32 @@ auto smb_file::remove() -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::size() const -> std::optional<std::uint64_t> {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not session_) {
|
||||
throw std::runtime_error("session not found|" + path_);
|
||||
}
|
||||
|
||||
auto rel_path = smb_create_relative_path(path_);
|
||||
smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())};
|
||||
if (not st) {
|
||||
throw std::runtime_error("failed to stat directory|" + rel_path);
|
||||
}
|
||||
|
||||
return smb_stat_get(st.get(), SMB_STAT_SIZE);
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto smb_file::truncate(std::size_t size) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
|
@ -39,8 +39,6 @@ void thread_file::close() {}
|
||||
|
||||
void thread_file::flush() const {}
|
||||
|
||||
auto thread_file::get_time(time_types type) const -> std::uint64_t {}
|
||||
|
||||
auto thread_file::move_to(std::string_view path) -> bool {}
|
||||
|
||||
auto thread_file::read(unsigned char *data, std::size_t to_read,
|
||||
@ -54,5 +52,5 @@ auto thread_file::write(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset,
|
||||
std::size_t *total_written) -> bool {}
|
||||
|
||||
auto thread_file::size() const -> std::uint64_t {}
|
||||
auto thread_file::size() const -> std::optional<std::uint64_t> {}
|
||||
} // namespace repertory::utils::file
|
||||
|
@ -148,7 +148,7 @@ auto find_program_in_path(const std::string &name_without_extension)
|
||||
for (auto &&extension : extension_list) {
|
||||
auto exec_path = combine(
|
||||
search_path, {name_without_extension + std::string{extension}});
|
||||
if (utils::file::is_file(exec_path)) {
|
||||
if (utils::file::file(exec_path).exists()) {
|
||||
found_items[name_without_extension] = exec_path;
|
||||
return exec_path;
|
||||
}
|
||||
|
@ -34,20 +34,6 @@ auto filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t {
|
||||
}
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
auto get_file_time_now() -> std::uint64_t {
|
||||
#if defined(_WIN32)
|
||||
SYSTEMTIME sys_time{};
|
||||
::GetSystemTime(&sys_time);
|
||||
|
||||
FILETIME file_time{};
|
||||
::SystemTimeToFileTime(&sys_time, &file_time);
|
||||
return static_cast<std::uint64_t>(
|
||||
(reinterpret_cast<LARGE_INTEGER *>(&file_time))->QuadPart);
|
||||
#else // !defined(_WIN32)
|
||||
return get_time_now();
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
void get_local_time_now(struct tm &local_time) {
|
||||
std::memset(&local_time, 0, sizeof(local_time));
|
||||
|
||||
@ -62,20 +48,10 @@ void get_local_time_now(struct tm &local_time) {
|
||||
|
||||
auto get_time_now() -> std::uint64_t {
|
||||
#if defined(_WIN32)
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
|
||||
return static_cast<std::uint64_t>(_time64(nullptr));
|
||||
#else // !defined(_WIN32)
|
||||
return static_cast<std::uint64_t>(time(nullptr));
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
#if defined(__APPLE__)
|
||||
return std::chrono::nanoseconds(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
#else // !defined(__APPLE__)
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::nanoseconds(
|
||||
std::chrono::high_resolution_clock::now().time_since_epoch())
|
||||
.count());
|
||||
#endif // defined(__APPLE__)
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
@ -41,7 +41,8 @@ static void delete_generated_files() {
|
||||
generated_files.clear();
|
||||
|
||||
if (parent_path.has_value()) {
|
||||
EXPECT_TRUE(repertory::utils::file::remove_directory(*parent_path, true));
|
||||
EXPECT_TRUE(
|
||||
repertory::utils::file::directory(*parent_path).remove_recursively());
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,8 +111,8 @@ auto get_test_output_dir() -> std::string {
|
||||
auto path = utils::path::combine("/tmp", {temp});
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
if (not utils::file::is_directory(path)) {
|
||||
EXPECT_TRUE(utils::file::create_directories(path));
|
||||
if (not utils::file::directory(path).exists()) {
|
||||
EXPECT_TRUE(utils::file::directory{path}.create_directory());
|
||||
}
|
||||
|
||||
return path;
|
||||
|
@ -29,13 +29,14 @@ namespace repertory {
|
||||
TEST(utils_file, can_create_file) {
|
||||
for (auto idx = 0U; idx < file_type_count; ++idx) {
|
||||
auto path = test::generate_test_file_name("utils_file");
|
||||
EXPECT_FALSE(utils::file::is_file(path) || utils::file::is_directory(path));
|
||||
EXPECT_FALSE(utils::file::file(path).exists() ||
|
||||
utils::file::directory(path).exists());
|
||||
|
||||
auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
|
||||
: utils::file::thread_file::open_or_create_file(path);
|
||||
EXPECT_TRUE(*file);
|
||||
|
||||
EXPECT_TRUE(utils::file::is_file(path));
|
||||
EXPECT_TRUE(utils::file::file(path).exists());
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ TEST(utils_file, write_fails_for_read_only_file) {
|
||||
auto file = idx == 0U
|
||||
? utils::file::file::open_or_create_file(path, true)
|
||||
: utils::file::thread_file::open_or_create_file(path, true);
|
||||
EXPECT_TRUE(utils::file::is_file(path));
|
||||
EXPECT_TRUE(utils::file::file(path).exists());
|
||||
EXPECT_TRUE(*file);
|
||||
std::size_t bytes_written{};
|
||||
EXPECT_FALSE(file->write(reinterpret_cast<const unsigned char *>("0"), 1U,
|
||||
|
Reference in New Issue
Block a user