basic_file.hpp 26 KB

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