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 {
20public:
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, std::string_view name_prefix);
32
33 TempDirectory() = default;
34 TempDirectory(TempDirectory&& other) noexcept;
35 TempDirectory& operator=(TempDirectory&& other) noexcept;
36 ~TempDirectory();
37
38 /// Take ownership of an existing directory
39 static TempDirectory Adopt(std::string path);
40
41 /// The directory path
42 const std::string& GetPath() const;
43
44 /// @brief Remove the directory early
45 /// @throws std::runtime_error
46 void Remove() &&;
47
48private:
49 explicit TempDirectory(std::string&& path);
50
51 std::string path_;
52};
53
54} // namespace fs::blocking
55
56USERVER_NAMESPACE_END