file_posix.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Copyright (c) 2015-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_FILE_POSIX_HPP
  10. #define BOOST_BEAST_CORE_FILE_POSIX_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #if ! defined(BOOST_BEAST_NO_POSIX_FILE)
  13. # if ! defined(__APPLE__) && ! defined(__linux__)
  14. # define BOOST_BEAST_NO_POSIX_FILE
  15. # endif
  16. #endif
  17. #if ! defined(BOOST_BEAST_USE_POSIX_FILE)
  18. # if ! defined(BOOST_BEAST_NO_POSIX_FILE)
  19. # define BOOST_BEAST_USE_POSIX_FILE 1
  20. # else
  21. # define BOOST_BEAST_USE_POSIX_FILE 0
  22. # endif
  23. #endif
  24. #if BOOST_BEAST_USE_POSIX_FILE
  25. #include <boost/beast/core/error.hpp>
  26. #include <boost/beast/core/file_base.hpp>
  27. #include <cstdint>
  28. namespace boost {
  29. namespace beast {
  30. /** An implementation of File for POSIX systems.
  31. This class implements a <em>File</em> using POSIX interfaces.
  32. */
  33. class file_posix
  34. {
  35. int fd_ = -1;
  36. BOOST_BEAST_DECL
  37. static
  38. int
  39. native_close(int& fd);
  40. public:
  41. /** The type of the underlying file handle.
  42. This is platform-specific.
  43. */
  44. using native_handle_type = int;
  45. /** Destructor
  46. If the file is open it is first closed.
  47. */
  48. BOOST_BEAST_DECL
  49. ~file_posix();
  50. /** Constructor
  51. There is no open file initially.
  52. */
  53. file_posix() = default;
  54. /** Constructor
  55. The moved-from object behaves as if default constructed.
  56. */
  57. BOOST_BEAST_DECL
  58. file_posix(file_posix&& other);
  59. /** Assignment
  60. The moved-from object behaves as if default constructed.
  61. */
  62. BOOST_BEAST_DECL
  63. file_posix& operator=(file_posix&& other);
  64. /// Returns the native handle associated with the file.
  65. native_handle_type
  66. native_handle() const
  67. {
  68. return fd_;
  69. }
  70. /** Set the native handle associated with the file.
  71. If the file is open it is first closed.
  72. @param fd The native file handle to assign.
  73. */
  74. BOOST_BEAST_DECL
  75. void
  76. native_handle(native_handle_type fd);
  77. /// Returns `true` if the file is open
  78. bool
  79. is_open() const
  80. {
  81. return fd_ != -1;
  82. }
  83. /** Close the file if open
  84. @param ec Set to the error, if any occurred.
  85. */
  86. BOOST_BEAST_DECL
  87. void
  88. close(error_code& ec);
  89. /** Open a file at the given path with the specified mode
  90. @param path The utf-8 encoded path to the file
  91. @param mode The file mode to use
  92. @param ec Set to the error, if any occurred
  93. */
  94. BOOST_BEAST_DECL
  95. void
  96. open(char const* path, file_mode mode, error_code& ec);
  97. /** Return the size of the open file
  98. @param ec Set to the error, if any occurred
  99. @return The size in bytes
  100. */
  101. BOOST_BEAST_DECL
  102. std::uint64_t
  103. size(error_code& ec) const;
  104. /** Return the current position in the open file
  105. @param ec Set to the error, if any occurred
  106. @return The offset in bytes from the beginning of the file
  107. */
  108. BOOST_BEAST_DECL
  109. std::uint64_t
  110. pos(error_code& ec) const;
  111. /** Adjust the current position in the open file
  112. @param offset The offset in bytes from the beginning of the file
  113. @param ec Set to the error, if any occurred
  114. */
  115. BOOST_BEAST_DECL
  116. void
  117. seek(std::uint64_t offset, error_code& ec);
  118. /** Read from the open file
  119. @param buffer The buffer for storing the result of the read
  120. @param n The number of bytes to read
  121. @param ec Set to the error, if any occurred
  122. */
  123. BOOST_BEAST_DECL
  124. std::size_t
  125. read(void* buffer, std::size_t n, error_code& ec) const;
  126. /** Write to the open file
  127. @param buffer The buffer holding the data to write
  128. @param n The number of bytes to write
  129. @param ec Set to the error, if any occurred
  130. */
  131. BOOST_BEAST_DECL
  132. std::size_t
  133. write(void const* buffer, std::size_t n, error_code& ec);
  134. };
  135. } // beast
  136. } // boost
  137. #ifdef BOOST_BEAST_HEADER_ONLY
  138. #include <boost/beast/core/impl/file_posix.ipp>
  139. #endif
  140. #endif
  141. #endif