userver: userver/fs/blocking/temp_file.hpp Source File
Loading...
Searching...
No Matches
temp_file.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/blocking/temp_file.hpp
4/// @brief @copybrief fs::blocking::TempFile
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 temporary file. The file is deleted when the `TempFile`
16/// object is destroyed.
17/// @note The file has permissions=0600. Any newly created parent directories
18/// have permissions=0700.
19/// @note The newly created file is immediately opened with read-write
20/// permissions. The file descriptor can be accessed using `File`.
21class TempFile final {
22public:
23 /// @brief Create the file at the default path for temporary files
24 /// @throws std::runtime_error
25 static TempFile Create();
26
27 /// @brief Create the file at the specified path
28 /// @param parent_path The directory where the temporary file will be created
29 /// @param name_prefix File name prefix, a random string will be added
30 /// after the prefix
31 /// @throws std::runtime_error
32 static TempFile Create(std::string_view parent_path, std::string_view name_prefix);
33
34 TempFile() = delete;
35 TempFile(TempFile&& other) noexcept;
36 TempFile& operator=(TempFile&& other) noexcept;
37 ~TempFile();
38
39 /// Take ownership of an existing file
40 static TempFile Adopt(std::string path);
41
42 /// The file path
43 const std::string& GetPath() const;
44
45 /// @brief Remove the file early
46 /// @throws std::runtime_error
47 void Remove() &&;
48
49private:
50 explicit TempFile(std::string&& path);
51
52 std::string path_;
53};
54
55} // namespace fs::blocking
56
57USERVER_NAMESPACE_END