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