メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TFileProperty.cc

解説を見る。
00001 // ============================================================================
00002 //  $Id: TFileProperty.cc,v 1.5 2004/06/24 15:15:26 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TFileProperty.cc,v $
00005 //  Revision 1.5  2004/06/24 15:15:26  goiwai
00006 //  たくさんの変更
00007 //  Tcout,Tcerr,Tlogをバッファ経由にした。
00008 //  何個かexternした。Tlog,Tcout,Tcerr,Tinfo...Tcritなど出力関係。
00009 //
00010 //  Revision 1.4  2004/03/07 17:38:46  goiwai
00011 //  *** empty log message ***
00012 //
00013 //  Revision 1.3  2004/03/07 16:56:04  goiwai
00014 //  *** empty log message ***
00015 //
00016 //  Revision 1.2  2004/03/07 10:30:34  goiwai
00017 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00018 //  全てにおいて完全に動作するわけではありません。
00019 //
00020 //  Revision 1.1  2003/10/06 16:37:37  goiwai
00021 //  ファイルの情報を表現するためのクラスです.
00022 //
00023 // ============================================================================
00024 #include "TFileProperty.hh"
00025 #include "TSystemClock.hh"
00026 
00027 TFileProperty::TFileProperty()
00028   : thePathName( "?" ),
00029     theDirectoryName( "?" ),
00030     theFileName( "?" ),
00031     theFileRootName( "?" ),
00032     theFileExtensionName( "?" ),
00033     theDeviceNumber( -1 ),
00034     theDeviceMajorNumber( -1 ),
00035     theDeviceMiorNumber( -1 ),
00036     theInodeNumber( -1 ),
00037     theProtection( -1 ),
00038     theNumberOfHardLinks( -1 ),
00039     theDeviceType( -1 ),
00040     theTotalSize( -1 ),
00041     theBlockSize( -1 ),
00042     theNumberOfBlocks( -1 ),
00043     theUserID( -1 ),
00044     theGroupID( -1 ),
00045     theLastAccess( -1 ),
00046     theLastModification( -1 ),
00047     theLastChange( -1 ),
00048     theStringUserID( "?" ),
00049     theStringGroupID( "?" ),
00050     theStringLastAccess( "?" ),
00051     theStringLastModification( "?" ),
00052     theStringLastChange( "?" )
00053 {;}
00054 
00055 TFileProperty::TFileProperty( const Tstring& path )
00056   : thePathName( path ),
00057     theDirectoryName( "?" ),
00058     theFileName( "?" ),
00059     theFileRootName( "?" ),
00060     theFileExtensionName( "?" ),
00061     theDeviceNumber( -1 ),
00062     theDeviceMajorNumber( -1 ),
00063     theDeviceMiorNumber( -1 ),
00064     theInodeNumber( -1 ),
00065     theProtection( -1 ),
00066     theNumberOfHardLinks( -1 ),
00067     theDeviceType( -1 ),
00068     theTotalSize( -1 ),
00069     theBlockSize( -1 ),
00070     theNumberOfBlocks( -1 ),
00071     theUserID( -1 ),
00072     theGroupID( -1 ),
00073     theLastAccess( -1 ),
00074     theLastModification( -1 ),
00075     theLastChange( -1 ),
00076     theStringUserID( "?" ),
00077     theStringGroupID( "?" ),
00078     theStringLastAccess( "?" ),
00079     theStringLastModification( "?" ),
00080     theStringLastChange( "?" )
00081 {
00082   Analyse();
00083 }
00084 
00085 TFileProperty::TFileProperty( const TFileProperty& right )
00086   : thePathName( right.thePathName ),
00087     theDirectoryName( "?" ),
00088     theFileName( "?" ),
00089     theFileRootName( "?" ),
00090     theFileExtensionName( "?" ),
00091     theDeviceNumber( -1 ),
00092     theDeviceMajorNumber( -1 ),
00093     theDeviceMiorNumber( -1 ),
00094     theInodeNumber( -1 ),
00095     theProtection( -1 ),
00096     theNumberOfHardLinks( -1 ),
00097     theDeviceType( -1 ),
00098     theTotalSize( -1 ),
00099     theBlockSize( -1 ),
00100     theNumberOfBlocks( -1 ),
00101     theUserID( -1 ),
00102     theGroupID( -1 ),
00103     theLastAccess( -1 ),
00104     theLastModification( -1 ),
00105     theLastChange( -1 ),
00106     theStringUserID( "?" ),
00107     theStringGroupID( "?" ),
00108     theStringLastAccess( "?" ),
00109     theStringLastModification( "?" ),
00110     theStringLastChange( "?" )
00111 {
00112   Analyse();
00113 }
00114 
00115 TFileProperty::~TFileProperty()
00116 {;}
00117 
00118 const TFileProperty& TFileProperty::operator=( const TFileProperty& right )
00119 {
00120   thePathName = right.thePathName;
00121   Analyse();
00122   return *this;
00123 }
00124 
00125 Tbool TFileProperty::operator==( const TFileProperty& right ) const
00126 {
00127   return thePathName == right.thePathName;
00128 }
00129 
00130 Tbool TFileProperty::operator!=( const TFileProperty& right ) const
00131 {
00132   return thePathName != right.thePathName;
00133 }
00134 
00135 Tvoid TFileProperty::Analyse()
00136 {
00137   if ( thePathName.empty() ) {
00138     //CLDAQ_WARN( "file name unset" );
00139     Tcout << "file name unset" << Tendl;
00140     return;
00141   }
00142 
00143   theDirectoryName = dirname( thePathName );
00144   theFileName = basename( thePathName );
00145 
00146   if ( ! theFileName.empty() ) {
00147     Tint pos = theFileName.find_last_of( '.' );
00148     if ( pos >= 0 ) {
00149       theFileExtensionName = theFileName.substr( pos + 1, theFileName.size() - pos - 1 );
00150       theFileRootName = theFileName.substr( 0, pos );
00151     }
00152   }
00153 
00154   struct stat buf;
00155   const char* fname = thePathName.c_str();
00156   if ( stat( fname, &buf ) != 0 ) {
00157     //CLDAQ_WARN( "fail to get file" );
00158     Tcout << "fail to get file" << Tendl;
00159     return;
00160   }
00161 
00162   theDeviceNumber = (Tint)buf.st_dev;
00163   theDeviceMajorNumber = major( theDeviceNumber );
00164   theDeviceMiorNumber = minor( theDeviceMiorNumber );
00165   theInodeNumber = (Tint)buf.st_ino;
00166   theProtection = (Tint)buf.st_mode;
00167   theNumberOfHardLinks = (Tint)buf.st_nlink;
00168   theUserID = (Tint)buf.st_uid;
00169   theGroupID = (Tint)buf.st_gid;
00170   theDeviceType = (Tint)buf.st_rdev;
00171   theTotalSize = (Tint)buf.st_size;
00172   theBlockSize = (Tint)buf.st_blksize;
00173   theNumberOfBlocks = (Tint)buf.st_blocks;
00174 
00175 
00176   struct passwd* pw = getpwuid( (uid_t)theUserID );
00177   if ( pw != 0 ) {
00178     theStringUserID = pw -> pw_name;
00179   }
00180 
00181 
00182   struct group* gr = getgrgid( (gid_t)theGroupID );
00183   if ( gr != 0 ) {
00184     theStringGroupID = gr -> gr_name;
00185   }
00186 
00187   theLastAccess = (Tint)buf.st_atime;
00188   theLastModification = (Tint)buf.st_mtime;
00189   theLastChange = (Tint)buf.st_ctime;
00190 
00191   TSystemClock clock;
00192   theStringLastAccess = clock.WhatTimeIsIt( theLastAccess );
00193   theStringLastModification = clock.WhatTimeIsIt( theLastModification );
00194   theStringLastChange = clock.WhatTimeIsIt( theLastChange );
00195 
00196   return;
00197 }
00198 
00199 Tbool TFileProperty::IsCharacterDevice() const
00200 {
00201   return S_ISREG( theProtection );
00202 }
00203 
00204 Tbool TFileProperty::IsBlockDevice() const
00205 {
00206   return S_ISBLK( theProtection );
00207 }
00208 
00209 Tbool TFileProperty::IsFIFO() const
00210 {
00211   return S_ISFIFO( theProtection );
00212 }
00213 
00214 Tbool TFileProperty::IsSocket() const
00215 {
00216   return S_ISSOCK( theProtection );
00217 }
00218 
00219 Tbool TFileProperty::IsSymbolicLink() const
00220 {
00221   return S_ISLNK( theProtection );
00222 }
00223 
00224 Tbool TFileProperty::IsRegularFile() const
00225 {
00226   return S_ISREG( theProtection );
00227 }
00228 
00229 Tbool TFileProperty::IsDirectory() const
00230 {
00231   return S_ISDIR( theProtection );
00232 }
00233 
00234 Tbool TFileProperty::IsReadable() const
00235 {
00236   if ( access( thePathName.c_str(), R_OK ) == 0 ) {
00237     return Ttrue;
00238   } else {
00239     return Tfalse;
00240   }
00241 }
00242 
00243 Tbool TFileProperty::IsWritable() const
00244 {
00245   if ( access( thePathName.c_str(), W_OK ) == 0 ) {
00246     return Ttrue;
00247   } else {
00248     return Tfalse;
00249   }
00250 }
00251 
00252 Tbool TFileProperty::IsExecutable() const
00253 {
00254   if ( access( thePathName.c_str(), X_OK ) == 0 ) {
00255     return Ttrue;
00256   } else {
00257     return Tfalse;
00258   }
00259 }
00260 
00261 Tostream& operator<<( Tostream& tos, const TFileProperty& right )
00262 {
00263   Tstring strbuf;
00264   if ( right.IsSymbolicLink() ) {
00265     strbuf = "l";
00266   } else if ( right.IsDirectory() ) {
00267     strbuf = "d";
00268   } else if ( right.IsRegularFile() ) {
00269     strbuf = "-";
00270   } else if ( right.IsCharacterDevice() ) {
00271     strbuf = "c";
00272   } else if ( right.IsBlockDevice() ) {
00273     strbuf = "b";
00274   } else if ( right.IsSocket() ) {
00275     strbuf = "s";
00276   } else if ( right.IsFIFO() ) {
00277     strbuf = "p";
00278   } else {
00279     strbuf = "?";
00280   }
00281 
00282   if ( right.IsReadable() ) {
00283     strbuf += "r";
00284   } else {
00285     strbuf += "-";
00286   }
00287 
00288 
00289   if ( right.IsWritable() ) {
00290     strbuf += "w";
00291   } else {
00292     strbuf += "-";
00293   }
00294 
00295   if ( right.IsExecutable() ) {
00296     strbuf += "x";
00297   } else {
00298     strbuf += "-";
00299   }
00300 
00301   tos << "Directory: " << right.theDirectoryName;
00302   tos << ", File: " << right.theFileName;
00303   tos << ", Root: " << right.theFileRootName;
00304   tos << ", Extension: " << right.theFileExtensionName << Tendl;
00305   tos << strbuf << "  ";
00306   tos << right.theStringUserID << "(" << right.theUserID << ")  ";
00307   tos << right.theStringGroupID << "(" << right.theGroupID << ")  ";
00308   tos << right.theTotalSize << "  ";
00309   tos << right.thePathName << Tendl;
00310   tos << "Last Access: " << right.theStringLastAccess << Tendl;
00311   tos << "Last Modification: " << right.theStringLastModification << Tendl;
00312   tos << "Last Change: " << right.theStringLastChange << Tflush;
00313 
00314   return tos;
00315 }
00316 
00317 #ifdef __CLDAQ_ROOT_DLL
00318     ClassImp(TFileProperty)
00319 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp