OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
DirectoryUtil.h
Go to the documentation of this file.
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 #ifndef __AGG_UTIL__DIRECTORY_UTIL_H__
30 #define __AGG_UTIL__DIRECTORY_UTIL_H__
31 
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 #include <time.h> // for time_t
37 
38 namespace libdap
39 {
40  class Regex; // GNU regular expression lib wrapper
41 };
42 
43 namespace agg_util
44 {
46  class FileInfo
47  {
48  public:
50  FileInfo(const std::string& path, const std::string& basename, bool isDir, time_t modTime);
51  ~FileInfo();
52 
54  const std::string& path() const;
55  const std::string& basename() const;
56  bool isDir() const;
57  time_t modTime() const;
58 
60  std::string getModTimeAsString() const;
61 
66  const std::string& getFullPath() const;
67 
68  std::string toString() const;
69 
72  inline bool operator<(const FileInfo& rhs) const
73  {
74  return (getFullPath() < rhs.getFullPath());
75  }
76 
77  private:
78  std::string _path; // path portion with no trailing "/"
79  std::string _basename; // just the basename
80  mutable std::string _fullPath; // cache of the full pathname, path + "/" + basename
81  bool _isDir; // true if a directory, else a file.
82  time_t _modTime; // last modification time
83  };
84 
89  {
90  public:
91  DirectoryUtil();
93 
95  const std::string& getRootDir() const;
96 
103  void setRootDir(const std::string& rootDir, bool allowRelativePaths=false, bool allowSymLinks=false);
104 
109  void setFilterSuffix(const std::string& suffix);
110 
125  void setFilterRegExp(const std::string& regexp);
126 
130  void clearRegExp();
131 
137  void setFilterModTimeOlderThan(time_t newestModTime);
138 
155  void getListingForPath(const std::string& path,
156  std::vector<FileInfo>* pRegularFiles,
157  std::vector<FileInfo>* pDirectories);
158 
168  void getListingForPathRecursive(const std::string& path,
169  std::vector<FileInfo>* pRegularFiles,
170  std::vector<FileInfo>* pDirectories);
171 
177  void getListingOfRegularFilesRecursive(const std::string& path,
178  std::vector<FileInfo>& rRegularFiles);
179 
181  static bool hasRelativePath(const std::string& path);
182 
184  static void removeTrailingSlashes(std::string& path);
185 
187  static void removePrecedingSlashes(std::string& path);
188 
190  static void printFileInfoList(std::ostream& os, const std::vector<FileInfo>& listing);
191 
193  static void printFileInfoList(const std::vector<FileInfo>& listing);
194 
203  static std::string getBESRootDir();
204 
205  static bool matchesSuffix(const std::string& filename, const std::string& suffix);
206 
207  private: // helper methods
208 
212  void throwErrorForOpendirFail(const std::string& fullPath);
213 
220  bool matchesAllFilters(const std::string& path, time_t modTime) const;
221 
222  private:
223 
224  // The search rootdir with no trailing slash
225  // defaults to "/" if not set.
226  std::string _rootDir;
227 
228  // if !empty(), files returned will end in this suffix.
229  std::string _suffix;
230 
231  // If a regular expression is specified, this will be
232  // non-null and used to match each filename.
233  libdap::Regex* _pRegExp;
234 
235  // True if there was a newest modtime filter set.
236  bool _filteringModTimes;
237 
238  // If _filteringModTimes, this will contain the
239  // newest modtime of files we want to include.
240  time_t _newestModTime;
241 
242  // Name to use in BESDEBUG channel for this class.
243  static const std::string _sDebugChannel;
244  };
245 }
246 
247 #endif /* __AGG_UTIL__DIRECTORY_UTIL_H__ */
Class to hold info on files as we get them.
Definition: DirectoryUtil.h:46
FileInfo(const std::string &path, const std::string &basename, bool isDir, time_t modTime)
strips any trailing "/" on path.
void getListingOfRegularFilesRecursive(const std::string &path, std::vector< FileInfo > &rRegularFiles)
Get recursive listing of all regular files in the directory subtree.
const std::string & basename() const
const std::string & getFullPath() const
Get the path and basename as path + "/" + basename We cache this after first call to allow for a cons...
void setFilterRegExp(const std::string &regexp)
Set a (GNU style) regular expression to be used to match against the full filename (relative path und...
const std::string & path() const
does not include trailing "/"
bool isDir() const
void getListingForPath(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
Get a listing of all the regular files and directories in the given path, which is assumed relative t...
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
bool operator<(const FileInfo &rhs) const
Comparator using string::operator< on the getFullPath() result.
Definition: DirectoryUtil.h:72
static bool matchesSuffix(const std::string &filename, const std::string &suffix)
std::string toString() const
std::string getModTimeAsString() const
Get a human readable string for the modTime()
static class NCMLUtil overview
Helper classes for using dirent.h, dir.h, stat.h, etc.
Definition: DirectoryUtil.h:88
void setFilterSuffix(const std::string &suffix)
Set the filter to be used for the nexy getListingForPath() call.
static std::string getBESRootDir()
Gets the BES root directory by checking the bes.conf settings for BES.
static void removeTrailingSlashes(std::string &path)
mutate to remove all trailing "/"
void setFilterModTimeOlderThan(time_t newestModTime)
Set a filter on the modification time of the files to be returned in a listing.
void clearRegExp()
Remove any filter using a regular expression.
const std::string & getRootDir() const
get the current root dir
void setRootDir(const std::string &rootDir, bool allowRelativePaths=false, bool allowSymLinks=false)
Makes sure the directory exists and is readable or throws an exception exception. ...
time_t modTime() const
static void removePrecedingSlashes(std::string &path)
mutate to remove and preceding (in the front) "/"
void getListingForPathRecursive(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
Get the listing for the path recursing into every directory found until it bottoms out...
static void printFileInfoList(std::ostream &os, const std::vector< FileInfo > &listing)
Print the list of files to the stream.
static bool hasRelativePath(const std::string &path)
Is there a "../" in path?