This commit is contained in:
parent
fd18ad9102
commit
4620dc72fe
@ -70,23 +70,23 @@ ring_buffer_open_file::ring_buffer_open_file(std::string buffer_directory,
|
|||||||
|
|
||||||
buffer_directory = utils::path::absolute(buffer_directory);
|
buffer_directory = utils::path::absolute(buffer_directory);
|
||||||
if (not utils::file::directory(buffer_directory).create_directory()) {
|
if (not utils::file::directory(buffer_directory).create_directory()) {
|
||||||
throw std::runtime_error("failed to create buffer directory|path|" +
|
throw std::runtime_error(
|
||||||
buffer_directory + "|err|" +
|
fmt::format("failed to create buffer directory|path|{}|err|{}",
|
||||||
std::to_string(utils::get_last_error_code()));
|
buffer_directory, utils::get_last_error_code()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fsi_.source_path =
|
fsi_.source_path =
|
||||||
utils::path::combine(buffer_directory, {utils::create_uuid_string()});
|
utils::path::combine(buffer_directory, {utils::create_uuid_string()});
|
||||||
nf_ = utils::file::file::open_or_create_file(fsi_.source_path);
|
nf_ = utils::file::file::open_or_create_file(fsi_.source_path);
|
||||||
if (not *nf_) {
|
if (not *nf_) {
|
||||||
throw std::runtime_error("failed to create buffer file|err|" +
|
throw std::runtime_error(fmt::format("failed to create buffer file|err|{}",
|
||||||
std::to_string(utils::get_last_error_code()));
|
utils::get_last_error_code()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (not nf_->truncate(ring_state_.size() * chunk_size)) {
|
if (not nf_->truncate(ring_state_.size() * chunk_size)) {
|
||||||
nf_->close();
|
nf_->close();
|
||||||
throw std::runtime_error("failed to resize buffer file|err|" +
|
throw std::runtime_error(fmt::format("failed to resize buffer file|err|{}",
|
||||||
std::to_string(utils::get_last_error_code()));
|
utils::get_last_error_code()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,12 +130,12 @@ auto ring_buffer_open_file::download_chunk(std::size_t chunk) -> api_error {
|
|||||||
if (res == api_error::success) {
|
if (res == api_error::success) {
|
||||||
res = do_io([&]() -> api_error {
|
res = do_io([&]() -> api_error {
|
||||||
std::size_t bytes_written{};
|
std::size_t bytes_written{};
|
||||||
if (not nf_->write(buffer, (chunk % ring_state_.size()) * chunk_size_,
|
if (nf_->write(buffer, (chunk % ring_state_.size()) * chunk_size_,
|
||||||
&bytes_written)) {
|
&bytes_written)) {
|
||||||
return api_error::os_error;
|
return api_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
return api_error::success;
|
return api_error::os_error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,30 +263,30 @@ auto ring_buffer_open_file::read(std::size_t read_size,
|
|||||||
|
|
||||||
reset_timeout();
|
reset_timeout();
|
||||||
res = download_chunk(chunk);
|
res = download_chunk(chunk);
|
||||||
if (res == api_error::success) {
|
if (res != api_error::success) {
|
||||||
const auto to_read = std::min(
|
continue;
|
||||||
static_cast<std::size_t>(chunk_size_ - read_offset), read_size);
|
|
||||||
res = do_io([this, &buffer, &chunk, &data, read_offset,
|
|
||||||
&to_read]() -> api_error {
|
|
||||||
std::size_t bytes_read{};
|
|
||||||
auto ret =
|
|
||||||
nf_->read(buffer, ((chunk % ring_state_.size()) * chunk_size_),
|
|
||||||
&bytes_read)
|
|
||||||
? api_error::success
|
|
||||||
: api_error::os_error;
|
|
||||||
if (ret == api_error::success) {
|
|
||||||
data.insert(data.end(),
|
|
||||||
buffer.begin() + static_cast<std::int64_t>(read_offset),
|
|
||||||
buffer.begin() +
|
|
||||||
static_cast<std::int64_t>(read_offset + to_read));
|
|
||||||
reset_timeout();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
});
|
|
||||||
read_offset = 0U;
|
|
||||||
read_size -= to_read;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto to_read = std::min(
|
||||||
|
static_cast<std::size_t>(chunk_size_ - read_offset), read_size);
|
||||||
|
res = do_io([this, &buffer, &chunk, &data, read_offset,
|
||||||
|
&to_read]() -> api_error {
|
||||||
|
std::size_t bytes_read{};
|
||||||
|
auto ret = nf_->read(buffer, ((chunk % ring_state_.size()) * chunk_size_),
|
||||||
|
&bytes_read)
|
||||||
|
? api_error::success
|
||||||
|
: api_error::os_error;
|
||||||
|
if (ret == api_error::success) {
|
||||||
|
data.insert(
|
||||||
|
data.end(), buffer.begin() + static_cast<std::int64_t>(read_offset),
|
||||||
|
buffer.begin() + static_cast<std::int64_t>(read_offset + to_read));
|
||||||
|
reset_timeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
read_offset = 0U;
|
||||||
|
read_size -= to_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user