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