1 /******************************************************************************
5 * Copyright (C) 1997-2001 by Dimitri van Heesch.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation under the terms of the GNU General Public License is hereby
9 * granted. No representations are made about the suitability of this software
10 * for any purpose. It is provided "as is" without express or implied warranty.
11 * See the GNU General Public License for more details.
13 * Documents produced by Doxygen are derivative works derived from the
14 * input used in their production; they are not affected by this license.
16 * Based on qfileinfo_unix.cpp
18 * Copyright (C) 1992-2000 Trolltech AS.
23 #include "qfileinfo.h"
24 #include "qfiledefs_p.h"
25 #include "qdatetime.h"
28 static void reslashify( QString& n )
30 for ( int i=0; i<(int)n.length(); i++ )
37 void QFileInfo::slashify( QString& n )
39 for ( int i=0; i<(int)n.length(); i++ )
46 void QFileInfo::makeAbs( QString & )
48 // TODO: what to do here?
52 extern bool qt_file_access( const QString& fn, int t );
55 Returns TRUE if we are pointing to a real file.
56 \sa isDir(), isSymLink()
58 bool QFileInfo::isFile() const
62 return fic ? (fic->st.st_mode & STAT_MASK) == STAT_REG : FALSE;
66 Returns TRUE if we are pointing to a directory or a symbolic link to
68 \sa isFile(), isSymLink()
71 bool QFileInfo::isDir() const
75 return fic ? (fic->st.st_mode & STAT_MASK) == STAT_DIR : FALSE;
79 Returns TRUE if we are pointing to a symbolic link.
80 \sa isFile(), isDir(), readLink()
83 bool QFileInfo::isSymLink() const
87 return fic ? fic->isSymLink : FALSE;
92 Returns the name a symlink points to, or a null QString if the
93 object does not refer to a symbolic link.
95 This name may not represent an existing file; it is only a string.
96 QFileInfo::exists() returns TRUE if the symlink points to an
99 \sa exists(), isSymLink(), isDir(), isFile()
102 QString QFileInfo::readLink() const
108 static const uint nobodyID = (uint) -2;
111 Returns the owner of the file.
113 On systems where files do not have owners this function returns
116 Note that this function can be time-consuming under UNIX. (in the order
117 of milliseconds on a 486 DX2/66 running Linux).
119 \sa ownerId(), group(), groupId()
122 QString QFileInfo::owner() const
124 return QString::null;
128 Returns the id of the owner of the file.
130 On systems where files do not have owners this function returns ((uint) -2).
132 \sa owner(), group(), groupId()
135 uint QFileInfo::ownerId() const
141 Returns the group the file belongs to.
143 On systems where files do not have groups this function always
146 Note that this function can be time-consuming under UNIX (in the order of
147 milliseconds on a 486 DX2/66 running Linux).
149 \sa groupId(), owner(), ownerId()
152 QString QFileInfo::group() const
154 return QString::null;
158 Returns the id of the group the file belongs to.
160 On systems where files do not have groups this function always
163 \sa group(), owner(), ownerId()
166 uint QFileInfo::groupId() const
173 \fn bool QFileInfo::permission( int permissionSpec ) const
175 Tests for file permissions. The \e permissionSpec argument can be several
176 flags of type PermissionSpec or'ed together to check for permission
179 On systems where files do not have permissions this function always
184 QFileInfo fi( "/tmp/tonsils" );
185 if ( fi.permission( QFileInfo::WriteUser | QFileInfo::ReadGroup ) )
186 qWarning( "Tonsils can be changed by me, and the group can read them.");
187 if ( fi.permission( QFileInfo::WriteGroup | QFileInfo::WriteOther ) )
188 qWarning( "Danger! Tonsils can be changed by the group or others!" );
191 \sa isReadable(), isWritable(), isExecutable()
194 bool QFileInfo::permission( int permissionSpec ) const
200 Returns the file size in bytes, or 0 if the file does not exist if the size
204 uint QFileInfo::size() const
206 if ( !fic || !cache )
209 return (uint)fic->st.st_size;
216 Returns the date and time when the file was last modified.
220 QDateTime QFileInfo::lastModified() const
223 if ( !fic || !cache )
226 dt.setTime_t( fic->st.st_mtime );
231 Returns the date and time when the file was last read (accessed).
233 On systems that do not support last read times, the modification time is
239 QDateTime QFileInfo::lastRead() const
242 if ( !fic || !cache )
245 dt.setTime_t( fic->st.st_atime );
250 void QFileInfo::doStat() const
252 QFileInfo *that = ((QFileInfo*)this); // mutable function
254 that->fic = new QFileInfoCache;
255 STATBUF *b = &that->fic->st;
256 that->fic->isSymLink = FALSE;
258 #if defined(__CYGWIN32_)
261 r = STAT( QFile::encodeName(fn), b );
270 #ifdef QT_LARGEFILE_SUPPORT
271 if ( _wstati64( (wchar_t*) file.ucs2(), b ) == -1 ) {
273 if ( _wstat( (wchar_t*) file.ucs2(), b ) == -1 ) {
282 Returns the directory path of the file.
284 If \e absPath is TRUE an absolute path is always returned.
286 \sa dir(), filePath(), fileName(), isRelative()
289 QString QFileInfo::dirPath( bool absPath ) const
296 int pos = s.findRev( '/' );
298 return QString::fromLatin1(".");
301 return QString::fromLatin1( "/" );
302 return s.left( pos );
307 Returns the name of the file, the file path is not included.
311 QFileInfo fi( "/tmp/abdomen.lower" );
312 QString name = fi.fileName(); // name = "abdomen.lower"
315 \sa isRelative(), filePath(), baseName(), extension()
318 QString QFileInfo::fileName() const
320 int p = fn.findRev( '/' );
329 Returns the absolute path name.
331 The absolute path name is the file name including the absolute path. If
332 the QFileInfo is absolute (i.e. not relative) this function will return
333 the same string as filePath().
335 Note that this function can be time-consuming under UNIX. (in the order
336 of milliseconds on a 486 DX2/66 running Linux).
338 \sa isRelative(), filePath()
341 QString QFileInfo::absFilePath() const
343 if ( QDir::isRelativePath(fn) ) {
344 QString tmp = QDir::currentDirPath();
348 return QDir::cleanDirPath( tmp );
352 return QDir::cleanDirPath( tmp );