00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 ©From)
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