Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
temporary_directory.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_TEMPORARY_DIRECTORY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_TEMPORARY_DIRECTORY_HPP_
7
8// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
9// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
10// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12
13#include <cstdint>
14#include <utility>
15
16#include "arene/base/filesystem/directory_handle.hpp"
17#include "arene/base/filesystem/path_string.hpp"
18#include "arene/base/inline_string.hpp"
19
20namespace arene {
21namespace base {
22namespace filesystem {
23
24/// @brief An RAII helper class to manage a temporary directory
25///
26/// The directory is created on construction, and recursively removed on destruction.
27class temporary_directory {
28 public:
29 /// @brief Create a new temporary directory with a unique name.
30 /// @param root parent directory which will contain the temporary directory
31 /// @throws std::system_error If an error occurs that prevents inspecting or modifying a directory.
32 ///
33 /// If the @c TMPDIR environment variable is set, the temporary directory is created under the specified directory,
34 /// otherwise it is created under @c root
35 explicit temporary_directory(path_string root);
36
37 /// @brief Create a new temporary directory with a unique name.
38 ///
39 /// If the @c TMPDIR environment variable is set, the temporary directory is created under the specified directory,
40 /// otherwise it is created under @c /tmp
41 temporary_directory()
42 : temporary_directory{"/tmp"} {}
43
44 /// @brief Remove the temporary directory and all its contents
45 ~temporary_directory();
46
47 /// @brief deleted copy constructor
48 temporary_directory(temporary_directory const&) = delete;
49 /// @brief deleted copy assignment operator
50 auto operator=(temporary_directory const&) -> temporary_directory& = delete;
51
52 /// @brief default move constructor
53 temporary_directory(temporary_directory&&) = default;
54 /// @brief default move assignment operator
55 auto operator=(temporary_directory&&) -> temporary_directory& = default;
56
57 /// @brief Get the full path of the temporary directory
58 /// @return reference to the path string
59 auto path() const noexcept -> path_string const& { return path_; }
60
61 private:
62 /// @brief Create a directory with a unique name under the current root.
63 /// @post relative_path_ contains the name of the directory.
64 /// @post handle_ contains a handle to the directory.
65 /// @throws std::system_error if an error occurs.
66 void create_uniquely_named_directory();
67
68 /// @brief The length of the relative path segment used to construct the temporary directory name
69 static constexpr std::uint32_t relative_path_length{16U};
70 /// @brief The path of the temporary directory relative to the parent folder
71 inline_string<relative_path_length> relative_path_;
72 /// @brief The full path of the temporary directory
73 path_string path_;
74 /// @brief A handle to the parent directory
75 directory_handle parent_handle_;
76 /// @brief A handle to the temporary directory itself
77 directory_handle handle_;
78};
79
80} // namespace filesystem
81} // namespace base
82} // namespace arene
83
84#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_TEMPORARY_DIRECTORY_HPP_
Definition directory_handle.cpp:39
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10