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.
boost::filesystem::path is the central class in Boost. Filesystem for representing and processing paths. Definitions can be found in the namespace boost::filesystem and in the header file boost/filesystem. hpp . Paths can be built by passing a string to the constructor of boost::filesystem::path (see Example 35.1).
The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.
Filesystem library (since C++17) The Filesystem library provides facilities for performing operations on file systems and their components, such as paths, regular files, and directories. The filesystem library was originally developed as boost.
Unlike a large portion of the Boost ecosystem, boost::filesystem is not header-only.
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
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With