Skip to content

Commit

Permalink
improve log; improve for uring
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jan 11, 2024
1 parent 77d704c commit cbd0a50
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
32 changes: 25 additions & 7 deletions include/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ class coro_file {
#endif

bool is_open() {
if (type_ == read_type::fread) {
return stream_file_ != nullptr;
if (type_ == read_type::pread) {
return fd_file_ != nullptr;
}

return fd_file_ != nullptr;
return stream_file_ != nullptr;
}

void flush() {
Expand Down Expand Up @@ -242,7 +242,9 @@ class coro_file {
executor_wrapper_.get_asio_executor());
}
} catch (std::exception& ex) {
std::cout << ex.what() << "\n";
stream_file_ = nullptr;
std::cout << "line " << __LINE__ << " coro_file create failed"
<< ex.what() << "\n";
co_return false;
}

Expand All @@ -251,7 +253,9 @@ class coro_file {
static_cast<asio::file_base::flags>(open_mode), ec);

if (ec) {
std::cout << ec.message() << "\n";
stream_file_ = nullptr;
std::cout << "line " << __LINE__ << " coro_file open failed" << ex.what()
<< "\n";
co_return false;
}

Expand All @@ -278,6 +282,10 @@ class coro_file {
uint64_t offset, char* data, size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring_random);
if (type_ != read_type::uring_random) {
co_return std::make_pair(
std::make_error_code(std::errc::bad_file_descriptor), 0);
}

auto [ec, read_size] = co_await coro_io::async_read_at(
offset,
Expand All @@ -297,6 +305,9 @@ class coro_file {
size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring_random);
if (type_ != read_type::uring_random) {
co_return std::make_error_code(std::errc::bad_file_descriptor);
}

auto [ec, write_size] = co_await coro_io::async_write_at(
offset,
Expand All @@ -309,6 +320,10 @@ class coro_file {
char* data, size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring);
if (type_ != read_type::uring) {
co_return std::make_pair(
std::make_error_code(std::errc::bad_file_descriptor), 0);
}

auto [ec, read_size] = co_await coro_io::async_read(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
Expand All @@ -325,6 +340,9 @@ class coro_file {
size_t size) {
assert(stream_file_);
assert(type_ == read_type::uring);
if (type_ != read_type::uring) {
co_return std::make_error_code(std::errc::bad_file_descriptor);
}

auto [ec, write_size] = co_await coro_io::async_write(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
Expand Down Expand Up @@ -378,8 +396,8 @@ class coro_file {
[this, &filepath, open_mode] {
auto fptr = fopen(filepath.data(), str_mode(open_mode).data());
if (fptr == nullptr) {
std::cout << "open file " << filepath << " failed "
<< "\n";
std::cout << "line " << __LINE__ << " coro_file open failed "
<< filepath << "\n";
return false;
}
stream_file_ = std::shared_ptr<FILE>(fptr, [](FILE* ptr) {
Expand Down
39 changes: 39 additions & 0 deletions src/coro_io/tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ TEST_CASE("validate corofile") {
async_simple::coro::syncAwait(file.async_pwrite(0, buf, 10));
CHECK(write_ec == std::make_error_code(std::errc::bad_file_descriptor));
}

#if defined(YLT_ENABLE_FILE_IO_URING)
{
coro_io::coro_file file{};
async_simple::coro::syncAwait(
file.async_open(filename.data(), coro_io::flags::read_only,
coro_io::read_type::uring_random));
CHECK(file.is_open());

char buf[100];
std::error_code ec;
size_t size;
std::tie(ec, size) =
async_simple::coro::syncAwait(file.async_read(buf, 10));
CHECK(ec == std::make_error_code(std::errc::bad_file_descriptor));
CHECK(size == 0);

ec = async_simple::coro::syncAwait(file.async_write(buf, 10));
CHECK(ec == std::make_error_code(std::errc::bad_file_descriptor));
}

{
coro_io::coro_file file{};
async_simple::coro::syncAwait(file.async_open(
filename.data(), coro_io::flags::read_only, coro_io::read_type::uring));
CHECK(file.is_open());

char buf[100];
std::error_code ec;
size_t size;
std::tie(ec, size) =
async_simple::coro::syncAwait(file.async_read_at(0, buf, 10));
CHECK(ec == std::make_error_code(std::errc::bad_file_descriptor));
CHECK(size == 0);

ec = async_simple::coro::syncAwait(file.async_write_at(0, buf, 10));
CHECK(ec == std::make_error_code(std::errc::bad_file_descriptor));
}
#endif
}

TEST_CASE("coro_file pread and pwrite basic test") {
Expand Down

0 comments on commit cbd0a50

Please sign in to comment.