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