bes  Updated for version 3.20.13
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 <unistd.h>
29 
30 #include <vector>
31 #include <string>
32 #include <map>
33 #include <mutex>
34 #include <memory>
35 
36 namespace bes {
37 
46 class TempFile {
47 private:
48  // Lifecycle controls
49  static struct sigaction cached_sigpipe_handler;
50  mutable std::recursive_mutex d_tf_lock_mutex;
51  static void init();
52  static std::once_flag d_init_once;
53 
54  // Holds the static list of all open files
55  static std::unique_ptr< std::map<std::string, int> > open_files;
56 
57  // Instance variables
58  int d_fd = -1;
59  std::string d_fname;
60  bool d_keep_temps;
61 
62  static void mk_temp_dir(const std::string &dir_name = "/tmp/hyrax_tmp") ;
63 
64  friend class TemporaryFileTest;
65 
66 public:
67  // Odd, but even with TemporaryFileTest declared as a friend, the tests won't
68  // compile unless this is declared public.
69  static void sigpipe_handler(int signal);
70 
71  explicit TempFile(bool keep_temps = false);
72  ~TempFile();
73 
74  std::string create(const std::string &dir_name = "/tmp/hyrax_tmp", const std::string &path_template = "opendap");
75 
77  int get_fd() const { return d_fd; }
78 
80  std::string get_name() const { return d_fname; }
81 };
82 
83 } // namespace bes
84 
85 #endif /* DAP_TEMPFILE_H_ */
Get a new temporary file.
Definition: TempFile.h:46
~TempFile()
Free the temporary file.
Definition: TempFile.cc:235
std::string get_name() const
Definition: TempFile.h:80
TempFile(bool keep_temps=false)
Definition: TempFile.cc:154
static void sigpipe_handler(int signal)
Definition: TempFile.cc:67
int get_fd() const
Definition: TempFile.h:77
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:170