updated build system

This commit is contained in:
2024-08-22 14:35:56 -05:00
parent 1236bf1829
commit 497b424840
36 changed files with 823 additions and 612 deletions

View File

@ -103,15 +103,15 @@ app_config::app_config(const provider_type &prov,
hc_.api_password = get_provider_api_password(prov_); hc_.api_password = get_provider_api_password(prov_);
hc_.api_port = default_api_port(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_); 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_); 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_); 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(); const auto config_file_path = get_config_file_path();
std::cout << config_file_path << std::endl; std::cout << config_file_path << std::endl;
recur_mutex_lock lock(read_write_mutex_); recur_mutex_lock lock(read_write_mutex_);
if (utils::file::is_file(config_file_path)) { if (utils::file::file(config_file_path).exists()) {
try { try {
std::ifstream config_file(config_file_path.data()); std::ifstream config_file(config_file_path.data());
if (config_file.is_open()) { if (config_file.is_open()) {
@ -713,9 +713,9 @@ void app_config::save() {
const auto file_path = get_config_file_path(); const auto file_path = get_config_file_path();
recur_mutex_lock lock(read_write_mutex_); recur_mutex_lock lock(read_write_mutex_);
if (config_changed_ || not utils::file::is_file(file_path)) { if (config_changed_ || not utils::file::file(file_path).exists()) {
if (not utils::file::is_directory(data_directory_)) { if (not utils::file::directory(data_directory_).exists()) {
if (not utils::file::create_directories(data_directory_)) { if (not utils::file::directory(data_directory_).create_directory()) {
utils::error::raise_error( utils::error::raise_error(
function_name, "failed to create directory|sp|" + data_directory_ + function_name, "failed to create directory|sp|" + data_directory_ +
"|err|" + "|err|" +

View File

@ -38,14 +38,15 @@ auto eviction::check_minimum_requirements(const std::string &file_path)
static_cast<const char *>(__FUNCTION__), static_cast<const char *>(__FUNCTION__),
}; };
std::uint64_t file_size{}; auto opt_size = utils::file::file{file_path}.size();
if (not utils::file::get_file_size(file_path, file_size)) { if (not opt_size.has_value()) {
utils::error::raise_error(function_name, utils::get_last_error_code(), utils::error::raise_error(function_name, utils::get_last_error_code(),
file_path, "failed to get file size"); file_path, "failed to get file size");
return false; return false;
} }
auto ret = false; auto file_size{opt_size.value()};
auto ret{false};
if (file_size != 0U) { if (file_size != 0U) {
std::uint64_t reference_time{}; std::uint64_t reference_time{};
ret = config_.get_eviction_uses_accessed_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) == if (provider_.get_filesystem_item_and_file(api_path, file, fsi) ==
api_error::success) { api_error::success) {
// Only evict files that match expected size // Only evict files that match expected size
std::uint64_t file_size{}; auto opt_size = utils::file::file{cached_files_list.front()}.size();
if (utils::file::get_file_size(cached_files_list.front(), if (opt_size.has_value()) {
file_size)) { auto file_size{opt_size.value()};
if (file_size == fsi.size) { if (file_size == fsi.size) {
// Try to evict file // Try to evict file
if (fm_.evict_file(fsi.api_path) && if (fm_.evict_file(fsi.api_path) &&

View File

@ -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); auto error = drive_.get_item_meta(api_path, META_ATTRIBUTES, meta_attributes);
if (error == api_error::success) { if (error == api_error::success) {
if (meta_attributes.empty()) { if (meta_attributes.empty()) {
meta_attributes = utils::file::is_directory(construct_path(api_path)) meta_attributes =
? std::to_string(FILE_ATTRIBUTE_DIRECTORY) utils::file::directory(construct_path(api_path)).exists()
: std::to_string(FILE_ATTRIBUTE_NORMAL); ? std::to_string(FILE_ATTRIBUTE_DIRECTORY)
: std::to_string(FILE_ATTRIBUTE_NORMAL);
drive_.set_item_meta(api_path, META_ATTRIBUTES, meta_attributes); drive_.set_item_meta(api_path, META_ATTRIBUTES, meta_attributes);
} }
const auto attributes = utils::string::to_uint32(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<native_handle>(handle), EBADF); auto res = has_open_info(static_cast<native_handle>(handle), EBADF);
if (res == 0) { if (res == 0) {
directory = utils::file::is_directory(file_path); directory = utils::file::directory(file_path).exists();
struct stat64 unix_st {}; struct stat64 unix_st {};
res = fstat64(static_cast<native_handle>(handle), &unix_st); res = fstat64(static_cast<native_handle>(handle), &unix_st);
if (res == 0) { 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); const auto parent_api_path = utils::path::get_parent_api_path(api_path);
memset(&r_stat, 0, sizeof(remote::stat)); 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 {}; struct stat64 unix_st {};
auto res = stat64(file_path.c_str(), &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; auto res = -1;
errno = ENOENT; errno = ENOENT;
if (utils::file::is_directory(file_path)) { if (utils::file::directory(file_path).exists()) {
auto iter = std::make_shared<directory_iterator>( auto iter = std::make_shared<directory_iterator>(
drive_.get_directory_items(utils::path::create_api_path(path))); 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)); STATUS_INVALID_HANDLE));
if (ret == STATUS_SUCCESS) { if (ret == STATUS_SUCCESS) {
ret = static_cast<packet::error_type>( ret = static_cast<packet::error_type>(
utils::file::is_directory(file_path) utils::file::directory(file_path).exists()
? drive_.get_directory_item_count( ? drive_.get_directory_item_count(
utils::path::create_api_path(relative_path)) utils::path::create_api_path(relative_path))
? STATUS_DIRECTORY_NOT_EMPTY ? 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); const auto file_path = construct_path(relative_path);
was_closed = 0; 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) { if (flags & FileSystemBase::FspCleanupDelete) {
remove_all(file_path); remove_all(file_path);
was_closed = 1; 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 relative_path = utils::string::to_utf8(file_name);
const auto file_path = construct_path(relative_path); 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) { if ((create_options & FILE_DIRECTORY_FILE) != 0U) {
attributes |= FILE_ATTRIBUTE_DIRECTORY; attributes |= FILE_ATTRIBUTE_DIRECTORY;
@ -1268,8 +1269,8 @@ auto remote_server::winfsp_get_security_by_name(
auto ret = static_cast<packet::error_type>(STATUS_SUCCESS); auto ret = static_cast<packet::error_type>(STATUS_SUCCESS);
const auto file_path = construct_path(file_name); const auto file_path = construct_path(file_name);
if (utils::file::is_file(file_path) || if (utils::file::file(file_path).exists() ||
(utils::file::is_directory(file_path))) { (utils::file::directory(file_path).exists())) {
if (attributes) { if (attributes) {
remote::file_info file_info{}; remote::file_info file_info{};
if ((ret = populate_file_info(construct_api_path(file_path), 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 relative_path = utils::string::to_utf8(file_name);
const auto file_path = construct_path(relative_path); 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) { if (directory) {
create_options |= FILE_DIRECTORY_FILE; create_options |= FILE_DIRECTORY_FILE;
} }
@ -1489,11 +1490,11 @@ auto remote_server::winfsp_rename(
auto res = -1; auto res = -1;
errno = ENOENT; 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), res = drive_.rename_file(construct_api_path(file_path),
construct_api_path(new_file_path), construct_api_path(new_file_path),
replace_if_exists != 0U); 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), res = drive_.rename_directory(construct_api_path(file_path),
construct_api_path(new_file_path)); construct_api_path(new_file_path));
} }
@ -1523,7 +1524,7 @@ auto remote_server::winfsp_set_basic_info(
if (attributes == INVALID_FILE_ATTRIBUTES) { if (attributes == INVALID_FILE_ATTRIBUTES) {
attributes = 0; attributes = 0;
} else if (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_DIRECTORY
: FILE_ATTRIBUTE_NORMAL; : FILE_ATTRIBUTE_NORMAL;
} }
@ -1679,7 +1680,7 @@ auto remote_server::json_create_directory_snapshot(
auto res = -1; auto res = -1;
errno = ENOENT; errno = ENOENT;
if (utils::file::is_directory(file_path)) { if (utils::file::directory(file_path).exists()) {
auto iter = std::make_shared<directory_iterator>( auto iter = std::make_shared<directory_iterator>(
drive_.get_directory_items(api_path)); drive_.get_directory_items(api_path));
auto handle = get_next_handle(); auto handle = get_next_handle();

View File

@ -186,7 +186,7 @@ auto remote_server::fuse_fgetattr(
auto res = has_compat_open_info(handle, EBADF); auto res = has_compat_open_info(handle, EBADF);
if (res == 0) { if (res == 0) {
directory = utils::file::is_directory(file_path); directory = utils::file::directory(file_path).exists();
struct _stat64 unix_st {}; struct _stat64 unix_st {};
res = _fstat64(static_cast<int>(handle), &unix_st); res = _fstat64(static_cast<int>(handle), &unix_st);
if (res == 0) { 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); const auto file_path = construct_path(path);
memset(&r_st, 0, sizeof(remote::stat)); memset(&r_st, 0, sizeof(remote::stat));
directory = utils::file::is_directory(file_path); directory = utils::file::directory(file_path).exists();
struct _stat64 st1 {}; struct _stat64 st1 {};
const auto res = _stat64(file_path.c_str(), &st1); const auto res = _stat64(file_path.c_str(), &st1);
@ -873,7 +873,7 @@ auto remote_server::json_create_directory_snapshot(
auto res = -1; auto res = -1;
errno = ENOENT; errno = ENOENT;
if (utils::file::is_directory(file_path)) { if (utils::file::directory(file_path).exists()) {
auto iter = std::make_shared<directory_iterator>( auto iter = std::make_shared<directory_iterator>(
drive_.get_directory_items(utils::path::create_api_path(path))); drive_.get_directory_items(utils::path::create_api_path(path)));
auto handle = get_next_handle(); 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( const auto file_path = utils::string::from_utf8(utils::path::combine(
mount_location_, {utils::string::to_utf8(file_name)})); mount_location_, {utils::string::to_utf8(file_name)}));
exists = static_cast<BOOLEAN>(utils::file::is_file(utils::path::combine( exists = static_cast<BOOLEAN>(
mount_location_, {utils::string::to_utf8(file_name)}))); utils::file::file(
utils::path::combine(mount_location_,
{utils::string::to_utf8(file_name)}))
.exists());
auto create_flags = FILE_FLAG_BACKUP_SEMANTICS; auto create_flags = FILE_FLAG_BACKUP_SEMANTICS;
if ((create_options & FILE_DIRECTORY_FILE) != 0U) { if ((create_options & FILE_DIRECTORY_FILE) != 0U) {

View File

@ -62,7 +62,7 @@ auto remote_winfsp_drive::winfsp_service::OnStart(ULONG, PWSTR *) -> NTSTATUS {
(mount_location[1u] == ':'); (mount_location[1u] == ':');
auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; 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); auto unicode_mount_location = utils::string::from_utf8(mount_location);
host_.SetFileSystemName(&unicode_mount_location[0u]); host_.SetFileSystemName(&unicode_mount_location[0u]);
if (config_.get_enable_mount_manager()) { 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)); utils::path::strip_to_file_name(item_path));
if (not marker || (marker && item_found)) { if (not marker || (marker && item_found)) {
// if (not utils::path::is_ads_file_path(item_path)) { // if (not utils::path::is_ads_file_path(item_path)) {
union { union {
UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) +
((MAX_PATH + 1) * sizeof(WCHAR))]; ((MAX_PATH + 1) * sizeof(WCHAR))];
FSP_FSCTL_DIR_INFO D; FSP_FSCTL_DIR_INFO D;
} directory_info_buffer; } directory_info_buffer;
auto *directory_info = &directory_info_buffer.D; auto *directory_info = &directory_info_buffer.D;
::ZeroMemory(directory_info, sizeof(*directory_info)); ::ZeroMemory(directory_info, sizeof(*directory_info));
directory_info->Size = static_cast<UINT16>( directory_info->Size = static_cast<UINT16>(
FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) +
(std::min((size_t)MAX_PATH, display_name.size()) * (std::min((size_t)MAX_PATH, display_name.size()) *
sizeof(WCHAR))); sizeof(WCHAR)));
if (not item["meta"].empty() || if (not item["meta"].empty() ||
((item_path != ".") && (item_path != ".."))) { ((item_path != ".") && (item_path != ".."))) {
populate_file_info(item, directory_info->FileInfo); populate_file_info(item, directory_info->FileInfo);
} }
if (ret == STATUS_SUCCESS) { if (ret == STATUS_SUCCESS) {
::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH, ::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH,
&display_name[0]); &display_name[0]);
FspFileSystemFillDirectoryBuffer(directory_buffer, FspFileSystemFillDirectoryBuffer(directory_buffer, directory_info,
directory_info, &ret); &ret);
if (ret != STATUS_SUCCESS) { if (ret != STATUS_SUCCESS) {
break; break;
}
} }
}
// } // }
} else { } else {
item_found = display_name == std::wstring(marker); item_found = display_name == std::wstring(marker);

View File

@ -81,7 +81,7 @@ auto winfsp_drive::winfsp_service::OnStart(ULONG /*Argc*/,
(mount_location[1U] == ':'); (mount_location[1U] == ':');
auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; 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); auto unicode_mount_location = utils::string::from_utf8(mount_location);
host_.SetFileSystemName(unicode_mount_location.data()); host_.SetFileSystemName(unicode_mount_location.data());
if (config_.get_enable_mount_manager()) { if (config_.get_enable_mount_manager()) {

View File

@ -778,11 +778,6 @@ auto file_manager::rename_file(const std::string &from_api_path,
return res; 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); res = remove_file(to_api_path);
if ((res == api_error::success) || (res == api_error::item_not_found)) { if ((res == api_error::success) || (res == api_error::item_not_found)) {
if (not utils::file::retry_delete_file(fsi.source_path)) { 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); auto res = provider_.get_filesystem_item(api_path, false, fsi);
if (res == api_error::success) { if (res == api_error::success) {
if (source_path == fsi.source_path) { if (source_path == fsi.source_path) {
std::uint64_t file_size{}; auto opt_size = utils::file::file{fsi.source_path}.size();
if (utils::file::get_file_size(fsi.source_path, file_size)) { if (opt_size.has_value()) {
auto file_size{opt_size.value()};
if (file_size == fsi.size) { if (file_size == fsi.size) {
auto closeable_file = std::make_shared<open_file>( auto closeable_file = std::make_shared<open_file>(
chunk_size, chunk_size,
@ -1027,7 +1023,7 @@ void file_manager::upload_completed(const file_upload_completed &evt) {
bool exists{}; bool exists{};
auto res = provider_.is_file(evt.get_api_path(), exists); auto res = provider_.is_file(evt.get_api_path(), exists);
if ((res == api_error::success && not exists) || if ((res == api_error::success && not exists) ||
not utils::file::is_file(evt.get_source().get<std::string>())) { not utils::file::file(evt.get_source().get<std::string>()).exists()) {
event_system::instance().raise<file_upload_not_found>( event_system::instance().raise<file_upload_not_found>(
evt.get_api_path(), evt.get_source()); evt.get_api_path(), evt.get_source());
remove_upload(evt.get_api_path(), true); remove_upload(evt.get_api_path(), true);

View File

@ -64,7 +64,7 @@ file_manager::ring_buffer_open_file::ring_buffer_open_file(
ring_state_.set(0U, ring_state_.size(), true); ring_state_.set(0U, ring_state_.size(), true);
buffer_directory = utils::path::absolute(buffer_directory); 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|" + throw std::runtime_error("failed to create buffer directory|path|" +
buffer_directory + "|err|" + buffer_directory + "|err|" +
std::to_string(utils::get_last_error_code())); std::to_string(utils::get_last_error_code()));

View File

@ -57,7 +57,7 @@ lock_data::~lock_data() {
auto lock_data::get_lock_data_file() -> std::string { auto lock_data::get_lock_data_file() -> std::string {
const auto dir = get_state_directory(); 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|" + throw startup_exception("failed to create directory|sp|" + dir + "|err|" +
std::to_string(utils::get_last_error_code())); 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 { auto lock_data::get_lock_file() -> std::string {
const auto dir = get_state_directory(); 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|" + throw startup_exception("failed to create directory|sp|" + dir + "|err|" +
std::to_string(utils::get_last_error_code())); std::to_string(utils::get_last_error_code()));
} }

View File

@ -482,10 +482,10 @@ void base_provider::remove_deleted_files() {
for (const auto &item : removed_list) { for (const auto &item : removed_list) {
if (not item.directory) { if (not item.directory) {
if (utils::file::is_file(item.source_path)) { if (utils::file::file(item.source_path).exists()) {
const auto orphaned_directory = const auto orphaned_directory =
utils::path::combine(config_.get_data_directory(), {"orphaned"}); 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 parts = utils::string::split(item.api_path, '/', false);
const auto orphaned_file = utils::path::combine( const auto orphaned_file = utils::path::combine(
orphaned_directory, orphaned_directory,

View File

@ -195,7 +195,7 @@ auto encrypt_provider::do_fs_operation(
: api_error::item_not_found; : api_error::item_not_found;
} }
auto exists = utils::file::is_file(source_path); auto exists = utils::file::file(source_path).exists();
if (exists && directory) { if (exists && directory) {
return api_error::item_exists; return api_error::item_exists;
} }
@ -203,7 +203,7 @@ auto encrypt_provider::do_fs_operation(
return api_error::item_not_found; return api_error::item_not_found;
} }
exists = utils::file::is_directory(source_path); exists = utils::file::directory(source_path).exists();
if (exists && not directory) { if (exists && not directory) {
return api_error::item_exists; return api_error::item_exists;
} }
@ -659,8 +659,9 @@ auto encrypt_provider::is_directory(const std::string &api_path,
return api_error::success; return api_error::success;
} }
exists = utils::file::is_directory( exists = utils::file::directory(
row->get_column("source_path").get_value<std::string>()); row->get_column("source_path").get_value<std::string>())
.exists();
return api_error::success; return api_error::success;
} }
@ -677,8 +678,9 @@ auto encrypt_provider::is_file(const std::string &api_path,
return api_error::success; return api_error::success;
} }
exists = utils::file::is_file( exists =
row->get_column("source_path").get_value<std::string>()); utils::file::file(row->get_column("source_path").get_value<std::string>())
.exists();
return api_error::success; 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(); auto file_data = row->get_column("data").get_value_as_json();
std::uint64_t file_size{}; auto opt_size = utils::file::file{source_path}.size();
if (not utils::file::get_file_size(source_path, file_size)) { if (opt_size.has_value()) {
return api_error::os_error; return api_error::os_error;
} }
auto file_size{opt_size.value()};
std::vector< std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list{}; iv_list{};

View File

@ -869,12 +869,18 @@ void s3_provider::stop() {
auto s3_provider::upload_file_impl(const std::string &api_path, auto s3_provider::upload_file_impl(const std::string &api_path,
const std::string &source_path, const std::string &source_path,
stop_type &stop_requested) -> api_error { stop_type &stop_requested) -> api_error {
std::uint64_t file_size{}; auto file = utils::file::file{source_path};
if (utils::file::is_file(source_path) && if (file.exists()) {
not utils::file::get_file_size(source_path, file_size)) {
return api_error::comm_error; 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 cfg = get_config().get_s3_config();
const auto is_encrypted = not cfg.encryption_token.empty(); const auto is_encrypted = not cfg.encryption_token.empty();

View File

@ -106,7 +106,8 @@ void change_to_process_directory() {
auto copy_file(std::string from_path, std::string to_path) -> bool { auto copy_file(std::string from_path, std::string to_path) -> bool {
from_path = utils::path::absolute(from_path); from_path = utils::path::absolute(from_path);
to_path = utils::path::absolute(to_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); 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 { std::string to_path) -> bool {
from_path = utils::path::absolute(from_path); from_path = utils::path::absolute(from_path);
to_path = utils::path::absolute(to_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 (ret) {
#if defined(_WIN32) #if defined(_WIN32)
WIN32_FIND_DATA fd{}; WIN32_FIND_DATA fd{};
@ -405,7 +406,7 @@ auto move_file(std::string from, std::string to) -> bool {
to = utils::path::absolute(to); to = utils::path::absolute(to);
const auto directory = utils::path::get_parent_directory(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; 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::string> { auto read_file_lines(const std::string &path) -> std::vector<std::string> {
std::vector<std::string> ret; std::vector<std::string> ret;
if (is_file(path)) { if (utils::file::file(path).exists()) {
std::ifstream fs(path); std::ifstream fs(path);
std::string current_line; std::string current_line;
while (not fs.eof() && std::getline(fs, current_line)) { while (not fs.eof() && std::getline(fs, current_line)) {

View File

@ -80,7 +80,7 @@ mount(std::vector<const char *> args, std::string data_directory,
std::cout << "Generated " << app_config::get_provider_display_name(prov) std::cout << "Generated " << app_config::get_provider_display_name(prov)
<< " Configuration" << std::endl; << " Configuration" << std::endl;
std::cout << config.get_config_file_path() << 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::success
: exit_code::file_creation_failed; : exit_code::file_creation_failed;
} else { } else {

View File

@ -68,10 +68,10 @@ protected:
ASSERT_TRUE(utils::file::remove_directory(test_directory, true)); ASSERT_TRUE(utils::file::remove_directory(test_directory, true));
mount_location = utils::path::combine(test_directory, {"mount"}); 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"}); 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<app_config>(provider_t::type, cfg_directory); config = std::make_unique<app_config>(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); open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd); EXPECT_LE(1, fd);
EXPECT_TRUE(utils::file::is_file(file_path)); EXPECT_TRUE(utils::file::file(file_path).exists());
EXPECT_FALSE(utils::file::is_directory(file_path)); EXPECT_FALSE(utils::file::directory(file_path).exists());
std::uint64_t file_size{}; auto opt_size = utils::file::file{file_path}.size();
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(0U, file_size); EXPECT_EQ(0U, opt_size.value());
EXPECT_EQ(0, close(fd)); EXPECT_EQ(0, close(fd));
std::this_thread::sleep_for(SLEEP_SECONDS); std::this_thread::sleep_for(SLEEP_SECONDS);
@ -197,8 +197,8 @@ public:
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
std::this_thread::sleep_for(SLEEP_SECONDS); std::this_thread::sleep_for(SLEEP_SECONDS);
EXPECT_FALSE(utils::file::is_directory(file_path)); EXPECT_FALSE(utils::file::directory(file_path).exists());
EXPECT_FALSE(utils::file::is_file(file_path)); EXPECT_FALSE(utils::file::file(file_path).exists());
} }
}; };

View File

@ -135,11 +135,11 @@ public:
if (not utils::file::retry_delete_file(to_file_path)) { if (not utils::file::retry_delete_file(to_file_path)) {
return -1; return -1;
} }
} else if (utils::file::is_directory(to_file_path) || } else if (utils::file::directory(to_file_path).exists()) ||
utils::file::is_file(to_file_path)) { utils::file::file(to_file_path).exists()) {
errno = EEXIST; errno = EEXIST;
return -1; return -1;
} }
return rename(from_file_path.c_str(), to_file_path.c_str()); return rename(from_file_path.c_str(), to_file_path.c_str());
} }

View File

@ -138,7 +138,7 @@ public:
auto populate_file_info(const std::string &api_path, auto populate_file_info(const std::string &api_path,
remote::file_info &file_info) -> api_error override { remote::file_info &file_info) -> api_error override {
const auto file_path = utils::path::combine(mount_location_, {api_path}); 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 = const auto attributes =
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_BACKUP_SEMANTICS |
(directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL); (directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL);
@ -148,8 +148,14 @@ public:
FILE_BASIC_INFO fi{}; FILE_BASIC_INFO fi{};
::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi)); ::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi));
if (not directory) { 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 = file_info.AllocationSize =
directory ? 0 directory ? 0
: utils::divide_with_ceiling(file_info.FileSize, : utils::divide_with_ceiling(file_info.FileSize,

View File

@ -165,10 +165,12 @@ TEST_F(config_test, sia_default_settings) {
json data; json data;
EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_TRUE(utils::file::read_json_file(config_file, data));
EXPECT_STREQ(DEFAULT_SIA_CONFIG.c_str(), data.dump(2).c_str()); EXPECT_STREQ(DEFAULT_SIA_CONFIG.c_str(), data.dump(2).c_str());
EXPECT_TRUE(utils::file::is_directory( EXPECT_TRUE(
utils::path::combine(sia_directory, {"cache"}))); utils::file::directory(utils::path::combine(sia_directory, {"cache"}))
EXPECT_TRUE(utils::file::is_directory( .exists());
utils::path::combine(sia_directory, {"logs"}))); 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; json data;
EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_TRUE(utils::file::read_json_file(config_file, data));
EXPECT_STREQ(DEFAULT_S3_CONFIG.c_str(), data.dump(2).c_str()); EXPECT_STREQ(DEFAULT_S3_CONFIG.c_str(), data.dump(2).c_str());
EXPECT_TRUE(utils::file::is_directory( EXPECT_TRUE(
utils::path::combine(s3_directory, {"cache"}))); utils::file::directory(utils::path::combine(s3_directory, {"cache"}))
EXPECT_TRUE(utils::file::is_directory( .exists());
utils::path::combine(s3_directory, {"logs"}))); EXPECT_TRUE(
utils::file::directory(utils::path::combine(s3_directory, {"logs"}))
.exists());
} }
} }

View File

@ -128,7 +128,7 @@ TEST(open_file, will_not_change_source_path_for_0_byte_file) {
o.close(); o.close();
EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); 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) { 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(); o.close();
EXPECT_EQ(api_error::download_stopped, o.get_api_error()); EXPECT_EQ(api_error::download_stopped, o.get_api_error());
EXPECT_STRNE(source_path.c_str(), o.get_source_path().c_str()); 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, TEST(open_file,
@ -192,7 +192,7 @@ TEST(open_file,
o.close(); o.close();
EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); 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) { 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_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) { 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_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) { 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_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) { TEST(open_file, resize_file_to_0_bytes) {

View File

@ -730,9 +730,9 @@ TEST(file_manager, can_evict_file) {
std::size_t bytes_written{}; std::size_t bytes_written{};
EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written));
std::uint64_t file_size{}; auto opt_size = utils::file::file{source_path}.size();
EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(static_cast<std::uint64_t>(data.size()), file_size); EXPECT_EQ(static_cast<std::uint64_t>(data.size()), opt_size.value());
} }
fm.close(handle); fm.close(handle);
@ -759,7 +759,7 @@ TEST(file_manager, can_evict_file) {
return api_error::success; return api_error::success;
}); });
EXPECT_TRUE(fm.evict_file("/test_evict.txt")); 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(); fm.stop();
} }
@ -1003,9 +1003,10 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) {
std::size_t bytes_written{}; std::size_t bytes_written{};
EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written));
std::uint64_t file_size{}; auto opt_size = utils::file::file{source_path}.size();
EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(static_cast<std::uint64_t>(data.size()), file_size); EXPECT_EQ(static_cast<std::uint64_t>(data.size()), opt_size.value());
fm.close(handle); fm.close(handle);
EXPECT_TRUE(utils::retryable_action( EXPECT_TRUE(utils::retryable_action(
@ -1015,7 +1016,7 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) {
capture.wait_for_empty(); capture.wait_for_empty();
EXPECT_TRUE(utils::file::is_file(source_path)); EXPECT_TRUE(utils::file::file(source_path).exists());
fm.stop(); fm.stop();
} }
@ -1600,7 +1601,7 @@ TEST(file_manager, can_remove_file) {
auto file = utils::file::file::open_or_create_file("./test_remove.txt"); auto file = utils::file::file::open_or_create_file("./test_remove.txt");
EXPECT_TRUE(*file); 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) EXPECT_CALL(mp, get_filesystem_item)
.WillOnce([](const std::string &api_path, bool directory, .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_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)); EXPECT_TRUE(utils::file::remove_directory(file_manager_dir, true));

View File

@ -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_ftruncate(api_path.c_str(), 100, handle));
EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle));
std::uint64_t file_size; auto opt_size = utils::file::file{test_file}.size();
EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(100u, file_size); EXPECT_EQ(100U, opt_size.value());
} }
EXPECT_TRUE(utils::file::retry_delete_file(test_file)); EXPECT_TRUE(utils::file::retry_delete_file(test_file));
@ -441,7 +441,7 @@ static void mkdir_test(repertory::remote_fuse::remote_client &client) {
#else #else
EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU));
#endif #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)); EXPECT_TRUE(utils::file::remove_directory(test_directory));
} }
@ -486,7 +486,7 @@ opendir_and_releasedir_test(repertory::remote_fuse::remote_client &client) {
#else #else
EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU));
#endif #endif
EXPECT_TRUE(utils::file::is_directory(test_directory)); EXPECT_TRUE(utils::file::directory(test_directory).exists());
remote::file_handle handle = 0; remote::file_handle handle = 0;
EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); 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 #else
EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU));
#endif #endif
EXPECT_TRUE(utils::file::is_directory(test_directory)); EXPECT_TRUE(utils::file::directory(test_directory).exists());
remote::file_handle handle = 0; remote::file_handle handle = 0;
EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); 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_release(api_path.c_str(), handle));
EXPECT_EQ(0, EXPECT_EQ(0,
client.fuse_rename(api_path.c_str(), renamed_api_path.c_str())); client.fuse_rename(api_path.c_str(), renamed_api_path.c_str()));
EXPECT_FALSE(utils::file::is_file(test_file)); EXPECT_FALSE(utils::file::file(test_file).exists());
EXPECT_TRUE(utils::file::is_file(renamed_test_file)); EXPECT_TRUE(utils::file::file(renamed_test_file).exists());
} }
EXPECT_TRUE(utils::file::retry_delete_file(test_file)); EXPECT_TRUE(utils::file::retry_delete_file(test_file));
@ -640,10 +640,10 @@ static void rmdir_test(repertory::remote_fuse::remote_client &client) {
#else #else
EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU));
#endif #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_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)); 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)); EXPECT_EQ(0, client.fuse_truncate(api_path.c_str(), 100));
std::uint64_t file_size; auto opt_size = utils::file::file{test_file}.size();
EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(100u, file_size); EXPECT_EQ(100U, opt_size.value());
} }
EXPECT_TRUE(utils::file::retry_delete_file(test_file)); 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) { if (ret == 0) {
EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle));
EXPECT_EQ(0, client.fuse_unlink(api_path.c_str())); 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)); EXPECT_TRUE(utils::file::retry_delete_file(test_file));

View File

@ -278,7 +278,7 @@ static void create_and_read_directory_test(remote_client &client) {
FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists); FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret); EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_TRUE(utils::file::is_directory(test_directory)); EXPECT_TRUE(utils::file::directory(test_directory).exists());
json list; json list;
ret = client.winfsp_read_directory(file_desc, nullptr, nullptr, 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_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<PVOID>(REPERTORY_INVALID_HANDLE); file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
ret = client.winfsp_open(&api_path[0], FILE_DIRECTORY_FILE, 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); ret = client.winfsp_rename(file_desc, &api_path[0], &api_path2[0], 0);
EXPECT_EQ(STATUS_SUCCESS, ret); EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_TRUE(utils::file::is_file(test_file2)); EXPECT_TRUE(utils::file::file(test_file2).exists());
EXPECT_FALSE(utils::file::is_file(test_file)); EXPECT_FALSE(utils::file::file(test_file).exists());
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); 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, client.winfsp_set_file_size(file_desc, new_file_size,
set_allocation_size, &fi)); set_allocation_size, &fi));
std::uint64_t file_size = 0u; auto opt_size = utils::file::file{test_file}.size();
EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(34u, file_size); EXPECT_EQ(34U, opt_size.value());
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));

View File

@ -69,12 +69,12 @@ static void execute_mount(winfsp_test *test,
static void unmount(winfsp_test *test, const std::string &mount_point) { static void unmount(winfsp_test *test, const std::string &mount_point) {
test->drive->shutdown(); 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++) { for (auto i = 0; mounted && (i < 50); i++) {
std::this_thread::sleep_for(100ms); 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) { 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_NE(INVALID_HANDLE_VALUE, handle);
EXPECT_TRUE(::CloseHandle(handle)); EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file)); EXPECT_TRUE(utils::file::file(file).exists());
std::uint64_t file_size; auto opt_size = utils::file::file(file).size();
EXPECT_TRUE(utils::file::get_file_size(file, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(0, file_size); EXPECT_EQ(0, opt_size.value());
std::string attr; std::string attr;
EXPECT_EQ(api_error::success, test->provider->get_item_meta( 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__); TEST_HEADER(__FUNCTION__);
event_capture ec({"file_removed"}); event_capture ec({"file_removed"});
EXPECT_TRUE(utils::file::retry_delete_file(file)); 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) { 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_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle)); EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file)); EXPECT_TRUE(utils::file::file(file).exists());
std::uint64_t file_size; auto opt_size = utils::file::file(file).size();
EXPECT_TRUE(utils::file::get_file_size(file, file_size)); EXPECT_TRUE(opt_size.has_value());
EXPECT_EQ(10, file_size); EXPECT_EQ(10U, opt_size.value());
} }
static void read_file_test(const std::string &mount_point) { 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"}); const auto file2 = utils::path::combine(mount_point, {"rename_file2.txt"});
EXPECT_TRUE(::MoveFile(&file[0], &file2[0])); EXPECT_TRUE(::MoveFile(&file[0], &file2[0]));
EXPECT_TRUE(utils::file::is_file(file2)); EXPECT_TRUE(utils::file::file(file2).exists());
EXPECT_FALSE(utils::file::is_file(file)); EXPECT_FALSE(utils::file::file(file).exists());
api_meta_map meta2{}; api_meta_map meta2{};
EXPECT_EQ(api_error::success, EXPECT_EQ(api_error::success,

View File

@ -27,6 +27,21 @@
#include "utils/path.hpp" #include "utils/path.hpp"
namespace repertory::utils::file { 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) #if defined(PROJECT_ENABLE_LIBDSM)
[[nodiscard]] auto [[nodiscard]] auto
smb_create_and_validate_relative_path(std::string_view smb_path, 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 { struct i_fs_item {
using fs_item_t = std::unique_ptr<i_fs_item>; using fs_item_t = std::unique_ptr<i_fs_item>;
enum class time_types { enum class time_types {
access, access,
creation, creation,
@ -74,8 +90,7 @@ struct i_fs_item {
[[nodiscard]] virtual auto get_path() const -> std::string = 0; [[nodiscard]] virtual auto get_path() const -> std::string = 0;
[[nodiscard]] virtual auto [[nodiscard]] virtual auto get_time(time_types type) const -> std::uint64_t;
get_time(time_types type) const -> std::uint64_t = 0;
[[nodiscard]] virtual auto is_directory_item() const -> bool = 0; [[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; 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); } [[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); }
@ -268,7 +283,7 @@ public:
return read_buffer_size; 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; [[nodiscard]] auto truncate(std::size_t size) -> bool override;
@ -335,7 +350,9 @@ public:
return file_->get_read_buffer_size(); 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 { [[nodiscard]] auto is_read_only() const -> bool override {
return file_->is_read_only(); return file_->is_read_only();
@ -353,7 +370,7 @@ public:
return file_->set_read_buffer_size(size); 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; [[nodiscard]] auto truncate(std::size_t size) -> bool override;
@ -445,7 +462,9 @@ public:
return file_->get_read_buffer_size(); 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 { [[nodiscard]] auto is_read_only() const -> bool override {
return file_->is_read_only(); return file_->is_read_only();
@ -463,7 +482,7 @@ public:
return file_->set_read_buffer_size(size); 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; [[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; count(bool recursive = false) const -> std::uint64_t = 0;
[[nodiscard]] virtual auto [[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, [[nodiscard]] virtual auto create_file(std::string_view file_name,
bool read_only) const -> fs_file_t = 0; bool read_only) const -> fs_file_t = 0;
@ -559,7 +578,7 @@ public:
count(bool recursive = false) const -> std::uint64_t override; count(bool recursive = false) const -> std::uint64_t override;
[[nodiscard]] auto [[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, [[nodiscard]] auto create_file(std::string_view file_name,
bool read_only) const -> fs_file_t override; 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_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 move_to(std::string_view new_path) -> bool override;
[[nodiscard]] auto remove() -> bool override; [[nodiscard]] auto remove() -> bool override;
@ -609,52 +626,35 @@ class smb_file final : public i_file {
public: public:
smb_file() = default; smb_file() = default;
smb_file(std::uint64_t access_time, std::uint64_t creation_time, smb_file(std::optional<smb_fd> fd, std::string path, smb_session_t session,
std::optional<smb_fd> fd, std::uint64_t modified_time, std::string_view share_name, smb_tid tid)
std::string path, smb_session_t session, std::string_view share_name, : fd_(std::move(fd)),
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),
path_(std::move(path)), path_(std::move(path)),
session_(std::move(session)), session_(std::move(session)),
share_name_(share_name), share_name_(share_name),
size_(size), tid_(tid) {}
tid_(tid),
write_time_(write_time) {}
smb_file(const smb_file &) = delete; smb_file(const smb_file &) = delete;
smb_file(smb_file &&f) noexcept smb_file(smb_file &&f) noexcept
: access_time_(f.access_time_), : fd_(std::move(f.fd_)),
creation_time_(f.creation_time_),
fd_(std::move(f.fd_)),
modified_time_(f.modified_time_),
path_(std::move(f.path_)), path_(std::move(f.path_)),
read_buffer_size(f.get_read_buffer_size()), read_buffer_size(f.get_read_buffer_size()),
read_only_(f.read_only_), read_only_(f.read_only_),
session_(std::move(f.session_)), session_(std::move(f.session_)),
share_name_(std::move(f.share_name_)), share_name_(std::move(f.share_name_)),
size_(f.size_), tid_(f.tid_) {}
tid_(f.tid_),
write_time_(f.write_time_) {}
~smb_file() override { close(); } ~smb_file() override { close(); }
private: private:
std::uint64_t access_time_;
std::uint64_t creation_time_;
std::optional<smb_fd> fd_; std::optional<smb_fd> fd_;
std::uint64_t modified_time_;
std::string path_; std::string path_;
std::atomic_uint32_t read_buffer_size{65536U}; std::atomic_uint32_t read_buffer_size{65536U};
bool read_only_; bool read_only_;
smb_session_t session_; smb_session_t session_;
std::string share_name_; std::string share_name_;
std::uint64_t size_;
smb_tid tid_; smb_tid tid_;
std::uint64_t write_time_;
public: public:
void close() override; void close() override;
@ -673,20 +673,12 @@ public:
return read_buffer_size; 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 { [[nodiscard]] auto get_time(time_types type) const -> std::uint64_t override {
switch (type) { return get_time(session_.get(), tid_, path_, 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_;
}
} }
[[nodiscard]] auto get_unc_path() const -> std::string { [[nodiscard]] auto get_unc_path() const -> std::string {
@ -722,7 +714,7 @@ public:
return read_buffer_size; 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; [[nodiscard]] auto truncate(std::size_t size) -> bool override;
@ -735,18 +727,13 @@ public:
auto operator=(smb_file &&move_file) noexcept -> smb_file & { auto operator=(smb_file &&move_file) noexcept -> smb_file & {
if (this != &move_file) { if (this != &move_file) {
access_time_ = move_file.access_time_;
creation_time_ = move_file.creation_time_;
fd_ = std::move(move_file.fd_); fd_ = std::move(move_file.fd_);
modified_time_ = move_file.modified_time_;
path_ = std::move(move_file.path_); path_ = std::move(move_file.path_);
read_buffer_size = move_file.get_read_buffer_size(); read_buffer_size = move_file.get_read_buffer_size();
read_only_ = move_file.read_only_; read_only_ = move_file.read_only_;
session_ = std::move(move_file.session_); session_ = std::move(move_file.session_);
share_name_ = std::move(move_file.share_name_); share_name_ = std::move(move_file.share_name_);
size_ = move_file.size_;
tid_ = move_file.tid_; tid_ = move_file.tid_;
write_time_ = move_file.write_time_;
} }
return *this; return *this;
@ -796,7 +783,7 @@ public:
count(bool recursive = false) const -> std::uint64_t override; count(bool recursive = false) const -> std::uint64_t override;
[[nodiscard]] auto [[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, [[nodiscard]] auto create_file(std::string_view file_name,
bool read_only) const -> fs_file_t override; 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_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 { [[nodiscard]] auto get_unc_path() const -> std::string {
return smb_get_unc_path(path_); return smb_get_unc_path(path_);
@ -853,44 +842,6 @@ public:
}; };
#endif // defined(PROJECT_ENABLE_LIBDSM) #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_JSON)
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
[[nodiscard]] auto [[nodiscard]] auto
@ -924,18 +875,38 @@ read_json_file(std::string_view path, nlohmann::json &data,
#endif // defined(PROJECT_ENABLE_JSON) #endif // defined(PROJECT_ENABLE_JSON)
template <typename string_t> 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> path,
std::basic_string_view<typename string_t::value_type> sub_directory) std::basic_string_view<typename string_t::value_type> sub_directory)
-> bool { -> bool {
return is_directory(utils::path::combine(path, {sub_directory})); return directory(utils::path::combine(path, {sub_directory})).exists();
} }
template <typename string_t> 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> path,
std::basic_string_view<typename string_t::value_type> file_name) -> bool { 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 } // namespace repertory::utils::file

View File

@ -127,10 +127,11 @@ protected:
reader_.set_read_position(reinterpret_cast<std::uintptr_t>(gptr())); 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_); ptr, 1U, static_cast<std::size_t>(count), &reader_);
if ((res == reader_.get_error_return()) || 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(); return traits_type::eof();
} }
@ -200,7 +201,13 @@ encrypting_reader::encrypting_reader(
encrypted_file_path_ += '/' + encrypted_file_name_; 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( const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_)); 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_path_ = encrypted_file_path;
encrypted_file_name_ = utils::path::strip_to_file_name(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( const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_)); 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_path_ = encrypted_file_path;
encrypted_file_name_ = utils::path::strip_to_file_name(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( const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_)); 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) auto encrypting_reader::calculate_encrypted_size(std::string_view source_path)
-> std::uint64_t { -> std::uint64_t {
std::uint64_t file_size{}; auto opt_size = utils::file::file{source_path}.size();
if (not utils::file::get_file_size(source_path, file_size)) { if (not opt_size.has_value()) {
throw std::runtime_error("get file size failed|src|" + throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' + std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code())); std::to_string(utils::get_last_error_code()));
} }
auto file_size{opt_size.value()};
const auto total_chunks = utils::divide_with_ceiling( const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_)); file_size, static_cast<std::uint64_t>(data_chunk_size_));
return file_size + (total_chunks * encryption_header_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 : ret ? total_read
: error_return_; : error_return_;
} }

View File

@ -26,56 +26,6 @@
#include "utils/path.hpp" #include "utils/path.hpp"
#include "utils/string.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 { namespace repertory::utils::file {
auto i_file::read_all(data_buffer &data, std::uint64_t offset, auto i_file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool { std::size_t *total_read) -> bool {
@ -107,131 +57,64 @@ auto i_file::read_all(data_buffer &data, std::uint64_t offset,
return false; return false;
} }
auto create_directories(std::string_view path) -> bool { auto i_fs_item::get_time(time_types type) const -> std::uint64_t {
if (is_directory(path)) { static constexpr const std::string_view function_name{
return true; static_cast<const char *>(__FUNCTION__),
} };
try {
#if defined(_WIN32) #if defined(_WIN32)
return (::SHCreateDirectory( struct _stat64 st {};
nullptr, _stat64(get_path().c_str(), &st);
utils::string::from_utf8(utils::path::absolute(path)).c_str()) == #else // !defined(_WIN32)
ERROR_SUCCESS); struct stat st {};
#else // !defined(_WIN32) stat(get_path().c_str(), &st);
auto ret{true}; #endif // defined(_WIN32)
auto paths = utils::string::split(utils::path::absolute(path),
utils::path::directory_seperator, false);
std::string current_path; switch (type) {
for (std::size_t idx = 0U; ret && (idx < paths.size()); idx++) { case time_types::access:
if (paths.at(idx).empty()) { #if defined(_WIN32)
current_path = utils::path::directory_seperator; return static_cast<std::uint64_t>(st.st_atime);
continue; #else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_atim.tv_nsec +
st.st_atim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::creation:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_ctime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_ctim.tv_nsec +
st.st_ctim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::modified:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::write:
#if defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
} }
} catch (const std::exception &e) {
current_path = utils::path::combine(current_path, {paths.at(idx)}); utils::error::handle_exception(function_name, e);
auto status = mkdir(current_path.c_str(), S_IRWXU); } catch (...) {
ret = ((status == 0) || (errno == EEXIST)); utils::error::handle_exception(function_name);
} }
return ret; return false;
#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);
} }
#if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_JSON)

View File

@ -21,35 +21,384 @@
*/ */
#include "utils/file.hpp" #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 { 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 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 { 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> { 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 {}; 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::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 } // namespace repertory::utils::file

View File

@ -32,8 +32,6 @@ void enc_file::close() {}
void enc_file::flush() const {} 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::move_to(std::string_view path) -> bool {}
auto enc_file::read(unsigned char *data, std::size_t to_read, 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, auto enc_file::write(const unsigned char *data, std::size_t to_write,
std::size_t offset, std::size_t *total_written) -> bool {} 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 } // namespace repertory::utils::file
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)

View File

@ -27,6 +27,41 @@
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/time.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 { namespace repertory::utils::file {
// auto file::attach_file(native_handle handle, // auto file::attach_file(native_handle handle,
// bool read_only) -> fs_file_t { // 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 { 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) #if defined(_WIN32)
recur_mutex_lock lock{*mtx_}; recur_mutex_lock lock{*mtx_};
#endif // defined(_WIN32) #endif // defined(_WIN32)
try { return i_fs_item::get_time(type);
#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;
} }
auto file::move_to(std::string_view path) -> bool { 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; 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 constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__), static_cast<const char *>(__FUNCTION__),
}; };
@ -491,6 +472,6 @@ auto file::size() const -> std::uint64_t {
utils::error::handle_exception(function_name); utils::error::handle_exception(function_name);
} }
return 0U; return std::nullopt;
} }
} // namespace repertory::utils::file } // namespace repertory::utils::file

View File

@ -201,19 +201,9 @@ auto smb_directory::create_file(std::string_view file_name,
return nullptr; 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>( return std::make_unique<smb_file>(
smb_stat_get(st.get(), SMB_STAT_ATIME), fd, smb_create_smb_path(path_, std::string{rel_path}), session_,
smb_stat_get(st.get(), SMB_STAT_CTIME), fd, share_name_, tid_);
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));
} catch (const std::exception &e) { } catch (const std::exception &e) {
utils::error::handle_exception(function_name, e); utils::error::handle_exception(function_name, e);
} catch (...) { } catch (...) {
@ -361,12 +351,8 @@ auto smb_directory::get_file(std::string_view path) const -> fs_file_t {
} }
return std::make_unique<smb_file>( return std::make_unique<smb_file>(
smb_stat_get(st.get(), SMB_STAT_ATIME), std::nullopt, smb_create_smb_path(path_, std::string{rel_path}),
smb_stat_get(st.get(), SMB_STAT_CTIME), std::nullopt, session_, share_name_, tid_);
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));
} catch (const std::exception &e) { } catch (const std::exception &e) {
utils::error::handle_exception(function_name, e); utils::error::handle_exception(function_name, e);
} catch (...) { } catch (...) {
@ -404,11 +390,8 @@ auto smb_directory::get_files() const -> std::vector<fs_file_t> {
std::string name{smb_stat_name(st)}; std::string name{smb_stat_name(st)};
ret.emplace_back(std::make_unique<smb_file>( 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_create_smb_path(path_, name), session_, share_name_,
std::nullopt, smb_stat_get(st, SMB_STAT_MTIME), tid_));
smb_create_smb_path(path_, name), session_, share_name_,
smb_stat_get(st, SMB_STAT_SIZE), tid_,
smb_stat_get(st, SMB_STAT_WTIME)));
} }
return ret; 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>( 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_create_smb_path(path_, name), session_, share_name_,
std::nullopt, smb_stat_get(st, SMB_STAT_MTIME), tid_));
smb_create_smb_path(path_, name), session_, share_name_, tid_,
smb_stat_get(st, SMB_STAT_SIZE), smb_stat_get(st, SMB_STAT_WTIME)));
} }
return ret; return ret;
@ -478,44 +459,6 @@ auto smb_directory::get_items() const -> std::vector<fs_item_t> {
return {}; 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 { auto smb_directory::move_to(std::string_view new_path) -> bool {
static constexpr const std::string_view function_name{ static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__), static_cast<const char *>(__FUNCTION__),

View File

@ -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 { auto smb_file::move_to(std::string_view new_path) -> bool {
static constexpr const std::string_view function_name{ static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__), static_cast<const char *>(__FUNCTION__),
@ -259,6 +298,32 @@ auto smb_file::remove() -> bool {
return false; 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 { auto smb_file::truncate(std::size_t size) -> bool {
static constexpr const std::string_view function_name{ static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__), static_cast<const char *>(__FUNCTION__),

View File

@ -39,8 +39,6 @@ void thread_file::close() {}
void thread_file::flush() const {} 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::move_to(std::string_view path) -> bool {}
auto thread_file::read(unsigned char *data, std::size_t to_read, 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 offset,
std::size_t *total_written) -> bool {} 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 } // namespace repertory::utils::file

View File

@ -148,7 +148,7 @@ auto find_program_in_path(const std::string &name_without_extension)
for (auto &&extension : extension_list) { for (auto &&extension : extension_list) {
auto exec_path = combine( auto exec_path = combine(
search_path, {name_without_extension + std::string{extension}}); 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; found_items[name_without_extension] = exec_path;
return exec_path; return exec_path;
} }

View File

@ -34,20 +34,6 @@ auto filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t {
} }
#endif // defined(_WIN32) #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) { void get_local_time_now(struct tm &local_time) {
std::memset(&local_time, 0, sizeof(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 { auto get_time_now() -> std::uint64_t {
#if defined(_WIN32) #if defined(_WIN32)
return static_cast<std::uint64_t>( return static_cast<std::uint64_t>(_time64(nullptr));
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())); #else // !defined(_WIN32)
return static_cast<std::uint64_t>(time(nullptr));
#endif // defined(_WIN32) #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) #if defined(_WIN32)

View File

@ -41,7 +41,8 @@ static void delete_generated_files() {
generated_files.clear(); generated_files.clear();
if (parent_path.has_value()) { 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}); auto path = utils::path::combine("/tmp", {temp});
#endif // defined(_WIN32) #endif // defined(_WIN32)
if (not utils::file::is_directory(path)) { if (not utils::file::directory(path).exists()) {
EXPECT_TRUE(utils::file::create_directories(path)); EXPECT_TRUE(utils::file::directory{path}.create_directory());
} }
return path; return path;

View File

@ -29,13 +29,14 @@ namespace repertory {
TEST(utils_file, can_create_file) { TEST(utils_file, can_create_file) {
for (auto idx = 0U; idx < file_type_count; ++idx) { for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file"); 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) auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path); : utils::file::thread_file::open_or_create_file(path);
EXPECT_TRUE(*file); 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 auto file = idx == 0U
? utils::file::file::open_or_create_file(path, true) ? utils::file::file::open_or_create_file(path, true)
: utils::file::thread_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); EXPECT_TRUE(*file);
std::size_t bytes_written{}; std::size_t bytes_written{};
EXPECT_FALSE(file->write(reinterpret_cast<const unsigned char *>("0"), 1U, EXPECT_FALSE(file->write(reinterpret_cast<const unsigned char *>("0"), 1U,