Reputation: 13710
std::filesystem
on C++17, and std::experimental::filesystem
for many pre-C++17 compilers, are based on boost::filesystem
and almost all of it is obvious to port to the newer std.
But I see no std::filesystem
equivalent to boost::filesystem::unique_path()
.
Is there an equivalent in std that I'm not noticing? Or is there a recommended approach I should take to mimic the implementation?
I'm really hoping to replace the boost::filesystem
dependency when my code notices it's compiling on a platform that supports std::filesystem
, and unique_path()
is the only not obvious part of my conversion.
Upvotes: 32
Views: 10803
Reputation: 10423
unique_path
was removed because it was a potential attack vector for malware. There is a window of opportunity between calling unique_path
and opening a file at that location during which some other process could create the same file. Depending on what the user does with the file, this may or may not constitute a security vulnerability. A similar issue exists with the POSIX function tmpnam
.
As noted in this discussion, this issue will be dealt with in the next iteration of the Filesystem library. Until then, you can either continue using Boost.Filesystem, use the std::tmpnam
function provided in <cstdio>
, or use safer platform-specific alternatives like mkstemp
.
Upvotes: 27
Reputation: 136
As far as I can tell there is really no exact equivalent in C++17.
You didn't really specify what exactly you want to do but if you just need to store a temporary file somewhere then you should be able to mimic a similar functionality with std::filesystem::temp_directory_path
which you can append with a randomly generated filename (which you can do like this, or modify it accordingly if you require the exact same naming format as boost::filesystem::unique_path()
)
Or if you just need to store any temporary file, you can use std::tmpfile
.
Upvotes: 5