bes  Updated for version 3.17.4
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  // open a directory stream
106  // make sure the directory is valid and readable
107  if ((dip = opendir(_dirName.c_str())) == NULL) {
108  string err_str = "ERROR: failed to open directory '" + _dirName + "'";
109  throw BESError(err_str, BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
110  }
111  else {
112  // read in the files in this directory
113  // add each filename to the list of filenames
114  while ((dit = readdir(dip)) != NULL) {
115  struct stat buf;
116  string dirEntry = dit->d_name;
117  if (dirEntry != "." && dirEntry != "..") {
118  string fullPath = _dirName + "/" + dirEntry;
119  if (-1 == stat(fullPath.c_str(), &buf))
120  throw BESError(string("Did not find the path: '") + fullPath + "'", BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
121 
122  // look at the mode and determine if this is a filename
123  // or a directory name
124  if (S_ISDIR(buf.st_mode)) {
125  _dirList.push_back(BESFSDir(fullPath));
126  }
127  else {
128  if (_fileExpr != "") {
129  BESRegex reg_expr(_fileExpr.c_str());
130  int match_ret = reg_expr.match(dirEntry.c_str(), dirEntry.length());
131  if (match_ret == static_cast<int>(dirEntry.length())) {
132  _fileList.push_back(BESFSFile(_dirName, dirEntry));
133  }
134  }
135  else {
136  _fileList.push_back(BESFSFile(_dirName, dirEntry));
137  }
138  }
139  }
140  }
141  }
142 
143  // close the directory
144  closedir(dip);
145 }
146 
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