From 497b4248409591a540143020a20e6ae505e1c188 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Thu, 22 Aug 2024 14:35:56 -0500 Subject: [PATCH] updated build system --- repertory/librepertory/src/app_config.cpp | 14 +- .../librepertory/src/drives/eviction.cpp | 13 +- .../drives/fuse/remotefuse/remote_server.cpp | 33 +- .../winfsp/remotewinfsp/remote_server.cpp | 13 +- .../remotewinfsp/remote_winfsp_drive.cpp | 50 +-- .../src/drives/winfsp/winfsp_drive.cpp | 2 +- .../src/file_manager/file_manager.cpp | 12 +- .../file_manager_ring_buffer_open_file.cpp | 2 +- .../src/platform/unix_platform.cpp | 4 +- .../src/providers/base_provider.cpp | 4 +- .../providers/encrypt/encrypt_provider.cpp | 20 +- .../src/providers/s3/s3_provider.cpp | 12 +- .../librepertory/src/utils/file_utils.cpp | 9 +- repertory/repertory/include/cli/mount.hpp | 2 +- .../include/fixtures/fuse_fixture.hpp | 18 +- .../include/mocks/mock_fuse_drive.hpp | 10 +- .../include/mocks/mock_winfsp_drive.hpp | 10 +- repertory/repertory_test/src/config_test.cpp | 20 +- .../src/file_manager_open_file_test.cpp | 12 +- .../repertory_test/src/file_manager_test.cpp | 21 +- .../repertory_test/src/remote_fuse_test.cpp | 28 +- .../repertory_test/src/remote_winfsp_test.cpp | 14 +- repertory/repertory_test/src/winfsp_test.cpp | 28 +- support/include/utils/file.hpp | 167 ++++---- support/src/utils/encrypting_reader.cpp | 38 +- support/src/utils/file.cpp | 221 +++-------- support/src/utils/file_directory.cpp | 371 +++++++++++++++++- support/src/utils/file_enc_file.cpp | 4 +- support/src/utils/file_file.cpp | 95 ++--- support/src/utils/file_smb_directory.cpp | 73 +--- support/src/utils/file_smb_file.cpp | 65 +++ support/src/utils/file_thread_file.cpp | 4 +- support/src/utils/path.cpp | 2 +- support/src/utils/time.cpp | 30 +- support/test/src/test.cpp | 7 +- support/test/src/utils/file_test.cpp | 7 +- 36 files changed, 823 insertions(+), 612 deletions(-) diff --git a/repertory/librepertory/src/app_config.cpp b/repertory/librepertory/src/app_config.cpp index 0357d639..1e3be2c6 100644 --- a/repertory/librepertory/src/app_config.cpp +++ b/repertory/librepertory/src/app_config.cpp @@ -103,15 +103,15 @@ app_config::app_config(const provider_type &prov, hc_.api_password = get_provider_api_password(prov_); hc_.api_port = default_api_port(prov_); - if (not utils::file::create_directories(data_directory_)) { + if (not utils::file::directory(data_directory_).create_directory()) { throw startup_exception("unable to create: " + data_directory_); } - if (not utils::file::create_directories(cache_directory_)) { + if (not utils::file::directory(cache_directory_).create_directory()) { throw startup_exception("unable to create: " + cache_directory_); } - if (not utils::file::create_directories(log_directory_)) { + if (not utils::file::directory(log_directory_).create_directory()) { throw startup_exception("unable to create: " + log_directory_); } @@ -547,7 +547,7 @@ auto app_config::load() -> bool { const auto config_file_path = get_config_file_path(); std::cout << config_file_path << std::endl; recur_mutex_lock lock(read_write_mutex_); - if (utils::file::is_file(config_file_path)) { + if (utils::file::file(config_file_path).exists()) { try { std::ifstream config_file(config_file_path.data()); if (config_file.is_open()) { @@ -713,9 +713,9 @@ void app_config::save() { const auto file_path = get_config_file_path(); recur_mutex_lock lock(read_write_mutex_); - if (config_changed_ || not utils::file::is_file(file_path)) { - if (not utils::file::is_directory(data_directory_)) { - if (not utils::file::create_directories(data_directory_)) { + if (config_changed_ || not utils::file::file(file_path).exists()) { + if (not utils::file::directory(data_directory_).exists()) { + if (not utils::file::directory(data_directory_).create_directory()) { utils::error::raise_error( function_name, "failed to create directory|sp|" + data_directory_ + "|err|" + diff --git a/repertory/librepertory/src/drives/eviction.cpp b/repertory/librepertory/src/drives/eviction.cpp index ceb0eab5..e152d270 100644 --- a/repertory/librepertory/src/drives/eviction.cpp +++ b/repertory/librepertory/src/drives/eviction.cpp @@ -38,14 +38,15 @@ auto eviction::check_minimum_requirements(const std::string &file_path) static_cast(__FUNCTION__), }; - std::uint64_t file_size{}; - if (not utils::file::get_file_size(file_path, file_size)) { + auto opt_size = utils::file::file{file_path}.size(); + if (not opt_size.has_value()) { utils::error::raise_error(function_name, utils::get_last_error_code(), file_path, "failed to get file size"); return false; } - auto ret = false; + auto file_size{opt_size.value()}; + auto ret{false}; if (file_size != 0U) { std::uint64_t reference_time{}; ret = config_.get_eviction_uses_accessed_time() @@ -110,9 +111,9 @@ void eviction::service_function() { if (provider_.get_filesystem_item_and_file(api_path, file, fsi) == api_error::success) { // Only evict files that match expected size - std::uint64_t file_size{}; - if (utils::file::get_file_size(cached_files_list.front(), - file_size)) { + auto opt_size = utils::file::file{cached_files_list.front()}.size(); + if (opt_size.has_value()) { + auto file_size{opt_size.value()}; if (file_size == fsi.size) { // Try to evict file if (fm_.evict_file(fsi.api_path) && diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp index 0c44b19a..4d168ad6 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp @@ -93,9 +93,10 @@ auto remote_server::populate_file_info(const std::string &api_path, auto error = drive_.get_item_meta(api_path, META_ATTRIBUTES, meta_attributes); if (error == api_error::success) { if (meta_attributes.empty()) { - meta_attributes = utils::file::is_directory(construct_path(api_path)) - ? std::to_string(FILE_ATTRIBUTE_DIRECTORY) - : std::to_string(FILE_ATTRIBUTE_NORMAL); + meta_attributes = + utils::file::directory(construct_path(api_path)).exists() + ? std::to_string(FILE_ATTRIBUTE_DIRECTORY) + : std::to_string(FILE_ATTRIBUTE_NORMAL); drive_.set_item_meta(api_path, META_ATTRIBUTES, meta_attributes); } const auto attributes = utils::string::to_uint32(meta_attributes); @@ -324,7 +325,7 @@ auto remote_server::fuse_fgetattr( auto res = has_open_info(static_cast(handle), EBADF); if (res == 0) { - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct stat64 unix_st {}; res = fstat64(static_cast(handle), &unix_st); if (res == 0) { @@ -488,7 +489,7 @@ auto remote_server::fuse_getattr(const char *path, remote::stat &r_stat, const auto parent_api_path = utils::path::get_parent_api_path(api_path); memset(&r_stat, 0, sizeof(remote::stat)); - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct stat64 unix_st {}; auto res = stat64(file_path.c_str(), &unix_st); @@ -651,7 +652,7 @@ auto remote_server::fuse_opendir(const char *path, remote::file_handle &handle) auto res = -1; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(utils::path::create_api_path(path))); @@ -1065,7 +1066,7 @@ auto remote_server::winfsp_can_delete(PVOID file_desc, STATUS_INVALID_HANDLE)); if (ret == STATUS_SUCCESS) { ret = static_cast( - utils::file::is_directory(file_path) + utils::file::directory(file_path).exists() ? drive_.get_directory_item_count( utils::path::create_api_path(relative_path)) ? STATUS_DIRECTORY_NOT_EMPTY @@ -1090,7 +1091,7 @@ auto remote_server::winfsp_cleanup(PVOID /*file_desc*/, PWSTR file_name, const auto file_path = construct_path(relative_path); was_closed = 0; - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); if (flags & FileSystemBase::FspCleanupDelete) { remove_all(file_path); was_closed = 1; @@ -1170,7 +1171,7 @@ auto remote_server::winfsp_create(PWSTR file_name, UINT32 create_options, const auto relative_path = utils::string::to_utf8(file_name); const auto file_path = construct_path(relative_path); - exists = utils::file::is_file(file_path); + exists = utils::file::file(file_path).exists(); if ((create_options & FILE_DIRECTORY_FILE) != 0U) { attributes |= FILE_ATTRIBUTE_DIRECTORY; @@ -1268,8 +1269,8 @@ auto remote_server::winfsp_get_security_by_name( auto ret = static_cast(STATUS_SUCCESS); const auto file_path = construct_path(file_name); - if (utils::file::is_file(file_path) || - (utils::file::is_directory(file_path))) { + if (utils::file::file(file_path).exists() || + (utils::file::directory(file_path).exists())) { if (attributes) { remote::file_info file_info{}; if ((ret = populate_file_info(construct_api_path(file_path), @@ -1318,7 +1319,7 @@ auto remote_server::winfsp_open( const auto relative_path = utils::string::to_utf8(file_name); const auto file_path = construct_path(relative_path); - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); if (directory) { create_options |= FILE_DIRECTORY_FILE; } @@ -1489,11 +1490,11 @@ auto remote_server::winfsp_rename( auto res = -1; errno = ENOENT; - if (utils::file::is_file(file_path)) { + if (utils::file::file(file_path).exists()) { res = drive_.rename_file(construct_api_path(file_path), construct_api_path(new_file_path), replace_if_exists != 0U); - } else if (utils::file::is_directory(file_path)) { + } else if (utils::file::directory(file_path).exists()) { res = drive_.rename_directory(construct_api_path(file_path), construct_api_path(new_file_path)); } @@ -1523,7 +1524,7 @@ auto remote_server::winfsp_set_basic_info( if (attributes == INVALID_FILE_ATTRIBUTES) { attributes = 0; } else if (attributes == 0) { - attributes = utils::file::is_directory(file_path) + attributes = utils::file::directory(file_path).exists() ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL; } @@ -1679,7 +1680,7 @@ auto remote_server::json_create_directory_snapshot( auto res = -1; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(api_path)); auto handle = get_next_handle(); diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp index a096e134..d98f3ea4 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp @@ -186,7 +186,7 @@ auto remote_server::fuse_fgetattr( auto res = has_compat_open_info(handle, EBADF); if (res == 0) { - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct _stat64 unix_st {}; res = _fstat64(static_cast(handle), &unix_st); if (res == 0) { @@ -282,7 +282,7 @@ auto remote_server::fuse_getattr(const char *path, remote::stat &r_st, const auto file_path = construct_path(path); memset(&r_st, 0, sizeof(remote::stat)); - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct _stat64 st1 {}; const auto res = _stat64(file_path.c_str(), &st1); @@ -873,7 +873,7 @@ auto remote_server::json_create_directory_snapshot( auto res = -1; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(utils::path::create_api_path(path))); auto handle = get_next_handle(); @@ -1011,8 +1011,11 @@ auto remote_server::winfsp_create(PWSTR file_name, UINT32 create_options, const auto file_path = utils::string::from_utf8(utils::path::combine( mount_location_, {utils::string::to_utf8(file_name)})); - exists = static_cast(utils::file::is_file(utils::path::combine( - mount_location_, {utils::string::to_utf8(file_name)}))); + exists = static_cast( + utils::file::file( + utils::path::combine(mount_location_, + {utils::string::to_utf8(file_name)})) + .exists()); auto create_flags = FILE_FLAG_BACKUP_SEMANTICS; if ((create_options & FILE_DIRECTORY_FILE) != 0U) { diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp index c8c238fa..e03bdffe 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp @@ -62,7 +62,7 @@ auto remote_winfsp_drive::winfsp_service::OnStart(ULONG, PWSTR *) -> NTSTATUS { (mount_location[1u] == ':'); auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; - if ((drive_letter && not utils::file::is_directory(mount_location))) { + if ((drive_letter && not utils::file::directory(mount_location).exists())) { auto unicode_mount_location = utils::string::from_utf8(mount_location); host_.SetFileSystemName(&unicode_mount_location[0u]); if (config_.get_enable_mount_manager()) { @@ -363,33 +363,33 @@ auto remote_winfsp_drive::ReadDirectory(PVOID /*file_node*/, PVOID file_desc, utils::path::strip_to_file_name(item_path)); if (not marker || (marker && item_found)) { // if (not utils::path::is_ads_file_path(item_path)) { - union { - UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + - ((MAX_PATH + 1) * sizeof(WCHAR))]; - FSP_FSCTL_DIR_INFO D; - } directory_info_buffer; + union { + UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + + ((MAX_PATH + 1) * sizeof(WCHAR))]; + FSP_FSCTL_DIR_INFO D; + } directory_info_buffer; - auto *directory_info = &directory_info_buffer.D; - ::ZeroMemory(directory_info, sizeof(*directory_info)); - directory_info->Size = static_cast( - FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + - (std::min((size_t)MAX_PATH, display_name.size()) * - sizeof(WCHAR))); + auto *directory_info = &directory_info_buffer.D; + ::ZeroMemory(directory_info, sizeof(*directory_info)); + directory_info->Size = static_cast( + FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + + (std::min((size_t)MAX_PATH, display_name.size()) * + sizeof(WCHAR))); - if (not item["meta"].empty() || - ((item_path != ".") && (item_path != ".."))) { - populate_file_info(item, directory_info->FileInfo); - } - if (ret == STATUS_SUCCESS) { - ::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH, - &display_name[0]); - - FspFileSystemFillDirectoryBuffer(directory_buffer, - directory_info, &ret); - if (ret != STATUS_SUCCESS) { - break; - } + if (not item["meta"].empty() || + ((item_path != ".") && (item_path != ".."))) { + populate_file_info(item, directory_info->FileInfo); + } + if (ret == STATUS_SUCCESS) { + ::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH, + &display_name[0]); + + FspFileSystemFillDirectoryBuffer(directory_buffer, directory_info, + &ret); + if (ret != STATUS_SUCCESS) { + break; } + } // } } else { item_found = display_name == std::wstring(marker); diff --git a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp index bf5eaf33..16a746a9 100644 --- a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp @@ -81,7 +81,7 @@ auto winfsp_drive::winfsp_service::OnStart(ULONG /*Argc*/, (mount_location[1U] == ':'); auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; - if ((drive_letter && not utils::file::is_directory(mount_location))) { + if ((drive_letter && not utils::file::directory(mount_location).exists())) { auto unicode_mount_location = utils::string::from_utf8(mount_location); host_.SetFileSystemName(unicode_mount_location.data()); if (config_.get_enable_mount_manager()) { diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index f27477c1..66efe502 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -778,11 +778,6 @@ auto file_manager::rename_file(const std::string &from_api_path, return res; } - std::uint64_t file_size{}; - if (not utils::file::get_file_size(fsi.source_path, file_size)) { - return api_error::os_error; - } - res = remove_file(to_api_path); if ((res == api_error::success) || (res == api_error::item_not_found)) { if (not utils::file::retry_delete_file(fsi.source_path)) { @@ -878,8 +873,9 @@ void file_manager::start() { auto res = provider_.get_filesystem_item(api_path, false, fsi); if (res == api_error::success) { if (source_path == fsi.source_path) { - std::uint64_t file_size{}; - if (utils::file::get_file_size(fsi.source_path, file_size)) { + auto opt_size = utils::file::file{fsi.source_path}.size(); + if (opt_size.has_value()) { + auto file_size{opt_size.value()}; if (file_size == fsi.size) { auto closeable_file = std::make_shared( chunk_size, @@ -1027,7 +1023,7 @@ void file_manager::upload_completed(const file_upload_completed &evt) { bool exists{}; auto res = provider_.is_file(evt.get_api_path(), exists); if ((res == api_error::success && not exists) || - not utils::file::is_file(evt.get_source().get())) { + not utils::file::file(evt.get_source().get()).exists()) { event_system::instance().raise( evt.get_api_path(), evt.get_source()); remove_upload(evt.get_api_path(), true); diff --git a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp index ba88240e..8cb91fa4 100644 --- a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp @@ -64,7 +64,7 @@ file_manager::ring_buffer_open_file::ring_buffer_open_file( ring_state_.set(0U, ring_state_.size(), true); buffer_directory = utils::path::absolute(buffer_directory); - if (not utils::file::create_directories(buffer_directory)) { + if (not utils::file::directory(buffer_directory).create_directory()) { throw std::runtime_error("failed to create buffer directory|path|" + buffer_directory + "|err|" + std::to_string(utils::get_last_error_code())); diff --git a/repertory/librepertory/src/platform/unix_platform.cpp b/repertory/librepertory/src/platform/unix_platform.cpp index a9f65e14..e60265b9 100644 --- a/repertory/librepertory/src/platform/unix_platform.cpp +++ b/repertory/librepertory/src/platform/unix_platform.cpp @@ -57,7 +57,7 @@ lock_data::~lock_data() { auto lock_data::get_lock_data_file() -> std::string { const auto dir = get_state_directory(); - if (not utils::file::create_directories(dir)) { + if (not utils::file::directory(dir).create_directory()) { throw startup_exception("failed to create directory|sp|" + dir + "|err|" + std::to_string(utils::get_last_error_code())); } @@ -67,7 +67,7 @@ auto lock_data::get_lock_data_file() -> std::string { auto lock_data::get_lock_file() -> std::string { const auto dir = get_state_directory(); - if (not utils::file::create_directories(dir)) { + if (not utils::file::directory(dir).create_directory()) { throw startup_exception("failed to create directory|sp|" + dir + "|err|" + std::to_string(utils::get_last_error_code())); } diff --git a/repertory/librepertory/src/providers/base_provider.cpp b/repertory/librepertory/src/providers/base_provider.cpp index b1e74c75..411ae390 100644 --- a/repertory/librepertory/src/providers/base_provider.cpp +++ b/repertory/librepertory/src/providers/base_provider.cpp @@ -482,10 +482,10 @@ void base_provider::remove_deleted_files() { for (const auto &item : removed_list) { if (not item.directory) { - if (utils::file::is_file(item.source_path)) { + if (utils::file::file(item.source_path).exists()) { const auto orphaned_directory = utils::path::combine(config_.get_data_directory(), {"orphaned"}); - if (utils::file::create_directories(orphaned_directory)) { + if (utils::file::directory(orphaned_directory).create_directory()) { const auto parts = utils::string::split(item.api_path, '/', false); const auto orphaned_file = utils::path::combine( orphaned_directory, diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index a3cee94d..b2b6b260 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -195,7 +195,7 @@ auto encrypt_provider::do_fs_operation( : api_error::item_not_found; } - auto exists = utils::file::is_file(source_path); + auto exists = utils::file::file(source_path).exists(); if (exists && directory) { return api_error::item_exists; } @@ -203,7 +203,7 @@ auto encrypt_provider::do_fs_operation( return api_error::item_not_found; } - exists = utils::file::is_directory(source_path); + exists = utils::file::directory(source_path).exists(); if (exists && not directory) { return api_error::item_exists; } @@ -659,8 +659,9 @@ auto encrypt_provider::is_directory(const std::string &api_path, return api_error::success; } - exists = utils::file::is_directory( - row->get_column("source_path").get_value()); + exists = utils::file::directory( + row->get_column("source_path").get_value()) + .exists(); return api_error::success; } @@ -677,8 +678,9 @@ auto encrypt_provider::is_file(const std::string &api_path, return api_error::success; } - exists = utils::file::is_file( - row->get_column("source_path").get_value()); + exists = + utils::file::file(row->get_column("source_path").get_value()) + .exists(); return api_error::success; } @@ -872,11 +874,13 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path, auto file_data = row->get_column("data").get_value_as_json(); - 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 (opt_size.has_value()) { return api_error::os_error; } + auto file_size{opt_size.value()}; + std::vector< std::array> iv_list{}; diff --git a/repertory/librepertory/src/providers/s3/s3_provider.cpp b/repertory/librepertory/src/providers/s3/s3_provider.cpp index 29fc8e7a..107b127a 100644 --- a/repertory/librepertory/src/providers/s3/s3_provider.cpp +++ b/repertory/librepertory/src/providers/s3/s3_provider.cpp @@ -869,12 +869,18 @@ void s3_provider::stop() { auto s3_provider::upload_file_impl(const std::string &api_path, const std::string &source_path, stop_type &stop_requested) -> api_error { - std::uint64_t file_size{}; - if (utils::file::is_file(source_path) && - not utils::file::get_file_size(source_path, file_size)) { + auto file = utils::file::file{source_path}; + if (file.exists()) { return api_error::comm_error; } + auto opt_size = file.size(); + if (opt_size.has_value()) { + return api_error::comm_error; + } + + auto file_size{opt_size.value()}; + const auto cfg = get_config().get_s3_config(); const auto is_encrypted = not cfg.encryption_token.empty(); diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index 641718ee..d1fd27f0 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -106,7 +106,8 @@ void change_to_process_directory() { auto copy_file(std::string from_path, std::string to_path) -> bool { from_path = utils::path::absolute(from_path); to_path = utils::path::absolute(to_path); - if (is_file(from_path) && not is_directory(to_path)) { + if (utils::file::file(from_path).exists() && + not directory(to_path).exists()) { return std::filesystem::copy_file(from_path, to_path); } @@ -117,7 +118,7 @@ auto copy_directory_recursively(std::string from_path, std::string to_path) -> bool { from_path = utils::path::absolute(from_path); to_path = utils::path::absolute(to_path); - auto ret = create_directories(to_path); + auto ret = utils::file::directory(to_path).create_directory(); if (ret) { #if defined(_WIN32) WIN32_FIND_DATA fd{}; @@ -405,7 +406,7 @@ auto move_file(std::string from, std::string to) -> bool { to = utils::path::absolute(to); const auto directory = utils::path::get_parent_directory(to); - if (not create_directories(directory)) { + if (not utils::file::directory(directory).create_directory()) { return false; } @@ -420,7 +421,7 @@ auto move_file(std::string from, std::string to) -> bool { auto read_file_lines(const std::string &path) -> std::vector { std::vector ret; - if (is_file(path)) { + if (utils::file::file(path).exists()) { std::ifstream fs(path); std::string current_line; while (not fs.eof() && std::getline(fs, current_line)) { diff --git a/repertory/repertory/include/cli/mount.hpp b/repertory/repertory/include/cli/mount.hpp index 220bdc12..c545e4f6 100644 --- a/repertory/repertory/include/cli/mount.hpp +++ b/repertory/repertory/include/cli/mount.hpp @@ -80,7 +80,7 @@ mount(std::vector args, std::string data_directory, std::cout << "Generated " << app_config::get_provider_display_name(prov) << " Configuration" << std::endl; std::cout << config.get_config_file_path() << std::endl; - ret = utils::file::is_file(config.get_config_file_path()) + ret = utils::file::file(config.get_config_file_path()).exists() ? exit_code::success : exit_code::file_creation_failed; } else { diff --git a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp index 706b88d9..9bbfc9ce 100644 --- a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp @@ -68,10 +68,10 @@ protected: ASSERT_TRUE(utils::file::remove_directory(test_directory, true)); mount_location = utils::path::combine(test_directory, {"mount"}); - ASSERT_TRUE(utils::file::create_directories(mount_location)); + ASSERT_TRUE(utils::file::directory(mount_location).create_directory()); cfg_directory = utils::path::combine(test_directory, {"cfg"}); - ASSERT_TRUE(utils::file::create_directories(cfg_directory)); + ASSERT_TRUE(utils::file::directory(cfg_directory).create_directory()); config = std::make_unique(provider_t::type, cfg_directory); @@ -153,12 +153,12 @@ public: open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); EXPECT_LE(1, fd); - EXPECT_TRUE(utils::file::is_file(file_path)); - EXPECT_FALSE(utils::file::is_directory(file_path)); + EXPECT_TRUE(utils::file::file(file_path).exists()); + EXPECT_FALSE(utils::file::directory(file_path).exists()); - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(0U, file_size); + auto opt_size = utils::file::file{file_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(0U, opt_size.value()); EXPECT_EQ(0, close(fd)); std::this_thread::sleep_for(SLEEP_SECONDS); @@ -197,8 +197,8 @@ public: EXPECT_EQ(0, ret); std::this_thread::sleep_for(SLEEP_SECONDS); - EXPECT_FALSE(utils::file::is_directory(file_path)); - EXPECT_FALSE(utils::file::is_file(file_path)); + EXPECT_FALSE(utils::file::directory(file_path).exists()); + EXPECT_FALSE(utils::file::file(file_path).exists()); } }; diff --git a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp index 089db907..3da3d60a 100644 --- a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp +++ b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp @@ -135,11 +135,11 @@ public: if (not utils::file::retry_delete_file(to_file_path)) { return -1; } - } else if (utils::file::is_directory(to_file_path) || - utils::file::is_file(to_file_path)) { - errno = EEXIST; - return -1; - } + } else if (utils::file::directory(to_file_path).exists()) || + utils::file::file(to_file_path).exists()) { + errno = EEXIST; + return -1; + } return rename(from_file_path.c_str(), to_file_path.c_str()); } diff --git a/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp b/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp index cfb635a8..180dfc01 100644 --- a/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp +++ b/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp @@ -138,7 +138,7 @@ public: auto populate_file_info(const std::string &api_path, remote::file_info &file_info) -> api_error override { const auto file_path = utils::path::combine(mount_location_, {api_path}); - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); const auto attributes = FILE_FLAG_BACKUP_SEMANTICS | (directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL); @@ -148,8 +148,14 @@ public: FILE_BASIC_INFO fi{}; ::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi)); if (not directory) { - utils::file::get_file_size(file_path, file_info.FileSize); + auto opt_size = utils::file::file{file_path}.size(); + if (not opt_size.has_value()) { + return api_error::os_error; + } + + file_info.FileSize = opt_size.value(); } + file_info.AllocationSize = directory ? 0 : utils::divide_with_ceiling(file_info.FileSize, diff --git a/repertory/repertory_test/src/config_test.cpp b/repertory/repertory_test/src/config_test.cpp index ce768ea6..8d5dd1d0 100644 --- a/repertory/repertory_test/src/config_test.cpp +++ b/repertory/repertory_test/src/config_test.cpp @@ -165,10 +165,12 @@ TEST_F(config_test, sia_default_settings) { json data; EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_STREQ(DEFAULT_SIA_CONFIG.c_str(), data.dump(2).c_str()); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(sia_directory, {"cache"}))); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(sia_directory, {"logs"}))); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(sia_directory, {"cache"})) + .exists()); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(sia_directory, {"logs"})) + .exists()); } } @@ -182,10 +184,12 @@ TEST_F(config_test, s3_default_settings) { json data; EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_STREQ(DEFAULT_S3_CONFIG.c_str(), data.dump(2).c_str()); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(s3_directory, {"cache"}))); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(s3_directory, {"logs"}))); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(s3_directory, {"cache"})) + .exists()); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(s3_directory, {"logs"})) + .exists()); } } diff --git a/repertory/repertory_test/src/file_manager_open_file_test.cpp b/repertory/repertory_test/src/file_manager_open_file_test.cpp index f31b7171..d322cc67 100644 --- a/repertory/repertory_test/src/file_manager_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_open_file_test.cpp @@ -128,7 +128,7 @@ TEST(open_file, will_not_change_source_path_for_0_byte_file) { o.close(); EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) { @@ -166,7 +166,7 @@ TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) { o.close(); EXPECT_EQ(api_error::download_stopped, o.get_api_error()); EXPECT_STRNE(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_FALSE(utils::file::is_file(source_path)); + EXPECT_FALSE(utils::file::file(source_path).exists()); } TEST(open_file, @@ -192,7 +192,7 @@ TEST(open_file, o.close(); EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(utils::file::is_file(source_path)); + EXPECT_TRUE(utils::file::file(source_path).exists()); } TEST(open_file, write_with_incomplete_download) { @@ -280,7 +280,7 @@ TEST(open_file, write_with_incomplete_download) { EXPECT_EQ(api_error::download_incomplete, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, write_new_file) { @@ -353,7 +353,7 @@ TEST(open_file, write_new_file) { EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, write_new_file_multiple_chunks) { @@ -445,7 +445,7 @@ TEST(open_file, write_new_file_multiple_chunks) { EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, resize_file_to_0_bytes) { diff --git a/repertory/repertory_test/src/file_manager_test.cpp b/repertory/repertory_test/src/file_manager_test.cpp index 0994c00c..5e59a49d 100644 --- a/repertory/repertory_test/src/file_manager_test.cpp +++ b/repertory/repertory_test/src/file_manager_test.cpp @@ -730,9 +730,9 @@ TEST(file_manager, can_evict_file) { std::size_t bytes_written{}; EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); - EXPECT_EQ(static_cast(data.size()), file_size); + auto opt_size = utils::file::file{source_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(static_cast(data.size()), opt_size.value()); } fm.close(handle); @@ -759,7 +759,7 @@ TEST(file_manager, can_evict_file) { return api_error::success; }); EXPECT_TRUE(fm.evict_file("/test_evict.txt")); - EXPECT_FALSE(utils::file::is_file(source_path)); + EXPECT_FALSE(utils::file::file(source_path).exists()); fm.stop(); } @@ -1003,9 +1003,10 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) { std::size_t bytes_written{}; EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); - EXPECT_EQ(static_cast(data.size()), file_size); + auto opt_size = utils::file::file{source_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(static_cast(data.size()), opt_size.value()); + fm.close(handle); EXPECT_TRUE(utils::retryable_action( @@ -1015,7 +1016,7 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) { capture.wait_for_empty(); - EXPECT_TRUE(utils::file::is_file(source_path)); + EXPECT_TRUE(utils::file::file(source_path).exists()); fm.stop(); } @@ -1600,7 +1601,7 @@ TEST(file_manager, can_remove_file) { auto file = utils::file::file::open_or_create_file("./test_remove.txt"); EXPECT_TRUE(*file); } - EXPECT_TRUE(utils::file::is_file("./test_remove.txt")); + EXPECT_TRUE(utils::file::file("./test_remove.txt").exists()); EXPECT_CALL(mp, get_filesystem_item) .WillOnce([](const std::string &api_path, bool directory, @@ -1619,7 +1620,7 @@ TEST(file_manager, can_remove_file) { EXPECT_EQ(api_error::success, fm.remove_file("/test_remove.txt")); - EXPECT_FALSE(utils::file::is_file("./test_remove.txt")); + EXPECT_FALSE(utils::file::file("./test_remove.txt").exists()); } EXPECT_TRUE(utils::file::remove_directory(file_manager_dir, true)); diff --git a/repertory/repertory_test/src/remote_fuse_test.cpp b/repertory/repertory_test/src/remote_fuse_test.cpp index 80544fc3..09f04248 100644 --- a/repertory/repertory_test/src/remote_fuse_test.cpp +++ b/repertory/repertory_test/src/remote_fuse_test.cpp @@ -289,9 +289,9 @@ static void ftruncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_ftruncate(api_path.c_str(), 100, handle)); EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(100u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(100U, opt_size.value()); } EXPECT_TRUE(utils::file::retry_delete_file(test_file)); @@ -441,7 +441,7 @@ static void mkdir_test(repertory::remote_fuse::remote_client &client) { #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); EXPECT_TRUE(utils::file::remove_directory(test_directory)); } @@ -486,7 +486,7 @@ opendir_and_releasedir_test(repertory::remote_fuse::remote_client &client) { #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); remote::file_handle handle = 0; EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); @@ -558,7 +558,7 @@ static void readdir_test(repertory::remote_fuse::remote_client &client) { #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); remote::file_handle handle = 0; EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); @@ -621,8 +621,8 @@ static void rename_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_rename(api_path.c_str(), renamed_api_path.c_str())); - EXPECT_FALSE(utils::file::is_file(test_file)); - EXPECT_TRUE(utils::file::is_file(renamed_test_file)); + EXPECT_FALSE(utils::file::file(test_file).exists()); + EXPECT_TRUE(utils::file::file(renamed_test_file).exists()); } EXPECT_TRUE(utils::file::retry_delete_file(test_file)); @@ -640,10 +640,10 @@ static void rmdir_test(repertory::remote_fuse::remote_client &client) { #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); EXPECT_EQ(0, client.fuse_rmdir(api_path.c_str())); - EXPECT_FALSE(utils::file::is_directory(test_directory)); + EXPECT_FALSE(utils::file::directory(test_directory).exists()); EXPECT_TRUE(utils::file::remove_directory(test_directory)); } @@ -871,9 +871,9 @@ static void truncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_truncate(api_path.c_str(), 100)); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(100u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(100U, opt_size.value()); } EXPECT_TRUE(utils::file::retry_delete_file(test_file)); @@ -892,7 +892,7 @@ static void unlink_test(repertory::remote_fuse::remote_client &client) { if (ret == 0) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_unlink(api_path.c_str())); - EXPECT_FALSE(utils::file::is_file(test_file)); + EXPECT_FALSE(utils::file::file(test_file).exists()); } EXPECT_TRUE(utils::file::retry_delete_file(test_file)); diff --git a/repertory/repertory_test/src/remote_winfsp_test.cpp b/repertory/repertory_test/src/remote_winfsp_test.cpp index 36f846ca..bbc292bd 100644 --- a/repertory/repertory_test/src/remote_winfsp_test.cpp +++ b/repertory/repertory_test/src/remote_winfsp_test.cpp @@ -278,7 +278,7 @@ static void create_and_read_directory_test(remote_client &client) { FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists); EXPECT_EQ(STATUS_SUCCESS, ret); - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); json list; ret = client.winfsp_read_directory(file_desc, nullptr, nullptr, list); @@ -308,7 +308,7 @@ static void open_and_read_directory_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); file_desc = reinterpret_cast(REPERTORY_INVALID_HANDLE); ret = client.winfsp_open(&api_path[0], FILE_DIRECTORY_FILE, @@ -389,8 +389,8 @@ static void rename_test(remote_client &client) { ret = client.winfsp_rename(file_desc, &api_path[0], &api_path2[0], 0); EXPECT_EQ(STATUS_SUCCESS, ret); - EXPECT_TRUE(utils::file::is_file(test_file2)); - EXPECT_FALSE(utils::file::is_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file2).exists()); + EXPECT_FALSE(utils::file::file(test_file).exists()); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); @@ -472,9 +472,9 @@ static void set_file_size_test(remote_client &client) { client.winfsp_set_file_size(file_desc, new_file_size, set_allocation_size, &fi)); - std::uint64_t file_size = 0u; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(34u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(34U, opt_size.value()); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); diff --git a/repertory/repertory_test/src/winfsp_test.cpp b/repertory/repertory_test/src/winfsp_test.cpp index 909f7042..8a52abd2 100644 --- a/repertory/repertory_test/src/winfsp_test.cpp +++ b/repertory/repertory_test/src/winfsp_test.cpp @@ -69,12 +69,12 @@ static void execute_mount(winfsp_test *test, static void unmount(winfsp_test *test, const std::string &mount_point) { test->drive->shutdown(); - auto mounted = utils::file::is_directory(mount_point); + auto mounted = utils::file::directory(mount_point).exists(); for (auto i = 0; mounted && (i < 50); i++) { std::this_thread::sleep_for(100ms); - mounted = utils::file::is_directory(mount_point); + mounted = utils::file::directory(mount_point).exists(); } - EXPECT_FALSE(utils::file::is_directory(mount_point)); + EXPECT_FALSE(utils::file::directory(mount_point).exists()); } static void root_creation_test(const std::string &mount_point) { @@ -96,11 +96,11 @@ static auto create_test(winfsp_test *test, const std::string &mount_point) { EXPECT_NE(INVALID_HANDLE_VALUE, handle); EXPECT_TRUE(::CloseHandle(handle)); - EXPECT_TRUE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file).exists()); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(file, file_size)); - EXPECT_EQ(0, file_size); + auto opt_size = utils::file::file(file).size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(0, opt_size.value()); std::string attr; EXPECT_EQ(api_error::success, test->provider->get_item_meta( @@ -114,7 +114,7 @@ static void delete_file_test(const std::string &file) { TEST_HEADER(__FUNCTION__); event_capture ec({"file_removed"}); EXPECT_TRUE(utils::file::retry_delete_file(file)); - EXPECT_FALSE(utils::file::is_file(file)); + EXPECT_FALSE(utils::file::file(file).exists()); } static void create_directory_test(const std::string &directory) { @@ -149,11 +149,11 @@ static void write_file_test(const std::string &mount_point) { EXPECT_EQ(10, bytes_written); EXPECT_TRUE(::CloseHandle(handle)); - EXPECT_TRUE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file).exists()); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(file, file_size)); - EXPECT_EQ(10, file_size); + auto opt_size = utils::file::file(file).size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(10U, opt_size.value()); } static void read_file_test(const std::string &mount_point) { @@ -199,8 +199,8 @@ static void rename_file_test(winfsp_test *test, const auto file2 = utils::path::combine(mount_point, {"rename_file2.txt"}); EXPECT_TRUE(::MoveFile(&file[0], &file2[0])); - EXPECT_TRUE(utils::file::is_file(file2)); - EXPECT_FALSE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file2).exists()); + EXPECT_FALSE(utils::file::file(file).exists()); api_meta_map meta2{}; EXPECT_EQ(api_error::success, diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index e0f682ac..11bcc995 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -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; + 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 = 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 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 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 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 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 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 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 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 -inline auto directory_exists_in_path_t( +[[nodiscard]] inline auto directory_exists_in_path_t( std::basic_string_view path, std::basic_string_view sub_directory) -> bool { - return is_directory(utils::path::combine(path, {sub_directory})); + return directory(utils::path::combine(path, {sub_directory})).exists(); } template -inline auto file_exists_in_path_t( +[[nodiscard]] inline auto file_exists_in_path_t( std::basic_string_view path, std::basic_string_view 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(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(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(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(path, file_name); } } // namespace repertory::utils::file diff --git a/support/src/utils/encrypting_reader.cpp b/support/src/utils/encrypting_reader.cpp index 614a8bec..1d371add 100644 --- a/support/src/utils/encrypting_reader.cpp +++ b/support/src/utils/encrypting_reader.cpp @@ -127,10 +127,11 @@ protected: reader_.set_read_position(reinterpret_cast(gptr())); - const auto res = encrypting_reader::reader_function( + auto res = encrypting_reader::reader_function( ptr, 1U, static_cast(count), &reader_); if ((res == reader_.get_error_return()) || - (reader_.get_stop_requested() && (res == CURL_READFUNC_ABORT))) { + (reader_.get_stop_requested() && + (res == static_cast(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(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(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(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(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(CURL_READFUNC_ABORT) : ret ? total_read : error_return_; } diff --git a/support/src/utils/file.cpp b/support/src/utils/file.cpp index 8979d4c4..4030a6c0 100644 --- a/support/src/utils/file.cpp +++ b/support/src/utils/file.cpp @@ -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(__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(st.st_atime); +#else // !defined(_WIN32) + return static_cast(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(st.st_ctime); +#else // !defined(_WIN32) + return static_cast(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(st.st_mtime); +#else // !defined(_WIN32) + return static_cast(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(st.st_mtime); +#else // !defined(_WIN32) + return static_cast(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(path, sub_directory); -} - -auto directory_exists_in_path(std::wstring_view path, - std::wstring_view sub_directory) -> bool { - return directory_exists_in_path_t(path, sub_directory); -} - -auto file_exists_in_path(std::string_view path, - std::string_view file_name) -> bool { - return file_exists_in_path_t(path, file_name); -} - -auto file_exists_in_path(std::wstring_view path, - std::wstring_view file_name) -> bool { - return file_exists_in_path_t(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(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) diff --git a/support/src/utils/file_directory.cpp b/support/src/utils/file_directory.cpp index aff658f8..2f0873a7 100644 --- a/support/src/utils/file_directory.cpp +++ b/support/src/utils/file_directory.cpp @@ -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 directory_action, + std::function 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(__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(__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(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(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(__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 { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector 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(__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 { return {}; } + return nullptr; +} -auto directory::get_items() const -> std::vector { return {}; } +auto directory::get_file(std::string_view path) const -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__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 { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector 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 { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector 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(__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(__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(__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 diff --git a/support/src/utils/file_enc_file.cpp b/support/src/utils/file_enc_file.cpp index 9beb135f..a0f8f901 100644 --- a/support/src/utils/file_enc_file.cpp +++ b/support/src/utils/file_enc_file.cpp @@ -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 {} } // namespace repertory::utils::file #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 1ea60fc3..c1cd7f92 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -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(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(__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(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(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(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(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 { static constexpr const std::string_view function_name{ static_cast(__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 diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp index c09bb51f..53e0ee73 100644 --- a/support/src/utils/file_smb_directory.cpp +++ b/support/src/utils/file_smb_directory.cpp @@ -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_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_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 { std::string name{smb_stat_name(st)}; ret.emplace_back(std::make_unique( - 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 { } ret.emplace_back(std::make_unique( - 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 { return {}; } -auto smb_directory::get_time(time_types type) const -> std::uint64_t { - static constexpr const std::string_view function_name{ - static_cast(__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(__FUNCTION__), diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index c60394df..671e6685 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -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(__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(__FUNCTION__), @@ -259,6 +298,32 @@ auto smb_file::remove() -> bool { return false; } +auto smb_file::size() const -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__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(__FUNCTION__), diff --git a/support/src/utils/file_thread_file.cpp b/support/src/utils/file_thread_file.cpp index e2e170ef..e1ff8f45 100644 --- a/support/src/utils/file_thread_file.cpp +++ b/support/src/utils/file_thread_file.cpp @@ -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 {} } // namespace repertory::utils::file diff --git a/support/src/utils/path.cpp b/support/src/utils/path.cpp index 172991e6..6204123c 100644 --- a/support/src/utils/path.cpp +++ b/support/src/utils/path.cpp @@ -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; } diff --git a/support/src/utils/time.cpp b/support/src/utils/time.cpp index 00b115c8..b8fb0c7d 100644 --- a/support/src/utils/time.cpp +++ b/support/src/utils/time.cpp @@ -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( - (reinterpret_cast(&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::chrono::system_clock::to_time_t(std::chrono::system_clock::now())); + return static_cast(_time64(nullptr)); +#else // !defined(_WIN32) + return static_cast(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::chrono::nanoseconds( - std::chrono::high_resolution_clock::now().time_since_epoch()) - .count()); -#endif // defined(__APPLE__) } #if defined(_WIN32) diff --git a/support/test/src/test.cpp b/support/test/src/test.cpp index 46188dcc..927f5d81 100644 --- a/support/test/src/test.cpp +++ b/support/test/src/test.cpp @@ -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; diff --git a/support/test/src/utils/file_test.cpp b/support/test/src/utils/file_test.cpp index fa48df10..c991c57f 100644 --- a/support/test/src/utils/file_test.cpp +++ b/support/test/src/utils/file_test.cpp @@ -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("0"), 1U,