00001
00002
00003 #include <errno.h>
00004 #include <string.h>
00005 #include "gfilestat.h"
00006
00007 bool sFileStat::IsDirectory ()
00008 {
00009 return S_ISDIR( mode );
00010 }
00011
00012 bool sFileStat::IsLink ()
00013 {
00014 #ifdef linux
00015 return S_ISLNK( mode );
00016 #else
00017 return false;
00018 #endif //linux
00019 }
00020
00021 long sFileStat::Size ()
00022 {
00023 if ( IsDirectory() ) return 0L;
00024 return
00025 uid==0xFFFF ? -1L : (long)size;
00026 }
00027
00028 t_uint32 sFileStat::USize ()
00029 {
00030 long v = Size();
00031 return v<0 ? 0 : (t_uint32)v;
00032 }
00033
00034 t_fs_perm sFileStat::Permission ()
00035 {
00036 return mode & 0x1FF;
00037 }
00038
00039 t_fs_perm sFileStat::AllPermission ()
00040 {
00041 ;
00042
00043 return mode & 0xFFF;
00044 }
00045
00046
00047
00048 gFileStat::gFileStat (char* fName)
00049 : lastOpError( 0 ),
00050 lastOpErrorL( 0 ),
00051 name( fName==NULL ? (char*)"\0" : fName ),
00052 hasStat( false ),
00053 fHandle( -1 )
00054 {
00055 if ( name.IsEmpty()==false ) {
00056 thisStatName( fName, -1, status, statusL );
00057 }
00058 }
00059
00060 gFileStat::gFileStat (gString& sName)
00061 : lastOpError( 0 ),
00062 lastOpErrorL( 0 ),
00063 name( sName ),
00064 hasStat( false ),
00065 fHandle( -1 )
00066 {
00067 if ( name.IsEmpty()==false ) {
00068 thisStatName( name.Str(), -1, status, statusL );
00069 }
00070 }
00071
00072 gFileStat::~gFileStat ()
00073 {
00074 }
00075
00076 bool gFileStat::Update (int fd)
00077 {
00078 fHandle = fd;
00079 if ( name.IsEmpty() ) {
00080 if ( fd==-1 ) return false;
00081 return thisFileStat(fd,status,statusL)==0;
00082 }
00083 return Update( name.Str() );
00084 }
00085
00086 bool gFileStat::Update (char* fName)
00087 {
00088 ASSERTION(fName!=0 && fName[0]!=0,"gFileStat::Update");
00089 name.Set( fName );
00090 return thisStatName( fName, fHandle, status, statusL )==0;
00091 }
00092
00093 gFileStat& gFileStat::CopyStat (gFileStat& copy)
00094 {
00095 lastOpError = copy.lastOpError;
00096 lastOpErrorL = copy.lastOpErrorL;
00097 name.Copy( copy.name );
00098 status.Copy( copy.status );
00099 statusL.Copy( copy.statusL );
00100 hasStat = copy.hasStat;
00101 fHandle = -1;
00102 return *this;
00103 }
00104
00105 int gFileStat::thisStatName (char* fName,
00106 int fd,
00107 sFileStat& st,
00108 sFileStat& stL)
00109 {
00110 int res, resL;
00111 struct stat zsStat;
00112 struct stat zsStatL;
00113
00114 lastOpError = lastOpErrorL = 0;
00115 st.ToDefault();
00116 stL.ToDefault();
00117
00118 hasStat = false;
00119 if ( fd!=-1 ) return thisFileStat(fd,st,stL);
00120
00121
00122
00123 memset( &zsStat, 0x0, sizeof(zsStat) );
00124
00125 memset( &zsStatL, 0x0, sizeof(zsStatL) );
00126
00127 ASSERTION(fName!=NULL && fName[0]!=0,"gFileStat::thisStatName");
00128 res = stat( fName, &zsStat );
00129 if ( res==-1 ) {
00130 lastOpError = errno;
00131 }
00132 #ifdef linux
00133 resL = lstat( fName, &zsStatL );
00134 #else
00135 resL = stat( fName, &zsStatL );
00136 #endif //linux
00137 if ( resL==-1 ) {
00138 lastOpErrorL = errno;
00139 return res;
00140 }
00141 DBGPRINT_MIN("stat(%s)=%d:%d (err=%d:%d)\n",fName,res,resL,lastOpError,lastOpErrorL);
00142
00143 st.inode = (t_inode)zsStat.st_ino;
00144 st.mode = zsStat.st_mode;
00145 st.uid = zsStat.st_uid;
00146 st.gid = zsStat.st_gid;
00147 if ( res==0 ) st.size = zsStat.st_size;
00148 #ifdef linux
00149 st.blocks = (t_uint32)zsStat.st_blocks;
00150 #else
00151 st.blocks = 1;
00152 #endif //linux
00153 st.nLinks = zsStat.st_nlink;
00154 st.aTime = zsStat.st_atime;
00155 st.mTime = zsStat.st_mtime;
00156 st.cTime = zsStat.st_ctime;
00157
00158 stL.inode = (t_uint64)zsStatL.st_ino;
00159 stL.mode = zsStatL.st_mode;
00160 stL.uid = zsStatL.st_uid;
00161 stL.gid = zsStatL.st_gid;
00162 stL.size = zsStatL.st_size;
00163 #ifdef linux
00164 stL.blocks = zsStatL.st_blocks;
00165 #else
00166 st.blocks = 1;
00167 #endif //linux
00168 st.nLinks = zsStat.st_nlink;
00169 stL.aTime = zsStatL.st_atime;
00170 stL.mTime = zsStatL.st_mtime;
00171 stL.cTime = zsStatL.st_ctime;
00172
00173 hasStat = true;
00174 return 0;
00175 }
00176
00177 int gFileStat::thisFileStat (int fd,
00178 sFileStat& st,
00179 sFileStat& stL)
00180 {
00181 int res;
00182 struct stat zsStat;
00183
00184 st.ToDefault();
00185 stL.ToDefault();
00186 lastOpError = lastOpErrorL = 0;
00187 hasStat = false;
00188
00189 if ( fd==-1 ) return 0;
00190 res = fstat(fd,&zsStat);
00191 if ( res==-1 ) {
00192 lastOpError = errno;
00193 return -1;
00194 }
00195
00196 st.mode = zsStat.st_mode;
00197 st.uid = zsStat.st_uid;
00198 st.gid = zsStat.st_gid;
00199 st.size = zsStat.st_size;
00200 st.aTime = zsStat.st_atime;
00201 st.mTime = zsStat.st_mtime;
00202 st.cTime = zsStat.st_ctime;
00203
00204 hasStat = true;
00205 return 0;
00206 }
00207
00208