userver: userver/fs/blocking/file_descriptor.hpp Source File
Loading...
Searching...
No Matches
file_descriptor.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/blocking/file_descriptor.hpp
4/// @brief @copybrief fs::blocking::FileDescriptor
5
6#include <span>
7#include <string_view>
8
9#include <userver/utils/boost_filesystem_file_status.hpp>
10#include <userver/utils/zstring_view.hpp>
11
12#include <userver/fs/blocking/open_mode.hpp>
13
14extern "C" {
15struct iovec;
16}
17
18USERVER_NAMESPACE_BEGIN
19
20namespace fs::blocking {
21
22/// @ingroup userver_universal userver_containers
23///
24/// @brief A file descriptor wrapper
25/// @details The file is closed in the destructor
26/// @note The operations on the file are blocking and not thread-safe
27class FileDescriptor final {
28public:
29 /// @brief Open a file using `::open`
30 /// @throws std::runtime_error
31 static FileDescriptor Open(
32 utils::zstring_view path,
33 OpenMode flags,
34 boost::filesystem::perms perms = boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_write
35 );
36
37 /// @brief Open a directory node
38 /// @note The only valid operation for such a `FileDescriptor` is `FSync`.
39 /// @throws std::runtime_error
40 static FileDescriptor OpenDirectory(utils::zstring_view path);
41
42 /// @brief Use the file descriptor directly
43 static FileDescriptor AdoptFd(int fd) noexcept;
44
45 /// @brief Duplicate the file descriptor
46 static FileDescriptor DupFd(int fd) noexcept;
47
48 /// @brief Default constructor
49 /// @note The file descriptor is not open
51
52 FileDescriptor(FileDescriptor&& other) noexcept;
53 FileDescriptor& operator=(FileDescriptor&& other) noexcept;
54 ~FileDescriptor();
55
56 /// @brief Checks if the file is open
57 /// @note Operations can only be performed on an open `FileDescriptor`.
58 bool IsOpen() const noexcept;
59
60 /// @brief Closes the file manually
61 /// @throws std::runtime_error
62 void Close() &&;
63
64 /// Returns the native file handle
65 int GetNative() const noexcept;
66
67 /// Passes the ownership of the file descriptor to the caller
68 int Release() &&;
69
70 /// @brief Writes data to the file
71 /// @warning Unless `FSync` is called, there is no guarantee the data is stored on disk safely.
72 /// @throws std::runtime_error
73 void Write(std::string_view contents);
74
75 /// @brief Writes data to the file, has no IOV_MAX limits on contents.size()
76 /// @warning Unless `FSync` is called, there is no guarantee the data is stored on disk safely.
77 /// @throws std::runtime_error
78 void Write(std::span<const struct iovec> contents);
79
80 /// @brief Reads data from the file at current offset
81 /// @returns The amount of bytes actually acquired, which can be equal to `max_size`, or less on end-of-file
82 /// @throws std::runtime_error
83 std::size_t Read(char* buffer, std::size_t max_size);
84
85 /// @brief Sets the file read/write offset from the beginning of the file
86 /// @throws std::runtime_error
87 void Seek(std::size_t offset_in_bytes);
88
89 /// @brief Makes sure the written data is actually stored on disk
90 /// @throws std::runtime_error
91 void FSync();
92
93 /// @brief Fetches the file size
94 /// @throws std::runtime_error
95 std::size_t GetSize() const;
96
97private:
98 explicit FileDescriptor(int fd);
99
100 friend class TempFile;
101
102 int fd_;
103};
104
105} // namespace fs::blocking
106
107USERVER_NAMESPACE_END