userver: userver/dump/operations_file.hpp Source File
Loading...
Searching...
No Matches
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 fs::blocking::CFile file_;
50 std::string path_;
51 std::string curr_chunk_;
52};
53
54class FileOperationsFactory final : public OperationsFactory {
55public:
56 explicit FileOperationsFactory(boost::filesystem::perms perms);
57
58 std::unique_ptr<Reader> CreateReader(std::string full_path) override;
59
60 std::unique_ptr<Writer> CreateWriter(std::string full_path, tracing::ScopeTime& scope) override;
61
62private:
63 const boost::filesystem::perms perms_;
64};
65
66} // namespace dump
67
68USERVER_NAMESPACE_END