fix file read
This commit is contained in:
parent
35aa8f5a94
commit
00cfb67b64
@ -27,6 +27,11 @@
|
|||||||
namespace repertory {
|
namespace repertory {
|
||||||
class native_file final {
|
class native_file final {
|
||||||
public:
|
public:
|
||||||
|
native_file(const native_file &) = delete;
|
||||||
|
native_file(native_file &&) = delete;
|
||||||
|
auto operator=(const native_file &) -> native_file & = delete;
|
||||||
|
auto operator=(native_file &&) -> native_file & = delete;
|
||||||
|
|
||||||
using native_file_ptr = std::shared_ptr<native_file>;
|
using native_file_ptr = std::shared_ptr<native_file>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -34,22 +39,21 @@ public:
|
|||||||
return std::shared_ptr<native_file>(new native_file(handle));
|
return std::shared_ptr<native_file>(new native_file(handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] static auto clone(const native_file_ptr &nativeFile)
|
[[nodiscard]] static auto clone(const native_file_ptr &ptr)
|
||||||
-> native_file_ptr;
|
-> native_file_ptr;
|
||||||
|
|
||||||
[[nodiscard]] static auto create_or_open(const std::string &source_path,
|
[[nodiscard]] static auto create_or_open(const std::string &source_path,
|
||||||
bool should_chmod,
|
bool read_only, native_file_ptr &ptr)
|
||||||
native_file_ptr &nf) -> api_error;
|
-> api_error;
|
||||||
|
|
||||||
[[nodiscard]] static auto create_or_open(const std::string &source_path,
|
[[nodiscard]] static auto create_or_open(const std::string &source_path,
|
||||||
native_file_ptr &nf) -> api_error;
|
native_file_ptr &ptr) -> api_error;
|
||||||
|
|
||||||
[[nodiscard]] static auto open(const std::string &source_path,
|
[[nodiscard]] static auto open(const std::string &source_path,
|
||||||
native_file_ptr &nf) -> api_error;
|
native_file_ptr &ptr) -> api_error;
|
||||||
|
|
||||||
[[nodiscard]] static auto open(const std::string &source_path,
|
[[nodiscard]] static auto open(const std::string &source_path, bool read_only,
|
||||||
bool should_chmod, native_file_ptr &nf)
|
native_file_ptr &ptr) -> api_error;
|
||||||
-> api_error;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit native_file(const native_handle &handle) : handle_(handle) {}
|
explicit native_file(const native_handle &handle) : handle_(handle) {}
|
||||||
@ -71,7 +75,7 @@ public:
|
|||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
[[nodiscard]] auto copy_from(const native_file_ptr &source) -> bool;
|
[[nodiscard]] auto copy_from(const native_file_ptr &ptr) -> bool;
|
||||||
|
|
||||||
[[nodiscard]] auto copy_from(const std::string &path) -> bool;
|
[[nodiscard]] auto copy_from(const std::string &path) -> bool;
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@ file_manager::open_file::open_file(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (not fsi.directory) {
|
if (not fsi.directory) {
|
||||||
set_api_error(native_file::create_or_open(
|
set_api_error(native_file::create_or_open(fsi.source_path,
|
||||||
fsi.source_path, not provider_.is_direct_only(), nf_));
|
provider_.is_direct_only(), nf_));
|
||||||
if (get_api_error() == api_error::success) {
|
if (get_api_error() == api_error::success) {
|
||||||
if (read_state.has_value()) {
|
if (read_state.has_value()) {
|
||||||
read_state_ = read_state.value();
|
read_state_ = read_state.value();
|
||||||
|
@ -170,8 +170,7 @@ encrypting_reader::encrypting_reader(
|
|||||||
: key_(utils::encryption::generate_key(token)),
|
: key_(utils::encryption::generate_key(token)),
|
||||||
stop_requested_(stop_requested),
|
stop_requested_(stop_requested),
|
||||||
error_return_(error_return) {
|
error_return_(error_return) {
|
||||||
const auto res = native_file::create_or_open(
|
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||||
source_path, not relative_parent_path.has_value(), source_file_);
|
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||||
api_error_to_string(res));
|
api_error_to_string(res));
|
||||||
@ -223,8 +222,7 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path,
|
|||||||
: key_(utils::encryption::generate_key(token)),
|
: key_(utils::encryption::generate_key(token)),
|
||||||
stop_requested_(stop_requested),
|
stop_requested_(stop_requested),
|
||||||
error_return_(error_return) {
|
error_return_(error_return) {
|
||||||
const auto res =
|
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||||
native_file::create_or_open(source_path, false, source_file_);
|
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||||
api_error_to_string(res));
|
api_error_to_string(res));
|
||||||
@ -265,8 +263,7 @@ encrypting_reader::encrypting_reader(
|
|||||||
: key_(utils::encryption::generate_key(token)),
|
: key_(utils::encryption::generate_key(token)),
|
||||||
stop_requested_(stop_requested),
|
stop_requested_(stop_requested),
|
||||||
error_return_(error_return) {
|
error_return_(error_return) {
|
||||||
const auto res =
|
const auto res = native_file::create_or_open(source_path, true, source_file_);
|
||||||
native_file::create_or_open(source_path, false, source_file_);
|
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
throw std::runtime_error("file open failed|src|" + source_path + '|' +
|
||||||
api_error_to_string(res));
|
api_error_to_string(res));
|
||||||
|
@ -35,20 +35,21 @@ native_file::~native_file() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::clone(const native_file_ptr &nf) -> native_file_ptr {
|
auto native_file::clone(const native_file_ptr &ptr) -> native_file_ptr {
|
||||||
std::string source_path;
|
std::string source_path;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
source_path.resize(MAX_PATH + 1);
|
source_path.resize(MAX_PATH + 1);
|
||||||
::GetFinalPathNameByHandleA(nf->get_handle(), &source_path[0u], MAX_PATH + 1,
|
::GetFinalPathNameByHandleA(nf->get_handle(), source_path.data(),
|
||||||
|
MAX_PATH + 1,
|
||||||
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
||||||
#else
|
#else
|
||||||
source_path.resize(PATH_MAX + 1);
|
source_path.resize(PATH_MAX + 1);
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
fcntl(nf->get_handle(), F_GETPATH, &source_path[0u]);
|
fcntl(nf->get_handle(), F_GETPATH, source_path.data());
|
||||||
#else
|
#else
|
||||||
readlink(("/proc/self/fd/" + std::to_string(nf->get_handle())).c_str(),
|
readlink(("/proc/self/fd/" + std::to_string(ptr->get_handle())).c_str(),
|
||||||
&source_path[0u], source_path.size());
|
source_path.data(), source_path.size());
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
source_path = source_path.c_str();
|
source_path = source_path.c_str();
|
||||||
@ -63,52 +64,60 @@ auto native_file::clone(const native_file_ptr &nf) -> native_file_ptr {
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::create_or_open(const std::string &source_path,
|
auto native_file::create_or_open(const std::string &source_path, bool read_only,
|
||||||
[[maybe_unused]] bool should_chmod,
|
native_file_ptr &ptr) -> api_error {
|
||||||
native_file_ptr &nf) -> api_error {
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
auto handle =
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
read_only
|
||||||
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
? ::CreateFileA(source_path.c_str(), GENERIC_READ,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||||
|
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr)
|
||||||
|
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||||
|
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
||||||
#else
|
#else
|
||||||
auto handle = should_chmod ? ::open(source_path.c_str(),
|
auto handle =
|
||||||
O_CREAT | O_RDWR | O_CLOEXEC, 0600u)
|
read_only
|
||||||
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
|
? ::open(source_path.c_str(), O_CREAT | O_RDONLY | O_CLOEXEC, 0600U)
|
||||||
if (should_chmod) {
|
: ::open(source_path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600U);
|
||||||
chmod(source_path.c_str(), 0600u);
|
if (not read_only) {
|
||||||
|
chmod(source_path.c_str(), 0600U);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
nf = native_file::attach(handle);
|
ptr = native_file::attach(handle);
|
||||||
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
||||||
: api_error::success);
|
: api_error::success);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::create_or_open(const std::string &source_path,
|
auto native_file::create_or_open(const std::string &source_path,
|
||||||
native_file_ptr &nf) -> api_error {
|
native_file_ptr &ptr) -> api_error {
|
||||||
return create_or_open(source_path, true, nf);
|
return create_or_open(source_path, false, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::open(const std::string &source_path, native_file_ptr &nf)
|
auto native_file::open(const std::string &source_path, native_file_ptr &ptr)
|
||||||
-> api_error {
|
-> api_error {
|
||||||
return open(source_path, true, nf);
|
return open(source_path, false, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::open(const std::string &source_path,
|
auto native_file::open(const std::string &source_path, bool read_only,
|
||||||
[[maybe_unused]] bool should_chmod, native_file_ptr &nf)
|
native_file_ptr &ptr) -> api_error {
|
||||||
-> api_error {
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
auto handle = ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
auto handle =
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
read_only
|
||||||
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
? ::CreateFileA(source_path.c_str(), GENERIC_READ,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||||
|
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr)
|
||||||
|
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||||
|
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
||||||
#else
|
#else
|
||||||
auto handle = should_chmod
|
auto handle = read_only ? ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC)
|
||||||
? ::open(source_path.c_str(), O_RDWR | O_CLOEXEC, 0600u)
|
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
|
||||||
: ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC);
|
if (not read_only) {
|
||||||
if (should_chmod) {
|
chmod(source_path.c_str(), 0600U);
|
||||||
chmod(source_path.c_str(), 0600u);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
nf = native_file::attach(handle);
|
ptr = native_file::attach(handle);
|
||||||
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error
|
||||||
: api_error::success);
|
: api_error::success);
|
||||||
}
|
}
|
||||||
@ -139,19 +148,19 @@ void native_file::close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto native_file::copy_from(const native_file_ptr &nf) -> bool {
|
auto native_file::copy_from(const native_file_ptr &ptr) -> bool {
|
||||||
auto ret = false;
|
std::uint64_t file_size{};
|
||||||
std::uint64_t file_size = 0u;
|
auto ret = ptr->get_file_size(file_size);
|
||||||
if ((ret = nf->get_file_size(file_size))) {
|
if (ret) {
|
||||||
data_buffer buffer;
|
data_buffer buffer;
|
||||||
buffer.resize(65536u * 2u);
|
buffer.resize(65536ULL * 2ULL);
|
||||||
std::uint64_t offset = 0u;
|
std::uint64_t offset{};
|
||||||
while (ret && (file_size > 0u)) {
|
while (ret && (file_size > 0U)) {
|
||||||
std::size_t bytes_read{};
|
std::size_t bytes_read{};
|
||||||
if ((ret = nf->read_bytes(&buffer[0u], buffer.size(), offset,
|
ret = ptr->read_bytes(buffer.data(), buffer.size(), offset, bytes_read);
|
||||||
bytes_read))) {
|
if (ret) {
|
||||||
std::size_t bytes_written = 0u;
|
std::size_t bytes_written{};
|
||||||
ret = write_bytes(&buffer[0u], bytes_read, offset, bytes_written);
|
ret = write_bytes(buffer.data(), bytes_read, offset, bytes_written);
|
||||||
file_size -= bytes_read;
|
file_size -= bytes_read;
|
||||||
offset += bytes_read;
|
offset += bytes_read;
|
||||||
}
|
}
|
||||||
@ -164,10 +173,10 @@ auto native_file::copy_from(const native_file_ptr &nf) -> bool {
|
|||||||
|
|
||||||
auto native_file::copy_from(const std::string &path) -> bool {
|
auto native_file::copy_from(const std::string &path) -> bool {
|
||||||
auto ret = false;
|
auto ret = false;
|
||||||
native_file_ptr nf;
|
native_file_ptr ptr;
|
||||||
if (native_file::create_or_open(path, nf) == api_error ::success) {
|
if (native_file::create_or_open(path, ptr) == api_error ::success) {
|
||||||
ret = copy_from(nf);
|
ret = copy_from(ptr);
|
||||||
nf->close();
|
ptr->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -194,11 +203,12 @@ auto native_file::get_file_size(std::uint64_t &file_size) -> bool {
|
|||||||
struct stat st {};
|
struct stat st {};
|
||||||
if (fstat(handle_, &st) >= 0) {
|
if (fstat(handle_, &st) >= 0) {
|
||||||
#else
|
#else
|
||||||
struct stat64 st {};
|
struct stat64 unix_st {};
|
||||||
if (fstat64(handle_, &st) >= 0) {
|
if (fstat64(handle_, &unix_st) >= 0) {
|
||||||
#endif
|
#endif
|
||||||
if ((ret = (st.st_size >= 0))) {
|
ret = (unix_st.st_size >= 0);
|
||||||
file_size = static_cast<uint64_t>(st.st_size);
|
if (ret) {
|
||||||
|
file_size = static_cast<uint64_t>(unix_st.st_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -289,7 +299,7 @@ auto native_file::write_bytes(const char *buffer, std::size_t write_size,
|
|||||||
std::uint64_t write_offset,
|
std::uint64_t write_offset,
|
||||||
std::size_t &bytes_written) -> bool {
|
std::size_t &bytes_written) -> bool {
|
||||||
bytes_written = 0U;
|
bytes_written = 0U;
|
||||||
ssize_t result = 0U;
|
ssize_t result{};
|
||||||
do {
|
do {
|
||||||
result =
|
result =
|
||||||
pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written,
|
pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user