source: rrlib_util/sFileIOUtils.h @ 52:2ee2ffcef24c

Last change on this file since 52:2ee2ffcef24c was 52:2ee2ffcef24c, checked in by Tobias Föhst <foehst@…>, 8 years ago

Backed out changeset 1793615c41cb

  • Was better solved by proper host configuration
File size: 10.3 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    sFileIOUtils.h
23 *
24 * \author  Bernd Helge Schaefer
25 * \author  Jens Wettach
26 * \date    15.11.05
27 *
28 * \brief   Contains static class sFileIOUtils
29 *
30 */
31//----------------------------------------------------------------------
32// Includes
33//----------------------------------------------------------------------
34#ifndef _rrlib_util_sFileIOUtils_h_
35#define _rrlib_util_sFileIOUtils_h_
36
37//STL includes
38#include <string>
39#include <vector>
40#include <map>
41
42extern "C"
43{
44#include <arpa/inet.h> // inet_ntoa
45}
46
47//----------------------------------------------------------------------
48// Namespace declaration
49//----------------------------------------------------------------------
50namespace rrlib
51{
52namespace util
53{
54
55class sFileIOUtils
56{
57public:
58  /*!
59   * \brief Returns all files in a given directory
60   *
61   * \return Returns whether or not the read was possible
62   */
63  static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files);
64
65  /*!
66   * \brief Returns the current directory
67   *
68   * \return Returns a string containing the current directory
69   */
70  static std::string GetCurrentDirectory();
71  /*!
72   * \brief Setsthe current directory
73   *
74   * Changes the current directory to \param dir .
75   * \return returns an error code similar to the chdir C command
76   */
77  static int ChangeCurrentDirectory(const std::string& dir);
78
79  /*!
80   * \brief Creates a new directory at the path provided as parameter.
81   *
82   * \param path Complete relative or absolute path to be created.
83   * \return returns false if directory creation fails, true otherwise.
84   * The existance of the folder is not regarded as a failure here!
85   */
86  static bool CreateDirectory(const std::string& path); // throw(runtime_error);
87
88  /*!
89   * \brief Creates a new temporary file via "mktemp".
90   *
91   * \return the new temp file
92   */
93  static std::string CreateTempfile();
94
95  /*!
96   * \brief Removes file with given name.
97   */
98  static void DeleteFile(const std::string& filename);
99
100  /*!
101   * \brief Calls "gzip" on input file and writes result to output file.
102   *
103   * \note output file gets suffix ".gz" automatically if not yet the case.
104   */
105  static void CompressFile(const std::string& input_filename, std::string& output_filename);
106
107  /*!
108   * \brief Calls "gunzip" on input file and writes result to output file.
109   *
110   * \note input file must have suffix ".gz".
111   */
112  static void DecompressFile(const std::string& input_filename, const std::string& output_filename);
113
114
115
116  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
117   *
118   * \param file_name   file name to be expanded (will contain the result afterwards)
119   *
120   * \returns Whether the method could be executed without failure or not
121   */
122  static bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__));
123
124  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
125  *
126  * Thus variables as e.g. $MCAPROJECTHOME can be used for specifying OIV files in scene description files.
127  *
128  * \param result      reference to std::string object that should contain the result afterwards
129  * \param file_name   file name to be expanded
130  *
131  * \returns Whether the method could be executed without failure or not
132  */
133  static bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__));
134
135
136  /*!
137   *    Splits the given filename into directory, single file name and file extension
138   *    \param complete_name complete file name (input)
139   *    \param file_dir directory (output)
140   *    \param file_base single file name (output)
141   *    \param file_ext file extension (output)
142   */
143  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext);
144
145
146  /*!
147   * Retrieves network host name via "hostname" and returns result.
148   * \param fqdn whether FQDN  (Fully  Qualified Domain Name) or short name shall be retrieved
149   */
150  static std::string GetHostName(bool fqdn = true);
151
152
153  /*!
154   * Retrieves ip address of host with given network name via "gethostbyname" and returns result.
155   */
156  static struct in_addr HostToIpViaGetHostByName(const std::string& name);
157
158  /*!
159   * Retrieves ip address of host with given network name via "nslookup" and returns result.
160   */
161  static struct in_addr HostToIpViaNslookup(const std::string& name);
162
163  /*!
164   * Retrieves ip address of host with given network name via "host" and returns result.
165   */
166  static struct in_addr HostToIpViaHost(const std::string& name);
167
168  /*!
169   * Retrieves ip address of host with given network name from "/etc/hosts" and returns result.
170   */
171  static struct in_addr HostToIpViaHostsFile(const std::string& name);
172
173  /*!
174   * Retrieves ip address of host with given network name using
175   * 1. \sa HostToIpViaHostsFile()
176   * 2. \sa HostToIpViaHostx()
177   * 2. \sa HostToIpViaNslookup()
178   */
179  static struct in_addr HostToIp(const std::string& name);
180
181  /*!
182   * Fetches file with given source_file_name from server (either addressed by network name or ip address) at given source_directory
183   * and puts it to the given target_directory (via <system( rsync -av ...)> ).
184   *
185   * \params source_host_name network name of server
186   * \params source_host_ip_address ip address name of server (used alternatively to its name)
187   * \params source_directory directory on server
188   * \params source_file_name file to fetch from server
189   * \params target_directory local directory for fetched file
190   * \params optional_rsync_flags optional flags for rsync command, appended to -av
191   *
192   * \returns result of system call
193   */
194  static int RSyncFile(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::string& source_file_name, const std::string& target_directory, const std::string &optional_rsync_flags = "");
195
196
197  /*!
198   * Fetches files with given source_file_names from server (either addressed by network name or ip address) at given source_directory
199   * and puts it to the given target_directory (via <system( rsync -avr --files-from ...)> ). Uses temp file on local host to
200   * compose list with files to fetch.
201   *
202   * \params source_host_name network name of server
203   * \params source_host_ip_address ip address name of server (used alternatively to its name)
204   * \params source_directory directory on server
205   * \params source_file_names files to fetch from server
206   * \params target_directory local directory for fetched file
207   * \params optional_rsync_flags optional flags for rsync command, appended to -avr
208   *
209   * \returns result of system call
210   */
211  static int RSyncFiles(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::vector<std::string>& source_file_names, const std::string& target_directory, const std::string &optional_rsync_flags = "");
212
213
214  /*!
215   * Checks whether given file is available on local host or can be rsync'ed from resource server and returns full qualified file_name on local host.
216   * The following checks are performed (with decreasing priority):
217   * 1. check location of file: "./ + resource_repository + filename"
218   * 2. check location of file: "local_resource_directory + resource_repository + filename"
219   * 3. check location of file: "server_resource_directory + resource_repository + filename" --> rsync to "local_resource_directory + resource_repository + file_name"
220   * As soon as one check succeeds the remaining checks are not executed.
221   *
222   * \param file_name name of file to search and fetch (may contain relative path)
223   * \param full_local_file_name full qualified name of file on local host after successful check/fetch operation (result)
224   * \param resource_repository relative path to be used as resource repository (on local host and on server, may be empty)
225   * \param resource_server network name of resource server (optional)
226   * \param local_resource_directory absolute path to resource_repository on local host (may be empty)
227   * \param server_resource_directory absolute path to resource_repository on server (may be empty)
228   * \param use_cache whether to take local host name and server ip address from local static cache or not
229   *
230   * \returns success of check/fetch operation
231   */
232  static bool CheckAndGetFile(const std::string &file_name, std::string &full_local_file_name, const std::string& resource_repository = "", const std::string& resource_server = "", const std::string& local_resource_directory = "", const std::string& server_resource_directory = "", bool use_cache = false);
233
234  /*!
235   * Clear cached local host name and server ip addresses
236   * \sa CheckAndGetFile()
237   */
238  static void ClearCaches();
239
240  /*!
241   * Clear cached resource file names
242   * \sa CheckAndGetFile()
243   */
244  static void ClearResourceCache();
245
246private:
247  static std::string cached_local_host; //!< local static variable to store name of local host
248  static std::map<std::string, std::string> host_name_to_ip_cache; //!< local static variable to store server ip addresses
249  static std::map<std::string, std::string> resource_cache; //!< local static variable to store names of already loaded resource files
250};
251//----------------------------------------------------------------------
252// End of namespace declaration
253//----------------------------------------------------------------------
254}; // namespace util
255}; // namespace rrlib
256#endif
Note: See TracBrowser for help on using the repository browser.