userver: userver/fs/blocking/c_file.hpp Source File
Loading...
Searching...
No Matches
c_file.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/blocking/c_file.hpp
4/// @brief @copybrief fs::blocking::CFile
5
6#include <cstdint>
7#include <cstdio>
8#include <filesystem>
9#include <span>
10#include <string_view>
11
12#include <userver/fs/blocking/impl/permissions.hpp>
13#include <userver/utils/zstring_view.hpp>
14
15#include <userver/fs/blocking/open_mode.hpp>
16#include <userver/utils/fast_pimpl.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace fs::blocking {
21
22/// @ingroup userver_universal userver_containers
23///
24/// @brief A `std::FILE*` wrapper
25/// @details The file is closed in the destructor
26/// @note The operations on the file are blocking and not thread-safe
27class CFile final {
28public:
29 /// Creates an empty file handle
31
32 CFile(CFile&&) noexcept;
33 CFile& operator=(CFile&&) noexcept;
34 ~CFile();
35
36 /// @brief Opens the file
37 /// @throws std::runtime_error
39 utils::zstring_view path,
40 OpenMode flags,
41 std::filesystem::perms perms = std::filesystem::perms::owner_read | std::filesystem::perms::owner_write
42 );
43
44 /// @overload
45 /// Accepts `boost::filesystem::perms`.
46 /// @deprecated use the std::filesystem overload
47 template <impl::LegacyPermissions Permissions>
48 CFile(utils::zstring_view path, OpenMode flags, Permissions perms)
49 : fs::blocking::CFile(path, flags, impl::ToStdPermissions(perms))
50 {}
51
52 /// @brief Adopt the `std::FILE*` directly
53 explicit CFile(std::FILE* file) noexcept;
54
55 /// Checks if the file is open
56 bool IsOpen() const noexcept;
57
58 /// Returns the underlying file handle
59 std::FILE* GetNative() &;
60
61 /// Passes the ownership of the file to the caller
62 std::FILE* Release() &&;
63
64 /// @brief Closes the file manually
65 /// @throws std::runtime_error
66 void Close() &&;
67
68 /// @brief Reads data from the file
69 /// @returns The amount of bytes actually acquired, which can be equal
70 /// to `max_size`, or less on end-of-file
71 /// @throws std::runtime_error
72 std::size_t Read(std::span<char> buffer);
73
74 /// @brief Writes data to the file
75 /// @warning Unless `Flush` is called, there is no guarantee the file on disk
76 /// is actually updated
77 /// @throws std::runtime_error
78 void Write(std::string_view data);
79
80 /// @brief Synchronizes the written data with the file on disk
81 /// @throws std::runtime_error
82 void Flush();
83
84 /// @brief Synchronizes the written data with the file on disk
85 /// without fsync
86 /// @throws std::runtime_error
87 void FlushLight();
88
89 /// @brief Fetches the current position in the file
90 /// @throws std::runtime_error
91 std::uint64_t GetPosition() const;
92
93 /// @brief Fetches the file size
94 /// @throws std::runtime_error
95 std::uint64_t GetSize() const;
96
97private:
98 struct Impl;
99 utils::FastPimpl<Impl, sizeof(char*), alignof(char*)> impl_;
100};
101
102} // namespace fs::blocking
103
104USERVER_NAMESPACE_END