Line data Source code
1 : //
2 : // Copyright (c) 2022 Vinnie Falco (vinnie.falco@gmail.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/cppalliance/http_proto
8 : //
9 :
10 : #ifndef BOOST_HTTP_PROTO_FILE_POSIX_HPP
11 : #define BOOST_HTTP_PROTO_FILE_POSIX_HPP
12 :
13 : #include <boost/http_proto/detail/config.hpp>
14 :
15 : #if ! defined(BOOST_HTTP_PROTO_NO_POSIX_FILE)
16 : # if ! defined(__APPLE__) && ! defined(__linux__)
17 : # define BOOST_HTTP_PROTO_NO_POSIX_FILE
18 : # endif
19 : #endif
20 :
21 : #if ! defined(BOOST_HTTP_PROTO_USE_POSIX_FILE)
22 : # if ! defined(BOOST_HTTP_PROTO_NO_POSIX_FILE)
23 : # define BOOST_HTTP_PROTO_USE_POSIX_FILE 1
24 : # else
25 : # define BOOST_HTTP_PROTO_USE_POSIX_FILE 0
26 : # endif
27 : #endif
28 :
29 : #if BOOST_HTTP_PROTO_USE_POSIX_FILE
30 :
31 : #include <boost/http_proto/error.hpp>
32 : #include <boost/http_proto/file_base.hpp>
33 : #include <boost/system/error_code.hpp>
34 : #include <cstdint>
35 :
36 : namespace boost {
37 : namespace http_proto {
38 :
39 : /** An implementation of File for POSIX systems.
40 :
41 : This class implements a <em>File</em> using POSIX interfaces.
42 : */
43 : class file_posix
44 : {
45 : int fd_ = -1;
46 :
47 : BOOST_HTTP_PROTO_DECL
48 : static
49 : int
50 : native_close(int& fd);
51 :
52 : public:
53 : /** The type of the underlying file handle.
54 :
55 : This is platform-specific.
56 : */
57 : using native_handle_type = int;
58 :
59 : /** Destructor
60 :
61 : If the file is open it is first closed.
62 : */
63 : BOOST_HTTP_PROTO_DECL
64 : ~file_posix();
65 :
66 : /** Constructor
67 :
68 : There is no open file initially.
69 : */
70 : file_posix() = default;
71 :
72 : /** Constructor
73 :
74 : The moved-from object behaves as if default constructed.
75 : */
76 : BOOST_HTTP_PROTO_DECL
77 : file_posix(
78 : file_posix&& other) noexcept;
79 :
80 : /** Assignment
81 :
82 : The moved-from object behaves as if default constructed.
83 : */
84 : BOOST_HTTP_PROTO_DECL
85 : file_posix&
86 : operator=(
87 : file_posix&& other) noexcept;
88 :
89 : /// Returns the native handle associated with the file.
90 : native_handle_type
91 2 : native_handle() const
92 : {
93 2 : return fd_;
94 : }
95 :
96 : /** Set the native handle associated with the file.
97 :
98 : If the file is open it is first closed.
99 :
100 : @param fd The native file handle to assign.
101 : */
102 : BOOST_HTTP_PROTO_DECL
103 : void
104 : native_handle(native_handle_type fd);
105 :
106 : /// Returns `true` if the file is open
107 : bool
108 10 : is_open() const
109 : {
110 10 : return fd_ != -1;
111 : }
112 :
113 : /** Close the file if open
114 :
115 : @param ec Set to the error, if any occurred.
116 : */
117 : BOOST_HTTP_PROTO_DECL
118 : void
119 : close(system::error_code& ec);
120 :
121 : /** Open a file at the given path with the specified mode
122 :
123 : @param path The utf-8 encoded path to the file
124 :
125 : @param mode The file mode to use
126 :
127 : @param ec Set to the error, if any occurred
128 : */
129 : BOOST_HTTP_PROTO_DECL
130 : void
131 : open(char const* path, file_mode mode, system::error_code& ec);
132 :
133 : /** Return the size of the open file
134 :
135 : @param ec Set to the error, if any occurred
136 :
137 : @return The size in bytes
138 : */
139 : BOOST_HTTP_PROTO_DECL
140 : std::uint64_t
141 : size(system::error_code& ec) const;
142 :
143 : /** Return the current position in the open file
144 :
145 : @param ec Set to the error, if any occurred
146 :
147 : @return The offset in bytes from the beginning of the file
148 : */
149 : BOOST_HTTP_PROTO_DECL
150 : std::uint64_t
151 : pos(system::error_code& ec) const;
152 :
153 : /** Adjust the current position in the open file
154 :
155 : @param offset The offset in bytes from the beginning of the file
156 :
157 : @param ec Set to the error, if any occurred
158 : */
159 : BOOST_HTTP_PROTO_DECL
160 : void
161 : seek(std::uint64_t offset, system::error_code& ec);
162 :
163 : /** Read from the open file
164 :
165 : @param buffer The buffer for storing the result of the read
166 :
167 : @param n The number of bytes to read
168 :
169 : @param ec Set to the error, if any occurred
170 : */
171 : BOOST_HTTP_PROTO_DECL
172 : std::size_t
173 : read(void* buffer, std::size_t n, system::error_code& ec) const;
174 :
175 : /** Write to the open file
176 :
177 : @param buffer The buffer holding the data to write
178 :
179 : @param n The number of bytes to write
180 :
181 : @param ec Set to the error, if any occurred
182 : */
183 : BOOST_HTTP_PROTO_DECL
184 : std::size_t
185 : write(void const* buffer, std::size_t n, system::error_code& ec);
186 : };
187 :
188 : } // http_proto
189 : } // boost
190 :
191 : #endif
192 :
193 : #endif
|