LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
unique_filename.cc
Go to the documentation of this file.
2 
4 
5 #include "boost/filesystem.hpp"
6 #include "boost/system/error_code.hpp"
7 
8 #include <cerrno>
9 #include <cstring>
10 #include <unistd.h>
11 
12 extern "C" {
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 }
16 
17 std::string
18 art::unique_filename(std::string stem, std::string extension)
19 {
20  boost::filesystem::path const p(stem + "-%%%%-%%%%-%%%%-%%%%" + extension);
21  boost::filesystem::path outpath;
22  boost::system::error_code ec;
23  int tmp_fd = -1, error = 0;
24  do {
25  outpath = boost::filesystem::unique_path(p, ec);
26  } while (!ec && (tmp_fd = creat(outpath.c_str(), S_IRUSR | S_IWUSR)) == -1 &&
27  (error = errno) == EEXIST);
28  if (tmp_fd != -1) {
29  close(tmp_fd);
30  } else {
32  e << "RootOutput cannot ascertain a unique temporary filename for output "
33  "based on stem\n\""
34  << stem << "\": ";
35  if (ec) {
36  e << ec;
37  } else {
38  e << strerror(error);
39  }
40  e << ".\n";
41  throw e;
42  }
43  return outpath.native();
44 }
std::string unique_filename(std::string stem, std::string extension=".root")
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
in close()
Float_t e
Definition: plot.C:35