00001 #ifndef OC_FILE_SYS_H
00002 #define OC_FILE_SYS_H
00003
00004 #include "ocString.h"
00005 #include <stdio.h>
00006 #include <sys/stat.h>
00007 #include <unistd.h>
00008 #include <sys/types.h>
00009 #include <dirent.h>
00010 #include <fstream>
00011 #include <vector>
00012 #include <string>
00013
00014
00015
00016
00017
00018 struct ocDirEntry
00019 {
00020 string name;
00021 ino_t fileno;
00022 unsigned char type;
00023
00024 mode_t file_mode;
00025 uid_t file_uid;
00026 gid_t file_gid;
00027 off_t file_size;
00028 time_t last_access;
00029 time_t last_mod;
00030 time_t last_statchg;
00031
00032 ocDirEntry():fileno(0),type(0),
00033 file_mode(0),file_uid(0),file_gid(0),file_size(0),last_access(0),last_mod(0),last_statchg(0)
00034 {;}
00035 ocDirEntry(dirent * pin, string path):fileno(0),type(0),
00036 file_mode(0),file_uid(0),file_gid(0),file_size(0),last_access(0),last_mod(0),last_statchg(0)
00037 {
00038 if(pin)
00039 {
00040 name = pin->d_name;
00041 fileno = pin->d_fileno;
00042 type = pin->d_type;
00043 struct stat buf;
00044 path +="/";
00045 path += name;
00046 int ret = stat(path.c_str(), &buf);
00047 if( ret==0 )
00048 {
00049 file_mode = buf.st_mode;
00050 file_uid = buf.st_uid;
00051 file_gid = buf.st_gid;
00052 file_size = buf.st_size;
00053 last_access = buf.st_atime;
00054 last_mod = buf.st_mtime;
00055 last_statchg = buf.st_ctime;
00056 }
00057 }
00058 }
00059 ocDirEntry( const ocDirEntry & in )
00060 :name(in.name),fileno(in.fileno),type(in.type){;}
00061 ocDirEntry & operator = ( const ocDirEntry & in )
00062 {
00063 name = in.name;
00064 fileno = in.fileno;
00065 type = in.type;
00066 return * this;
00067 }
00068 virtual ~ocDirEntry(){;}
00069 };
00070
00071 typedef vector<ocDirEntry> ocDirectory;
00072
00073 class ocFileSys
00074 {
00075 DIR *dp;
00076 struct dirent *ep;
00077 mode_t mode;
00078 ocDirectory m_directory;
00079 int rc;
00080 string error;
00081
00082 public:
00083 ocFileSys():dp(NULL),ep(NULL)
00084 {
00085
00086 mode = S_IRUSR | S_IWUSR | S_IXUSR |
00087 S_IRGRP | S_IWGRP | S_IXGRP |
00088 S_IROTH | S_IXOTH;
00089 }
00090 ~ocFileSys()
00091 {
00092
00093 }
00094
00095
00096
00097
00098 void setMode( mode_t in )
00099 {
00100 mode = in;
00101 }
00102
00103 bool remove( string path )
00104 {
00105 rc = ::remove( path.c_str() );
00106 return rc == 0;
00107 }
00108
00109 string check( void )
00110 {
00111 error = "";
00112 if(rc)
00113 {
00114 if( errno == EFAULT )
00115 {
00116 error = "EFAULT pathname points outside your accessible address space.";
00117 }
00118 if( errno == EACCES )
00119 {
00120 error = "EACCES Write access to the directory containing pathname is not allowed "
00121 " for the process's effective uid, or one of the directories in"
00122 " pathname did not allow search (execute) permission.";
00123 }
00124 if( errno == EPERM )
00125 {
00126 error = "EPERM The directory containing pathname has the sticky-bit (S_ISVTX) "
00127 " set and the process's effective uid is neither the uid of the "
00128 " file to be deleted nor that of the directory containing it. ";
00129 }
00130 if( errno == ENAMETOOLONG )
00131 {
00132 error = "ENAMETOOLONG pathname was too long.";
00133 }
00134 if( errno == ENOENT )
00135 {
00136 error = "ENOENT A directory component in pathname does not exist or is a dangling symbolic link.";
00137 }
00138 if( errno == ENOTDIR)
00139 {
00140 error = "ENOTDIR A component used as a directory in pathname is not, in fact, a directory.";
00141 }
00142 if( errno == ENOMEM )
00143 {
00144 error = "ENOMEM Insufficient kernel memory was available.";
00145 }
00146 if( errno == EROFS )
00147 {
00148 error = "EROFS pathname refers to a file on a read-only filesystem.";
00149 }
00150 }
00151 return error;
00152 }
00153
00154 bool rename( string path, string newpath )
00155 {
00156 rc = ::rename( path.c_str(), newpath.c_str() );
00157 return rc == 0;
00158 }
00159 bool copy( string path, string newpath )
00160 {
00161 bool isOK = false;
00162 ifstream src(path.c_str(), ifstream::in | ifstream::binary);
00163 ofstream tgt(newpath.c_str(), ifstream::trunc | ifstream::binary);
00164 if( src.good() && tgt.good() )
00165 {
00166
00167 src.seekg (0, ios::end);
00168 int length = src.tellg();
00169 src.seekg (0, ios::beg);
00170
00171 char * buffer = new char [length];
00172 if( length > 0 && buffer )
00173 {
00174
00175 src.read (buffer,length);
00176 tgt.write(buffer,length);
00177 delete [] buffer;
00178 src.close();
00179 tgt.close();
00180 isOK = true;
00181 }
00182 }
00183 return isOK;
00184 }
00185 bool is( string path )
00186 {
00187 rc = access(path.c_str(), F_OK);
00188 return rc == 0;
00189 }
00190
00191
00192
00193 bool openDir( string path )
00194 {
00195 bool bRet = false;
00196 dp = opendir( path.c_str() );
00197 if(dp)
00198 {
00199 while (ep = readdir (dp))
00200 {
00201 ocDirEntry temp(ep,path);
00202 m_directory.push_back(temp);
00203 }
00204 closedir (dp);
00205 bRet = true;
00206 }
00207 return bRet;
00208 }
00209 ocDirectory & getDirectoryEntries( void )
00210 {
00211 return m_directory;
00212 }
00213 bool isDir( string path )
00214 {
00215 bool bRet = false;
00216 struct stat buf;
00217 int ret = stat(path.c_str(), &buf);
00218 if( ret==0 && S_ISDIR(buf.st_mode) )
00219 {
00220 bRet = true;
00221 }
00222 return bRet;
00223 }
00224 off_t fileSize( string path )
00225 {
00226 off_t size = 0;
00227 struct stat buf;
00228 int ret = stat(path.c_str(), &buf);
00229 if( ret==0 )
00230 {
00231 size = buf.st_size;
00232 }
00233 return size;
00234 }
00235 bool makeDir( string dir )
00236 {
00237 bool bRet = false;
00238 bRet = (mkdir( dir.c_str(), mode) == 0);
00239 return bRet;
00240 }
00241
00242 bool makePath( string path )
00243 {
00244 bool bRet = false;
00245
00246 if( path.length() )
00247 {
00248 string buildpath("");
00249
00250 if( path[0] == '/' ) buildpath = "/";
00251 ocString pathParts(path);
00252 while( !pathParts.endOfParse() )
00253 {
00254 buildpath += pathParts.parse("/");
00255 buildpath += "/";
00256 if( isDir( buildpath ) == false )
00257 {
00258 makeDir( buildpath );
00259 }
00260 }
00261
00262 }
00263 return bRet;
00264 }
00265 };
00266
00267 #endif
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377