38 #include <BESInternalError.h>
39 #include <BESInternalFatalError.h>
48 std::map<string, int> *TempFile::open_files =
new std::map<string, int>;
49 struct sigaction TempFile::cached_sigpipe_handler;
56 void TempFile::sigpipe_handler(int sig)
59 std::map<string, int>::iterator it;
60 for (it = open_files->begin(); it != open_files->end(); ++it) {
61 if (unlink((it->first).c_str()) == -1)
62 ERROR_LOG(
string(
"Error unlinking temporary file: '").append(it->first).append(
"': ").append(strerror(errno)).append(
"\n"));
65 sigaction(SIGPIPE, &cached_sigpipe_handler, 0);
84 TempFile::TempFile(
const std::string &path_template,
bool keep_temps)
85 : d_keep_temps(keep_temps)
87 char tmp_name[path_template.length() + 1];
88 std::string::size_type len = path_template.copy(tmp_name, path_template.length());
93 mode_t original_mode = umask(077);
94 d_fd = mkstemp(tmp_name);
97 if (d_fd == -1)
throw BESInternalError(
"Failed to open the temporary file.", __FILE__, __LINE__);
99 d_fname.assign(tmp_name);
102 if (open_files->size() == 0) {
103 struct sigaction act;
104 sigemptyset(&act.sa_mask);
105 sigaddset(&act.sa_mask, SIGPIPE);
110 if (sigaction(SIGPIPE, &act, &cached_sigpipe_handler)) {
115 open_files->insert(std::pair<string, int>(d_fname, d_fd));
126 if (close(d_fd) == -1) {
127 ERROR_LOG(
string(
"Error closing temporary file: '").append(d_fname).append(
"': ").append(strerror(errno)).append(
"\n"));
130 if (unlink(d_fname.c_str()) == -1) {
131 ERROR_LOG(
string(
"Error unlinking temporary file: '").append(d_fname).append(
"': ").append(strerror(errno)).append(
"\n"));
138 cerr <<
"Could not close temporary file '" << d_fname <<
"' due to an error in BESlog (" << e.get_verbose_message() <<
").";
141 cerr <<
"Could not close temporary file '" << d_fname <<
"' due to an error in BESlog.";
144 open_files->erase(d_fname);
146 if (open_files->size() == 0) {
147 if (sigaction(SIGPIPE, &cached_sigpipe_handler, 0)) {
148 ERROR_LOG(
string(
"Could not register a handler to catch SIGPIPE. ").append(
"(").append(strerror(errno)).append(
")"));
Abstract exception class for the BES with basic string message.
exception thrown if internal error encountered
exception thrown if an internal error is found and is fatal to the BES
~TempFile()
Free the temporary file.
static void sigpipe_handler(int signal)