bes  Updated for version 3.20.6
DirectoryUtil.h
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 class Regex;
40 // GNU regular expression lib wrapper
41 }
42 ;
43 
44 namespace agg_util {
46 class FileInfo {
47 public:
49  FileInfo(const std::string& path, const std::string& basename, bool isDir, time_t modTime);
50  ~FileInfo();
51 
53  const std::string& path() const;
54  const std::string& basename() const;
55  bool isDir() const;
56  time_t modTime() const;
57 
59  std::string getModTimeAsString() const;
60 
65  const std::string& getFullPath() const;
66 
67  std::string toString() const;
68 
71  inline bool operator<(const FileInfo& rhs) const
72  {
73  return (getFullPath() < rhs.getFullPath());
74  }
75 
76 private:
77  std::string _path; // path portion with no trailing "/"
78  std::string _basename; // just the basename
79  mutable std::string _fullPath; // cache of the full pathname, path + "/" + basename
80  bool _isDir; // true if a directory, else a file.
81  time_t _modTime; // last modification time
82 };
83 
88 public:
89  DirectoryUtil();
90  ~DirectoryUtil();
91 
93  const std::string& getRootDir() const;
94 
101  void setRootDir(const std::string& rootDir, bool allowRelativePaths = false, bool allowSymLinks = false);
102 
107  void setFilterSuffix(const std::string& suffix);
108 
123  void setFilterRegExp(const std::string& regexp);
124 
128  void clearRegExp();
129 
135  void setFilterModTimeOlderThan(time_t newestModTime);
136 
153  void getListingForPath(const std::string& path, std::vector<FileInfo>* pRegularFiles,
154  std::vector<FileInfo>* pDirectories);
155 
165  void getListingForPathRecursive(const std::string& path, std::vector<FileInfo>* pRegularFiles,
166  std::vector<FileInfo>* pDirectories);
167 
173  void getListingOfRegularFilesRecursive(const std::string& path, std::vector<FileInfo>& rRegularFiles);
174 
176  static bool hasRelativePath(const std::string& path);
177 
179  static void removeTrailingSlashes(std::string& path);
180 
182  static void removePrecedingSlashes(std::string& path);
183 
185  static void printFileInfoList(std::ostream& os, const std::vector<FileInfo>& listing);
186 
188  static void printFileInfoList(const std::vector<FileInfo>& listing);
189 
198  static std::string getBESRootDir();
199 
200  static bool matchesSuffix(const std::string& filename, const std::string& suffix);
201 
202 private:
203  // helper methods
204 
208  void throwErrorForOpendirFail(const std::string& fullPath);
209 
216  bool matchesAllFilters(const std::string& path, time_t modTime) const;
217 
218 private:
219 
220  // The search rootdir with no trailing slash
221  // defaults to "/" if not set.
222  std::string _rootDir;
223 
224  // if !empty(), files returned will end in this suffix.
225  std::string _suffix;
226 
227  // If a regular expression is specified, this will be
228  // non-null and used to match each filename.
229  libdap::Regex* _pRegExp;
230 
231  // True if there was a newest modtime filter set.
232  bool _filteringModTimes;
233 
234  // If _filteringModTimes, this will contain the
235  // newest modtime of files we want to include.
236  time_t _newestModTime;
237 
238  // Name to use in BESDEBUG channel for this class.
239  static const std::string _sDebugChannel;
240 };
241 }
242 
243 #endif /* __AGG_UTIL__DIRECTORY_UTIL_H__ */
agg_util::DirectoryUtil::getListingOfRegularFilesRecursive
void getListingOfRegularFilesRecursive(const std::string &path, std::vector< FileInfo > &rRegularFiles)
Definition: DirectoryUtil.cc:313
agg_util::FileInfo
Definition: DirectoryUtil.h:46
agg_util::DirectoryUtil::removePrecedingSlashes
static void removePrecedingSlashes(std::string &path)
Definition: DirectoryUtil.cc:402
agg_util::DirectoryUtil::setRootDir
void setRootDir(const std::string &rootDir, bool allowRelativePaths=false, bool allowSymLinks=false)
Definition: DirectoryUtil.cc:184
agg_util::DirectoryUtil::setFilterRegExp
void setFilterRegExp(const std::string &regexp)
Definition: DirectoryUtil.cc:211
agg_util::FileInfo::operator<
bool operator<(const FileInfo &rhs) const
Definition: DirectoryUtil.h:71
agg_util
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
Definition: AggMemberDataset.cc:38
libdap
Definition: BESDapFunctionResponseCache.h:35
agg_util::DirectoryUtil::removeTrailingSlashes
static void removeTrailingSlashes(std::string &path)
Definition: DirectoryUtil.cc:392
agg_util::DirectoryUtil::setFilterSuffix
void setFilterSuffix(const std::string &suffix)
Definition: DirectoryUtil.cc:206
agg_util::FileInfo::FileInfo
FileInfo(const std::string &path, const std::string &basename, bool isDir, time_t modTime)
Definition: DirectoryUtil.cc:95
agg_util::DirectoryUtil::getRootDir
const std::string & getRootDir() const
Definition: DirectoryUtil.cc:174
agg_util::FileInfo::path
const std::string & path() const
Definition: DirectoryUtil.cc:108
agg_util::FileInfo::getModTimeAsString
std::string getModTimeAsString() const
Definition: DirectoryUtil.cc:129
agg_util::FileInfo::getFullPath
const std::string & getFullPath() const
Definition: DirectoryUtil.cc:140
agg_util::DirectoryUtil::printFileInfoList
static void printFileInfoList(std::ostream &os, const std::vector< FileInfo > &listing)
Definition: DirectoryUtil.cc:417
agg_util::DirectoryUtil::setFilterModTimeOlderThan
void setFilterModTimeOlderThan(time_t newestModTime)
Definition: DirectoryUtil.cc:225
agg_util::DirectoryUtil::getBESRootDir
static std::string getBESRootDir()
Definition: DirectoryUtil.cc:424
agg_util::DirectoryUtil::getListingForPath
void getListingForPath(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
Definition: DirectoryUtil.cc:231
agg_util::DirectoryUtil::getListingForPathRecursive
void getListingForPathRecursive(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
Definition: DirectoryUtil.cc:280
agg_util::DirectoryUtil
Definition: DirectoryUtil.h:87
agg_util::DirectoryUtil::clearRegExp
void clearRegExp()
Definition: DirectoryUtil.cc:219
agg_util::DirectoryUtil::hasRelativePath
static bool hasRelativePath(const std::string &path)
Definition: DirectoryUtil.cc:387