Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C++17 equivalent to boost::filesystem::unique_path()?

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.

like image 972
Larry Gritz Avatar asked Apr 10 '17 06:04

Larry Gritz


People also ask

What is boost :: filesystem :: path?

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).

What is boost filesystem?

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.

What library is filesystem in C++?

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.

Is boost filesystem header only?

Unlike a large portion of the Boost ecosystem, boost::filesystem is not header-only.


2 Answers

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.

like image 187
Joseph Thomson Avatar answered Sep 28 '22 05:09

Joseph Thomson


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.

like image 20
user2047610 Avatar answered Sep 28 '22 03:09

user2047610