bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
TempFile.h
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) 2018 OPeNDAP, Inc.
7// Author: Nathan Potter <ndp@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#ifndef DAP_TEMPFILE_H_
26#define DAP_TEMPFILE_H_
27
28#include <csignal>
29#include <string>
30#include <map>
31#include <mutex>
32
33namespace bes {
34
43class TempFile {
44
45 friend class TemporaryFileTest;
46
47 // Lifecycle controls
48 static struct sigaction cached_sigpipe_handler;
49 // FIXME This needs to be static if the intent is to lock something shared by
50 // two or more TempFile instances at once. jhrg 8/13/24
51 mutable std::recursive_mutex d_tf_lock_mutex;
52
53 // Holds the static list of all open files
54 static std::map<std::string, int, std::less<>> open_files;
55
56 // Instance variables
57 int d_fd = -1;
58 std::string d_fname;
59 bool d_keep_temps = false;
60
61 static bool mk_temp_dir(const std::string &dir_name = "/tmp/hyrax_tmp");
62
63public:
64 // Odd, but even with TemporaryFileTest declared as a friend, the tests won't
65 // compile unless this is declared public.
66 // FIXME This code needs to restore the existing SIGPIPE handler. See server/ServerApp.cc. jhrg 8/13/24
67 static void sigpipe_handler(int signal);
68
69 TempFile() = default;
70 explicit TempFile(bool keep_temps) : d_keep_temps(keep_temps) {}
71
72 TempFile(const TempFile&) = delete;
73 TempFile& operator=(const TempFile&) = delete;
74 TempFile(TempFile&&) = delete;
75 TempFile& operator=(TempFile&&) = delete;
76
77 ~TempFile();
78
79
80
81 std::string create(const std::string &dir_name = "/tmp/hyrax_tmp", const std::string &path_template = "opendap");
82
84 int get_fd() const { return d_fd; }
85
87 std::string get_name() const { return d_fname; }
88};
89
90} // namespace bes
91
92#endif /* DAP_TEMPFILE_H_ */
~TempFile()
Free the temporary file.
Definition TempFile.cc:232
std::string get_name() const
Definition TempFile.h:87
static void sigpipe_handler(int signal)
Definition TempFile.cc:74
int get_fd() const
Definition TempFile.h:84
std::string create(const std::string &dir_name="/tmp/hyrax_tmp", const std::string &path_template="opendap")
Create a new temporary file.
Definition TempFile.cc:164