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 {
20 public:
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,
24 tracing::ScopeTime& scope);
25
26 void Finish() override;
27
28 private:
29 void WriteRaw(std::string_view data) override;
30
31 fs::blocking::CFile file_;
32 std::string final_path_;
33 std::string path_;
34 boost::filesystem::perms perms_;
35 utils::StreamingCpuRelax cpu_relax_;
36};
37
38/// A handle to a dump file. File operations block the thread.
39class FileReader final : public Reader {
40 public:
41 /// @brief Opens an existing dump file
42 /// @throws `Error` on a filesystem error
43 explicit FileReader(std::string path);
44
45 void Finish() override;
46
47 private:
48 std::string_view ReadRaw(std::size_t max_size) override;
49
50 fs::blocking::CFile file_;
51 std::string path_;
52 std::string curr_chunk_;
53};
54
55class FileOperationsFactory final : public OperationsFactory {
56 public:
57 explicit FileOperationsFactory(boost::filesystem::perms perms);
58
59 std::unique_ptr<Reader> CreateReader(std::string full_path) override;
60
61 std::unique_ptr<Writer> CreateWriter(std::string full_path,
62 tracing::ScopeTime& scope) override;
63
64 private:
65 const boost::filesystem::perms perms_;
66};
67
68} // namespace dump
69
70USERVER_NAMESPACE_END