win_iocp_file_service.ipp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // detail/impl/win_iocp_file_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_IMPL_WIN_IOCP_FILE_SERVICE_IPP
  11. #define ASIO_DETAIL_IMPL_WIN_IOCP_FILE_SERVICE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_FILE) \
  17. && defined(ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  18. #include <cstring>
  19. #include <sys/stat.h>
  20. #include "asio/detail/win_iocp_file_service.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. win_iocp_file_service::win_iocp_file_service(
  25. execution_context& context)
  26. : execution_context_service_base<win_iocp_file_service>(context),
  27. handle_service_(context),
  28. nt_flush_buffers_file_ex_(0)
  29. {
  30. if (FARPROC nt_flush_buffers_file_ex_ptr = ::GetProcAddress(
  31. ::GetModuleHandleA("NTDLL"), "NtFlushBuffersFileEx"))
  32. {
  33. nt_flush_buffers_file_ex_ = reinterpret_cast<nt_flush_buffers_file_ex_fn>(
  34. reinterpret_cast<void*>(nt_flush_buffers_file_ex_ptr));
  35. }
  36. }
  37. void win_iocp_file_service::shutdown()
  38. {
  39. handle_service_.shutdown();
  40. }
  41. asio::error_code win_iocp_file_service::open(
  42. win_iocp_file_service::implementation_type& impl,
  43. const char* path, file_base::flags open_flags,
  44. asio::error_code& ec)
  45. {
  46. if (is_open(impl))
  47. {
  48. ec = asio::error::already_open;
  49. ASIO_ERROR_LOCATION(ec);
  50. return ec;
  51. }
  52. DWORD access = 0;
  53. if ((open_flags & file_base::read_only) != 0)
  54. access = GENERIC_READ;
  55. else if ((open_flags & file_base::write_only) != 0)
  56. access = GENERIC_WRITE;
  57. else if ((open_flags & file_base::read_write) != 0)
  58. access = GENERIC_READ | GENERIC_WRITE;
  59. DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE;
  60. DWORD disposition = 0;
  61. if ((open_flags & file_base::create) != 0)
  62. {
  63. if ((open_flags & file_base::exclusive) != 0)
  64. disposition = CREATE_NEW;
  65. else
  66. disposition = OPEN_ALWAYS;
  67. }
  68. else
  69. {
  70. if ((open_flags & file_base::truncate) != 0)
  71. disposition = TRUNCATE_EXISTING;
  72. else
  73. disposition = OPEN_EXISTING;
  74. }
  75. DWORD flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
  76. if (impl.is_stream_)
  77. flags |= FILE_FLAG_SEQUENTIAL_SCAN;
  78. else
  79. flags |= FILE_FLAG_RANDOM_ACCESS;
  80. if ((open_flags & file_base::sync_all_on_write) != 0)
  81. flags |= FILE_FLAG_WRITE_THROUGH;
  82. HANDLE handle = ::CreateFileA(path, access, share, 0, disposition, flags, 0);
  83. if (handle != INVALID_HANDLE_VALUE)
  84. {
  85. if (disposition == OPEN_ALWAYS && (open_flags & file_base::truncate) != 0)
  86. {
  87. if (!::SetEndOfFile(handle))
  88. {
  89. DWORD last_error = ::GetLastError();
  90. ::CloseHandle(handle);
  91. ec.assign(last_error, asio::error::get_system_category());
  92. ASIO_ERROR_LOCATION(ec);
  93. return ec;
  94. }
  95. }
  96. handle_service_.assign(impl, handle, ec);
  97. if (ec)
  98. ::CloseHandle(handle);
  99. impl.offset_ = 0;
  100. ASIO_ERROR_LOCATION(ec);
  101. return ec;
  102. }
  103. else
  104. {
  105. DWORD last_error = ::GetLastError();
  106. ec.assign(last_error, asio::error::get_system_category());
  107. ASIO_ERROR_LOCATION(ec);
  108. return ec;
  109. }
  110. }
  111. uint64_t win_iocp_file_service::size(
  112. const win_iocp_file_service::implementation_type& impl,
  113. asio::error_code& ec) const
  114. {
  115. LARGE_INTEGER result;
  116. if (::GetFileSizeEx(native_handle(impl), &result))
  117. {
  118. asio::error::clear(ec);
  119. return static_cast<uint64_t>(result.QuadPart);
  120. }
  121. else
  122. {
  123. DWORD last_error = ::GetLastError();
  124. ec.assign(last_error, asio::error::get_system_category());
  125. ASIO_ERROR_LOCATION(ec);
  126. return 0;
  127. }
  128. }
  129. asio::error_code win_iocp_file_service::resize(
  130. win_iocp_file_service::implementation_type& impl,
  131. uint64_t n, asio::error_code& ec)
  132. {
  133. LARGE_INTEGER distance;
  134. distance.QuadPart = n;
  135. if (::SetFilePointerEx(native_handle(impl), distance, 0, FILE_BEGIN))
  136. {
  137. BOOL result = ::SetEndOfFile(native_handle(impl));
  138. DWORD last_error = ::GetLastError();
  139. distance.QuadPart = static_cast<LONGLONG>(impl.offset_);
  140. if (!::SetFilePointerEx(native_handle(impl), distance, 0, FILE_BEGIN))
  141. {
  142. result = FALSE;
  143. last_error = ::GetLastError();
  144. }
  145. if (result)
  146. asio::error::clear(ec);
  147. else
  148. ec.assign(last_error, asio::error::get_system_category());
  149. ASIO_ERROR_LOCATION(ec);
  150. return ec;
  151. }
  152. else
  153. {
  154. DWORD last_error = ::GetLastError();
  155. ec.assign(last_error, asio::error::get_system_category());
  156. ASIO_ERROR_LOCATION(ec);
  157. return ec;
  158. }
  159. }
  160. asio::error_code win_iocp_file_service::sync_all(
  161. win_iocp_file_service::implementation_type& impl,
  162. asio::error_code& ec)
  163. {
  164. BOOL result = ::FlushFileBuffers(native_handle(impl));
  165. if (result)
  166. {
  167. asio::error::clear(ec);
  168. return ec;
  169. }
  170. else
  171. {
  172. DWORD last_error = ::GetLastError();
  173. ec.assign(last_error, asio::error::get_system_category());
  174. ASIO_ERROR_LOCATION(ec);
  175. return ec;
  176. }
  177. }
  178. asio::error_code win_iocp_file_service::sync_data(
  179. win_iocp_file_service::implementation_type& impl,
  180. asio::error_code& ec)
  181. {
  182. if (nt_flush_buffers_file_ex_)
  183. {
  184. io_status_block status = {};
  185. if (!nt_flush_buffers_file_ex_(native_handle(impl),
  186. flush_flags_file_data_sync_only, 0, 0, &status))
  187. {
  188. asio::error::clear(ec);
  189. return ec;
  190. }
  191. }
  192. return sync_all(impl, ec);
  193. }
  194. uint64_t win_iocp_file_service::seek(
  195. win_iocp_file_service::implementation_type& impl, int64_t offset,
  196. file_base::seek_basis whence, asio::error_code& ec)
  197. {
  198. DWORD method;
  199. switch (whence)
  200. {
  201. case file_base::seek_set:
  202. method = FILE_BEGIN;
  203. break;
  204. case file_base::seek_cur:
  205. method = FILE_BEGIN;
  206. offset = static_cast<int64_t>(impl.offset_) + offset;
  207. break;
  208. case file_base::seek_end:
  209. method = FILE_END;
  210. break;
  211. default:
  212. ec = asio::error::invalid_argument;
  213. ASIO_ERROR_LOCATION(ec);
  214. return 0;
  215. }
  216. LARGE_INTEGER distance, new_offset;
  217. distance.QuadPart = offset;
  218. if (::SetFilePointerEx(native_handle(impl), distance, &new_offset, method))
  219. {
  220. impl.offset_ = new_offset.QuadPart;
  221. asio::error::clear(ec);
  222. return impl.offset_;
  223. }
  224. else
  225. {
  226. DWORD last_error = ::GetLastError();
  227. ec.assign(last_error, asio::error::get_system_category());
  228. ASIO_ERROR_LOCATION(ec);
  229. return 0;
  230. }
  231. }
  232. } // namespace detail
  233. } // namespace asio
  234. #include "asio/detail/pop_options.hpp"
  235. #endif // defined(ASIO_HAS_FILE)
  236. // && defined(ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  237. #endif // ASIO_DETAIL_IMPL_WIN_IOCP_FILE_SERVICE_IPP