cloud_file.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2016-2020, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file cloud_file.h
24  \brief File for definition of \ref o2scl_hdf::cloud_file
25 */
26 #ifndef O2SCL_CLOUD_FILE_H
27 #define O2SCL_CLOUD_FILE_H
28 
29 #include <iostream>
30 // For getenv()
31 #include <cstdlib>
32 // For struct stat and associated functions
33 #include <sys/stat.h>
34 
35 #ifdef O2SCL_USE_BOOST_FILESYSTEM
36 #include <boost/filesystem.hpp>
37 #endif
38 
39 #include <o2scl/err_hnd.h>
40 #include <o2scl/hdf_file.h>
41 
42 #ifndef DOXYGEN_NO_O2NS
43 namespace o2scl_hdf {
44 #endif
45 
46  /** \brief Read a file and download from a URL if necessary
47 
48  \note This class requires POSIX I/O calls and a system call
49  which uses <tt>mkdir -p</tt>, thus will probably only work on
50  unix-like systems.
51 
52  \note This class uses system calls to <tt>curl</tt> or
53  <tt>wget</tt> which must be installed separatley.
54 
55  \future Convert to use boost::filesystem .
56 
57  \warning This class has several potential security issues
58  and should not be used without due care.
59  */
60  class cloud_file {
61 
62  public:
63 
64  /** \brief If true, allow the use of \c wget to download the file
65  (default true)
66  */
67  bool allow_wget;
68  /** \brief If true, allow the use of \c curl to download the file
69  (default true)
70  */
71  bool allow_curl;
72  /** \brief Verbosity parameter (default 1)
73  */
74  int verbose;
75  /** \brief If true, throw an exception on failure (default true)
76  */
78 
79  /// \name Specify hash type
80  //@{
81  /// Current hash type (default sha256)
82  int hash_type;
83  static const int sha256=0;
84  static const int md5=1;
85  static const int md5sum=2;
86  //@}
87 
88  cloud_file();
89 
90  /** \brief Open an HDF file named \c file in directory \c dir
91  downloading from URL \c url if necessary
92  */
93  int hdf5_open(hdf_file &hf, std::string file,
94  std::string url, std::string dir="");
95 
96  /** \brief Open an HDF file named \c file in directory \c dir
97  with hash \c hash, downloading from URL \c url if
98  necessary
99  */
100  int hdf5_open_hash(hdf_file &hf, std::string file, std::string url,
101  std::string hash, std::string dir="");
102 
103  /** \brief Get file named \c file in directory \c dir
104  from url \c url
105  */
106  int get_file(std::string file, std::string url,
107  std::string dir="");
108 
109  /** \brief Get file named \c file in directory \c dir
110  in subdirectory \c subdir from url \c url
111 
112  This function begins with the directory \c dir. If \c dir is
113  not present and cannot be created, the user is prompted for
114  the correct data directory. This function then searches for
115  file \c file in the directory. If it is found, it is compared
116  with the specified hash. If the hash matches, then the full
117  filename is returned. If the hash does not match or if the
118  file is not found, then this function uses curl (or wget if
119  curl was unsuccessful) to download the file from \c url. The
120  file is then compared with the hash again, and the full
121  filename is returned if the hash matches. Otherwise the error
122  handler is called.
123  */
124  int get_file_hash(std::string file, std::string url, std::string hash="",
125  std::string dir="");
126 
127  };
128 
129 #ifndef DOXYGEN_NO_O2NS
130 }
131 #endif
132 
133 #endif
o2scl_hdf::cloud_file::hash_type
int hash_type
Current hash type (default sha256)
Definition: cloud_file.h:82
o2scl_hdf::cloud_file::get_file
int get_file(std::string file, std::string url, std::string dir="")
Get file named file in directory dir from url url.
o2scl_hdf::cloud_file::hdf5_open
int hdf5_open(hdf_file &hf, std::string file, std::string url, std::string dir="")
Open an HDF file named file in directory dir downloading from URL url if necessary.
o2scl_hdf::cloud_file::hdf5_open_hash
int hdf5_open_hash(hdf_file &hf, std::string file, std::string url, std::string hash, std::string dir="")
Open an HDF file named file in directory dir with hash hash, downloading from URL url if necessary.
o2scl_hdf
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
Definition: table.h:59
o2scl_hdf::cloud_file::get_file_hash
int get_file_hash(std::string file, std::string url, std::string hash="", std::string dir="")
Get file named file in directory dir in subdirectory subdir from url url.
o2scl_hdf::cloud_file::throw_on_fail
bool throw_on_fail
If true, throw an exception on failure (default true)
Definition: cloud_file.h:77
o2scl_hdf::cloud_file::allow_curl
bool allow_curl
If true, allow the use of curl to download the file (default true)
Definition: cloud_file.h:71
o2scl_hdf::cloud_file
Read a file and download from a URL if necessary.
Definition: cloud_file.h:60
o2scl_hdf::hdf_file
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
Definition: hdf_file.h:105
o2scl_hdf::cloud_file::verbose
int verbose
Verbosity parameter (default 1)
Definition: cloud_file.h:74
o2scl_hdf::cloud_file::allow_wget
bool allow_wget
If true, allow the use of wget to download the file (default true)
Definition: cloud_file.h:67

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).