bes  Updated for version 3.19.1
BESFSDir.cc
1 // BESFSDir.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <dirent.h>
36 #ifdef WIN32
37 #include <config.h> // for S_ISDIR macro
38 #endif
39 #include <stdio.h>
40 
41 #include "BESFSDir.h"
42 #include "BESRegex.h"
43 #include "BESInternalError.h"
44 
45 BESFSDir::BESFSDir(const string &dirName) :
46  _dirName(dirName), _fileExpr(""), _dirLoaded(false)
47 {
48 }
49 
50 BESFSDir::BESFSDir(const string &dirName, const string &fileExpr) :
51  _dirName(dirName), _fileExpr(fileExpr), _dirLoaded(false)
52 {
53 }
54 
55 BESFSDir::BESFSDir(const BESFSDir &copyFrom) :
56  _dirName(copyFrom._dirName), _fileExpr(copyFrom._fileExpr), _dirLoaded(false)
57 {
58 }
59 
60 BESFSDir::~BESFSDir()
61 {
62 }
63 
64 BESFSDir::dirIterator BESFSDir::beginOfDirList()
65 {
66  if (_dirLoaded == false) {
67  loadDir();
68  _dirLoaded = true;
69  }
70  return _dirList.begin();
71 }
72 
73 BESFSDir::dirIterator BESFSDir::endOfDirList()
74 {
75  if (_dirLoaded == false) {
76  loadDir();
77  _dirLoaded = true;
78  }
79  return _dirList.end();
80 }
81 
82 BESFSDir::fileIterator BESFSDir::beginOfFileList()
83 {
84  if (_dirLoaded == false) {
85  loadDir();
86  _dirLoaded = true;
87  }
88  return _fileList.begin();
89 }
90 
91 BESFSDir::fileIterator BESFSDir::endOfFileList()
92 {
93  if (_dirLoaded == false) {
94  loadDir();
95  _dirLoaded = true;
96  }
97  return _fileList.end();
98 }
99 
100 void BESFSDir::loadDir()
101 {
102  DIR * dip;
103  struct dirent *dit;
104 
105  try {
106  // open a directory stream
107  // make sure the directory is valid and readable
108  if ((dip = opendir(_dirName.c_str())) == NULL) {
109  string err_str = "ERROR: failed to open directory '" + _dirName + "'";
110  throw BESError(err_str, BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
111  }
112  else {
113  // read in the files in this directory
114  // add each filename to the list of filenames
115  while ((dit = readdir(dip)) != NULL) {
116  struct stat buf;
117  string dirEntry = dit->d_name;
118  if (dirEntry != "." && dirEntry != "..") {
119  string fullPath = _dirName + "/" + dirEntry;
120 
121  // This test used to throw a BES_NOT_FOUND_ERROR which I don't think is
122  // correct. If opendir() is used to open _dirName and iterate over the
123  // entries returned by repeated calls to readdir() then how can the
124  // paths be bad? One way, it turns out, is if there is a second process
125  // adding and removing files in _dirName. But I think it's better to
126  // ignore that case then return an error - is it really a BES error if
127  // another process is rude? This showed up when running parallel tests
128  // because the tests keysT and pvolT were stepping on each other. However,
129  // I don't think this is limited to 'make check -j9' since it could be
130  // exploited to crash the bes. jhrg 3/30/17
131  if (-1 == stat(fullPath.c_str(), &buf))
132  continue;
133 #if 0
134  // Replaced by 'continue'. jhrg 3/30/17
135  throw BESError(string("Did not find the path: '") + fullPath + "'", BES_NOT_FOUND_ERROR,
136  __FILE__, __LINE__);
137 #endif
138  // look at the mode and determine if this is a filename
139  // or a directory name
140  if (S_ISDIR(buf.st_mode)) {
141  _dirList.push_back(BESFSDir(fullPath));
142  }
143  else {
144  if (_fileExpr != "") {
145  BESRegex reg_expr(_fileExpr.c_str());
146  int match_ret = reg_expr.match(dirEntry.c_str(), dirEntry.length());
147  if (match_ret == static_cast<int>(dirEntry.length())) {
148  _fileList.push_back(BESFSFile(_dirName, dirEntry));
149  }
150  }
151  else {
152  _fileList.push_back(BESFSFile(_dirName, dirEntry));
153  }
154  }
155  }
156  }
157  }
158 
159  // close the directory
160  closedir(dip);
161  }
162  catch (...) {
163  // close the directory
164  closedir(dip);
165  throw;
166  }
167 }
168 
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: BESRegex.cc:103
Abstract exception class for the BES with basic string message.
Definition: BESError.h:56