<分区>
我正在尝试在系统临时文件夹中创建一个唯一的临时目录,并且一直在阅读有关 tmpnam() 的安全和文件创建问题。
我写了下面的代码,想知道它是否能满足这些问题,我对 tmpnam() 函数的使用是否正确以及是否抛出 filesystem_error?我是否应该添加对其他内容的检查(例如 temp_directory_path,它也会引发异常)?
// Unique temporary directory path in the system temporary directory path.
std::filesystem::path tmp_dir_path {std::filesystem::temp_directory_path() /= std::tmpnam(nullptr)};
// Attempt to create the directory.
if (std::filesystem::create_directories(tmp_dir_path)) {
// Directory successfully created.
return tmp_dir_path;
} else {
// Directory could not be created.
throw std::filesystem_error("directory could not be created.");
}