bes  Updated for version 3.20.10
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 class BESRegex;
39 
40 namespace agg_util {
42 class FileInfo {
43 public:
45  FileInfo(const std::string& path, const std::string& basename, bool isDir, time_t modTime);
46  ~FileInfo();
47 
49  const std::string& path() const;
50  const std::string& basename() const;
51  bool isDir() const;
52  time_t modTime() const;
53 
55  std::string getModTimeAsString() const;
56 
61  const std::string& getFullPath() const;
62 
63  std::string toString() const;
64 
67  inline bool operator<(const FileInfo& rhs) const
68  {
69  return (getFullPath() < rhs.getFullPath());
70  }
71 
72 private:
73  std::string _path; // path portion with no trailing "/"
74  std::string _basename; // just the basename
75  mutable std::string _fullPath; // cache of the full pathname, path + "/" + basename
76  bool _isDir; // true if a directory, else a file.
77  time_t _modTime; // last modification time
78 };
79 
84 public:
85  DirectoryUtil();
86  ~DirectoryUtil();
87 
89  const std::string& getRootDir() const;
90 
97  void setRootDir(const std::string& rootDir, bool allowRelativePaths = false, bool allowSymLinks = false);
98 
103  void setFilterSuffix(const std::string& suffix);
104 
119  void setFilterRegExp(const std::string& regexp);
120 
124  void clearRegExp();
125 
131  void setFilterModTimeOlderThan(time_t newestModTime);
132 
149  void getListingForPath(const std::string& path, std::vector<FileInfo>* pRegularFiles,
150  std::vector<FileInfo>* pDirectories);
151 
161  void getListingForPathRecursive(const std::string& path, std::vector<FileInfo>* pRegularFiles,
162  std::vector<FileInfo>* pDirectories);
163 
169  void getListingOfRegularFilesRecursive(const std::string& path, std::vector<FileInfo>& rRegularFiles);
170 
172  static bool hasRelativePath(const std::string& path);
173 
175  static void removeTrailingSlashes(std::string& path);
176 
178  static void removePrecedingSlashes(std::string& path);
179 
181  static void printFileInfoList(std::ostream& os, const std::vector<FileInfo>& listing);
182 
184  static void printFileInfoList(const std::vector<FileInfo>& listing);
185 
194  static std::string getBESRootDir();
195 
196  static bool matchesSuffix(const std::string& filename, const std::string& suffix);
197 
198 private:
199  // helper methods
200 
204  void throwErrorForOpendirFail(const std::string& fullPath);
205 
212  bool matchesAllFilters(const std::string& path, time_t modTime) const;
213 
214 private:
215 
216  // The search rootdir with no trailing slash
217  // defaults to "/" if not set.
218  std::string _rootDir;
219 
220  // if !empty(), files returned will end in this suffix.
221  std::string _suffix;
222 
223  // If a regular expression is specified, this will be
224  // non-null and used to match each filename.
225  BESRegex* _pRegExp;
226 
227  // True if there was a newest modtime filter set.
228  bool _filteringModTimes;
229 
230  // If _filteringModTimes, this will contain the
231  // newest modtime of files we want to include.
232  time_t _newestModTime;
233 
234  // Name to use in BESDEBUG channel for this class.
235  static const std::string _sDebugChannel;
236 };
237 }
238 
239 #endif /* __AGG_UTIL__DIRECTORY_UTIL_H__ */
Regular expression matching.
Definition: BESRegex.h:53
static std::string getBESRootDir()
void setFilterRegExp(const std::string &regexp)
static void removePrecedingSlashes(std::string &path)
static bool hasRelativePath(const std::string &path)
static void printFileInfoList(std::ostream &os, const std::vector< FileInfo > &listing)
void setRootDir(const std::string &rootDir, bool allowRelativePaths=false, bool allowSymLinks=false)
void getListingOfRegularFilesRecursive(const std::string &path, std::vector< FileInfo > &rRegularFiles)
void setFilterSuffix(const std::string &suffix)
static void removeTrailingSlashes(std::string &path)
void getListingForPath(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
void getListingForPathRecursive(const std::string &path, std::vector< FileInfo > *pRegularFiles, std::vector< FileInfo > *pDirectories)
void setFilterModTimeOlderThan(time_t newestModTime)
const std::string & getRootDir() const
bool operator<(const FileInfo &rhs) const
Definition: DirectoryUtil.h:67
std::string getModTimeAsString() const
FileInfo(const std::string &path, const std::string &basename, bool isDir, time_t modTime)
const std::string & path() const
const std::string & getFullPath() const
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...