2121namespace tmp {
2222namespace {
2323
24- // / Checks that the given label is valid to attach to a temporary entry path
25- // / @param[in] label The label to check validity for
26- // / @returns `true` if the label is valid, `false` otherwise
27- bool is_label_valid (const fs::path& label) {
28- return label.empty () || (++label.begin () == label.end () &&
29- label.is_relative () && !label.has_root_path () &&
30- label.filename () != " ." && label.filename () != " .." );
24+ // / Checks if the given prefix is valid to attach to a temporary directory name
25+ // / @param[in] prefix The prefix to check validity for
26+ // / @returns `true` if the prefix is valid, `false` otherwise
27+ bool is_prefix_valid (const fs::path& prefix) {
28+ return prefix.empty () ||
29+ (++prefix.begin () == prefix.end () && prefix.is_relative () &&
30+ !prefix.has_root_path () && prefix.filename () != " ." &&
31+ prefix.filename () != " .." );
3132}
3233
33- // / Checks that the given label is valid to attach to a temporary entry path
34- // / @param label The label to check validity for
35- // / @throws std::invalid_argument if the label cannot be attached to a path
36- void validate_label (const fs::path& label ) {
37- if (!is_label_valid (label )) {
34+ // / Checks that the given prefix is valid to attach to a temporary entry path
35+ // / @param prefix The prefix to check validity for
36+ // / @throws std::invalid_argument if the prefix cannot be attached to a path
37+ void validate_prefix (const fs::path& prefix ) {
38+ if (!is_prefix_valid (prefix )) {
3839 throw std::invalid_argument (
39- " Cannot create a temporary entry: label must be empty or a valid "
40+ " Cannot create a temporary entry: prefix must be empty or a valid "
4041 " single-segmented relative pathname" );
4142 }
4243}
4344
4445#ifdef _WIN32
45- // / Creates a temporary path with the given label
46- // / @note label must be valid
47- // / @param[in] label A label to attach to the path pattern
46+ // / Creates a temporary path with the given prefix
47+ // / @note prefix must be valid
48+ // / @param[in] prefix A prefix to attach to the path pattern
4849// / @returns A unique temporary path
49- fs::path make_path (std::string_view label ) {
50+ fs::path make_path (std::string_view prefix ) {
5051 constexpr static std::size_t CHARS_IN_GUID = 39 ;
5152 GUID guid;
5253 CoCreateGuid (&guid);
@@ -58,25 +59,23 @@ fs::path make_path(std::string_view label) {
5859 guid.Data4 [3 ], guid.Data4 [4 ], guid.Data4 [5 ], guid.Data4 [6 ],
5960 guid.Data4 [7 ]);
6061
61- return fs::temp_directory_path () / label / name;
62+ fs::path path = fs::temp_directory_path () / prefix;
63+ path += name;
64+
65+ return path;
6266}
6367#else
64- // / Creates a temporary path pattern with the given label
65- // / @note label must be valid
66- // / @param[in] label A label to attach to the path pattern
68+ // / Creates a temporary path pattern with the given prefix
69+ // / @note prefix must be valid
70+ // / @param[in] prefix A prefix to attach to the path pattern
6771// / @returns A path pattern for the unique temporary path
68- fs::path make_pattern (std::string_view label) {
69- return fs::temp_directory_path () / label / " XXXXXX" ;
70- }
71- #endif
72+ fs::path make_pattern (std::string_view prefix) {
73+ fs::path path = fs::temp_directory_path () / prefix;
74+ path += " XXXXXX" ; // TODO: add '.', like `com.github.bugdea1er.tmp.yotR2k`?
7275
73- // / Creates the parent directory of the given path if it does not exist
74- // / @param[in] path The path for which the parent directory needs to be created
75- // / @param[out] ec Parameter for error reporting
76- // / @returns `true` if a parent directory was newly created, `false` otherwise
77- bool create_parent (const fs::path& path, std::error_code& ec) {
78- return fs::create_directories (path.parent_path (), ec);
76+ return path;
7977}
78+ #endif
8079
8180#ifdef _WIN32
8281// / Makes a mode string for the `_wfdopen` function
@@ -147,10 +146,6 @@ std::pair<fs::path, filebuf> create_file(std::ios::openmode mode,
147146#else
148147 fs::path::string_type path = make_pattern (" " );
149148#endif
150- create_parent (path, ec);
151- if (ec) {
152- return {};
153- }
154149
155150 mode |= std::ios::in | std::ios::out;
156151
@@ -188,11 +183,11 @@ std::pair<fs::path, filebuf> create_file(std::ios::openmode mode,
188183 return std::make_pair (path, std::move (filebuf));
189184}
190185
191- fs::path create_directory (std::string_view label ) {
192- validate_label (label ); // throws std::invalid_argument with a proper text
186+ fs::path create_directory (std::string_view prefix ) {
187+ validate_prefix (prefix ); // throws std::invalid_argument with a proper text
193188
194189 std::error_code ec;
195- fs::path directory = create_directory (label , ec);
190+ fs::path directory = create_directory (prefix , ec);
196191
197192 if (ec) {
198193 throw fs::filesystem_error (" Cannot create a temporary directory" , ec);
@@ -201,21 +196,17 @@ fs::path create_directory(std::string_view label) {
201196 return directory;
202197}
203198
204- fs::path create_directory (std::string_view label , std::error_code& ec) {
205- if (!is_label_valid (label )) {
199+ fs::path create_directory (std::string_view prefix , std::error_code& ec) {
200+ if (!is_prefix_valid (prefix )) {
206201 ec = std::make_error_code (std::errc::invalid_argument);
207202 return fs::path ();
208203 }
209204
210205#ifdef _WIN32
211- fs::path::string_type path = make_path (label );
206+ fs::path::string_type path = make_path (prefix );
212207#else
213- fs::path::string_type path = make_pattern (label );
208+ fs::path::string_type path = make_pattern (prefix );
214209#endif
215- create_parent (path, ec);
216- if (ec) {
217- return fs::path ();
218- }
219210
220211#ifdef _WIN32
221212 if (!CreateDirectory (path.c_str (), nullptr )) {
0 commit comments