1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
/****************************************************************************
**
** Implementation of TQFileInfo class
**
** Created : 950628
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the tools module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "qplatformdefs.h"
#include "ntqfileinfo.h"
#include "ntqdatetime.h"
#include "ntqdir.h"
#include "qfiledefs_p.h"
#include "ntqdeepcopy.h"
#if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4)
#include <limits.h>
#endif
extern bool qt_file_access( const TQString& fn, int t );
/*!
\class TQFileInfo
\reentrant
\brief The TQFileInfo class provides system-independent file information.
\ingroup io
TQFileInfo provides information about a file's name and position
(path) in the file system, its access rights and whether it is a
directory or symbolic link, etc. The file's size and last
modified/read times are also available.
A TQFileInfo can point to a file with either a relative or an
absolute file path. Absolute file paths begin with the directory
separator "/" (or with a drive specification on Windows). Relative
file names begin with a directory name or a file name and specify
a path relative to the current working directory. An example of an
absolute path is the string "/tmp/quartz". A relative path might
look like "src/fatlib". You can use the function isRelative() to
check whether a TQFileInfo is using a relative or an absolute file
path. You can call the function convertToAbs() to convert a
relative TQFileInfo's path to an absolute path.
The file that the TQFileInfo works on is set in the constructor or
later with setFile(). Use exists() to see if the file exists and
size() to get its size.
To speed up performance, TQFileInfo caches information about the
file. Because files can be changed by other users or programs, or
even by other parts of the same program, there is a function that
refreshes the file information: refresh(). If you want to switch
off a TQFileInfo's caching and force it to access the file system
every time you request information from it call setCaching(FALSE).
The file's type is obtained with isFile(), isDir() and
isSymLink(). The readLink() function provides the name of the file
the symlink points to.
Elements of the file's name can be extracted with dirPath() and
fileName(). The fileName()'s parts can be extracted with
baseName() and extension().
The file's dates are returned by created(), lastModified() and
lastRead(). Information about the file's access permissions is
obtained with isReadable(), isWritable() and isExecutable(). The
file's ownership is available from owner(), ownerId(), group() and
groupId(). You can examine a file's permissions and ownership in a
single statement using the permission() function.
If you need to read and traverse directories, see the TQDir class.
*/
/*!
\enum TQFileInfo::PermissionSpec
This enum is used by the permission() function to report the
permissions and ownership of a file. The values may be OR-ed
together to test multiple permissions and ownership values.
\value ReadOwner The file is readable by the owner of the file.
\value WriteOwner The file is writable by the owner of the file.
\value ExeOwner The file is executable by the owner of the file.
\value ReadUser The file is readable by the user.
\value WriteUser The file is writable by the user.
\value ExeUser The file is executable by the user.
\value ReadGroup The file is readable by the group.
\value WriteGroup The file is writable by the group.
\value ExeGroup The file is executable by the group.
\value ReadOther The file is readable by anyone.
\value WriteOther The file is writable by anyone.
\value ExeOther The file is executable by anyone.
\warning The semantics of \c ReadUser, \c WriteUser and \c ExeUser are
unfortunately not platform independent: on Unix, the rights of the owner of
the file are returned and on Windows the rights of the current user are
returned. This behavior might change in a future TQt version. If you want to
find the rights of the owner of the file, you should use the flags \c
ReadOwner, \c WriteOwner and \c ExeOwner. If you want to find out the
rights of the current user, you should use isReadable(), isWritable() and
isExecutable().
*/
/*!
Constructs a new empty TQFileInfo.
*/
TQFileInfo::TQFileInfo()
{
fic = 0;
cache = TRUE;
#if defined(Q_OS_UNIX)
symLink = FALSE;
#endif
}
/*!
Constructs a new TQFileInfo that gives information about the given
file. The \a file can also include an absolute or relative path.
\warning Some functions might behave in a counter-intuitive way
if \a file has a trailing directory separator.
\sa setFile(), isRelative(), TQDir::setCurrent(), TQDir::isRelativePath()
*/
TQFileInfo::TQFileInfo( const TQString &file )
{
fn = file;
slashify( fn );
fic = 0;
cache = TRUE;
#if defined(Q_OS_UNIX)
symLink = FALSE;
#endif
}
/*!
Constructs a new TQFileInfo that gives information about file \a
file.
If the \a file has a relative path, the TQFileInfo will also have a
relative path.
\sa isRelative()
*/
TQFileInfo::TQFileInfo( const TQFile &file )
{
fn = file.name();
slashify( fn );
fic = 0;
cache = TRUE;
#if defined(Q_OS_UNIX)
symLink = FALSE;
#endif
}
/*!
Constructs a new TQFileInfo that gives information about the file
called \a fileName in the directory \a d.
If \a d has a relative path, the TQFileInfo will also have a
relative path.
\sa isRelative()
*/
#ifndef TQT_NO_DIR
TQFileInfo::TQFileInfo( const TQDir &d, const TQString &fileName )
{
fn = d.filePath( fileName );
slashify( fn );
fic = 0;
cache = TRUE;
#if defined(Q_OS_UNIX)
symLink = FALSE;
#endif
}
#endif
/*!
Constructs a new TQFileInfo that is a copy of \a fi.
*/
TQFileInfo::TQFileInfo( const TQFileInfo &fi )
{
fn = fi.fn;
if ( fi.fic ) {
fic = new TQFileInfoCache;
*fic = *fi.fic;
} else {
fic = 0;
}
cache = fi.cache;
#if defined(Q_OS_UNIX)
symLink = fi.symLink;
#endif
}
/*!
Destroys the TQFileInfo and frees its resources.
*/
TQFileInfo::~TQFileInfo()
{
delete fic;
}
/*!
Makes a copy of \a fi and assigns it to this TQFileInfo.
*/
TQFileInfo &TQFileInfo::operator=( const TQFileInfo &fi )
{
fn = fi.fn;
if ( !fi.fic ) {
delete fic;
fic = 0;
} else {
if ( !fic ) {
fic = new TQFileInfoCache;
TQ_CHECK_PTR( fic );
}
*fic = *fi.fic;
}
cache = fi.cache;
#if defined(Q_OS_UNIX)
symLink = fi.symLink;
#endif
return *this;
}
/*!
Sets the file that the TQFileInfo provides information about to \a
file.
The \a file can also include an absolute or relative file path.
Absolute paths begin with the directory separator (e.g. "/" under
Unix) or a drive specification (under Windows). Relative file
names begin with a directory name or a file name and specify a
path relative to the current directory.
Example:
\code
TQString absolute = "/local/bin";
TQString relative = "local/bin";
TQFileInfo absFile( absolute );
TQFileInfo relFile( relative );
TQDir::setCurrent( TQDir::rootDirPath() );
// absFile and relFile now point to the same file
TQDir::setCurrent( "/tmp" );
// absFile now points to "/local/bin",
// while relFile points to "/tmp/local/bin"
\endcode
\sa isRelative(), TQDir::setCurrent(), TQDir::isRelativePath()
*/
void TQFileInfo::setFile( const TQString &file )
{
fn = file;
slashify( fn );
delete fic;
fic = 0;
}
/*!
\overload
Sets the file that the TQFileInfo provides information about to \a
file.
If \a file includes a relative path, the TQFileInfo will also have
a relative path.
\sa isRelative()
*/
void TQFileInfo::setFile( const TQFile &file )
{
fn = file.name();
slashify( fn );
delete fic;
fic = 0;
}
/*!
\overload
Sets the file that the TQFileInfo provides information about to \a
fileName in directory \a d.
If \a fileName includes a relative path, the TQFileInfo will also
have a relative path.
\sa isRelative()
*/
#ifndef TQT_NO_DIR
void TQFileInfo::setFile( const TQDir &d, const TQString &fileName )
{
fn = d.filePath( fileName );
slashify( fn );
delete fic;
fic = 0;
}
#endif
/*!
Returns TRUE if the file exists; otherwise returns FALSE.
*/
bool TQFileInfo::exists() const
{
return qt_file_access( fn, F_OK );
}
/*!
Refreshes the information about the file, i.e. reads in information
from the file system the next time a cached property is fetched.
\sa setCaching()
*/
void TQFileInfo::refresh() const
{
TQFileInfo *that = (TQFileInfo*)this; // Mutable function
delete that->fic;
that->fic = 0;
}
/*!
\fn bool TQFileInfo::caching() const
Returns TRUE if caching is enabled; otherwise returns FALSE.
\sa setCaching(), refresh()
*/
/*!
If \a enable is TRUE, enables caching of file information. If \a
enable is FALSE caching is disabled.
When caching is enabled, TQFileInfo reads the file information from
the file system the first time it's needed, but generally not
later.
Caching is enabled by default.
\sa refresh(), caching()
*/
void TQFileInfo::setCaching( bool enable )
{
if ( cache == enable )
return;
cache = enable;
if ( cache ) {
delete fic;
fic = 0;
}
}
/*!
Returns the file name, including the path (which may be absolute
or relative).
\sa isRelative(), absFilePath()
*/
TQString TQFileInfo::filePath() const
{
return fn;
}
/*!
Returns the base name of the file.
If \a complete is FALSE (the default) the base name consists of
all characters in the file name up to (but not including) the \e
first '.' character.
If \a complete is TRUE the base name consists of all characters in
the file up to (but not including) the \e last '.' character.
The path is not included in either case.
Example:
\code
TQFileInfo fi( "/tmp/archive.tar.gz" );
TQString base = fi.baseName(); // base = "archive"
base = fi.baseName( TRUE ); // base = "archive.tar"
\endcode
\sa fileName(), extension()
*/
TQString TQFileInfo::baseName( bool complete ) const
{
TQString tmp = fileName();
int pos = complete ? tmp.findRev( '.' ) : tmp.find( '.' );
if ( pos == -1 )
return tmp;
else
return tmp.left( pos );
}
/*!
Returns the file's extension name.
If \a complete is TRUE (the default), extension() returns the
string of all characters in the file name after (but not
including) the first '.' character.
If \a complete is FALSE, extension() returns the string of all
characters in the file name after (but not including) the last '.'
character.
Example:
\code
TQFileInfo fi( "/tmp/archive.tar.gz" );
TQString ext = fi.extension(); // ext = "tar.gz"
ext = fi.extension( FALSE ); // ext = "gz"
\endcode
\sa fileName(), baseName()
*/
TQString TQFileInfo::extension( bool complete ) const
{
TQString s = fileName();
int pos = complete ? s.find( '.' ) : s.findRev( '.' );
if ( pos < 0 )
return TQString::fromLatin1( "" );
else
return s.right( s.length() - pos - 1 );
}
/*!
Returns the file's path as a TQDir object.
If the TQFileInfo is relative and \a absPath is FALSE, the TQDir
will be relative; otherwise it will be absolute.
\sa dirPath(), filePath(), fileName(), isRelative()
*/
#ifndef TQT_NO_DIR
TQDir TQFileInfo::dir( bool absPath ) const
{
return TQDir( dirPath(absPath) );
}
#endif
/*!
Returns TRUE if the file is readable; otherwise returns FALSE.
\sa isWritable(), isExecutable(), permission()
*/
bool TQFileInfo::isReadable() const
{
#ifdef Q_WS_WIN
return qt_file_access( fn, R_OK ) && permission( ReadUser );
#else
return qt_file_access( fn, R_OK );
#endif
}
/*!
Returns TRUE if the file is writable; otherwise returns FALSE.
\sa isReadable(), isExecutable(), permission()
*/
bool TQFileInfo::isWritable() const
{
#ifdef Q_WS_WIN
return qt_file_access( fn, W_OK ) && permission( WriteUser );
#else
return qt_file_access( fn, W_OK );
#endif
}
/*!
Returns TRUE if the file is executable; otherwise returns FALSE.
\sa isReadable(), isWritable(), permission()
*/
bool TQFileInfo::isExecutable() const
{
#ifdef Q_WS_WIN
#if defined(_MSC_VER) && _MSC_VER >= 1400
return permission( ExeUser );
#else
return qt_file_access( fn, X_OK ) && permission( ExeUser );
#endif //_MSC_VER >= 1400
#else
return qt_file_access( fn, X_OK );
#endif
}
#ifndef Q_WS_WIN
bool TQFileInfo::isHidden() const
{
return fileName()[ 0 ] == TQChar( '.' );
}
#endif
/*!
Returns TRUE if the file path name is relative. Returns FALSE if
the path is absolute (e.g. under Unix a path is absolute if it
begins with a "/").
*/
#ifndef TQT_NO_DIR
bool TQFileInfo::isRelative() const
{
return TQDir::isRelativePath( fn );
}
/*!
Converts the file's path to an absolute path.
If it is already absolute, nothing is done.
\sa filePath(), isRelative()
*/
bool TQFileInfo::convertToAbs()
{
if ( isRelative() )
fn = absFilePath();
return TQDir::isRelativePath( fn );
}
#endif
/*!
Returns the file size in bytes, or 0 if the file does not exist or
if the size is 0 or if the size cannot be fetched.
*/
#if defined(QT_ABI_QT4)
TQIODevice::Offset TQFileInfo::size() const
#else
uint TQFileInfo::size() const
#endif
{
if ( !fic || !cache )
doStat();
if ( fic )
#if defined(QT_ABI_QT4)
return (TQIODevice::Offset)fic->st.st_size;
#elif defined(QT_LARGEFILE_SUPPORT)
return (uint)fic->st.st_size > UINT_MAX ? UINT_MAX : (uint)fic->st.st_size;
#else
return (uint)fic->st.st_size;
#endif
else
return 0;
}
/*!
Returns the date and time when the file was created.
On platforms where this information is not available, returns the
same as lastModified().
\sa created() lastModified() lastRead()
*/
TQDateTime TQFileInfo::created() const
{
TQDateTime dt;
if ( !fic || !cache )
doStat();
if ( fic && fic->st.st_ctime != 0 ) {
dt.setTime_t( fic->st.st_ctime );
return dt;
} else {
return lastModified();
}
}
/*!
Returns the date and time when the file was last modified.
\sa created() lastModified() lastRead()
*/
TQDateTime TQFileInfo::lastModified() const
{
TQDateTime dt;
if ( !fic || !cache )
doStat();
if ( fic )
dt.setTime_t( fic->st.st_mtime );
return dt;
}
/*!
Returns the date and time when the file was last read (accessed).
On platforms where this information is not available, returns the
same as lastModified().
\sa created() lastModified() lastRead()
*/
TQDateTime TQFileInfo::lastRead() const
{
TQDateTime dt;
if ( !fic || !cache )
doStat();
if ( fic && fic->st.st_atime != 0 ) {
dt.setTime_t( fic->st.st_atime );
return dt;
} else {
return lastModified();
}
}
#ifndef TQT_NO_DIR
/*!
Returns the absolute path including the file name.
The absolute path name consists of the full path and the file
name. On Unix this will always begin with the root, '/',
directory. On Windows this will always begin 'D:/' where D is a
drive letter, except for network shares that are not mapped to a
drive letter, in which case the path will begin '//sharename/'.
This function returns the same as filePath(), unless isRelative()
is TRUE.
If the TQFileInfo is empty it returns TQDir::currentDirPath().
This function can be time consuming under Unix (in the order of
milliseconds).
\sa isRelative(), filePath()
*/
TQString TQFileInfo::absFilePath() const
{
TQString tmp;
if ( TQDir::isRelativePath(fn)
#if defined(Q_OS_WIN32)
&& fn[1] != ':'
#endif
) {
tmp = TQDir::currentDirPath();
tmp += '/';
}
tmp += fn;
makeAbs( tmp );
return TQDir::cleanDirPath( tmp );
}
/*! \internal
Detaches all internal data.
*/
void TQFileInfo::detach()
{
fn = TQDeepCopy<TQString>( fn );
if ( fic ) {
TQFileInfoCache *cur = fic;
fic = new TQFileInfoCache;
*fic = *cur;
delete cur;
}
}
#endif
|