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