diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index 07e33b91..62baef4a 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -114,7 +114,8 @@ auto encrypt_provider::create_api_file( file.creation_date = (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; - ft = utils::time::unix_time_to_filetime(utils::time::time64_to_unix_time(buf.st_mtime)); + ft = utils::time::unix_time_to_filetime( + utils::time::time64_to_unix_time(buf.st_mtime)); file.modified_date = (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; #else diff --git a/repertory/repertory_test/src/file_manager_open_file_test.cpp b/repertory/repertory_test/src/file_manager_open_file_test.cpp index b72c5a10..8b68bfb7 100644 --- a/repertory/repertory_test/src/file_manager_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_open_file_test.cpp @@ -196,8 +196,8 @@ TEST(open_file, } TEST(open_file, write_with_incomplete_download) { + const auto source_path = test::generate_test_file_name("test"); auto nf = test::create_random_file(test_chunk_size * 2u); - const auto source_path = nf.get_path().string(); mock_provider mp; mock_upload_manager um; diff --git a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp index bb3ea75e..cc3df4b7 100644 --- a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp @@ -437,10 +437,9 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { auto nf = test::create_random_file(test_chunk_size * 32u); - const auto download_source_path = - test::generate_test_file_name("ring_buffer_open_file"); + const auto download_source_path = nf.get_path().string(); - const auto dest_path = test::generate_test_file_name("ring_buffer_open_file"); + const auto dest_path = test::generate_test_file_name("test"); mock_provider mp; @@ -450,7 +449,7 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { fsi.directory = false; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 32u; - fsi.source_path = test::generate_test_file_name("ring_buffer_open_file"); + fsi.source_path = test::generate_test_file_name("test"); EXPECT_CALL(mp, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, @@ -471,6 +470,8 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { auto nf2 = utils::file::file::open_or_create_file(dest_path); EXPECT_TRUE(nf2); + // EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, + // nf2)); auto total_read = std::uint64_t(0u); @@ -479,7 +480,8 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { EXPECT_EQ(api_error::success, rb.read(3u, total_read, data)); std::size_t bytes_written{}; - EXPECT_TRUE(nf2.write(data, total_read, &bytes_written)); + EXPECT_TRUE( + nf2.write(data.data(), data.size(), total_read, &bytes_written)); total_read += data.size(); } nf2.close(); diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 12a8798d..dcddac37 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -42,13 +42,13 @@ auto file::attach_file(native_handle handle, bool read_only) -> file { static_cast(path.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); #else // !defined(_WIN32) - source_path.resize(PATH_MAX + 1); + path.resize(PATH_MAX + 1); #if defined(__APPLE__) fcntl(handle, F_GETPATH, source_path.data()); #else // !defined(__APPLE__) - readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(), - source_path.data(), source_path.size()); + readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(), path.data(), + path.size()); #endif // defined(__APPLE__) #endif // defined(_WIN32) @@ -87,14 +87,6 @@ auto file::open_file(std::filesystem::path path, bool read_only) -> file { throw std::runtime_error("file not found: " + path.string()); } - if (not read_only) { -#if defined(_WIN32) - _chmod(path.string().c_str(), 0600U); -#else // !defined(_WIN32) - chmod(path.string().c_str(), 0600U); -#endif // defined(_WIN32) - } - #if defined(_WIN32) auto *ptr = _fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO); @@ -121,9 +113,9 @@ auto file::open_or_create_file(std::filesystem::path path, #if defined(_WIN32) int old_mode{}; - _umask_s(0600U, &old_mode); + _umask_s(077, &old_mode); #else // !defined(_WIN32) - auto old_mode = umask(0600U); + auto old_mode = umask(077); #endif // defined(_WIN32) #if defined(_WIN32) @@ -361,7 +353,7 @@ auto file::write(const unsigned char *data, std::size_t to_write, auto bytes_written = fwrite(reinterpret_cast(data), 1U, to_write, file_.get()); if (not feof(file_.get()) && ferror(file_.get())) { - throw std::runtime_error("failed to read file bytes"); + throw std::runtime_error("failed to write file bytes"); } flush(); diff --git a/support/test/src/utils/file_test.cpp b/support/test/src/utils/file_test.cpp index cb7fd81e..65f195d8 100644 --- a/support/test/src/utils/file_test.cpp +++ b/support/test/src/utils/file_test.cpp @@ -22,6 +22,58 @@ #include "test.hpp" namespace repertory { +TEST(utils_file, can_create_file) { + auto path = test::generate_test_file_name("utils_file"); + EXPECT_FALSE(utils::file::is_file(path) || utils::file::is_directory(path)); + + auto file = utils::file::file::open_or_create_file(path); + EXPECT_TRUE(file); + + EXPECT_TRUE(utils::file::is_file(path)); +} + +TEST(utils_file, can_open_file) { + auto path = test::generate_test_file_name("utils_file"); + + { + auto file = utils::file::file::open_or_create_file(path); + EXPECT_TRUE(file); + } + + { + auto file = utils::file::file::open_file(path); + EXPECT_TRUE(file); + } +} + +TEST(utils_file, open_file_fails_if_not_found) { + auto path = test::generate_test_file_name("utils_file"); + + auto file = utils::file::file::open_file(path); + EXPECT_FALSE(file); +} + +TEST(utils_file, write_fails_for_read_only_file) { + auto path = test::generate_test_file_name("utils_file"); + + auto file = utils::file::file::open_or_create_file(path, true); + EXPECT_TRUE(utils::file::is_file(path)); + EXPECT_TRUE(file); + std::size_t bytes_written{}; + EXPECT_FALSE(file.write(reinterpret_cast("0"), 1U, 0U, + &bytes_written)); + EXPECT_EQ(0U, bytes_written); +} + +TEST(utils_file, can_attach_file) { + auto path = test::generate_test_file_name("utils_file"); + auto file = utils::file::file::open_or_create_file(path); + auto file2 = utils::file::file::attach_file(file.get_handle()); + EXPECT_TRUE(file); + EXPECT_TRUE(file2); + EXPECT_EQ(file.get_path(), file2.get_path()); +} + #if defined(PROJECT_ENABLE_JSON) TEST(utils_file, read_and_write_json_file) { auto path = test::generate_test_file_name("utils_file");