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 
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 string
Definition: nybbler.cc:12
error
Definition: include.cc:26
int errno
Contains the last error code.
Definition: structcmd.h:53
std::string unique_filename(std::string stem, std::string extension=".root")
int close(int)
Closes the file descriptor fd.
const double e
p
Definition: test.py:223
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66