diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 9dac9def..1ea60fc3 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -319,9 +319,18 @@ auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset, throw std::runtime_error("failed to seek before read"); } - auto bytes_read = fread(data, 1U, to_read, file_.get()); - if (not feof(file_.get()) && ferror(file_.get())) { - throw std::runtime_error("failed to read file bytes"); + std::size_t bytes_read{0U}; + while (bytes_read != to_read) { + res = fread(&data[bytes_read], 1U, to_read - bytes_read, file_.get()); + if (not feof(file_.get()) && ferror(file_.get())) { + throw std::runtime_error("failed to read file bytes"); + } + + if (res == 0) { + break; + } + + bytes_read += static_cast(res); } if (total_read != nullptr) { @@ -416,10 +425,19 @@ auto file::write(const unsigned char *data, std::size_t to_write, throw std::runtime_error("failed to seek before 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 write file bytes"); + std::size_t bytes_written{0U}; + while (bytes_written != to_write) { + res = fwrite(reinterpret_cast(&data[bytes_written]), 1U, + to_write - bytes_written, file_.get()); + if (not feof(file_.get()) && ferror(file_.get())) { + throw std::runtime_error("failed to write file bytes"); + } + + if (res == 0) { + break; + } + + bytes_written += static_cast(res); } flush(); diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index d920e193..c60394df 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -178,6 +178,10 @@ auto smb_file::read(unsigned char *data, std::size_t to_read, }; try { + if (total_read != nullptr) { + (*total_read) = 0U; + } + if (not fd_.has_value()) { throw std::runtime_error("failed to read file|" + path_ + "|file not open"); @@ -191,15 +195,25 @@ auto smb_file::read(unsigned char *data, std::size_t to_read, std::to_string(res)); } - res = smb_fread(session_.get(), *fd_, data, to_read); - if (res == -1) { - throw std::runtime_error("failed to read file|" + path_ + '|' + - std::to_string(to_read) + '|' + - std::to_string(res)); + std::size_t bytes_read{0U}; + while (bytes_read != to_read) { + res = smb_fread(session_.get(), *fd_, &data[bytes_read], + to_read - bytes_read); + if (res == -1) { + throw std::runtime_error("failed to read file|" + path_ + '|' + + std::to_string(to_read) + '|' + + std::to_string(res)); + } + + if (res == 0) { + break; + } + + bytes_read += static_cast(res); } if (total_read != nullptr) { - (*total_read) = static_cast(res); + (*total_read) = bytes_read; } return true; @@ -269,6 +283,10 @@ auto smb_file::write(const unsigned char *data, std::size_t to_write, }; try { + if (total_written != nullptr) { + (*total_written) = 0U; + } + if (not fd_.has_value()) { throw std::runtime_error("failed to write file|" + path_ + "|file not open"); @@ -282,16 +300,26 @@ auto smb_file::write(const unsigned char *data, std::size_t to_write, std::to_string(res)); } - res = smb_fwrite(session_.get(), *fd_, const_cast(data), - to_write); - if (res == -1) { - throw std::runtime_error("failed to write file|" + path_ + '|' + - std::to_string(to_write) + '|' + - std::to_string(res)); + std::size_t bytes_written{0U}; + while (bytes_written != to_write) { + res = smb_fwrite(session_.get(), *fd_, + const_cast(&data[bytes_written]), + to_write - bytes_written); + if (res == -1) { + throw std::runtime_error("failed to write file|" + path_ + '|' + + std::to_string(to_write) + '|' + + std::to_string(res)); + } + + if (res == 0) { + break; + } + + bytes_written += static_cast(res); } if (total_written != nullptr) { - (*total_written) = static_cast(res); + (*total_written) = bytes_written; } return true;