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
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 ©From)
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