00001 /**************************************************************************** 00002 ** $Id: qfileinfo_unix.cpp,v 1.2 2003/07/10 02:40:11 llornkcor Exp $ 00003 ** 00004 ** Implementation of QFileInfo class 00005 ** 00006 ** Created : 950628 00007 ** 00008 ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. 00009 ** 00010 ** This file is part of the tools module of the Qt GUI Toolkit. 00011 ** 00012 ** This file may be distributed under the terms of the Q Public License 00013 ** as defined by Trolltech AS of Norway and appearing in the file 00014 ** LICENSE.QPL included in the packaging of this file. 00015 ** 00016 ** This file may be distributed and/or modified under the terms of the 00017 ** GNU General Public License version 2 as published by the Free Software 00018 ** Foundation and appearing in the file LICENSE.GPL included in the 00019 ** packaging of this file. 00020 ** 00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition 00022 ** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance 00023 ** with the Qt Commercial License Agreement provided with the Software. 00024 ** 00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00027 ** 00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 00029 ** information about Qt Commercial License Agreements. 00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information. 00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00032 ** 00033 ** Contact info@trolltech.com if any conditions of this licensing are 00034 ** not clear to you. 00035 ** 00036 **********************************************************************/ 00037 00038 #include "qplatformdefs.h" 00039 #include "qfileinfo.h" 00040 #include "qfiledefs_p.h" 00041 #include "qdatetime.h" 00042 #include "qdir.h" 00043 00044 #include <limits.h> 00045 00046 00047 void QFileInfo::slashify( QString& ) 00048 { 00049 return; 00050 } 00051 00052 00053 void QFileInfo::makeAbs( QString & ) 00054 { 00055 return; 00056 } 00057 00065 bool QFileInfo::isFile() const 00066 { 00067 if ( !fic || !cache ) 00068 doStat(); 00069 return fic ? (fic->st.st_mode & S_IFMT) == S_IFREG : FALSE; 00070 } 00071 00078 bool QFileInfo::isDir() const 00079 { 00080 if ( !fic || !cache ) 00081 doStat(); 00082 return fic ? (fic->st.st_mode & S_IFMT) == S_IFDIR : FALSE; 00083 } 00084 00092 bool QFileInfo::isSymLink() const 00093 { 00094 if ( !fic || !cache ) 00095 doStat(); 00096 return symLink; 00097 } 00098 00110 QString QFileInfo::readLink() const 00111 { 00112 QString r; 00113 00114 #if defined(Q_OS_UNIX) && !defined(Q_OS_OS2EMX) 00115 char s[PATH_MAX+1]; 00116 if ( !isSymLink() ) 00117 return QString(); 00118 int len = readlink( QFile::encodeName(fn).data(), s, PATH_MAX ); 00119 if ( len >= 0 ) { 00120 s[len] = '\0'; 00121 r = QFile::decodeName(s); 00122 } 00123 #endif 00124 00125 return r; 00126 } 00127 00128 static const uint nobodyID = (uint) -2; 00129 00141 QString QFileInfo::owner() const 00142 { 00143 passwd *pw = getpwuid( ownerId() ); 00144 if ( pw ) 00145 return QFile::decodeName( pw->pw_name ); 00146 return QString::null; 00147 } 00148 00158 uint QFileInfo::ownerId() const 00159 { 00160 if ( !fic || !cache ) 00161 doStat(); 00162 if ( fic ) 00163 return fic->st.st_uid; 00164 return nobodyID; 00165 } 00166 00178 QString QFileInfo::group() const 00179 { 00180 struct group *gr = getgrgid( groupId() ); 00181 if ( gr ) 00182 return QFile::decodeName( gr->gr_name ); 00183 return QString::null; 00184 } 00185 00195 uint QFileInfo::groupId() const 00196 { 00197 if ( !fic || !cache ) 00198 doStat(); 00199 if ( fic ) 00200 return fic->st.st_gid; 00201 return nobodyID; 00202 } 00203 00204 00225 bool QFileInfo::permission( int permissionSpec ) const 00226 { 00227 if ( !fic || !cache ) 00228 doStat(); 00229 if ( fic ) { 00230 uint mask = 0; 00231 if ( permissionSpec & ReadUser ) 00232 mask |= S_IRUSR; 00233 if ( permissionSpec & WriteUser ) 00234 mask |= S_IWUSR; 00235 if ( permissionSpec & ExeUser ) 00236 mask |= S_IXUSR; 00237 if ( permissionSpec & ReadGroup ) 00238 mask |= S_IRGRP; 00239 if ( permissionSpec & WriteGroup ) 00240 mask |= S_IWGRP; 00241 if ( permissionSpec & ExeGroup ) 00242 mask |= S_IXGRP; 00243 if ( permissionSpec & ReadOther ) 00244 mask |= S_IROTH; 00245 if ( permissionSpec & WriteOther ) 00246 mask |= S_IWOTH; 00247 if ( permissionSpec & ExeOther ) 00248 mask |= S_IXOTH; 00249 if ( mask ) { 00250 return (fic->st.st_mode & mask) == mask; 00251 } else { 00252 #if defined(QT_CHECK_NULL) 00253 qWarning( "QFileInfo::permission: permissionSpec is 0" ); 00254 #endif 00255 return TRUE; 00256 } 00257 } else { 00258 return FALSE; 00259 } 00260 } 00261 00262 void QFileInfo::doStat() const 00263 { 00264 QFileInfo *that = ((QFileInfo*)this); // mutable function 00265 if ( !that->fic ) 00266 that->fic = new QFileInfoCache; 00267 that->symLink = FALSE; 00268 struct stat *b = &that->fic->st; 00269 #if defined(Q_OS_UNIX) && defined(S_IFLNK) 00270 if ( ::lstat( QFile::encodeName(fn), b ) == 0 ) { 00271 if ( S_ISLNK( b->st_mode ) ) 00272 that->symLink = TRUE; 00273 else 00274 return; 00275 } 00276 #endif 00277 00278 int r = ::stat( QFile::encodeName(fn), b ); 00279 if ( r != 0 && !that->symLink ) { 00280 delete that->fic; 00281 that->fic = 0; 00282 } 00283 } 00284 00292 #ifndef QT_NO_DIR 00293 QString QFileInfo::dirPath( bool absPath ) const 00294 { 00295 QString s; 00296 if ( absPath ) 00297 s = absFilePath(); 00298 else 00299 s = fn; 00300 int pos = s.findRev( '/' ); 00301 if ( pos == -1 ) { 00302 return QString::fromLatin1( "." ); 00303 } else { 00304 if ( pos == 0 ) 00305 return QString::fromLatin1( "/" ); 00306 return s.left( pos ); 00307 } 00308 } 00309 #endif 00310 00323 QString QFileInfo::fileName() const 00324 { 00325 int p = fn.findRev( '/' ); 00326 if ( p == -1 ) { 00327 return fn; 00328 } else { 00329 return fn.mid( p + 1 ); 00330 } 00331 }
1.4.2