diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index 6675d49c..8f65a9c9 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -33,6 +33,13 @@ namespace repertory::utils::file { [[nodiscard]] auto change_to_process_directory() -> bool; +// INFO: has test +[[nodiscard]] auto create_temp_name(std::string_view file_part) -> std::string; + +// INFO: has test +[[nodiscard]] auto +create_temp_name(std::wstring_view file_part) -> std::wstring; + // INFO: has test [[nodiscard]] inline auto directory_exists_in_path(std::string_view path, @@ -52,9 +59,11 @@ file_exists_in_path(std::string_view path, std::string_view file_name) -> bool; file_exists_in_path(std::wstring_view path, std::wstring_view file_name) -> bool; +// INFO: has test [[nodiscard]] auto get_free_drive_space(std::string_view path) -> std::optional; +// INFO: has test [[nodiscard]] auto get_free_drive_space(std::wstring_view path) -> std::optional; @@ -64,15 +73,19 @@ get_free_drive_space(std::wstring_view path) -> std::optional; [[nodiscard]] auto get_time(std::wstring_view path, time_type type) -> std::optional; +// INFO: has test [[nodiscard]] auto get_times(std::string_view path) -> std::optional; +// INFO: has test [[nodiscard]] auto get_times(std::wstring_view path) -> std::optional; +// INFO: has test [[nodiscard]] auto get_total_drive_space(std::string_view path) -> std::optional; +// INFO: has test [[nodiscard]] auto get_total_drive_space(std::wstring_view path) -> std::optional; diff --git a/support/src/utils/file.cpp b/support/src/utils/file.cpp index b9f7b04d..66634de6 100644 --- a/support/src/utils/file.cpp +++ b/support/src/utils/file.cpp @@ -21,6 +21,7 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/encryption.hpp" #include "utils/error.hpp" #include "utils/path.hpp" @@ -74,6 +75,31 @@ auto change_to_process_directory() -> bool { return false; } +auto create_temp_name(std::string_view file_part) -> std::string { + std::array data{ + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + }; + + auto name = std::string{file_part} + '_'; + for (auto &&val : data) { + name += std::to_string(val); + } + + return name; +} + +auto create_temp_name(std::wstring_view file_part) -> std::wstring { + return utils::string::from_utf8( + create_temp_name(utils::string::to_utf8(file_part))); +} + auto get_free_drive_space(std::string_view path) -> std::optional { static constexpr const std::string_view function_name{ @@ -225,6 +251,7 @@ auto get_total_drive_space(std::string_view path) std::string{path} + '|' + std::to_string(utils::get_last_error_code())); } + return li.QuadPart; #endif // defined(_WIN32) diff --git a/support/test/src/test.cpp b/support/test/src/test.cpp index 050fad72..46f6c315 100644 --- a/support/test/src/test.cpp +++ b/support/test/src/test.cpp @@ -118,8 +118,7 @@ auto get_test_input_dir() -> std::string { auto get_test_output_dir() -> std::string { static auto test_path = ([]() -> std::string { - std::string name{"project_test_XXXXXX"}; - std::string temp{mktemp(name.data())}; + auto temp = utils::file::create_temp_name("project_test"); #if defined(_WIN32) auto path = utils::path::combine("%TEMP%", {temp}); diff --git a/support/test/src/utils/file_test.cpp b/support/test/src/utils/file_test.cpp index 569b75db..1a94000e 100644 --- a/support/test/src/utils/file_test.cpp +++ b/support/test/src/utils/file_test.cpp @@ -263,7 +263,7 @@ TEST(utils_file, smb_parent_is_not_same) { } #endif // defined(PROJECT_ENABLE_LIBDSM) -TEST(util_file, directory_exists_in_path) { +TEST(utils_file, directory_exists_in_path) { auto &test_dir = test::generate_test_directory(); EXPECT_FALSE( utils::file::directory_exists_in_path(test_dir.get_path(), "moose")); @@ -293,7 +293,7 @@ TEST(util_file, directory_exists_in_path) { } } -TEST(util_file, file_exists_in_path) { +TEST(utils_file, file_exists_in_path) { auto &test_dir = test::generate_test_directory(); EXPECT_FALSE( utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt")); @@ -323,4 +323,104 @@ TEST(util_file, file_exists_in_path) { utils::string::from_utf8(test_dir.get_path()), L"moose.txt")); } } + +TEST(utils_file, get_free_drive_space) { +#if defined(_WIN32) + auto space = utils::file::get_free_drive_space("C:"); + auto space2 = utils::file::get_free_drive_space(L"C:"); +#else // defined(_WIN32) + auto space = utils::file::get_free_drive_space("/"); + auto space2 = utils::file::get_free_drive_space(L"/"); +#endif // !defined(_WIN32) + + EXPECT_TRUE(space.has_value()); + EXPECT_LT(0U, space.value()); + + EXPECT_TRUE(space2.has_value()); + EXPECT_EQ(space.value(), space2.value()); +} + +TEST(utils_file, get_free_drive_space_fails_for_bad_path) { + std::string name{"free_drive_space_test_XXXXXX"}; + auto temp = utils::file::create_temp_name("free_drive_space_test"); + + auto space = utils::file::get_free_drive_space(temp); + EXPECT_FALSE(space.has_value()); +} + +TEST(utils_file, get_total_drive_space) { +#if defined(_WIN32) + auto space = utils::file::get_total_drive_space("C:"); + auto space2 = utils::file::get_total_drive_space(L"C:"); +#else // defined(_WIN32) + auto space = utils::file::get_total_drive_space("/"); + auto space2 = utils::file::get_total_drive_space(L"/"); +#endif // !defined(_WIN32) + + EXPECT_TRUE(space.has_value()); + EXPECT_LT(0U, space.value()); + + EXPECT_TRUE(space2.has_value()); + EXPECT_EQ(space.value(), space2.value()); +} + +TEST(utils_file, create_temp_name) { + { + auto temp = utils::file::create_temp_name("test_temp"); + EXPECT_EQ(18U, temp.size()); + + auto temp2 = utils::file::create_temp_name("test_temp"); + EXPECT_STRNE(temp.c_str(), temp2.c_str()); + + EXPECT_TRUE(utils::string::begins_with(temp, "test_temp_")); + } + + { + auto temp = utils::file::create_temp_name(L"test_temp"); + EXPECT_EQ(18U, temp.size()); + + auto temp2 = utils::file::create_temp_name(L"test_temp"); + EXPECT_STRNE(temp.c_str(), temp2.c_str()); + + EXPECT_TRUE(utils::string::begins_with(temp, L"test_temp_")); + } +} + +TEST(utils_file, get_total_drive_space_fails_for_bad_path) { + auto temp = utils::file::create_temp_name("total_drive_space_test"); + auto space = utils::file::get_total_drive_space(temp); + EXPECT_FALSE(space.has_value()); +} + +TEST(utils_file, get_times) { + { + auto times = utils::file::get_times(__FILE__); + EXPECT_TRUE(times.has_value()); + EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); + EXPECT_LT(0U, times->get(utils::file::time_type::created)); + EXPECT_LT(0U, times->get(utils::file::time_type::modified)); + EXPECT_LT(0U, times->get(utils::file::time_type::written)); + } + + { + auto times = utils::file::get_times(utils::string::from_utf8(__FILE__)); + EXPECT_TRUE(times.has_value()); + EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); + EXPECT_LT(0U, times->get(utils::file::time_type::created)); + EXPECT_LT(0U, times->get(utils::file::time_type::modified)); + EXPECT_LT(0U, times->get(utils::file::time_type::written)); + } +} + +TEST(utils_file, get_times_fails_if_not_found) { + auto temp = utils::path::combine(".", {"get_times_test"}); + auto times = utils::file::get_times(temp); + EXPECT_FALSE(times.has_value()); +} + +TEST(utils_file, get_time) {} + +TEST(utils_file, get_time_fails_if_not_found) { + auto temp = utils::path::combine(".", {"get_times_test"}); +} } // namespace repertory