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

source code of boost/libs/beast/include/boost/beast/core/file_posix.hpp