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