Skip to content

Commit 9dca484

Browse files
authored
Breaking: Remove file labels (#170)
This is the next step to removing the path() method of the temporary file (#165): if there is no path, there is definitely no labels in file path
1 parent 96ed1b5 commit 9dca484

File tree

6 files changed

+34
-80
lines changed

6 files changed

+34
-80
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The location of temporary files generated by this library is determined by the `
7575
...
7676
{
7777
// Create a unique temporary file
78-
auto tmpfile = tmp::file("org.example.product", std::ios::binary);
78+
auto tmpfile = tmp::file(std::ios::binary);
7979

8080
// Write its contents and metadata
8181
tmpfile << contents << metadata;

include/tmp/file

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace tmp {
3737
/// #include <tmp/file>
3838
///
3939
/// auto func(std::string_view content) {
40-
/// auto tmpfile = tmp::file("org.example.product", std::ios::out);
40+
/// auto tmpfile = tmp::file(std::ios::bin);
4141
/// tmpfile << content << std::flush;
4242
///
4343
/// // the temporary file is deleted recursively when the
@@ -48,23 +48,17 @@ class TMP_EXPORT file : public entry, public std::iostream {
4848
public:
4949
/// Creates a unique temporary file and opens it for reading and writing
5050
/// @note std::ios::in | std::ios::out are always added to the `mode`
51-
/// @param label A label to attach to the temporary file path
52-
/// @param mode Specifies stream open mode
51+
/// @param mode Specifies stream open mode
5352
/// @throws std::filesystem::filesystem_error if cannot create a file
54-
/// @throws std::invalid_argument if the label is ill-formatted
55-
explicit file(std::string_view label = "",
56-
std::ios::openmode mode = std::ios::in | std::ios::out);
53+
explicit file(std::ios::openmode mode = std::ios::in | std::ios::out);
5754

5855
/// Creates a unique temporary copy from the given path
5956
/// @note std::ios::in | std::ios::out are always added to the `mode`
60-
/// @param path A path to make a temporary copy from
61-
/// @param label A label to attach to the temporary file path
62-
/// @param mode Specifies stream open mode
57+
/// @param path A path to make a temporary copy from
58+
/// @param mode Specifies stream open mode
6359
/// @returns The new temporary file
6460
/// @throws std::filesystem::filesystem_error if given path is not a file
65-
/// @throws std::invalid_argument if the label is ill-formatted
6661
static file copy(const std::filesystem::path& path,
67-
std::string_view label = "",
6862
std::ios::openmode mode = std::ios::in | std::ios::out);
6963

7064
#if __cpp_lib_fstream_native_handle >= 202306L

src/create.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,9 @@ void close(filebuf::native_handle_type handle) noexcept {
129129
}
130130
} // namespace
131131

132-
std::pair<fs::path, filebuf> create_file(std::string_view label,
133-
std::ios::openmode mode) {
134-
validate_label(label); // throws std::invalid_argument with a proper text
135-
132+
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode) {
136133
std::error_code ec;
137-
auto file = create_file(label, mode, ec);
134+
std::pair<fs::path, filebuf> file = create_file(mode, ec);
138135

139136
if (ec) {
140137
throw fs::filesystem_error("Cannot create a temporary file", ec);
@@ -143,18 +140,12 @@ std::pair<fs::path, filebuf> create_file(std::string_view label,
143140
return file;
144141
}
145142

146-
std::pair<fs::path, filebuf> create_file(std::string_view label,
147-
std::ios::openmode mode,
143+
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode,
148144
std::error_code& ec) {
149-
if (!is_label_valid(label)) {
150-
ec = std::make_error_code(std::errc::invalid_argument);
151-
return {};
152-
}
153-
154145
#ifdef _WIN32
155-
fs::path::string_type path = make_path(label);
146+
fs::path::string_type path = make_path("");
156147
#else
157-
fs::path::string_type path = make_pattern(label);
148+
fs::path::string_type path = make_pattern("");
158149
#endif
159150
create_parent(path, ec);
160151
if (ec) {

src/create.hpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,19 @@
1212
namespace tmp {
1313
namespace fs = std::filesystem;
1414

15-
/// Creates a temporary file with the given label in the system's
16-
/// temporary directory, and opens it for reading and writing
17-
/// @param[in] label A label to attach to the temporary file path
18-
/// @param[in] mode Specifies stream open mode
15+
/// Creates a temporary file in the system's temporary directory,
16+
/// and opens it for reading and writing
17+
/// @param[in] mode Specifies stream open mode
1918
/// @returns A path to the created temporary file and a handle to it
2019
/// @throws fs::filesystem_error if cannot create a temporary file
21-
/// @throws std::invalid_argument if the label is ill-formatted
22-
std::pair<fs::path, filebuf> create_file(std::string_view label,
23-
std::ios::openmode mode);
24-
25-
/// Creates a temporary file with the given label in the system's
26-
/// temporary directory, and opens it for reading and writing
27-
/// @param[in] label A label to attach to the temporary file path
28-
/// @param[in] mode Specifies stream open mode
29-
/// @param[out] ec Parameter for error reporting
20+
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode);
21+
22+
/// Creates a temporary file in the system's temporary directory,
23+
/// and opens it for reading and writing
24+
/// @param[in] mode Specifies stream open mode
25+
/// @param[out] ec Parameter for error reporting
3026
/// @returns A path to the created temporary file and a handle to it
31-
std::pair<fs::path, filebuf> create_file(std::string_view label,
32-
std::ios::openmode mode,
27+
std::pair<fs::path, filebuf> create_file(std::ios::openmode mode,
3328
std::error_code& ec);
3429

3530
/// Creates a temporary directory with the given label in the system's

src/file.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ file::file(std::pair<fs::path, filebuf> handle) noexcept
1919
std::iostream(std::addressof(sb)),
2020
sb(std::move(handle.second)) {}
2121

22-
file::file(std::string_view label, openmode mode)
23-
: file(create_file(label, mode)) {}
22+
file::file(std::ios::openmode mode)
23+
: file(create_file(mode)) {}
2424

25-
file file::copy(const fs::path& path, std::string_view label, openmode mode) {
26-
file tmpfile = file(label, mode);
25+
file file::copy(const fs::path& path, std::ios::openmode mode) {
26+
file tmpfile = file(mode);
2727

2828
std::error_code ec;
2929
fs::copy_file(path, tmpfile, fs::copy_options::overwrite_existing, ec);

tests/file.cpp

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ TEST(file, type_traits) {
5252
static_assert(std::is_same_v<file::off_type, traits::off_type>);
5353
}
5454

55-
/// Tests file creation with label
56-
TEST(file, create_with_label) {
57-
file tmpfile = file(LABEL);
55+
/// Tests file creation
56+
TEST(file, create) {
57+
file tmpfile = file();
5858
fs::path parent = tmpfile.path().parent_path();
5959

6060
EXPECT_TRUE(fs::exists(tmpfile));
6161
EXPECT_TRUE(fs::is_regular_file(tmpfile));
62-
EXPECT_TRUE(fs::equivalent(parent, fs::temp_directory_path() / LABEL));
62+
EXPECT_TRUE(fs::equivalent(parent, fs::temp_directory_path()));
6363
EXPECT_TRUE(is_open(tmpfile));
6464

6565
#if __cpp_lib_fstream_native_handle >= 202306L
@@ -76,49 +76,23 @@ TEST(file, create_with_label) {
7676
#endif
7777
}
7878

79-
/// Tests file creation without label
80-
TEST(file, create_without_label) {
81-
file tmpfile = file();
82-
fs::path parent = tmpfile.path().parent_path();
83-
84-
EXPECT_TRUE(fs::exists(tmpfile));
85-
EXPECT_TRUE(fs::is_regular_file(tmpfile));
86-
EXPECT_TRUE(fs::equivalent(parent, fs::temp_directory_path()));
87-
EXPECT_TRUE(is_open(tmpfile));
88-
}
89-
90-
/// Tests multiple file creation with the same label
79+
/// Tests multiple file creation
9180
TEST(file, create_multiple) {
92-
file fst = file(LABEL);
93-
file snd = file(LABEL);
81+
file fst = file();
82+
file snd = file();
9483

9584
EXPECT_FALSE(fs::equivalent(fst, snd));
9685
}
9786

98-
/// Tests error handling with invalid labels
99-
TEST(file, create_invalid_label) {
100-
EXPECT_THROW(file("multi/segment"), std::invalid_argument);
101-
EXPECT_THROW(file("/root"), std::invalid_argument);
102-
EXPECT_THROW(file(".."), std::invalid_argument);
103-
EXPECT_THROW(file("."), std::invalid_argument);
104-
105-
fs::path root = fs::temp_directory_path().root_name();
106-
if (!root.empty()) {
107-
EXPECT_THROW(file(root.string() + "relative"), std::invalid_argument);
108-
EXPECT_THROW(file(root.string() + "/root"), std::invalid_argument);
109-
}
110-
}
111-
11287
/// Tests error handling with invalid open mode
11388
TEST(file, create_invalid_openmode) {
11489
// C++ standard forbids opening a filebuf with `trunc | app`
115-
std::ios::openmode openmode = std::ios::trunc | std::ios::app;
116-
EXPECT_THROW(file("", openmode), fs::filesystem_error);
90+
EXPECT_THROW(file(std::ios::trunc | std::ios::app), fs::filesystem_error);
11791
}
11892

11993
/// Tests that file adds std::ios::in and std::ios::out flags
12094
TEST(file, ios_flags) {
121-
file tmpfile = file("", std::ios::binary);
95+
file tmpfile = file(std::ios::binary);
12296
tmpfile << "Hello, world!" << std::flush;
12397

12498
std::ifstream stream = std::ifstream(tmpfile.path());

0 commit comments

Comments
 (0)