BESFSFile.cc

Go to the documentation of this file.
00001 // BESFSFile.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 "config.h"
00034 
00035 #if HAVE_UNISTD_H
00036 #include <unistd.h>
00037 #endif
00038 #include <cerrno>
00039 
00040 #include "BESFSFile.h"
00041 
00042 BESFSFile::BESFSFile(const string &fullPath)
00043         : _dirName(""),
00044         _fileName(""),
00045         _baseName(""),
00046         _extension("")
00047 {
00048     breakApart(fullPath) ;
00049 }
00050 
00051 BESFSFile::BESFSFile(const string &dirName, const string &fileName)
00052         : _dirName(dirName),
00053         _fileName(fileName),
00054         _baseName(""),
00055         _extension("")
00056 {
00057     breakExtension() ;
00058 }
00059 
00060 BESFSFile::BESFSFile(const BESFSFile &copyFrom)
00061         : _dirName(copyFrom._dirName),
00062         _fileName(copyFrom._fileName),
00063         _baseName(copyFrom._baseName),
00064         _extension(copyFrom._extension)
00065 {}
00066 
00067 BESFSFile::~BESFSFile()
00068 {}
00069 
00070 string
00071 BESFSFile::getDirName()
00072 {
00073     return _dirName ;
00074 }
00075 
00076 string
00077 BESFSFile::getFileName()
00078 {
00079     return _fileName ;
00080 }
00081 
00082 string
00083 BESFSFile::getBaseName()
00084 {
00085     return _baseName ;
00086 }
00087 
00088 string
00089 BESFSFile::getExtension()
00090 {
00091     return _extension ;
00092 }
00093 
00094 string
00095 BESFSFile::getFullPath()
00096 {
00097     return _dirName + "/" + _fileName ;
00098 }
00099 
00100 void
00101 BESFSFile::breakApart(const string &fullPath)
00102 {
00103     string::size_type pos = fullPath.rfind("/") ;
00104     if (pos != string::npos) {
00105         _dirName = fullPath.substr(0, pos) ;
00106         _fileName = fullPath.substr(pos + 1, fullPath.length() - pos) ;
00107     }
00108     else {
00109         _dirName = "./" ;
00110         _fileName = fullPath ;
00111     }
00112 
00113     breakExtension() ;
00114 }
00115 
00116 void
00117 BESFSFile::breakExtension()
00118 {
00119     string::size_type pos = _fileName.rfind(".") ;
00120     if (pos != string::npos) {
00121         _baseName = _fileName.substr(0, pos) ;
00122         _extension = _fileName.substr(pos + 1, _fileName.length() - pos) ;
00123     }
00124     else {
00125         _baseName = _fileName ;
00126     }
00127 }
00128 
00129 bool
00130 BESFSFile::exists( string &reason )
00131 {
00132     bool ret = false ;
00133     if( !access( getFullPath().c_str(), F_OK ) )
00134     {
00135         ret = true ;
00136     }
00137     else
00138     {
00139         char *err = strerror( errno ) ;
00140         if( err )
00141         {
00142             reason += err ;
00143         }
00144         else
00145         {
00146             reason += "Unknown error" ;
00147         }
00148     }
00149     return ret ;
00150 }
00151 
00152 bool
00153 BESFSFile::isReadable( string &reason )
00154 {
00155     bool ret = false ;
00156     if( !access( getFullPath().c_str(), R_OK ) )
00157     {
00158         ret = true ;
00159     }
00160     else
00161     {
00162         char *err = strerror( errno ) ;
00163         if( err )
00164         {
00165             reason += err ;
00166         }
00167         else
00168         {
00169             reason += "Unknown error" ;
00170         }
00171     }
00172     return ret ;
00173 }
00174 
00175 bool
00176 BESFSFile::isWritable( string &reason )
00177 {
00178     bool ret = false ;
00179     if( !access( getFullPath().c_str(), W_OK ) )
00180     {
00181         ret = true ;
00182     }
00183     else
00184     {
00185         char *err = strerror( errno ) ;
00186         if( err )
00187         {
00188             reason += err ;
00189         }
00190         else
00191         {
00192             reason += "Unknown error" ;
00193         }
00194     }
00195     return ret ;
00196 }
00197 
00198 bool
00199 BESFSFile::isExecutable( string &reason )
00200 {
00201     bool ret = false ;
00202     if( !access( getFullPath().c_str(), X_OK ) )
00203     {
00204         ret = true ;
00205     }
00206     else
00207     {
00208         char *err = strerror( errno ) ;
00209         if( err )
00210         {
00211             reason += err ;
00212         }
00213         else
00214         {
00215             reason += "Unknown error" ;
00216         }
00217     }
00218     return ret ;
00219 }
00220 
00221 bool
00222 BESFSFile::hasDotDot()
00223 {
00224     bool ret = false ;
00225     string fp = getFullPath() ;
00226     if( fp.find( ".." ) != string::npos )
00227     {
00228         ret = true ;
00229     }
00230     return ret ;
00231 }
00232 

Generated on Thu Feb 11 09:13:14 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7