cloud_file.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2016-2018, 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  /** \brief The environment variable which stores the directory
79  (default "")
80  */
81  std::string env_var;
82  /// \name Specify hash type
83  //@{
84  /// Current hash type (default sha256)
85  int hash_type;
86  static const int sha256=0;
87  static const int md5=1;
88  static const int md5sum=2;
89  //@}
90 
91  cloud_file();
92 
93  /** \brief Open an HDF file named \c file in directory \c dir
94  downloading from URL \c url if necessary
95  */
96  int hdf5_open(hdf_file &hf, std::string file,
97  std::string url, std::string dir="");
98 
99  /** \brief Open an HDF file named \c file in directory \c dir
100  with hash \c hash, downloading from URL \c url if
101  necessary
102  */
103  int hdf5_open_hash(hdf_file &hf, std::string file, std::string hash,
104  std::string url, std::string dir="");
105 
106  /** \brief Open an HDF file named \c file in directory \c dir
107  in subdirectory \c subdir, downloading from URL \c url if
108  necessary
109  */
110  int hdf5_open_subdir(hdf_file &hf, std::string file, std::string subdir,
111  std::string url, std::string dir="");
112 
113  /** \brief Open an HDF file named \c file in directory \c dir
114  in subdirectory \c subdir with hash \c hash,
115  downloading from URL \c url if necessary
116  */
117  int hdf5_open_hash_subdir(hdf_file &hf, std::string file, std::string hash,
118  std::string subdir, std::string url,
119  std::string dir="");
120 
121 
122  /** \brief Get file named \c file in directory \c dir
123  from url \c url
124  */
125  int get_file(std::string file, std::string url,
126  std::string &fname, std::string dir="");
127 
128  /** \brief Get file named \c file in directory \c dir
129  in subdirectory \c subdir from url \c url
130  */
131  int get_file_hash(std::string file, std::string hash, std::string url,
132  std::string &fname, std::string dir="");
133 
134  /** \brief Get file named \c file in directory \c dir
135  in subdirectory \c subdir from url \c url
136  */
137  int get_file_subdir(std::string file, std::string subdir, std::string url,
138  std::string &fname, std::string dir="");
139 
140  /** \brief Get file named \c file in directory \c dir
141  in subdirectory \c subdir from url \c url
142 
143  This function attempts to find a file named \c file in
144  subdirectory \c subdir of the data directory \c dir. If \c dir
145  is empty, it attempts to set it equal to the value of the
146  environment variable \ref env_var. If that environment
147  variable is not present, the user is prompted for the correct
148  data directory. If the file is not found, then this function
149  uses curl (or wget if curl was unsuccessful) to download the
150  file from \c url. If this process was successful at finding or
151  downloading the file, then the full filename is returned.
152  Otherwise, an exception is thrown.
153  */
154  int get_file_hash_subdir(std::string file, std::string hash,
155  std::string subdir, std::string url,
156  std::string &fname, std::string dir="");
157 
158  };
159 
160 #ifndef DOXYGEN_NO_O2NS
161 }
162 #endif
163 
164 #endif
int hash_type
Current hash type (default sha256)
Definition: cloud_file.h:85
int hdf5_open_hash(hdf_file &hf, std::string file, std::string hash, std::string url, std::string dir="")
Open an HDF file named file in directory dir with hash hash, downloading from URL url if necessary...
int get_file_subdir(std::string file, std::string subdir, std::string url, std::string &fname, std::string dir="")
Get file named file in directory dir in subdirectory subdir from url url.
int hdf5_open_subdir(hdf_file &hf, std::string file, std::string subdir, std::string url, std::string dir="")
Open an HDF file named file in directory dir in subdirectory subdir, downloading from URL url if nece...
std::string env_var
The environment variable which stores the directory (default "")
Definition: cloud_file.h:81
Read a file and download from a URL if necessary.
Definition: cloud_file.h:60
int get_file_hash_subdir(std::string file, std::string hash, std::string subdir, std::string url, std::string &fname, std::string dir="")
Get file named file in directory dir in subdirectory subdir from url url.
int verbose
Verbosity parameter (default 1)
Definition: cloud_file.h:74
bool allow_wget
If true, allow the use of wget to download the file (default true)
Definition: cloud_file.h:67
The O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl namespace ...
Definition: table.h:53
bool allow_curl
If true, allow the use of curl to download the file (default true)
Definition: cloud_file.h:71
bool throw_on_fail
If true, throw an exception on failure (default true)
Definition: cloud_file.h:77
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.
Store data in an O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$sc...
Definition: hdf_file.h:100
int get_file_hash(std::string file, std::string hash, std::string url, std::string &fname, std::string dir="")
Get file named file in directory dir in subdirectory subdir from url url.
int get_file(std::string file, std::string url, std::string &fname, std::string dir="")
Get file named file in directory dir from url url.
int hdf5_open_hash_subdir(hdf_file &hf, std::string file, std::string hash, std::string subdir, std::string url, std::string dir="")
Open an HDF file named file in directory dir in subdirectory subdir with hash hash, downloading from URL url if necessary.

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