123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- //
- // basic_file.hpp
- // ~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_BASIC_FILE_HPP
- #define BOOST_ASIO_BASIC_FILE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #if defined(BOOST_ASIO_HAS_FILE) \
- || defined(GENERATING_DOCUMENTATION)
- #include <string>
- #include <utility>
- #include <boost/asio/any_io_executor.hpp>
- #include <boost/asio/async_result.hpp>
- #include <boost/asio/detail/cstdint.hpp>
- #include <boost/asio/detail/handler_type_requirements.hpp>
- #include <boost/asio/detail/io_object_impl.hpp>
- #include <boost/asio/detail/non_const_lvalue.hpp>
- #include <boost/asio/detail/throw_error.hpp>
- #include <boost/asio/detail/type_traits.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/execution_context.hpp>
- #include <boost/asio/post.hpp>
- #include <boost/asio/file_base.hpp>
- #if defined(BOOST_ASIO_HAS_IOCP)
- # include <boost/asio/detail/win_iocp_file_service.hpp>
- #elif defined(BOOST_ASIO_HAS_IO_URING)
- # include <boost/asio/detail/io_uring_file_service.hpp>
- #endif
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- #if !defined(BOOST_ASIO_BASIC_FILE_FWD_DECL)
- #define BOOST_ASIO_BASIC_FILE_FWD_DECL
- // Forward declaration with defaulted arguments.
- template <typename Executor = any_io_executor>
- class basic_file;
- #endif // !defined(BOOST_ASIO_BASIC_FILE_FWD_DECL)
- /// Provides file functionality.
- /**
- * The basic_file class template provides functionality that is common to both
- * stream-oriented and random-access files.
- *
- * @par Thread Safety
- * @e Distinct @e objects: Safe.@n
- * @e Shared @e objects: Unsafe.
- */
- template <typename Executor>
- class basic_file
- : public file_base
- {
- public:
- /// The type of the executor associated with the object.
- typedef Executor executor_type;
- /// Rebinds the file type to another executor.
- template <typename Executor1>
- struct rebind_executor
- {
- /// The file type when rebound to the specified executor.
- typedef basic_file<Executor1> other;
- };
- /// The native representation of a file.
- #if defined(GENERATING_DOCUMENTATION)
- typedef implementation_defined native_handle_type;
- #elif defined(BOOST_ASIO_HAS_IOCP)
- typedef detail::win_iocp_file_service::native_handle_type native_handle_type;
- #elif defined(BOOST_ASIO_HAS_IO_URING)
- typedef detail::io_uring_file_service::native_handle_type native_handle_type;
- #endif
- /// Construct a basic_file without opening it.
- /**
- * This constructor initialises a file without opening it.
- *
- * @param ex The I/O executor that the file will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the file.
- */
- explicit basic_file(const executor_type& ex)
- : impl_(0, ex)
- {
- }
- /// Construct a basic_file without opening it.
- /**
- * This constructor initialises a file without opening it.
- *
- * @param context An execution context which provides the I/O executor that
- * the file will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the file.
- */
- template <typename ExecutionContext>
- explicit basic_file(ExecutionContext& context,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : impl_(0, 0, context)
- {
- }
- /// Construct and open a basic_file.
- /**
- * This constructor initialises a file and opens it.
- *
- * @param ex The I/O executor that the file will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the file.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- */
- explicit basic_file(const executor_type& ex,
- const char* path, file_base::flags open_flags)
- : impl_(0, ex)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Construct a basic_file without opening it.
- /**
- * This constructor initialises a file and opens it.
- *
- * @param context An execution context which provides the I/O executor that
- * the file will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the file.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- */
- template <typename ExecutionContext>
- explicit basic_file(ExecutionContext& context,
- const char* path, file_base::flags open_flags,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : impl_(0, 0, context)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Construct and open a basic_file.
- /**
- * This constructor initialises a file and opens it.
- *
- * @param ex The I/O executor that the file will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the file.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- */
- explicit basic_file(const executor_type& ex,
- const std::string& path, file_base::flags open_flags)
- : impl_(0, ex)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(),
- path.c_str(), open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Construct a basic_file without opening it.
- /**
- * This constructor initialises a file and opens it.
- *
- * @param context An execution context which provides the I/O executor that
- * the file will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the file.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- */
- template <typename ExecutionContext>
- explicit basic_file(ExecutionContext& context,
- const std::string& path, file_base::flags open_flags,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : impl_(0, 0, context)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(),
- path.c_str(), open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Construct a basic_file on an existing native file handle.
- /**
- * This constructor initialises a file object to hold an existing native file.
- *
- * @param ex The I/O executor that the file will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the file.
- *
- * @param native_file A native file handle.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- basic_file(const executor_type& ex, const native_handle_type& native_file)
- : impl_(0, ex)
- {
- boost::system::error_code ec;
- impl_.get_service().assign(
- impl_.get_implementation(), native_file, ec);
- boost::asio::detail::throw_error(ec, "assign");
- }
- /// Construct a basic_file on an existing native file.
- /**
- * This constructor initialises a file object to hold an existing native file.
- *
- * @param context An execution context which provides the I/O executor that
- * the file will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the file.
- *
- * @param native_file A native file.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- template <typename ExecutionContext>
- basic_file(ExecutionContext& context, const native_handle_type& native_file,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : impl_(0, 0, context)
- {
- boost::system::error_code ec;
- impl_.get_service().assign(
- impl_.get_implementation(), native_file, ec);
- boost::asio::detail::throw_error(ec, "assign");
- }
- /// Move-construct a basic_file from another.
- /**
- * This constructor moves a file from one object to another.
- *
- * @param other The other basic_file object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_file(const executor_type&) constructor.
- */
- basic_file(basic_file&& other) noexcept
- : impl_(std::move(other.impl_))
- {
- }
- /// Move-assign a basic_file from another.
- /**
- * This assignment operator moves a file from one object to another.
- *
- * @param other The other basic_file object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_file(const executor_type&) constructor.
- */
- basic_file& operator=(basic_file&& other)
- {
- impl_ = std::move(other.impl_);
- return *this;
- }
- // All files have access to each other's implementations.
- template <typename Executor1>
- friend class basic_file;
- /// Move-construct a basic_file from a file of another executor type.
- /**
- * This constructor moves a file from one object to another.
- *
- * @param other The other basic_file object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_file(const executor_type&) constructor.
- */
- template <typename Executor1>
- basic_file(basic_file<Executor1>&& other,
- constraint_t<
- is_convertible<Executor1, Executor>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : impl_(std::move(other.impl_))
- {
- }
- /// Move-assign a basic_file from a file of another executor type.
- /**
- * This assignment operator moves a file from one object to another.
- *
- * @param other The other basic_file object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_file(const executor_type&) constructor.
- */
- template <typename Executor1>
- constraint_t<
- is_convertible<Executor1, Executor>::value,
- basic_file&
- > operator=(basic_file<Executor1>&& other)
- {
- basic_file tmp(std::move(other));
- impl_ = std::move(tmp.impl_);
- return *this;
- }
- /// Get the executor associated with the object.
- const executor_type& get_executor() noexcept
- {
- return impl_.get_executor();
- }
- /// Open the file using the specified path.
- /**
- * This function opens the file so that it will use the specified path.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @par Example
- * @code
- * boost::asio::stream_file file(my_context);
- * file.open("/path/to/my/file", boost::asio::stream_file::read_only);
- * @endcode
- */
- void open(const char* path, file_base::flags open_flags)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Open the file using the specified path.
- /**
- * This function opens the file so that it will use the specified path.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @par Example
- * @code
- * boost::asio::stream_file file(my_context);
- * boost::system::error_code ec;
- * file.open("/path/to/my/file", boost::asio::stream_file::read_only, ec);
- * if (ec)
- * {
- * // An error occurred.
- * }
- * @endcode
- */
- BOOST_ASIO_SYNC_OP_VOID open(const char* path,
- file_base::flags open_flags, boost::system::error_code& ec)
- {
- impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Open the file using the specified path.
- /**
- * This function opens the file so that it will use the specified path.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @par Example
- * @code
- * boost::asio::stream_file file(my_context);
- * file.open("/path/to/my/file", boost::asio::stream_file::read_only);
- * @endcode
- */
- void open(const std::string& path, file_base::flags open_flags)
- {
- boost::system::error_code ec;
- impl_.get_service().open(impl_.get_implementation(),
- path.c_str(), open_flags, ec);
- boost::asio::detail::throw_error(ec, "open");
- }
- /// Open the file using the specified path.
- /**
- * This function opens the file so that it will use the specified path.
- *
- * @param path The path name identifying the file to be opened.
- *
- * @param open_flags A set of flags that determine how the file should be
- * opened.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @par Example
- * @code
- * boost::asio::stream_file file(my_context);
- * boost::system::error_code ec;
- * file.open("/path/to/my/file", boost::asio::stream_file::read_only, ec);
- * if (ec)
- * {
- * // An error occurred.
- * }
- * @endcode
- */
- BOOST_ASIO_SYNC_OP_VOID open(const std::string& path,
- file_base::flags open_flags, boost::system::error_code& ec)
- {
- impl_.get_service().open(impl_.get_implementation(),
- path.c_str(), open_flags, ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Assign an existing native file to the file.
- /*
- * This function opens the file to hold an existing native file.
- *
- * @param native_file A native file.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void assign(const native_handle_type& native_file)
- {
- boost::system::error_code ec;
- impl_.get_service().assign(
- impl_.get_implementation(), native_file, ec);
- boost::asio::detail::throw_error(ec, "assign");
- }
- /// Assign an existing native file to the file.
- /*
- * This function opens the file to hold an existing native file.
- *
- * @param native_file A native file.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_file,
- boost::system::error_code& ec)
- {
- impl_.get_service().assign(
- impl_.get_implementation(), native_file, ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Determine whether the file is open.
- bool is_open() const
- {
- return impl_.get_service().is_open(impl_.get_implementation());
- }
- /// Close the file.
- /**
- * This function is used to close the file. Any asynchronous read or write
- * operations will be cancelled immediately, and will complete with the
- * boost::asio::error::operation_aborted error.
- *
- * @throws boost::system::system_error Thrown on failure. Note that, even if
- * the function indicates an error, the underlying descriptor is closed.
- */
- void close()
- {
- boost::system::error_code ec;
- impl_.get_service().close(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "close");
- }
- /// Close the file.
- /**
- * This function is used to close the file. Any asynchronous read or write
- * operations will be cancelled immediately, and will complete with the
- * boost::asio::error::operation_aborted error.
- *
- * @param ec Set to indicate what error occurred, if any. Note that, even if
- * the function indicates an error, the underlying descriptor is closed.
- *
- * @par Example
- * @code
- * boost::asio::stream_file file(my_context);
- * ...
- * boost::system::error_code ec;
- * file.close(ec);
- * if (ec)
- * {
- * // An error occurred.
- * }
- * @endcode
- */
- BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
- {
- impl_.get_service().close(impl_.get_implementation(), ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Release ownership of the underlying native file.
- /**
- * This function causes all outstanding asynchronous read and write
- * operations to finish immediately, and the handlers for cancelled
- * operations will be passed the boost::asio::error::operation_aborted error.
- * Ownership of the native file is then transferred to the caller.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note This function is unsupported on Windows versions prior to Windows
- * 8.1, and will fail with boost::asio::error::operation_not_supported on
- * these platforms.
- */
- #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
- && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
- __declspec(deprecated("This function always fails with "
- "operation_not_supported when used on Windows versions "
- "prior to Windows 8.1."))
- #endif
- native_handle_type release()
- {
- boost::system::error_code ec;
- native_handle_type s = impl_.get_service().release(
- impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "release");
- return s;
- }
- /// Release ownership of the underlying native file.
- /**
- * This function causes all outstanding asynchronous read and write
- * operations to finish immediately, and the handlers for cancelled
- * operations will be passed the boost::asio::error::operation_aborted error.
- * Ownership of the native file is then transferred to the caller.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @note This function is unsupported on Windows versions prior to Windows
- * 8.1, and will fail with boost::asio::error::operation_not_supported on
- * these platforms.
- */
- #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
- && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
- __declspec(deprecated("This function always fails with "
- "operation_not_supported when used on Windows versions "
- "prior to Windows 8.1."))
- #endif
- native_handle_type release(boost::system::error_code& ec)
- {
- return impl_.get_service().release(impl_.get_implementation(), ec);
- }
- /// Get the native file representation.
- /**
- * This function may be used to obtain the underlying representation of the
- * file. This is intended to allow access to native file functionality
- * that is not otherwise provided.
- */
- native_handle_type native_handle()
- {
- return impl_.get_service().native_handle(impl_.get_implementation());
- }
- /// Cancel all asynchronous operations associated with the file.
- /**
- * This function causes all outstanding asynchronous read and write
- * operations to finish immediately, and the handlers for cancelled
- * operations will be passed the boost::asio::error::operation_aborted error.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note Calls to cancel() will always fail with
- * boost::asio::error::operation_not_supported when run on Windows XP, Windows
- * Server 2003, and earlier versions of Windows, unless
- * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
- * two issues that should be considered before enabling its use:
- *
- * @li It will only cancel asynchronous operations that were initiated in the
- * current thread.
- *
- * @li It can appear to complete without error, but the request to cancel the
- * unfinished operations may be silently ignored by the operating system.
- * Whether it works or not seems to depend on the drivers that are installed.
- *
- * For portable cancellation, consider using the close() function to
- * simultaneously cancel the outstanding operations and close the file.
- *
- * When running on Windows Vista, Windows Server 2008, and later, the
- * CancelIoEx function is always used. This function does not have the
- * problems described above.
- */
- #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
- && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
- && !defined(BOOST_ASIO_ENABLE_CANCELIO)
- __declspec(deprecated("By default, this function always fails with "
- "operation_not_supported when used on Windows XP, Windows Server 2003, "
- "or earlier. Consult documentation for details."))
- #endif
- void cancel()
- {
- boost::system::error_code ec;
- impl_.get_service().cancel(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "cancel");
- }
- /// Cancel all asynchronous operations associated with the file.
- /**
- * This function causes all outstanding asynchronous read and write
- * operations to finish immediately, and the handlers for cancelled
- * operations will be passed the boost::asio::error::operation_aborted error.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @note Calls to cancel() will always fail with
- * boost::asio::error::operation_not_supported when run on Windows XP, Windows
- * Server 2003, and earlier versions of Windows, unless
- * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
- * two issues that should be considered before enabling its use:
- *
- * @li It will only cancel asynchronous operations that were initiated in the
- * current thread.
- *
- * @li It can appear to complete without error, but the request to cancel the
- * unfinished operations may be silently ignored by the operating system.
- * Whether it works or not seems to depend on the drivers that are installed.
- *
- * For portable cancellation, consider using the close() function to
- * simultaneously cancel the outstanding operations and close the file.
- *
- * When running on Windows Vista, Windows Server 2008, and later, the
- * CancelIoEx function is always used. This function does not have the
- * problems described above.
- */
- #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
- && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
- && !defined(BOOST_ASIO_ENABLE_CANCELIO)
- __declspec(deprecated("By default, this function always fails with "
- "operation_not_supported when used on Windows XP, Windows Server 2003, "
- "or earlier. Consult documentation for details."))
- #endif
- BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
- {
- impl_.get_service().cancel(impl_.get_implementation(), ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Get the size of the file.
- /**
- * This function determines the size of the file, in bytes.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- uint64_t size() const
- {
- boost::system::error_code ec;
- uint64_t s = impl_.get_service().size(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "size");
- return s;
- }
- /// Get the size of the file.
- /**
- * This function determines the size of the file, in bytes.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- uint64_t size(boost::system::error_code& ec) const
- {
- return impl_.get_service().size(impl_.get_implementation(), ec);
- }
- /// Alter the size of the file.
- /**
- * This function resizes the file to the specified size, in bytes. If the
- * current file size exceeds @c n then any extra data is discarded. If the
- * current size is less than @c n then the file is extended and filled with
- * zeroes.
- *
- * @param n The new size for the file.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void resize(uint64_t n)
- {
- boost::system::error_code ec;
- impl_.get_service().resize(impl_.get_implementation(), n, ec);
- boost::asio::detail::throw_error(ec, "resize");
- }
- /// Alter the size of the file.
- /**
- * This function resizes the file to the specified size, in bytes. If the
- * current file size exceeds @c n then any extra data is discarded. If the
- * current size is less than @c n then the file is extended and filled with
- * zeroes.
- *
- * @param n The new size for the file.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- BOOST_ASIO_SYNC_OP_VOID resize(uint64_t n, boost::system::error_code& ec)
- {
- impl_.get_service().resize(impl_.get_implementation(), n, ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Synchronise the file to disk.
- /**
- * This function synchronises the file data and metadata to disk. Note that
- * the semantics of this synchronisation vary between operation systems.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void sync_all()
- {
- boost::system::error_code ec;
- impl_.get_service().sync_all(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "sync_all");
- }
- /// Synchronise the file to disk.
- /**
- * This function synchronises the file data and metadata to disk. Note that
- * the semantics of this synchronisation vary between operation systems.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- BOOST_ASIO_SYNC_OP_VOID sync_all(boost::system::error_code& ec)
- {
- impl_.get_service().sync_all(impl_.get_implementation(), ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- /// Synchronise the file data to disk.
- /**
- * This function synchronises the file data to disk. Note that the semantics
- * of this synchronisation vary between operation systems.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void sync_data()
- {
- boost::system::error_code ec;
- impl_.get_service().sync_data(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "sync_data");
- }
- /// Synchronise the file data to disk.
- /**
- * This function synchronises the file data to disk. Note that the semantics
- * of this synchronisation vary between operation systems.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- BOOST_ASIO_SYNC_OP_VOID sync_data(boost::system::error_code& ec)
- {
- impl_.get_service().sync_data(impl_.get_implementation(), ec);
- BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
- }
- protected:
- /// Protected destructor to prevent deletion through this type.
- /**
- * This function destroys the file, cancelling any outstanding asynchronous
- * operations associated with the file as if by calling @c cancel.
- */
- ~basic_file()
- {
- }
- #if defined(BOOST_ASIO_HAS_IOCP)
- detail::io_object_impl<detail::win_iocp_file_service, Executor> impl_;
- #elif defined(BOOST_ASIO_HAS_IO_URING)
- detail::io_object_impl<detail::io_uring_file_service, Executor> impl_;
- #endif
- private:
- // Disallow copying and assignment.
- basic_file(const basic_file&) = delete;
- basic_file& operator=(const basic_file&) = delete;
- };
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // defined(BOOST_ASIO_HAS_FILE)
- // || defined(GENERATING_DOCUMENTATION)
- #endif // BOOST_ASIO_BASIC_FILE_HPP
|