@@ -125,21 +125,29 @@ constexpr inline flags to_flags(std::ios::ios_base::openmode mode) {
125125template <bool seq, typename File, typename Executor>
126126inline bool open_native_async_file (File &file, Executor &executor,
127127 std::string_view filepath,
128- flags open_flags) {
128+ flags open_flags, bool use_direct_io = false ) {
129129 if (file && file->is_open ()) {
130130 return true ;
131131 }
132132
133133 try {
134+ asio::file_base::flags asio_flags = static_cast <asio::file_base::flags>(open_flags);
135+
136+ if (use_direct_io) {
137+ #if defined(ASIO_WINDOWS)
138+ #else
139+ asio_flags = static_cast <asio::file_base::flags>(
140+ static_cast <int >(asio_flags) | O_DIRECT);
141+ #endif
142+ }
143+
134144 if constexpr (seq) {
135145 file = std::make_shared<asio::stream_file>(
136- executor.get_asio_executor (), std::string (filepath),
137- static_cast <asio::file_base::flags>(open_flags));
146+ executor.get_asio_executor (), std::string (filepath), asio_flags);
138147 }
139148 else {
140149 file = std::make_shared<asio::random_access_file>(
141- executor.get_asio_executor (), std::string (filepath),
142- static_cast <asio::file_base::flags>(open_flags));
150+ executor.get_asio_executor (), std::string (filepath), asio_flags);
143151 }
144152 } catch (std::exception &ex) {
145153 ELOG_INFO << " line " << __LINE__ << " coro_file open failed" << ex.what ()
@@ -404,18 +412,18 @@ class basic_random_coro_file {
404412 }
405413
406414 bool open (std::string_view filepath,
407- std::ios::ios_base::openmode open_flags) {
415+ std::ios::ios_base::openmode open_flags, bool use_direct_io = false ) {
408416 file_path_ = std::string{filepath};
409417 if constexpr (execute_type == execution_type::thread_pool) {
410- return open_fd (filepath, to_flags (open_flags));
418+ return open_fd (filepath, to_flags (open_flags), use_direct_io );
411419 }
412420 else {
413421#if defined(ASIO_HAS_FILE)
414422 return open_native_async_file<false >(async_random_file_,
415423 executor_wrapper_, filepath,
416- to_flags (open_flags));
424+ to_flags (open_flags), use_direct_io );
417425#else
418- return open_fd (filepath, to_flags (open_flags));
426+ return open_fd (filepath, to_flags (open_flags), use_direct_io );
419427#endif
420428 }
421429 }
@@ -518,11 +526,18 @@ class basic_random_coro_file {
518526 std::string_view file_path () const { return file_path_; }
519527
520528 private:
521- bool open_fd (std::string_view filepath, int open_flags) {
529+ bool open_fd (std::string_view filepath, int open_flags, bool use_direct_io = false ) {
522530 if (prw_random_file_) {
523531 return true ;
524532 }
525533
534+ if (use_direct_io) {
535+ #if defined(ASIO_WINDOWS)
536+ #else
537+ open_flags |= O_DIRECT;
538+ #endif
539+ }
540+
526541#if defined(ASIO_WINDOWS)
527542 int fd = _open (filepath.data (), adjust_flags (open_flags));
528543#else
0 commit comments