bes  Updated for version 3.17.4
BESHandlerUtil.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the BES, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2016 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include <cstdlib>
29 #include <vector>
30 #include <string>
31 
32 #include <BESInternalError.h>
33 
34 #include "BESHandlerUtil.h"
35 
36 using namespace std;
37 
38 namespace bes {
39 
52 TemporaryFile::TemporaryFile(const std::string &path_template)
53 {
54  //string temp_file_name = FONcRequestHandler::temp_dir + "/ncXXXXXX";
55  //vector<char> temp_file(path_template.length() + 1);
56  d_name.reserve(path_template.length() + 1);
57 
58  string::size_type len = path_template.copy(&d_name[0], path_template.length());
59  d_name[len] = '\0';
60 
61  // cover the case where older versions of mkstemp() create the file using
62  // a mode of 666.
63  mode_t original_mode = umask(077);
64  d_fd = mkstemp(&d_name[0]);
65  umask(original_mode);
66 
67  if (d_fd == -1) throw BESInternalError("Failed to open the temporary file.", __FILE__, __LINE__);
68 }
69 
70 } // namespace bes
71 
72 
exception thrown if inernal error encountered
STL namespace.