OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
|
00001 // BESFSDir.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <sys/types.h> 00034 #include <sys/stat.h> 00035 #include <dirent.h> 00036 #ifdef WIN32 00037 #include <config.h> // for S_ISDIR macro 00038 #endif 00039 #include <stdio.h> 00040 00041 #include "BESFSDir.h" 00042 #include "BESRegex.h" 00043 #include "BESInternalError.h" 00044 00045 BESFSDir::BESFSDir(const string &dirName) 00046 : _dirName(dirName), 00047 _fileExpr(""), 00048 _dirLoaded(false) 00049 {} 00050 00051 BESFSDir::BESFSDir(const string &dirName, const string &fileExpr) 00052 : _dirName(dirName), 00053 _fileExpr(fileExpr), 00054 _dirLoaded(false) 00055 {} 00056 00057 BESFSDir::BESFSDir(const BESFSDir ©From) 00058 : _dirName(copyFrom._dirName), 00059 _fileExpr(copyFrom._fileExpr), 00060 _dirLoaded(false) 00061 {} 00062 00063 BESFSDir::~BESFSDir() 00064 {} 00065 00066 BESFSDir::dirIterator 00067 BESFSDir::beginOfDirList() 00068 { 00069 if (_dirLoaded == false) { 00070 loadDir() ; 00071 _dirLoaded = true ; 00072 } 00073 return _dirList.begin() ; 00074 } 00075 00076 BESFSDir::dirIterator 00077 BESFSDir::endOfDirList() 00078 { 00079 if (_dirLoaded == false) { 00080 loadDir() ; 00081 _dirLoaded = true ; 00082 } 00083 return _dirList.end() ; 00084 } 00085 00086 BESFSDir::fileIterator 00087 BESFSDir::beginOfFileList() 00088 { 00089 if (_dirLoaded == false) { 00090 loadDir() ; 00091 _dirLoaded = true ; 00092 } 00093 return _fileList.begin() ; 00094 } 00095 00096 BESFSDir::fileIterator 00097 BESFSDir::endOfFileList() 00098 { 00099 if (_dirLoaded == false) { 00100 loadDir() ; 00101 _dirLoaded = true ; 00102 } 00103 return _fileList.end() ; 00104 } 00105 00106 void 00107 BESFSDir::loadDir() 00108 { 00109 DIR * dip; 00110 struct dirent *dit; 00111 00112 // open a directory stream 00113 // make sure the directory is valid and readable 00114 if( ( dip = opendir( _dirName.c_str() ) ) == NULL ) 00115 { 00116 string err_str = "ERROR: failed to open directory '" + _dirName + "'" ; 00117 throw BESInternalError( err_str, __FILE__, __LINE__ ) ; 00118 } 00119 else 00120 { 00121 // read in the files in this directory 00122 // add each filename to the list of filenames 00123 while ((dit = readdir(dip)) != NULL) 00124 { 00125 struct stat buf; 00126 string dirEntry = dit->d_name ; 00127 if (dirEntry != "." && dirEntry != "..") { 00128 string fullPath = _dirName + "/" + dirEntry ; 00129 stat(fullPath.c_str(), &buf) ; 00130 00131 // look at the mode and determine if this is a filename 00132 // or a directory name 00133 if (S_ISDIR(buf.st_mode)) { 00134 _dirList.push_back(BESFSDir(fullPath)) ; 00135 } 00136 else { 00137 if (_fileExpr != "") { 00138 BESRegex reg_expr(_fileExpr.c_str()) ; 00139 int match_ret = reg_expr.match( dirEntry.c_str(), 00140 dirEntry.length() ) ; 00141 if( match_ret == static_cast<int>(dirEntry.length()) ) 00142 { 00143 _fileList.push_back(BESFSFile(_dirName, dirEntry)); 00144 } 00145 } 00146 else 00147 { 00148 _fileList.push_back(BESFSFile(_dirName, dirEntry)) ; 00149 } 00150 } 00151 } 00152 } 00153 } 00154 00155 // close the directory 00156 closedir(dip) ; 00157 } 00158