18#if defined(OPENVPN_PLATFORM_WIN)
19#error unix file methods not supported on Windows
46 const std::uint64_t mtime_ns,
51 int flags = O_WRONLY | O_CLOEXEC;
53 flags |= O_CREAT | O_TRUNC;
57 const int eno = errno;
58 throw file_unix_error(fn +
" : open for write : " +
strerror_str(eno));
69 const int eno = errno;
70 throw file_unix_error(fn +
" : write error : " +
strerror_str(eno));
73 throw file_unix_error(fn +
" : incomplete write, request_size=" + std::to_string(size) +
" actual_size=" + std::to_string(len));
85 throw file_unix_error(fn +
" : close for write : " +
strerror_str(eno));
91 const std::uint64_t mtime_ns,
99 const std::uint64_t mtime_ns,
107 const std::uint64_t mtime_ns,
108 const std::string &content)
118 const std::uint64_t max_size = 0,
119 const unsigned int buffer_flags = 0,
120 std::uint64_t *mtime_ns =
nullptr)
123 ScopedFD fd(::open(fn.c_str(), O_RDONLY | O_CLOEXEC));
126 const int eno = errno;
129 throw file_unix_error(fn +
" : open for read : " +
strerror_str(eno));
137 const off_t length = ::lseek(fd(), 0, SEEK_END);
140 const int eno = errno;
141 throw file_unix_error(fn +
" : seek end error : " +
strerror_str(eno));
143 if (::lseek(fd(), 0, SEEK_SET) != 0)
145 const int eno = errno;
146 throw file_unix_error(fn +
" : seek begin error : " +
strerror_str(eno));
150 if (max_size && std::uint64_t(length) > max_size)
151 throw file_unix_error(fn +
" : file too large [" + std::to_string(length) +
'/' + std::to_string(max_size) +
']');
164 throw file_unix_error(fn +
" : close for read : " +
strerror_str(eno));
171template <
typename STRING>
174 std::uint64_t *mtime_ns =
nullptr)
183 const size_t remaining =
out.remaining(0);
186 const ssize_t status = ::read(fd(),
out.data_end(), remaining);
191 out.inc_size(status);
196 const std::uint64_t max_size = 0,
197 const unsigned int buffer_flags = 0,
198 std::uint64_t *mtime_ns =
nullptr)
204 return std::string();
const T * c_data() const
Returns a const pointer to the start of the buffer.
size_t size() const
Returns the size of the buffer in T objects.
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
#define OPENVPN_EXCEPTION(C)
const char * to_cstring(const std::string &str)
Support deferred server-side state creation when client connects.
int update_file_mod_time_nanoseconds(const std::string &filename, const std::uint64_t nanoseconds_since_epooch)
void write_binary_unix(const std::string &fn, const mode_t mode, const std::uint64_t mtime_ns, const void *buf, const ssize_t size)
int read_binary_unix_fast(const STRING &fn, Buffer &out, std::uint64_t *mtime_ns=nullptr)
bool buf_read(const int fd, Buffer &buf, const std::string &title)
ssize_t write_retry(int fd, const void *buf, size_t count)
std::string read_text_unix(const std::string &filename, const std::uint64_t max_size=0, const unsigned int buffer_flags=0, std::uint64_t *mtime_ns=nullptr)
BufferPtr read_binary_unix(const std::string &fn, const std::uint64_t max_size=0, const unsigned int buffer_flags=0, std::uint64_t *mtime_ns=nullptr)
static constexpr mode_t WRITE_BINARY_UNIX_EXISTING
std::uint64_t fd_mod_time_nanoseconds(const int fd)
void write_text_unix(const std::string &fn, const mode_t mode, const std::uint64_t mtime_ns, const std::string &content)
RCPtr< BufferAllocatedRc > BufferPtr
std::string buf_to_string(const Buffer &buf)
std::string strerror_str(const int errnum)
static std::stringstream out