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
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.
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
43
44 /// @brief Remove the temporary directory and all its contents
46
47 /// @brief deleted copy constructor
49 /// @brief deleted copy assignment operator
51
52 /// @brief default move constructor
54 /// @brief default move assignment operator
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
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_
auto operator=(temporary_directory const &) -> temporary_directory &=delete
deleted copy assignment operator
temporary_directory(path_string root)
Create a new temporary directory with a unique name.
Definition temporary_directory.cpp:323
~temporary_directory()
Remove the temporary directory and all its contents.
Definition temporary_directory.cpp:338
temporary_directory()
Create a new temporary directory with a unique name.
Definition temporary_directory.hpp:41
auto operator=(temporary_directory &&) -> temporary_directory &=default
default move assignment operator
temporary_directory(temporary_directory &&)=default
default move constructor
auto path() const noexcept -> path_string const &
Get the full path of the temporary directory.
Definition temporary_directory.hpp:59
temporary_directory(temporary_directory const &)=delete
deleted copy constructor
A string type with a fixed maximum capacity, where the data is stored internally without allocations.
Definition inline_string.hpp:162
Public export header for the inline_string subpackage.
Definition directory_handle.cpp:39
inline_string< PATH_MAX > path_string
An inline_string with storage large enough to hold a file path.
Definition path_string.hpp:21
Definition accumulate.hpp:37
Definition accumulate.hpp:36
::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition cstdint.hpp:41