basic_file.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. //
  2. // basic_file.hpp
  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 BOOST_ASIO_BASIC_FILE_HPP
  11. #define BOOST_ASIO_BASIC_FILE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_FILE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <string>
  19. #include <boost/asio/any_io_executor.hpp>
  20. #include <boost/asio/async_result.hpp>
  21. #include <boost/asio/detail/cstdint.hpp>
  22. #include <boost/asio/detail/handler_type_requirements.hpp>
  23. #include <boost/asio/detail/io_object_impl.hpp>
  24. #include <boost/asio/detail/non_const_lvalue.hpp>
  25. #include <boost/asio/detail/throw_error.hpp>
  26. #include <boost/asio/detail/type_traits.hpp>
  27. #include <boost/asio/error.hpp>
  28. #include <boost/asio/execution_context.hpp>
  29. #include <boost/asio/post.hpp>
  30. #include <boost/asio/file_base.hpp>
  31. #if defined(BOOST_ASIO_HAS_IOCP)
  32. # include <boost/asio/detail/win_iocp_file_service.hpp>
  33. #elif defined(BOOST_ASIO_HAS_IO_URING)
  34. # include <boost/asio/detail/io_uring_file_service.hpp>
  35. #endif
  36. #if defined(BOOST_ASIO_HAS_MOVE)
  37. # include <utility>
  38. #endif // defined(BOOST_ASIO_HAS_MOVE)
  39. #include <boost/asio/detail/push_options.hpp>
  40. namespace boost {
  41. namespace asio {
  42. #if !defined(BOOST_ASIO_BASIC_FILE_FWD_DECL)
  43. #define BOOST_ASIO_BASIC_FILE_FWD_DECL
  44. // Forward declaration with defaulted arguments.
  45. template <typename Executor = any_io_executor>
  46. class basic_file;
  47. #endif // !defined(BOOST_ASIO_BASIC_FILE_FWD_DECL)
  48. /// Provides file functionality.
  49. /**
  50. * The basic_file class template provides functionality that is common to both
  51. * stream-oriented and random-access files.
  52. *
  53. * @par Thread Safety
  54. * @e Distinct @e objects: Safe.@n
  55. * @e Shared @e objects: Unsafe.
  56. */
  57. template <typename Executor>
  58. class basic_file
  59. : public file_base
  60. {
  61. public:
  62. /// The type of the executor associated with the object.
  63. typedef Executor executor_type;
  64. /// Rebinds the file type to another executor.
  65. template <typename Executor1>
  66. struct rebind_executor
  67. {
  68. /// The file type when rebound to the specified executor.
  69. typedef basic_file<Executor1> other;
  70. };
  71. /// The native representation of a file.
  72. #if defined(GENERATING_DOCUMENTATION)
  73. typedef implementation_defined native_handle_type;
  74. #elif defined(BOOST_ASIO_HAS_IOCP)
  75. typedef detail::win_iocp_file_service::native_handle_type native_handle_type;
  76. #elif defined(BOOST_ASIO_HAS_IO_URING)
  77. typedef detail::io_uring_file_service::native_handle_type native_handle_type;
  78. #endif
  79. /// Construct a basic_file without opening it.
  80. /**
  81. * This constructor initialises a file without opening it.
  82. *
  83. * @param ex The I/O executor that the file will use, by default, to
  84. * dispatch handlers for any asynchronous operations performed on the file.
  85. */
  86. explicit basic_file(const executor_type& ex)
  87. : impl_(0, ex)
  88. {
  89. }
  90. /// Construct a basic_file without opening it.
  91. /**
  92. * This constructor initialises a file without opening it.
  93. *
  94. * @param context An execution context which provides the I/O executor that
  95. * the file will use, by default, to dispatch handlers for any asynchronous
  96. * operations performed on the file.
  97. */
  98. template <typename ExecutionContext>
  99. explicit basic_file(ExecutionContext& context,
  100. typename constraint<
  101. is_convertible<ExecutionContext&, execution_context&>::value,
  102. defaulted_constraint
  103. >::type = defaulted_constraint())
  104. : impl_(0, 0, context)
  105. {
  106. }
  107. /// Construct and open a basic_file.
  108. /**
  109. * This constructor initialises a file and opens it.
  110. *
  111. * @param ex The I/O executor that the file will use, by default, to
  112. * dispatch handlers for any asynchronous operations performed on the file.
  113. *
  114. * @param path The path name identifying the file to be opened.
  115. *
  116. * @param open_flags A set of flags that determine how the file should be
  117. * opened.
  118. */
  119. explicit basic_file(const executor_type& ex,
  120. const char* path, file_base::flags open_flags)
  121. : impl_(0, ex)
  122. {
  123. boost::system::error_code ec;
  124. impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
  125. boost::asio::detail::throw_error(ec, "open");
  126. }
  127. /// Construct a basic_file without opening it.
  128. /**
  129. * This constructor initialises a file and opens it.
  130. *
  131. * @param context An execution context which provides the I/O executor that
  132. * the file will use, by default, to dispatch handlers for any asynchronous
  133. * operations performed on the file.
  134. *
  135. * @param path The path name identifying the file to be opened.
  136. *
  137. * @param open_flags A set of flags that determine how the file should be
  138. * opened.
  139. */
  140. template <typename ExecutionContext>
  141. explicit basic_file(ExecutionContext& context,
  142. const char* path, file_base::flags open_flags,
  143. typename constraint<
  144. is_convertible<ExecutionContext&, execution_context&>::value,
  145. defaulted_constraint
  146. >::type = defaulted_constraint())
  147. : impl_(0, 0, context)
  148. {
  149. boost::system::error_code ec;
  150. impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
  151. boost::asio::detail::throw_error(ec, "open");
  152. }
  153. /// Construct and open a basic_file.
  154. /**
  155. * This constructor initialises a file and opens it.
  156. *
  157. * @param ex The I/O executor that the file will use, by default, to
  158. * dispatch handlers for any asynchronous operations performed on the file.
  159. *
  160. * @param path The path name identifying the file to be opened.
  161. *
  162. * @param open_flags A set of flags that determine how the file should be
  163. * opened.
  164. */
  165. explicit basic_file(const executor_type& ex,
  166. const std::string& path, file_base::flags open_flags)
  167. : impl_(0, ex)
  168. {
  169. boost::system::error_code ec;
  170. impl_.get_service().open(impl_.get_implementation(),
  171. path.c_str(), open_flags, ec);
  172. boost::asio::detail::throw_error(ec, "open");
  173. }
  174. /// Construct a basic_file without opening it.
  175. /**
  176. * This constructor initialises a file and opens it.
  177. *
  178. * @param context An execution context which provides the I/O executor that
  179. * the file will use, by default, to dispatch handlers for any asynchronous
  180. * operations performed on the file.
  181. *
  182. * @param path The path name identifying the file to be opened.
  183. *
  184. * @param open_flags A set of flags that determine how the file should be
  185. * opened.
  186. */
  187. template <typename ExecutionContext>
  188. explicit basic_file(ExecutionContext& context,
  189. const std::string& path, file_base::flags open_flags,
  190. typename constraint<
  191. is_convertible<ExecutionContext&, execution_context&>::value,
  192. defaulted_constraint
  193. >::type = defaulted_constraint())
  194. : impl_(0, 0, context)
  195. {
  196. boost::system::error_code ec;
  197. impl_.get_service().open(impl_.get_implementation(),
  198. path.c_str(), open_flags, ec);
  199. boost::asio::detail::throw_error(ec, "open");
  200. }
  201. /// Construct a basic_file on an existing native file handle.
  202. /**
  203. * This constructor initialises a file object to hold an existing native file.
  204. *
  205. * @param ex The I/O executor that the file will use, by default, to
  206. * dispatch handlers for any asynchronous operations performed on the file.
  207. *
  208. * @param native_file A native file handle.
  209. *
  210. * @throws boost::system::system_error Thrown on failure.
  211. */
  212. basic_file(const executor_type& ex, const native_handle_type& native_file)
  213. : impl_(0, ex)
  214. {
  215. boost::system::error_code ec;
  216. impl_.get_service().assign(
  217. impl_.get_implementation(), native_file, ec);
  218. boost::asio::detail::throw_error(ec, "assign");
  219. }
  220. /// Construct a basic_file on an existing native file.
  221. /**
  222. * This constructor initialises a file object to hold an existing native file.
  223. *
  224. * @param context An execution context which provides the I/O executor that
  225. * the file will use, by default, to dispatch handlers for any asynchronous
  226. * operations performed on the file.
  227. *
  228. * @param native_file A native file.
  229. *
  230. * @throws boost::system::system_error Thrown on failure.
  231. */
  232. template <typename ExecutionContext>
  233. basic_file(ExecutionContext& context, const native_handle_type& native_file,
  234. typename constraint<
  235. is_convertible<ExecutionContext&, execution_context&>::value,
  236. defaulted_constraint
  237. >::type = defaulted_constraint())
  238. : impl_(0, 0, context)
  239. {
  240. boost::system::error_code ec;
  241. impl_.get_service().assign(
  242. impl_.get_implementation(), native_file, ec);
  243. boost::asio::detail::throw_error(ec, "assign");
  244. }
  245. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  246. /// Move-construct a basic_file from another.
  247. /**
  248. * This constructor moves a file from one object to another.
  249. *
  250. * @param other The other basic_file object from which the move will
  251. * occur.
  252. *
  253. * @note Following the move, the moved-from object is in the same state as if
  254. * constructed using the @c basic_file(const executor_type&) constructor.
  255. */
  256. basic_file(basic_file&& other) BOOST_ASIO_NOEXCEPT
  257. : impl_(std::move(other.impl_))
  258. {
  259. }
  260. /// Move-assign a basic_file from another.
  261. /**
  262. * This assignment operator moves a file from one object to another.
  263. *
  264. * @param other The other basic_file object from which the move will
  265. * occur.
  266. *
  267. * @note Following the move, the moved-from object is in the same state as if
  268. * constructed using the @c basic_file(const executor_type&) constructor.
  269. */
  270. basic_file& operator=(basic_file&& other)
  271. {
  272. impl_ = std::move(other.impl_);
  273. return *this;
  274. }
  275. // All files have access to each other's implementations.
  276. template <typename Executor1>
  277. friend class basic_file;
  278. /// Move-construct a basic_file from a file of another executor type.
  279. /**
  280. * This constructor moves a file from one object to another.
  281. *
  282. * @param other The other basic_file object from which the move will
  283. * occur.
  284. *
  285. * @note Following the move, the moved-from object is in the same state as if
  286. * constructed using the @c basic_file(const executor_type&) constructor.
  287. */
  288. template <typename Executor1>
  289. basic_file(basic_file<Executor1>&& other,
  290. typename constraint<
  291. is_convertible<Executor1, Executor>::value,
  292. defaulted_constraint
  293. >::type = defaulted_constraint())
  294. : impl_(std::move(other.impl_))
  295. {
  296. }
  297. /// Move-assign a basic_file from a file of another executor type.
  298. /**
  299. * This assignment operator moves a file from one object to another.
  300. *
  301. * @param other The other basic_file object from which the move will
  302. * occur.
  303. *
  304. * @note Following the move, the moved-from object is in the same state as if
  305. * constructed using the @c basic_file(const executor_type&) constructor.
  306. */
  307. template <typename Executor1>
  308. typename constraint<
  309. is_convertible<Executor1, Executor>::value,
  310. basic_file&
  311. >::type operator=(basic_file<Executor1>&& other)
  312. {
  313. basic_file tmp(std::move(other));
  314. impl_ = std::move(tmp.impl_);
  315. return *this;
  316. }
  317. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  318. /// Get the executor associated with the object.
  319. const executor_type& get_executor() BOOST_ASIO_NOEXCEPT
  320. {
  321. return impl_.get_executor();
  322. }
  323. /// Open the file using the specified path.
  324. /**
  325. * This function opens the file so that it will use the specified path.
  326. *
  327. * @param path The path name identifying the file to be opened.
  328. *
  329. * @param open_flags A set of flags that determine how the file should be
  330. * opened.
  331. *
  332. * @throws boost::system::system_error Thrown on failure.
  333. *
  334. * @par Example
  335. * @code
  336. * boost::asio::stream_file file(my_context);
  337. * file.open("/path/to/my/file", boost::asio::stream_file::read_only);
  338. * @endcode
  339. */
  340. void open(const char* path, file_base::flags open_flags)
  341. {
  342. boost::system::error_code ec;
  343. impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
  344. boost::asio::detail::throw_error(ec, "open");
  345. }
  346. /// Open the file using the specified path.
  347. /**
  348. * This function opens the file so that it will use the specified path.
  349. *
  350. * @param path The path name identifying the file to be opened.
  351. *
  352. * @param open_flags A set of flags that determine how the file should be
  353. * opened.
  354. *
  355. * @param ec Set to indicate what error occurred, if any.
  356. *
  357. * @par Example
  358. * @code
  359. * boost::asio::stream_file file(my_context);
  360. * boost::system::error_code ec;
  361. * file.open("/path/to/my/file", boost::asio::stream_file::read_only, ec);
  362. * if (ec)
  363. * {
  364. * // An error occurred.
  365. * }
  366. * @endcode
  367. */
  368. BOOST_ASIO_SYNC_OP_VOID open(const char* path,
  369. file_base::flags open_flags, boost::system::error_code& ec)
  370. {
  371. impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
  372. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  373. }
  374. /// Open the file using the specified path.
  375. /**
  376. * This function opens the file so that it will use the specified path.
  377. *
  378. * @param path The path name identifying the file to be opened.
  379. *
  380. * @param open_flags A set of flags that determine how the file should be
  381. * opened.
  382. *
  383. * @throws boost::system::system_error Thrown on failure.
  384. *
  385. * @par Example
  386. * @code
  387. * boost::asio::stream_file file(my_context);
  388. * file.open("/path/to/my/file", boost::asio::stream_file::read_only);
  389. * @endcode
  390. */
  391. void open(const std::string& path, file_base::flags open_flags)
  392. {
  393. boost::system::error_code ec;
  394. impl_.get_service().open(impl_.get_implementation(),
  395. path.c_str(), open_flags, ec);
  396. boost::asio::detail::throw_error(ec, "open");
  397. }
  398. /// Open the file using the specified path.
  399. /**
  400. * This function opens the file so that it will use the specified path.
  401. *
  402. * @param path The path name identifying the file to be opened.
  403. *
  404. * @param open_flags A set of flags that determine how the file should be
  405. * opened.
  406. *
  407. * @param ec Set to indicate what error occurred, if any.
  408. *
  409. * @par Example
  410. * @code
  411. * boost::asio::stream_file file(my_context);
  412. * boost::system::error_code ec;
  413. * file.open("/path/to/my/file", boost::asio::stream_file::read_only, ec);
  414. * if (ec)
  415. * {
  416. * // An error occurred.
  417. * }
  418. * @endcode
  419. */
  420. BOOST_ASIO_SYNC_OP_VOID open(const std::string& path,
  421. file_base::flags open_flags, boost::system::error_code& ec)
  422. {
  423. impl_.get_service().open(impl_.get_implementation(),
  424. path.c_str(), open_flags, ec);
  425. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  426. }
  427. /// Assign an existing native file to the file.
  428. /*
  429. * This function opens the file to hold an existing native file.
  430. *
  431. * @param native_file A native file.
  432. *
  433. * @throws boost::system::system_error Thrown on failure.
  434. */
  435. void assign(const native_handle_type& native_file)
  436. {
  437. boost::system::error_code ec;
  438. impl_.get_service().assign(
  439. impl_.get_implementation(), native_file, ec);
  440. boost::asio::detail::throw_error(ec, "assign");
  441. }
  442. /// Assign an existing native file to the file.
  443. /*
  444. * This function opens the file to hold an existing native file.
  445. *
  446. * @param native_file A native file.
  447. *
  448. * @param ec Set to indicate what error occurred, if any.
  449. */
  450. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_file,
  451. boost::system::error_code& ec)
  452. {
  453. impl_.get_service().assign(
  454. impl_.get_implementation(), native_file, ec);
  455. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  456. }
  457. /// Determine whether the file is open.
  458. bool is_open() const
  459. {
  460. return impl_.get_service().is_open(impl_.get_implementation());
  461. }
  462. /// Close the file.
  463. /**
  464. * This function is used to close the file. Any asynchronous read or write
  465. * operations will be cancelled immediately, and will complete with the
  466. * boost::asio::error::operation_aborted error.
  467. *
  468. * @throws boost::system::system_error Thrown on failure. Note that, even if
  469. * the function indicates an error, the underlying descriptor is closed.
  470. */
  471. void close()
  472. {
  473. boost::system::error_code ec;
  474. impl_.get_service().close(impl_.get_implementation(), ec);
  475. boost::asio::detail::throw_error(ec, "close");
  476. }
  477. /// Close the file.
  478. /**
  479. * This function is used to close the file. Any asynchronous read or write
  480. * operations will be cancelled immediately, and will complete with the
  481. * boost::asio::error::operation_aborted error.
  482. *
  483. * @param ec Set to indicate what error occurred, if any. Note that, even if
  484. * the function indicates an error, the underlying descriptor is closed.
  485. *
  486. * @par Example
  487. * @code
  488. * boost::asio::stream_file file(my_context);
  489. * ...
  490. * boost::system::error_code ec;
  491. * file.close(ec);
  492. * if (ec)
  493. * {
  494. * // An error occurred.
  495. * }
  496. * @endcode
  497. */
  498. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  499. {
  500. impl_.get_service().close(impl_.get_implementation(), ec);
  501. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  502. }
  503. /// Release ownership of the underlying native file.
  504. /**
  505. * This function causes all outstanding asynchronous read and write
  506. * operations to finish immediately, and the handlers for cancelled
  507. * operations will be passed the boost::asio::error::operation_aborted error.
  508. * Ownership of the native file is then transferred to the caller.
  509. *
  510. * @throws boost::system::system_error Thrown on failure.
  511. *
  512. * @note This function is unsupported on Windows versions prior to Windows
  513. * 8.1, and will fail with boost::asio::error::operation_not_supported on
  514. * these platforms.
  515. */
  516. #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  517. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  518. __declspec(deprecated("This function always fails with "
  519. "operation_not_supported when used on Windows versions "
  520. "prior to Windows 8.1."))
  521. #endif
  522. native_handle_type release()
  523. {
  524. boost::system::error_code ec;
  525. native_handle_type s = impl_.get_service().release(
  526. impl_.get_implementation(), ec);
  527. boost::asio::detail::throw_error(ec, "release");
  528. return s;
  529. }
  530. /// Release ownership of the underlying native file.
  531. /**
  532. * This function causes all outstanding asynchronous read and write
  533. * operations to finish immediately, and the handlers for cancelled
  534. * operations will be passed the boost::asio::error::operation_aborted error.
  535. * Ownership of the native file is then transferred to the caller.
  536. *
  537. * @param ec Set to indicate what error occurred, if any.
  538. *
  539. * @note This function is unsupported on Windows versions prior to Windows
  540. * 8.1, and will fail with boost::asio::error::operation_not_supported on
  541. * these platforms.
  542. */
  543. #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  544. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  545. __declspec(deprecated("This function always fails with "
  546. "operation_not_supported when used on Windows versions "
  547. "prior to Windows 8.1."))
  548. #endif
  549. native_handle_type release(boost::system::error_code& ec)
  550. {
  551. return impl_.get_service().release(impl_.get_implementation(), ec);
  552. }
  553. /// Get the native file representation.
  554. /**
  555. * This function may be used to obtain the underlying representation of the
  556. * file. This is intended to allow access to native file functionality
  557. * that is not otherwise provided.
  558. */
  559. native_handle_type native_handle()
  560. {
  561. return impl_.get_service().native_handle(impl_.get_implementation());
  562. }
  563. /// Cancel all asynchronous operations associated with the file.
  564. /**
  565. * This function causes all outstanding asynchronous read and write
  566. * operations to finish immediately, and the handlers for cancelled
  567. * operations will be passed the boost::asio::error::operation_aborted error.
  568. *
  569. * @throws boost::system::system_error Thrown on failure.
  570. *
  571. * @note Calls to cancel() will always fail with
  572. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  573. * Server 2003, and earlier versions of Windows, unless
  574. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  575. * two issues that should be considered before enabling its use:
  576. *
  577. * @li It will only cancel asynchronous operations that were initiated in the
  578. * current thread.
  579. *
  580. * @li It can appear to complete without error, but the request to cancel the
  581. * unfinished operations may be silently ignored by the operating system.
  582. * Whether it works or not seems to depend on the drivers that are installed.
  583. *
  584. * For portable cancellation, consider using the close() function to
  585. * simultaneously cancel the outstanding operations and close the file.
  586. *
  587. * When running on Windows Vista, Windows Server 2008, and later, the
  588. * CancelIoEx function is always used. This function does not have the
  589. * problems described above.
  590. */
  591. #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  592. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  593. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  594. __declspec(deprecated("By default, this function always fails with "
  595. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  596. "or earlier. Consult documentation for details."))
  597. #endif
  598. void cancel()
  599. {
  600. boost::system::error_code ec;
  601. impl_.get_service().cancel(impl_.get_implementation(), ec);
  602. boost::asio::detail::throw_error(ec, "cancel");
  603. }
  604. /// Cancel all asynchronous operations associated with the file.
  605. /**
  606. * This function causes all outstanding asynchronous read and write
  607. * operations to finish immediately, and the handlers for cancelled
  608. * operations will be passed the boost::asio::error::operation_aborted error.
  609. *
  610. * @param ec Set to indicate what error occurred, if any.
  611. *
  612. * @note Calls to cancel() will always fail with
  613. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  614. * Server 2003, and earlier versions of Windows, unless
  615. * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  616. * two issues that should be considered before enabling its use:
  617. *
  618. * @li It will only cancel asynchronous operations that were initiated in the
  619. * current thread.
  620. *
  621. * @li It can appear to complete without error, but the request to cancel the
  622. * unfinished operations may be silently ignored by the operating system.
  623. * Whether it works or not seems to depend on the drivers that are installed.
  624. *
  625. * For portable cancellation, consider using the close() function to
  626. * simultaneously cancel the outstanding operations and close the file.
  627. *
  628. * When running on Windows Vista, Windows Server 2008, and later, the
  629. * CancelIoEx function is always used. This function does not have the
  630. * problems described above.
  631. */
  632. #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  633. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  634. && !defined(BOOST_ASIO_ENABLE_CANCELIO)
  635. __declspec(deprecated("By default, this function always fails with "
  636. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  637. "or earlier. Consult documentation for details."))
  638. #endif
  639. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  640. {
  641. impl_.get_service().cancel(impl_.get_implementation(), ec);
  642. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  643. }
  644. /// Get the size of the file.
  645. /**
  646. * This function determines the size of the file, in bytes.
  647. *
  648. * @throws boost::system::system_error Thrown on failure.
  649. */
  650. uint64_t size() const
  651. {
  652. boost::system::error_code ec;
  653. uint64_t s = impl_.get_service().size(impl_.get_implementation(), ec);
  654. boost::asio::detail::throw_error(ec, "size");
  655. return s;
  656. }
  657. /// Get the size of the file.
  658. /**
  659. * This function determines the size of the file, in bytes.
  660. *
  661. * @param ec Set to indicate what error occurred, if any.
  662. */
  663. uint64_t size(boost::system::error_code& ec) const
  664. {
  665. return impl_.get_service().size(impl_.get_implementation(), ec);
  666. }
  667. /// Alter the size of the file.
  668. /**
  669. * This function resizes the file to the specified size, in bytes. If the
  670. * current file size exceeds @c n then any extra data is discarded. If the
  671. * current size is less than @c n then the file is extended and filled with
  672. * zeroes.
  673. *
  674. * @param n The new size for the file.
  675. *
  676. * @throws boost::system::system_error Thrown on failure.
  677. */
  678. void resize(uint64_t n)
  679. {
  680. boost::system::error_code ec;
  681. impl_.get_service().resize(impl_.get_implementation(), n, ec);
  682. boost::asio::detail::throw_error(ec, "resize");
  683. }
  684. /// Alter the size of the file.
  685. /**
  686. * This function resizes the file to the specified size, in bytes. If the
  687. * current file size exceeds @c n then any extra data is discarded. If the
  688. * current size is less than @c n then the file is extended and filled with
  689. * zeroes.
  690. *
  691. * @param n The new size for the file.
  692. *
  693. * @param ec Set to indicate what error occurred, if any.
  694. */
  695. BOOST_ASIO_SYNC_OP_VOID resize(uint64_t n, boost::system::error_code& ec)
  696. {
  697. impl_.get_service().resize(impl_.get_implementation(), n, ec);
  698. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  699. }
  700. /// Synchronise the file to disk.
  701. /**
  702. * This function synchronises the file data and metadata to disk. Note that
  703. * the semantics of this synchronisation vary between operation systems.
  704. *
  705. * @throws boost::system::system_error Thrown on failure.
  706. */
  707. void sync_all()
  708. {
  709. boost::system::error_code ec;
  710. impl_.get_service().sync_all(impl_.get_implementation(), ec);
  711. boost::asio::detail::throw_error(ec, "sync_all");
  712. }
  713. /// Synchronise the file to disk.
  714. /**
  715. * This function synchronises the file data and metadata to disk. Note that
  716. * the semantics of this synchronisation vary between operation systems.
  717. *
  718. * @param ec Set to indicate what error occurred, if any.
  719. */
  720. BOOST_ASIO_SYNC_OP_VOID sync_all(boost::system::error_code& ec)
  721. {
  722. impl_.get_service().sync_all(impl_.get_implementation(), ec);
  723. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  724. }
  725. /// Synchronise the file data to disk.
  726. /**
  727. * This function synchronises the file data to disk. Note that the semantics
  728. * of this synchronisation vary between operation systems.
  729. *
  730. * @throws boost::system::system_error Thrown on failure.
  731. */
  732. void sync_data()
  733. {
  734. boost::system::error_code ec;
  735. impl_.get_service().sync_data(impl_.get_implementation(), ec);
  736. boost::asio::detail::throw_error(ec, "sync_data");
  737. }
  738. /// Synchronise the file data to disk.
  739. /**
  740. * This function synchronises the file data to disk. Note that the semantics
  741. * of this synchronisation vary between operation systems.
  742. *
  743. * @param ec Set to indicate what error occurred, if any.
  744. */
  745. BOOST_ASIO_SYNC_OP_VOID sync_data(boost::system::error_code& ec)
  746. {
  747. impl_.get_service().sync_data(impl_.get_implementation(), ec);
  748. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  749. }
  750. protected:
  751. /// Protected destructor to prevent deletion through this type.
  752. /**
  753. * This function destroys the file, cancelling any outstanding asynchronous
  754. * operations associated with the file as if by calling @c cancel.
  755. */
  756. ~basic_file()
  757. {
  758. }
  759. #if defined(BOOST_ASIO_HAS_IOCP)
  760. detail::io_object_impl<detail::win_iocp_file_service, Executor> impl_;
  761. #elif defined(BOOST_ASIO_HAS_IO_URING)
  762. detail::io_object_impl<detail::io_uring_file_service, Executor> impl_;
  763. #endif
  764. private:
  765. // Disallow copying and assignment.
  766. basic_file(const basic_file&) BOOST_ASIO_DELETED;
  767. basic_file& operator=(const basic_file&) BOOST_ASIO_DELETED;
  768. };
  769. } // namespace asio
  770. } // namespace boost
  771. #include <boost/asio/detail/pop_options.hpp>
  772. #endif // defined(BOOST_ASIO_HAS_FILE)
  773. // || defined(GENERATING_DOCUMENTATION)
  774. #endif // BOOST_ASIO_BASIC_FILE_HPP