bes  Updated for version 3.20.8
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 namespace agg_util {
45 class FileInfo {
46 public:
48  FileInfo(const std::string& path, const std::string& basename, bool isDir, time_t modTime);
49  ~FileInfo();
50 
52  const std::string& path() const;
53  const std::string& basename() const;
54  bool isDir() const;
55  time_t modTime() const;
56 
58  std::string getModTimeAsString() const;
59 
64  const std::string& getFullPath() const;
65 
66  std::string toString() const;
67 
70  inline bool operator<(const FileInfo& rhs) const
71  {
72  return (getFullPath() < rhs.getFullPath());
73  }
74 
75 private:
76  std::string _path; // path portion with no trailing "/"
77  std::string _basename; // just the basename
78  mutable std::string _fullPath; // cache of the full pathname, path + "/" + basename
79  bool _isDir; // true if a directory, else a file.
80  time_t _modTime; // last modification time
81 };
82 
87 public:
88  DirectoryUtil();
89  ~DirectoryUtil();
90 
92  const std::string& getRootDir() const;
93 
100  void setRootDir(const std::string& rootDir, bool allowRelativePaths = false, bool allowSymLinks = false);
101 
106  void setFilterSuffix(const std::string& suffix);
107 
122  void setFilterRegExp(const std::string& regexp);
123 
127  void clearRegExp();
128 
134  void setFilterModTimeOlderThan(time_t newestModTime);
135 
152  void getListingForPath(const std::string& path, std::vector<FileInfo>* pRegularFiles,
153  std::vector<FileInfo>* pDirectories);
154 
164  void getListingForPathRecursive(const std::string& path, std::vector<FileInfo>* pRegularFiles,
165  std::vector<FileInfo>* pDirectories);
166 
172  void getListingOfRegularFilesRecursive(const std::string& path, std::vector<FileInfo>& rRegularFiles);
173 
175  static bool hasRelativePath(const std::string& path);
176 
178  static void removeTrailingSlashes(std::string& path);
179 
181  static void removePrecedingSlashes(std::string& path);
182 
184  static void printFileInfoList(std::ostream& os, const std::vector<FileInfo>& listing);
185 
187  static void printFileInfoList(const std::vector<FileInfo>& listing);
188 
197  static std::string getBESRootDir();
198 
199  static bool matchesSuffix(const std::string& filename, const std::string& suffix);
200 
201 private:
202  // helper methods
203 
207  void throwErrorForOpendirFail(const std::string& fullPath);
208 
215  bool matchesAllFilters(const std::string& path, time_t modTime) const;
216 
217 private:
218 
219  // The search rootdir with no trailing slash
220  // defaults to "/" if not set.
221  std::string _rootDir;
222 
223  // if !empty(), files returned will end in this suffix.
224  std::string _suffix;
225 
226  // If a regular expression is specified, this will be
227  // non-null and used to match each filename.
228  libdap::Regex* _pRegExp;
229 
230  // True if there was a newest modtime filter set.
231  bool _filteringModTimes;
232 
233  // If _filteringModTimes, this will contain the
234  // newest modtime of files we want to include.
235  time_t _newestModTime;
236 
237  // Name to use in BESDEBUG channel for this class.
238  static const std::string _sDebugChannel;
239 };
240 }
241 
242 #endif /* __AGG_UTIL__DIRECTORY_UTIL_H__ */
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:70
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...