basic_file.hpp 30 KB

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