[8] | 1 | /****************************************************************************
|
---|
| 2 | ** $Id: qfileinfo_pm.cpp 8 2005-11-16 19:36:46Z dmik $
|
---|
| 3 | **
|
---|
| 4 | ** Implementation of QFileInfo class
|
---|
| 5 | **
|
---|
| 6 | ** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
|
---|
| 7 | ** Copyright (C) 2004 Norman ASA. Initial OS/2 Port.
|
---|
| 8 | ** Copyright (C) 2005 netlabs.org. Further OS/2 Development.
|
---|
| 9 | **
|
---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
---|
| 11 | **
|
---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
| 15 | **
|
---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 19 | ** packaging of this file.
|
---|
| 20 | **
|
---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
| 23 | ** Agreement provided with the Software.
|
---|
| 24 | **
|
---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 27 | **
|
---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
| 29 | ** information about Qt Commercial License Agreements.
|
---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
| 32 | **
|
---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
| 34 | ** not clear to you.
|
---|
| 35 | **
|
---|
| 36 | **********************************************************************/
|
---|
| 37 |
|
---|
| 38 | #include "qplatformdefs.h"
|
---|
| 39 |
|
---|
| 40 | #include "qlibrary.h"
|
---|
| 41 | #include "qfileinfo.h"
|
---|
| 42 | #include "qfiledefs_p.h"
|
---|
| 43 | #include "qdatetime.h"
|
---|
| 44 | #include "qdir.h"
|
---|
| 45 |
|
---|
| 46 | #ifdef QT_THREAD_SUPPORT
|
---|
| 47 | # include <private/qmutexpool_p.h>
|
---|
| 48 | #endif // QT_THREAD_SUPPORT
|
---|
| 49 |
|
---|
| 50 | #include "qt_os2.h"
|
---|
| 51 | #include <ctype.h>
|
---|
| 52 |
|
---|
| 53 | extern bool qt_file_access( const QString& fn, int t );
|
---|
| 54 |
|
---|
| 55 | static QString currentDirOfDrive( char ch )
|
---|
| 56 | {
|
---|
| 57 | QString result;
|
---|
| 58 |
|
---|
| 59 | #if defined(Q_CC_GNU)
|
---|
| 60 | char currentName[PATH_MAX];
|
---|
| 61 | if ( _getcwd1( currentName, toupper( (uchar) ch ) ) == 0 ) {
|
---|
| 62 | result = QFile::decodeName( currentName );
|
---|
| 63 | }
|
---|
| 64 | #else
|
---|
| 65 | char currentName[PATH_MAX];
|
---|
| 66 | if ( _getdcwd( toupper( (uchar) ch ) - 'A' + 1, currentName, PATH_MAX ) != 0 ) {
|
---|
| 67 | result = QFile::decodeName( currentName );
|
---|
| 68 | }
|
---|
| 69 | #endif // Q_CC_GNU
|
---|
| 70 | return result;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | void QFileInfo::slashify( QString &s )
|
---|
| 75 | {
|
---|
| 76 | for (int i=0; i<(int)s.length(); i++) {
|
---|
| 77 | if ( s[i] == '\\' )
|
---|
| 78 | s[i] = '/';
|
---|
| 79 | }
|
---|
| 80 | if ( s[ (int)s.length() - 1 ] == '/' && s.length() > 3 )
|
---|
| 81 | s.remove( (int)s.length() - 1, 1 );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void QFileInfo::makeAbs( QString &s )
|
---|
| 85 | {
|
---|
| 86 | if ( s[ 1 ] != ':' && s[ 1 ] != '/' ) {
|
---|
| 87 | s.prepend( ":" );
|
---|
| 88 | #if defined(Q_CC_GNU)
|
---|
| 89 | s.prepend( _getdrive() );
|
---|
| 90 | #else
|
---|
| 91 | s.prepend( (char)_getdrive() + 'A' - 1 );
|
---|
| 92 | #endif
|
---|
| 93 | }
|
---|
| 94 | if ( s[ 1 ] == ':' && s.length() > 3 && s[ 2 ] != '/' ) {
|
---|
| 95 | QString d = currentDirOfDrive( (char)s[ 0 ].latin1() );
|
---|
| 96 | slashify( d );
|
---|
| 97 | s = d + "/" + s.mid( 2, 0xFFFFFF );
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | bool QFileInfo::isFile() const
|
---|
| 102 | {
|
---|
| 103 | if ( !fic || !cache )
|
---|
| 104 | doStat();
|
---|
| 105 | return fic ? (fic->st.st_mode & QT_STAT_MASK) == QT_STAT_REG : FALSE;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool QFileInfo::isDir() const
|
---|
| 109 | {
|
---|
| 110 | if ( !fic || !cache )
|
---|
| 111 | doStat();
|
---|
| 112 | return fic ? (fic->st.st_mode & QT_STAT_MASK) == QT_STAT_DIR : FALSE;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | bool QFileInfo::isSymLink() const
|
---|
| 116 | {
|
---|
| 117 | return FALSE;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | QString QFileInfo::readLink() const
|
---|
| 121 | {
|
---|
| 122 | return QString();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | QString QFileInfo::owner() const
|
---|
| 126 | {
|
---|
| 127 | return QString::null;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static const uint nobodyID = (uint) -2;
|
---|
| 131 |
|
---|
| 132 | uint QFileInfo::ownerId() const
|
---|
| 133 | {
|
---|
| 134 | return nobodyID;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | QString QFileInfo::group() const
|
---|
| 138 | {
|
---|
| 139 | return QString::null;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | uint QFileInfo::groupId() const
|
---|
| 143 | {
|
---|
| 144 | return nobodyID;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | bool QFileInfo::permission( int p ) const
|
---|
| 148 | {
|
---|
| 149 | //@@TODO (dmik): do checks for permissions using file ACLs when possible...
|
---|
| 150 |
|
---|
| 151 | // just check if it's ReadOnly
|
---|
| 152 |
|
---|
| 153 | if ( p & ( WriteOwner | WriteUser | WriteGroup | WriteOther ) ) {
|
---|
| 154 | return qt_file_access( fn, W_OK );
|
---|
| 155 | }
|
---|
| 156 | return TRUE;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | void QFileInfo::doStat() const
|
---|
| 160 | {
|
---|
| 161 | if ( fn.isEmpty() )
|
---|
| 162 | return;
|
---|
| 163 |
|
---|
| 164 | QFileInfo *that = ((QFileInfo*)this); // mutable function
|
---|
| 165 | if ( !that->fic )
|
---|
| 166 | that->fic = new QFileInfoCache;
|
---|
| 167 | QT_STATBUF *b = &that->fic->st;
|
---|
| 168 |
|
---|
| 169 | int r = QT_STAT( QFile::encodeName( fn ), b );
|
---|
| 170 | if ( r != 0 ) {
|
---|
| 171 | // special handling for situations when stat() returns error but
|
---|
| 172 | // we can guess whether it is a directory name or not.
|
---|
| 173 | bool is_dir = FALSE;
|
---|
| 174 | if (
|
---|
| 175 | (fn[0] == '/' && fn[1] == '/') ||
|
---|
| 176 | (fn[0] == '\\' && fn[1] == '\\')
|
---|
| 177 | ) {
|
---|
| 178 | // UNC
|
---|
| 179 | int s = fn.find(fn[0],2);
|
---|
| 180 | if ( s > 0 ) {
|
---|
| 181 | // "\\server\..."
|
---|
| 182 | s = fn.find(fn[0],s+1);
|
---|
| 183 | if ( s > 0 ) {
|
---|
| 184 | // "\\server\share\..."
|
---|
| 185 | if ( fn[s+1] ) {
|
---|
| 186 | // "\\server\share\notfound"
|
---|
| 187 | } else {
|
---|
| 188 | // "\\server\share\"
|
---|
| 189 | is_dir = TRUE;
|
---|
| 190 | }
|
---|
| 191 | } else {
|
---|
| 192 | // "\\server\share"
|
---|
| 193 | is_dir = TRUE;
|
---|
| 194 | }
|
---|
| 195 | } else {
|
---|
| 196 | // "\\server"
|
---|
| 197 | is_dir = TRUE;
|
---|
| 198 | }
|
---|
| 199 | } else if (
|
---|
| 200 | (fn.length() == 2 && fn[1] == ':') ||
|
---|
| 201 | (fn.length() == 3 && fn[1] == ':' && (fn[2] == '/' || fn[2] == '\\'))
|
---|
| 202 | ) {
|
---|
| 203 | if ( fn[0].isLetter() )
|
---|
| 204 | // "x:", "x:\" or "x:/" can be only a dir
|
---|
| 205 | is_dir = TRUE;
|
---|
| 206 | }
|
---|
| 207 | if ( is_dir ) {
|
---|
| 208 | // looks like a dir, is a dir.
|
---|
| 209 | memset( b, 0, sizeof(*b) );
|
---|
| 210 | b->st_mode = QT_STAT_DIR;
|
---|
| 211 | b->st_nlink = 1;
|
---|
| 212 | r = 0;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | if ( r != 0 ) {
|
---|
| 217 | delete that->fic;
|
---|
| 218 | that->fic = 0;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | QString QFileInfo::dirPath( bool absPath ) const
|
---|
| 223 | {
|
---|
| 224 | QString s;
|
---|
| 225 | if ( absPath )
|
---|
| 226 | s = absFilePath();
|
---|
| 227 | else
|
---|
| 228 | s = fn;
|
---|
| 229 | int pos = s.findRev( '/' );
|
---|
| 230 | if ( pos == -1 ) {
|
---|
| 231 | if ( s[ 2 ] == '/' )
|
---|
| 232 | return s.left( 3 );
|
---|
| 233 | if ( s[ 1 ] == ':' ) {
|
---|
| 234 | if ( absPath )
|
---|
| 235 | return s.left( 2 ) + "/";
|
---|
| 236 | return s.left( 2 );
|
---|
| 237 | }
|
---|
| 238 | return QString::fromLatin1(".");
|
---|
| 239 | } else {
|
---|
| 240 | if ( pos == 0 )
|
---|
| 241 | return QString::fromLatin1( "/" );
|
---|
| 242 | if ( pos == 2 && s[ 1 ] == ':' && s[ 2 ] == '/')
|
---|
| 243 | pos++;
|
---|
| 244 | return s.left( pos );
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | QString QFileInfo::fileName() const
|
---|
| 249 | {
|
---|
| 250 | int p = fn.findRev( '/' );
|
---|
| 251 | if ( p == -1 ) {
|
---|
| 252 | int p = fn.findRev( ':' );
|
---|
| 253 | if ( p != -1 )
|
---|
| 254 | return fn.mid( p + 1 );
|
---|
| 255 | return fn;
|
---|
| 256 | } else {
|
---|
| 257 | return fn.mid( p + 1 );
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /*!
|
---|
| 262 | Returns TRUE if the file is hidden; otherwise returns FALSE.
|
---|
| 263 |
|
---|
| 264 | On Unix-like operating systems, including Mac OS X, a file is
|
---|
| 265 | hidden if its name begins with ".". On Windows and OS/2 a file is hidden if
|
---|
| 266 | its hidden attribute is set.
|
---|
| 267 | */
|
---|
| 268 | bool QFileInfo::isHidden() const
|
---|
| 269 | {
|
---|
| 270 | if ( !fic || !cache )
|
---|
| 271 | doStat();
|
---|
| 272 | return fic ? (fic->st.st_attr & FILE_HIDDEN) == FILE_HIDDEN : FALSE;
|
---|
| 273 | }
|
---|