userver: userver/fs/blocking/temp_directory.hpp Source File
Loading...
Searching...
No Matches
temp_directory.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/blocking/temp_directory.hpp
4/// @brief @copybrief fs::blocking::TempDirectory
5
6#include <string>
7#include <string_view>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace fs::blocking {
12
13/// @ingroup userver_universal userver_containers
14///
15/// @brief A unique directory for temporary files. The directory is deleted when
16/// the `TempDirectory` is destroyed.
17/// @note The directory, as well as any newly created parent directories,
18/// has permissions=0700.
19class TempDirectory final {
20 public:
21 /// @brief Create the directory at the default path for temporary files
22 /// @throws std::runtime_error
23 static TempDirectory Create();
24
25 /// @brief Create the directory at the specified path
26 /// @param parent_path The directory where the temporary directory
27 /// will be created
28 /// @param name_prefix Directory name prefix, a random string will be added
29 /// after the prefix
30 /// @throws std::runtime_error
31 static TempDirectory Create(std::string_view parent_path,
32 std::string_view name_prefix);
33
34 TempDirectory() = default;
35 TempDirectory(TempDirectory&& other) noexcept;
36 TempDirectory& operator=(TempDirectory&& other) noexcept;
37 ~TempDirectory();
38
39 /// Take ownership of an existing directory
40 static TempDirectory Adopt(std::string path);
41
42 /// The directory path
43 const std::string& GetPath() const;
44
45 /// @brief Remove the directory early
46 /// @throws std::runtime_error
47 void Remove() &&;
48
49 private:
50 explicit TempDirectory(std::string&& path);
51
52 std::string path_;
53};
54
55} // namespace fs::blocking
56
57USERVER_NAMESPACE_END