Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows 下写文件无法追加内容 #648

Open
osabc opened this issue Nov 19, 2024 · 4 comments
Open

windows 下写文件无法追加内容 #648

osabc opened this issue Nov 19, 2024 · 4 comments

Comments

@osabc
Copy link

osabc commented Nov 19, 2024

coro_io::coro_file file{};
file.open("test.txt", std::ios::app);
co_await file.async_write("test");
@qicosmos
Copy link
Owner

qicosmos commented Dec 2, 2024

windows 上work around 一下,创建coro_file 的时候设置为thread pool:

coro_io::basic_seq_coro_file<coro_io::execution_type::thread_pool> file{};
file.open("test.txt", std::ios::app);
co_await file.async_write("test");

@osabc osabc closed this as completed Dec 2, 2024
@osabc osabc reopened this Dec 2, 2024
@osabc
Copy link
Author

osabc commented Dec 3, 2024

解决方法,修改asio源码

1

diff --git "a/include/asio/detail/impl/win_iocp_file_service.ipp" "b/include/asio/detail/impl/win_iocp_file_service.ipp"
index a1a7503..a9cc87a 100644
--- "a/include/asio/detail/impl/win_iocp_file_service.ipp"
+++ "b/include/asio/detail/impl/win_iocp_file_service.ipp"
@@ -67,6 +67,8 @@ asio::error_code win_iocp_file_service::open(
     access = GENERIC_WRITE;
   else if ((open_flags & file_base::read_write) != 0)
     access = GENERIC_READ | GENERIC_WRITE;
+  else if ((open_flags & file_base::append) != 0)
+    access = GENERIC_WRITE;
 
   DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE;
 
@@ -82,6 +84,8 @@ asio::error_code win_iocp_file_service::open(
   {
     if ((open_flags & file_base::truncate) != 0)
       disposition = TRUNCATE_EXISTING;
+    else if ((open_flags & file_base::append) != 0)
+      disposition = OPEN_ALWAYS;
     else
       disposition = OPEN_EXISTING;
   }
@@ -94,6 +98,7 @@ asio::error_code win_iocp_file_service::open(
   if ((open_flags & file_base::sync_all_on_write) != 0)
     flags |= FILE_FLAG_WRITE_THROUGH;
 
+  impl.offset_ = 0;
   HANDLE handle = ::CreateFileA(path, access, share, 0, disposition, flags, 0);
   if (handle != INVALID_HANDLE_VALUE)
   {
@@ -109,10 +114,24 @@ asio::error_code win_iocp_file_service::open(
       }
     }
 
+    if (disposition == OPEN_ALWAYS || (open_flags & file_base::append) != 0) {
+      LARGE_INTEGER distance, new_offset;
+      distance.QuadPart = 0;
+      if (::SetFilePointerEx(handle, distance, &new_offset, FILE_END)) {
+        impl.offset_ = static_cast<uint64_t>(new_offset.QuadPart);
+      }
+      else {
+        DWORD last_error = ::GetLastError();
+        ::CloseHandle(handle);
+        ec.assign(last_error, asio::error::get_system_category());
+        ASIO_ERROR_LOCATION(ec);
+        return ec;
+      }
+    }
+
     handle_service_.assign(impl, handle, ec);
     if (ec)
       ::CloseHandle(handle);
-    impl.offset_ = 0;
     ASIO_ERROR_LOCATION(ec);
     return ec;
   }

@qicosmos
Copy link
Owner

qicosmos commented Dec 3, 2024

或者你在asio 这个pr:chriskohlhoff/asio#1500
里回复一下正确的解决方法。

@qicosmos
Copy link
Owner

qicosmos commented Dec 3, 2024

再发一个diff的截图吧。

@osabc osabc closed this as completed Dec 4, 2024
@osabc osabc reopened this Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants