userver: userver/dump/operations_file.hpp Source File
Loading...
Searching...
No Matches
operations_file.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dump/operations_file.hpp
4/// @brief File-based dump Reader and Writer implementations
5
6#include <filesystem>
7#include <utility>
8
9#include <userver/fs/blocking/c_file.hpp>
10#include <userver/fs/blocking/impl/permissions.hpp>
11#include <userver/utils/cpu_relax.hpp>
12#include <userver/utils/fast_pimpl.hpp>
13
14#include <userver/dump/factory.hpp>
15#include <userver/dump/operations.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace dump {
20
21/// A handle to a dump file. File operations block the thread.
22class FileWriter final : public Writer {
23public:
24 /// @brief Creates a new dump file and opens it
25 /// @param perms final permissions to set when finalizing the dump
26 /// @throws `Error` on a filesystem error
27 explicit FileWriter(std::string path, std::filesystem::perms perms, tracing::ScopeTime& scope);
28
29 /// @overload
30 /// Accepts `boost::filesystem::perms`; permission option flags are ignored.
31 /// @deprecated use the std::filesystem overload
32 template <fs::blocking::impl::LegacyPermissions Permissions>
33 explicit FileWriter(std::string path, Permissions perms, tracing::ScopeTime& scope)
34 : dump::FileWriter(std::move(path), fs::blocking::impl::ToStdPermissions(perms), scope)
35 {}
36
37 void Finish() override;
38
39private:
40 void WriteRaw(std::string_view data) override;
41
42 fs::blocking::CFile file_;
43 std::string final_path_;
44 std::string path_;
45 std::filesystem::perms perms_;
46 utils::StreamingCpuRelax cpu_relax_;
47};
48
49/// A handle to a dump file. File operations block the thread.
50class FileReader final : public Reader {
51public:
52 /// @brief Opens an existing dump file
53 /// @throws `Error` on a filesystem error
54 explicit FileReader(std::string path);
55
56 void Finish() override;
57
58private:
59 std::string_view ReadRaw(std::size_t max_size) override;
60
61 void BackUp(std::size_t size) override;
62
63 fs::blocking::CFile file_;
64 std::string path_;
65 std::string curr_chunk_;
66};
67
68class FileOperationsFactory final : public OperationsFactory {
69public:
70 /// @brief Creates a factory that writes dumps with the specified permissions.
71 explicit FileOperationsFactory(std::filesystem::perms perms);
72
73 /// @overload
74 /// Accepts `boost::filesystem::perms`; permission option flags are ignored.
75 /// @deprecated use the std::filesystem overload
76 template <fs::blocking::impl::LegacyPermissions Permissions>
77 explicit FileOperationsFactory(Permissions perms)
78 : dump::FileOperationsFactory(fs::blocking::impl::ToStdPermissions(perms))
79 {}
80
81 std::unique_ptr<Reader> CreateReader(std::string full_path) override;
82
83 std::unique_ptr<Writer> CreateWriter(std::string full_path, tracing::ScopeTime& scope) override;
84
85private:
86 const std::filesystem::perms perms_;
87};
88
89} // namespace dump
90
91USERVER_NAMESPACE_END