cloud_file.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2016-2017, 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  \todo Convert to use boost::filesystem .
53 
54  \warning This class has several potential security issues
55  and should not be used without due care.
56  */
57  class cloud_file {
58 
59  public:
60 
61  /** \brief If true, allow the use of \c wget to download the file
62  (default true)
63  */
64  bool allow_wget;
65  /** \brief If true, allow the use of \c curl to download the file
66  (default true)
67  */
68  bool allow_curl;
69  /** \brief Verbosity parameter (default 1)
70  */
71  int verbose;
72  /** \brief If true, throw an exception on failure (default true)
73  */
75  /** \brief The environment variable which stores the directory
76  (default "")
77  */
78  std::string env_var;
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 hash,
101  std::string url, std::string dir="");
102 
103  /** \brief Open an HDF file named \c file in directory \c dir
104  in subdirectory \c subdir, downloading from URL \c url if
105  necessary
106  */
107  int hdf5_open_subdir(hdf_file &hf, std::string file, std::string subdir,
108  std::string url, std::string dir="");
109 
110  /** \brief Open an HDF file named \c file in directory \c dir
111  in subdirectory \c subdir with hash \c hash,
112  downloading from URL \c url if necessary
113  */
114  int hdf5_open_hash_subdir(hdf_file &hf, std::string file, std::string hash,
115  std::string subdir, std::string url,
116  std::string dir="");
117 
118 
119  /** \brief Get file named \c file in directory \c dir
120  in subdirectory \c subdir from url \c url
121  */
122  int get_file(std::string file, std::string url,
123  std::string &fname, std::string dir="");
124 
125  /** \brief Get file named \c file in directory \c dir
126  in subdirectory \c subdir from url \c url
127  */
128  int get_file_hash(std::string file, std::string hash, std::string url,
129  std::string &fname, std::string dir="");
130 
131  /** \brief Get file named \c file in directory \c dir
132  in subdirectory \c subdir from url \c url
133  */
134  int get_file_subdir(std::string file, std::string subdir, std::string url,
135  std::string &fname, std::string dir="");
136 
137  /** \brief Get file named \c file in directory \c dir
138  in subdirectory \c subdir from url \c url
139 
140  This function attempts to find a file named \c file in
141  subdirectory \c subdir of the data directory \c dir. If \c dir
142  is empty, it attempts to set it equal to the value of the
143  environment variable \ref env_var. If that environment
144  variable is not present, the user is prompted for the correct
145  data directory. If the file is not found, then this function
146  uses curl (or wget if curl was unsuccessful) to download the
147  file from \c url. If this process was successful at finding or
148  downloading the file, then the full filename is returned.
149  Otherwise, an exception is thrown.
150  */
151  int get_file_hash_subdir(std::string file, std::string hash,
152  std::string subdir, std::string url,
153  std::string &fname, std::string dir="");
154 
155  };
156 
157 #ifndef DOXYGEN_NO_O2NS
158 }
159 #endif
160 
161 #endif
int hash_type
Current hash type (default sha256)
Definition: cloud_file.h:82
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:78
Read a file and download from a URL if necessary.
Definition: cloud_file.h:57
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:71
bool allow_wget
If true, allow the use of wget to download the file (default true)
Definition: cloud_file.h:64
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:68
bool throw_on_fail
If true, throw an exception on failure (default true)
Definition: cloud_file.h:74
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:96
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 in subdirectory subdir 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).