Compare commits
3 Commits
4620dc72fe
...
efe61762b5
Author | SHA1 | Date | |
---|---|---|---|
efe61762b5 | |||
3b72e971e6 | |||
ad01aa72b2 |
103
repertory/librepertory/include/file_manager/direct_open_file.hpp
Normal file
103
repertory/librepertory/include/file_manager/direct_open_file.hpp
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef REPERTORY_INCLUDE_FILE_MANAGER_DIRECT_OPEN_FILE_HPP_
|
||||
#define REPERTORY_INCLUDE_FILE_MANAGER_DIRECT_OPEN_FILE_HPP_
|
||||
|
||||
#include "file_manager/open_file_base.hpp"
|
||||
|
||||
#include "types/repertory.hpp"
|
||||
|
||||
namespace repertory {
|
||||
class i_provider;
|
||||
class i_upload_manager;
|
||||
|
||||
class direct_open_file final : public open_file_base {
|
||||
public:
|
||||
direct_open_file(std::uint64_t chunk_size, std::uint8_t chunk_timeout,
|
||||
filesystem_item fsi, i_provider &provider);
|
||||
|
||||
~direct_open_file() override;
|
||||
|
||||
public:
|
||||
direct_open_file() = delete;
|
||||
direct_open_file(const direct_open_file &) noexcept = delete;
|
||||
direct_open_file(direct_open_file &&) noexcept = delete;
|
||||
auto operator=(direct_open_file &&) noexcept -> direct_open_file & = delete;
|
||||
auto
|
||||
operator=(const direct_open_file &) noexcept -> direct_open_file & = delete;
|
||||
|
||||
private:
|
||||
std::size_t total_chunks_;
|
||||
|
||||
protected:
|
||||
[[nodiscard]] auto is_download_complete() const -> bool override {
|
||||
return false;
|
||||
}
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto is_complete() const -> bool override { return true; }
|
||||
|
||||
[[nodiscard]] auto is_write_supported() const -> bool override {
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto
|
||||
get_read_state() const -> boost::dynamic_bitset<> override {
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] auto
|
||||
get_read_state(std::size_t /* chunk */) const -> bool override {
|
||||
return false;
|
||||
}
|
||||
[[nodiscard]] auto get_total_chunks() const -> std::uint64_t {
|
||||
return total_chunks_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto native_operation(native_operation_callback /* callback */)
|
||||
-> api_error override {
|
||||
return api_error::not_supported;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto native_operation(std::uint64_t /* new_file_size */,
|
||||
native_operation_callback /*callback*/)
|
||||
-> api_error override {
|
||||
return api_error::not_supported;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto read(std::size_t read_size, std::uint64_t read_offset,
|
||||
data_buffer &data) -> api_error override;
|
||||
|
||||
[[nodiscard]] auto resize(std::uint64_t /*size*/) -> api_error override {
|
||||
return api_error::not_supported;
|
||||
}
|
||||
|
||||
void set_api_path(const std::string &api_path) override;
|
||||
|
||||
[[nodiscard]] auto write(std::uint64_t, const data_buffer &,
|
||||
std::size_t &) -> api_error override {
|
||||
return api_error::not_supported;
|
||||
}
|
||||
};
|
||||
} // namespace repertory
|
||||
|
||||
#endif // REPERTORY_INCLUDE_FILE_MANAGER_DIRECT_OPEN_FILE_HPP_
|
@ -98,7 +98,7 @@ public:
|
||||
|
||||
[[nodiscard]] auto is_complete() const -> bool override;
|
||||
|
||||
auto is_write_supported() const -> bool override { return true; }
|
||||
[[nodiscard]] auto is_write_supported() const -> bool override { return true; }
|
||||
|
||||
[[nodiscard]] auto native_operation(native_operation_callback callback)
|
||||
-> api_error override;
|
||||
|
@ -130,7 +130,7 @@ private:
|
||||
protected:
|
||||
[[nodiscard]] auto do_io(std::function<api_error()> action) -> api_error;
|
||||
|
||||
virtual auto is_download_complete() const -> bool = 0;
|
||||
[[nodiscard]] virtual auto is_download_complete() const -> bool = 0;
|
||||
|
||||
void reset_timeout();
|
||||
|
||||
@ -157,17 +157,17 @@ public:
|
||||
|
||||
[[nodiscard]] auto get_handles() const -> std::vector<std::uint64_t> override;
|
||||
|
||||
[[nodiscard]] auto get_open_data()
|
||||
-> std::map<std::uint64_t, open_file_data> & override;
|
||||
[[nodiscard]] auto
|
||||
get_open_data() -> std::map<std::uint64_t, open_file_data> & override;
|
||||
|
||||
[[nodiscard]] auto get_open_data() const
|
||||
-> const std::map<std::uint64_t, open_file_data> & override;
|
||||
|
||||
[[nodiscard]] auto get_open_data(std::uint64_t handle)
|
||||
-> open_file_data & override;
|
||||
[[nodiscard]] auto
|
||||
get_open_data(std::uint64_t handle) -> open_file_data & override;
|
||||
|
||||
[[nodiscard]] auto get_open_data(std::uint64_t handle) const
|
||||
-> const open_file_data & override;
|
||||
[[nodiscard]] auto
|
||||
get_open_data(std::uint64_t handle) const -> const open_file_data & override;
|
||||
|
||||
[[nodiscard]] auto get_open_file_count() const -> std::size_t override;
|
||||
|
||||
|
@ -46,8 +46,8 @@ public:
|
||||
ring_buffer_open_file() = delete;
|
||||
ring_buffer_open_file(const ring_buffer_open_file &) noexcept = delete;
|
||||
ring_buffer_open_file(ring_buffer_open_file &&) noexcept = delete;
|
||||
auto operator=(ring_buffer_open_file &&) noexcept
|
||||
-> ring_buffer_open_file & = delete;
|
||||
auto operator=(ring_buffer_open_file &&) noexcept -> ring_buffer_open_file & =
|
||||
delete;
|
||||
auto operator=(const ring_buffer_open_file &) noexcept
|
||||
-> ring_buffer_open_file & = delete;
|
||||
|
||||
@ -72,7 +72,9 @@ private:
|
||||
void reverse_reader_thread(std::size_t count);
|
||||
|
||||
protected:
|
||||
auto is_download_complete() const -> bool override;
|
||||
[[nodiscard]] auto is_download_complete() const -> bool override {
|
||||
return false;
|
||||
}
|
||||
|
||||
public:
|
||||
void forward(std::size_t count);
|
||||
@ -99,10 +101,12 @@ public:
|
||||
|
||||
[[nodiscard]] auto is_complete() const -> bool override { return true; }
|
||||
|
||||
auto is_write_supported() const -> bool override { return false; }
|
||||
[[nodiscard]] auto is_write_supported() const -> bool override {
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto native_operation(native_operation_callback callback)
|
||||
-> api_error override;
|
||||
[[nodiscard]] auto
|
||||
native_operation(native_operation_callback callback) -> api_error override;
|
||||
|
||||
[[nodiscard]] auto native_operation(std::uint64_t, native_operation_callback)
|
||||
-> api_error override {
|
||||
@ -122,8 +126,8 @@ public:
|
||||
|
||||
void set_api_path(const std::string &api_path) override;
|
||||
|
||||
[[nodiscard]] auto write(std::uint64_t, const data_buffer &, std::size_t &)
|
||||
-> api_error override {
|
||||
[[nodiscard]] auto write(std::uint64_t, const data_buffer &,
|
||||
std::size_t &) -> api_error override {
|
||||
return api_error::not_supported;
|
||||
}
|
||||
};
|
||||
|
63
repertory/librepertory/src/file_manager/direct_open_file.cpp
Normal file
63
repertory/librepertory/src/file_manager/direct_open_file.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "file_manager/direct_open_file.hpp"
|
||||
|
||||
#include "file_manager/open_file_base.hpp"
|
||||
#include "providers/i_provider.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
#include "utils/common.hpp"
|
||||
|
||||
namespace repertory {
|
||||
direct_open_file::direct_open_file(std::uint64_t chunk_size,
|
||||
std::uint8_t chunk_timeout,
|
||||
filesystem_item fsi, i_provider &provider)
|
||||
: open_file_base(chunk_size, chunk_timeout, fsi, provider),
|
||||
total_chunks_(static_cast<std::size_t>(
|
||||
utils::divide_with_ceiling(fsi.size, chunk_size))) {}
|
||||
|
||||
direct_open_file::~direct_open_file() { close(); }
|
||||
|
||||
auto direct_open_file::read(std::size_t read_size, std::uint64_t read_offset,
|
||||
data_buffer &data) -> api_error {
|
||||
if (fsi_.directory) {
|
||||
return api_error::invalid_operation;
|
||||
}
|
||||
|
||||
reset_timeout();
|
||||
|
||||
read_size = utils::calculate_read_size(fsi_.size, read_size, read_offset);
|
||||
if (read_size == 0U) {
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
stop_type stop_requested{false};
|
||||
auto res = provider_.read_file_bytes(fsi_.api_path, read_size, read_offset,
|
||||
data, stop_requested);
|
||||
reset_timeout();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void direct_open_file::set_api_path(const std::string &api_path) {
|
||||
open_file_base::set_api_path(api_path);
|
||||
}
|
||||
} // namespace repertory
|
@ -24,6 +24,7 @@
|
||||
#include "app_config.hpp"
|
||||
#include "db/file_mgr_db.hpp"
|
||||
#include "file_manager/cache_size_mgr.hpp"
|
||||
#include "file_manager/direct_open_file.hpp"
|
||||
#include "file_manager/events.hpp"
|
||||
#include "file_manager/open_file.hpp"
|
||||
#include "file_manager/open_file_base.hpp"
|
||||
@ -403,12 +404,56 @@ auto file_manager::open(
|
||||
}
|
||||
|
||||
if (not closeable_file) {
|
||||
const auto get_download_type = [this,
|
||||
&fsi](download_type type) -> download_type {
|
||||
if (fsi.size == 0U) {
|
||||
return download_type::fallback;
|
||||
}
|
||||
|
||||
if (type == download_type::direct) {
|
||||
return type;
|
||||
}
|
||||
|
||||
if (type == download_type::fallback) {
|
||||
auto free_space =
|
||||
utils::file::get_free_drive_space(config_.get_cache_directory());
|
||||
if (fsi.size < free_space) {
|
||||
return download_type::fallback;
|
||||
}
|
||||
}
|
||||
|
||||
auto free_space = utils::file::get_free_drive_space(
|
||||
utils::path::combine(config_.get_data_directory(), {"buffer"}));
|
||||
if (config_.get_ring_buffer_file_size() < free_space) {
|
||||
return download_type::ring_buffer;
|
||||
}
|
||||
|
||||
return download_type::direct;
|
||||
};
|
||||
|
||||
auto chunk_timeout = config_.get_enable_download_timeout()
|
||||
? config_.get_download_timeout_secs()
|
||||
: 0U;
|
||||
switch (get_download_type(config_.get_preferred_download_type())) {
|
||||
case repertory::download_type::direct: {
|
||||
closeable_file = std::make_shared<direct_open_file>(
|
||||
utils::encryption::encrypting_reader::get_data_chunk_size(),
|
||||
chunk_timeout, fsi, provider_);
|
||||
} break;
|
||||
|
||||
case repertory::download_type::ring_buffer: {
|
||||
closeable_file = std::make_shared<ring_buffer_open_file>(
|
||||
utils::path::combine(config_.get_data_directory(), {"buffer"}),
|
||||
utils::encryption::encrypting_reader::get_data_chunk_size(),
|
||||
chunk_timeout, fsi, provider_);
|
||||
} break;
|
||||
|
||||
default: {
|
||||
closeable_file = std::make_shared<open_file>(
|
||||
utils::encryption::encrypting_reader::get_data_chunk_size(),
|
||||
config_.get_enable_download_timeout()
|
||||
? config_.get_download_timeout_secs()
|
||||
: 0U,
|
||||
fsi, provider_, *this);
|
||||
chunk_timeout, fsi, provider_, *this);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
open_file_lookup_[api_path] = closeable_file;
|
||||
create_and_add_handle(closeable_file);
|
||||
|
@ -52,7 +52,7 @@ ring_buffer_open_file::ring_buffer_open_file(std::string buffer_directory,
|
||||
: open_file_base(chunk_size, chunk_timeout, fsi, provider),
|
||||
ring_state_(ring_size),
|
||||
total_chunks_(static_cast<std::size_t>(
|
||||
utils::divide_with_ceiling(fsi.size, chunk_size_))) {
|
||||
utils::divide_with_ceiling(fsi.size, chunk_size))) {
|
||||
if ((ring_size % 2U) != 0U) {
|
||||
throw std::runtime_error("ring size must be a multiple of 2");
|
||||
}
|
||||
@ -194,10 +194,6 @@ auto ring_buffer_open_file::get_read_state(std::size_t chunk) const -> bool {
|
||||
return not ring_state_[chunk % ring_state_.size()];
|
||||
}
|
||||
|
||||
auto ring_buffer_open_file::is_download_complete() const -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto ring_buffer_open_file::native_operation(
|
||||
i_open_file::native_operation_callback callback) -> api_error {
|
||||
return do_io([&]() -> api_error { return callback(nf_->get_handle()); });
|
||||
|
273
repertory/repertory_test/src/direct_open_file_test.cpp
Normal file
273
repertory/repertory_test/src/direct_open_file_test.cpp
Normal file
@ -0,0 +1,273 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "test_common.hpp"
|
||||
|
||||
#include "file_manager/direct_open_file.hpp"
|
||||
#include "mocks/mock_provider.hpp"
|
||||
|
||||
namespace {
|
||||
constexpr const std::size_t test_chunk_size{1024U};
|
||||
} // namespace
|
||||
|
||||
namespace repertory {
|
||||
TEST(direct_open_file, read_full_file) {
|
||||
auto &source_file = test::create_random_file(test_chunk_size * 32U);
|
||||
|
||||
auto dest_path = test::generate_test_file_name("direct_open_file");
|
||||
|
||||
mock_provider provider;
|
||||
EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false));
|
||||
|
||||
filesystem_item fsi;
|
||||
fsi.api_path = "/test.txt";
|
||||
fsi.directory = false;
|
||||
fsi.size = test_chunk_size * 32U;
|
||||
|
||||
EXPECT_CALL(provider, read_file_bytes)
|
||||
.WillRepeatedly([&source_file](const std::string & /* api_path */,
|
||||
std::size_t size, std::uint64_t offset,
|
||||
data_buffer &data,
|
||||
stop_type &stop_requested) -> api_error {
|
||||
EXPECT_FALSE(stop_requested);
|
||||
std::size_t bytes_read{};
|
||||
data.resize(size);
|
||||
auto ret = source_file.read(data, offset, &bytes_read)
|
||||
? api_error::success
|
||||
: api_error::os_error;
|
||||
EXPECT_EQ(bytes_read, data.size());
|
||||
return ret;
|
||||
});
|
||||
{
|
||||
direct_open_file file(test_chunk_size, 30U, fsi, provider);
|
||||
|
||||
auto dest_file = utils::file::file::open_or_create_file(dest_path);
|
||||
EXPECT_TRUE(dest_file);
|
||||
|
||||
auto to_read{fsi.size};
|
||||
std::size_t chunk{0U};
|
||||
while (to_read > 0U) {
|
||||
data_buffer data{};
|
||||
EXPECT_EQ(api_error::success,
|
||||
file.read(test_chunk_size, chunk * test_chunk_size, data));
|
||||
|
||||
std::size_t bytes_written{};
|
||||
EXPECT_TRUE(
|
||||
dest_file->write(data, chunk * test_chunk_size, &bytes_written));
|
||||
++chunk;
|
||||
to_read -= data.size();
|
||||
}
|
||||
dest_file->close();
|
||||
source_file.close();
|
||||
|
||||
auto hash1 = utils::file::file(source_file.get_path()).sha256();
|
||||
auto hash2 = utils::file::file(dest_path).sha256();
|
||||
|
||||
EXPECT_TRUE(hash1.has_value());
|
||||
EXPECT_TRUE(hash2.has_value());
|
||||
if (hash1.has_value() && hash2.has_value()) {
|
||||
EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(direct_open_file, read_full_file_in_reverse) {
|
||||
auto &source_file = test::create_random_file(test_chunk_size * 32U);
|
||||
|
||||
auto dest_path = test::generate_test_file_name("direct_open_file");
|
||||
|
||||
mock_provider provider;
|
||||
EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false));
|
||||
|
||||
filesystem_item fsi;
|
||||
fsi.api_path = "/test.txt";
|
||||
fsi.directory = false;
|
||||
fsi.size = test_chunk_size * 32U;
|
||||
|
||||
EXPECT_CALL(provider, read_file_bytes)
|
||||
.WillRepeatedly([&source_file](const std::string & /* api_path */,
|
||||
std::size_t size, std::uint64_t offset,
|
||||
data_buffer &data,
|
||||
stop_type &stop_requested) -> api_error {
|
||||
EXPECT_FALSE(stop_requested);
|
||||
std::size_t bytes_read{};
|
||||
data.resize(size);
|
||||
auto ret = source_file.read(data, offset, &bytes_read)
|
||||
? api_error::success
|
||||
: api_error::os_error;
|
||||
EXPECT_EQ(bytes_read, data.size());
|
||||
return ret;
|
||||
});
|
||||
{
|
||||
direct_open_file file(test_chunk_size, 30U, fsi, provider);
|
||||
|
||||
auto dest_file = utils::file::file::open_or_create_file(dest_path);
|
||||
EXPECT_TRUE(dest_file);
|
||||
|
||||
auto to_read{fsi.size};
|
||||
std::size_t chunk{file.get_total_chunks() - 1U};
|
||||
while (to_read > 0U) {
|
||||
data_buffer data{};
|
||||
EXPECT_EQ(api_error::success,
|
||||
file.read(test_chunk_size, chunk * test_chunk_size, data));
|
||||
|
||||
std::size_t bytes_written{};
|
||||
EXPECT_TRUE(
|
||||
dest_file->write(data, chunk * test_chunk_size, &bytes_written));
|
||||
--chunk;
|
||||
to_read -= data.size();
|
||||
}
|
||||
dest_file->close();
|
||||
source_file.close();
|
||||
|
||||
auto hash1 = utils::file::file(source_file.get_path()).sha256();
|
||||
auto hash2 = utils::file::file(dest_path).sha256();
|
||||
|
||||
EXPECT_TRUE(hash1.has_value());
|
||||
EXPECT_TRUE(hash2.has_value());
|
||||
if (hash1.has_value() && hash2.has_value()) {
|
||||
EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(direct_open_file, read_full_file_in_partial_chunks) {
|
||||
auto &source_file = test::create_random_file(test_chunk_size * 32U);
|
||||
|
||||
auto dest_path = test::generate_test_file_name("test");
|
||||
|
||||
mock_provider provider;
|
||||
EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false));
|
||||
|
||||
filesystem_item fsi;
|
||||
fsi.directory = false;
|
||||
fsi.api_path = "/test.txt";
|
||||
fsi.size = test_chunk_size * 32U;
|
||||
|
||||
EXPECT_CALL(provider, read_file_bytes)
|
||||
.WillRepeatedly([&source_file](const std::string & /* api_path */,
|
||||
std::size_t size, std::uint64_t offset,
|
||||
data_buffer &data,
|
||||
stop_type &stop_requested) -> api_error {
|
||||
EXPECT_FALSE(stop_requested);
|
||||
std::size_t bytes_read{};
|
||||
data.resize(size);
|
||||
auto ret = source_file.read(data, offset, &bytes_read)
|
||||
? api_error::success
|
||||
: api_error::os_error;
|
||||
EXPECT_EQ(bytes_read, data.size());
|
||||
return ret;
|
||||
});
|
||||
{
|
||||
direct_open_file file(test_chunk_size, 30U, fsi, provider);
|
||||
|
||||
auto dest_file = utils::file::file::open_or_create_file(dest_path);
|
||||
EXPECT_TRUE(dest_file);
|
||||
|
||||
auto total_read{std::uint64_t(0U)};
|
||||
while (total_read < fsi.size) {
|
||||
data_buffer data{};
|
||||
EXPECT_EQ(api_error::success, file.read(3U, total_read, data));
|
||||
|
||||
std::size_t bytes_written{};
|
||||
EXPECT_TRUE(dest_file->write(data.data(), data.size(), total_read,
|
||||
&bytes_written));
|
||||
total_read += data.size();
|
||||
}
|
||||
dest_file->close();
|
||||
source_file.close();
|
||||
|
||||
auto hash1 = utils::file::file(source_file.get_path()).sha256();
|
||||
auto hash2 = utils::file::file(dest_path).sha256();
|
||||
|
||||
EXPECT_TRUE(hash1.has_value());
|
||||
EXPECT_TRUE(hash2.has_value());
|
||||
if (hash1.has_value() && hash2.has_value()) {
|
||||
EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(direct_open_file, read_full_file_in_partial_chunks_in_reverse) {
|
||||
auto &source_file = test::create_random_file(test_chunk_size * 32U);
|
||||
|
||||
auto dest_path = test::generate_test_file_name("direct_open_file");
|
||||
|
||||
mock_provider provider;
|
||||
EXPECT_CALL(provider, is_read_only()).WillRepeatedly(Return(false));
|
||||
|
||||
filesystem_item fsi;
|
||||
fsi.directory = false;
|
||||
fsi.api_path = "/test.txt";
|
||||
fsi.size = test_chunk_size * 32U;
|
||||
|
||||
EXPECT_CALL(provider, read_file_bytes)
|
||||
.WillRepeatedly([&source_file](const std::string & /* api_path */,
|
||||
std::size_t size, std::uint64_t offset,
|
||||
data_buffer &data,
|
||||
stop_type &stop_requested) -> api_error {
|
||||
EXPECT_FALSE(stop_requested);
|
||||
std::size_t bytes_read{};
|
||||
data.resize(size);
|
||||
auto ret = source_file.read(data, offset, &bytes_read)
|
||||
? api_error::success
|
||||
: api_error::os_error;
|
||||
EXPECT_EQ(bytes_read, data.size());
|
||||
return ret;
|
||||
});
|
||||
{
|
||||
direct_open_file file(test_chunk_size, 30U, fsi, provider);
|
||||
|
||||
auto dest_file = utils::file::file::open_or_create_file(dest_path);
|
||||
EXPECT_TRUE(dest_file);
|
||||
|
||||
std::uint64_t total_read{0U};
|
||||
auto read_size{3U};
|
||||
|
||||
while (total_read < fsi.size) {
|
||||
auto offset = fsi.size - total_read - read_size;
|
||||
auto remain = fsi.size - total_read;
|
||||
|
||||
data_buffer data{};
|
||||
EXPECT_EQ(api_error::success,
|
||||
file.read(static_cast<std::size_t>(
|
||||
std::min(remain, std::uint64_t(read_size))),
|
||||
(remain >= read_size) ? offset : 0U, data));
|
||||
|
||||
std::size_t bytes_written{};
|
||||
EXPECT_TRUE(dest_file->write(data, (remain >= read_size) ? offset : 0U,
|
||||
&bytes_written));
|
||||
total_read += data.size();
|
||||
}
|
||||
dest_file->close();
|
||||
source_file.close();
|
||||
|
||||
auto hash1 = utils::file::file(source_file.get_path()).sha256();
|
||||
auto hash2 = utils::file::file(dest_path).sha256();
|
||||
|
||||
EXPECT_TRUE(hash1.has_value());
|
||||
EXPECT_TRUE(hash2.has_value());
|
||||
if (hash1.has_value() && hash2.has_value()) {
|
||||
EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace repertory
|
Reference in New Issue
Block a user