userver: userver/dump/operations_file.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
operations_file.hpp
1#pragma once
2
3#include <chrono>
4
5#include <boost/filesystem/operations.hpp>
6
7#include <userver/fs/blocking/c_file.hpp>
8#include <userver/utils/cpu_relax.hpp>
9#include <userver/utils/fast_pimpl.hpp>
10
11#include <userver/dump/factory.hpp>
12#include <userver/dump/operations.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace dump {
17
18/// A handle to a dump file. File operations block the thread.
19class FileWriter final : public Writer {
20public:
21 /// @brief Creates a new dump file and opens it
22 /// @throws `Error` on a filesystem error
23 explicit FileWriter(std::string path, boost::filesystem::perms perms, tracing::ScopeTime& scope);
24
25 void Finish() override;
26
27private:
28 void WriteRaw(std::string_view data) override;
29
30 fs::blocking::CFile file_;
31 std::string final_path_;
32 std::string path_;
33 boost::filesystem::perms perms_;
34 utils::StreamingCpuRelax cpu_relax_;
35};
36
37/// A handle to a dump file. File operations block the thread.
38class FileReader final : public Reader {
39public:
40 /// @brief Opens an existing dump file
41 /// @throws `Error` on a filesystem error
42 explicit FileReader(std::string path);
43
44 void Finish() override;
45
46private:
47 std::string_view ReadRaw(std::size_t max_size) override;
48
49 void BackUp(std::size_t size) override;
50
51 fs::blocking::CFile file_;
52 std::string path_;
53 std::string curr_chunk_;
54};
55
56class FileOperationsFactory final : public OperationsFactory {
57public:
58 explicit FileOperationsFactory(boost::filesystem::perms perms);
59
60 std::unique_ptr<Reader> CreateReader(std::string full_path) override;
61
62 std::unique_ptr<Writer> CreateWriter(std::string full_path, tracing::ScopeTime& scope) override;
63
64private:
65 const boost::filesystem::perms perms_;
66};
67
68} // namespace dump
69
70USERVER_NAMESPACE_END