36 #include <BESInternalError.h>
37 #include <BESInternalFatalError.h>
46 std::map<string, int> *TempFile::open_files =
new std::map<string, int>;
47 struct sigaction TempFile::cached_sigpipe_handler;
54 void TempFile::sigpipe_handler(int sig)
57 std::map<string, int>::iterator it;
58 for (it = open_files->begin(); it != open_files->end(); ++it) {
59 if (unlink((it->first).c_str()) == -1)
60 ERROR(
string(
"Error unlinking temporary file: '").append(it->first).append(
"': ").append(strerror(errno)).append(
"\n"));
63 sigaction(SIGPIPE, &cached_sigpipe_handler, 0);
82 TempFile::TempFile(
const std::string &path_template,
bool keep_temps)
83 : d_keep_temps(keep_temps)
85 char tmp_name[path_template.length() + 1];
86 std::string::size_type len = path_template.copy(tmp_name, path_template.length());
91 mode_t original_mode = umask(077);
92 d_fd = mkstemp(tmp_name);
95 if (d_fd == -1)
throw BESInternalError(
"Failed to open the temporary file.", __FILE__, __LINE__);
97 d_fname.assign(tmp_name);
100 if (open_files->size() == 0) {
101 struct sigaction act;
102 sigemptyset(&act.sa_mask);
103 sigaddset(&act.sa_mask, SIGPIPE);
108 if (sigaction(SIGPIPE, &act, &cached_sigpipe_handler)) {
113 open_files->insert(std::pair<string, int>(d_fname, d_fd));
124 if (close(d_fd) == -1) {
125 ERROR(
string(
"Error closing temporary file: '").append(d_fname).append(
"': ").append(strerror(errno)).append(
"\n"));
128 if (unlink(d_fname.c_str()) == -1) {
129 ERROR(
string(
"Error unlinking temporary file: '").append(d_fname).append(
"': ").append(strerror(errno)).append(
"\n"));
136 cerr <<
"Could not close temporary file '" << d_fname <<
"' due to an error in BESlog (" << e.get_verbose_message() <<
").";
139 cerr <<
"Could not close temporary file '" << d_fname <<
"' due to an error in BESlog.";
142 open_files->erase(d_fname);
144 if (open_files->size() == 0) {
145 if (sigaction(SIGPIPE, &cached_sigpipe_handler, 0)) {
146 ERROR(
string(
"Could not register a handler to catch SIGPIPE. ").append(
"(").append(strerror(errno)).append(
")"));