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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qplatformdefs.h"
#include "qabstractfileengine.h"
#include "private/qfsfileengine_p.h"
#include "private/qcore_unix_p.h"
#ifndef QT_NO_FSFILEENGINE
#include "qfile.h"
#include "qdir.h"
#include "qdatetime.h"
#include "qvarlengtharray.h"
#include <sys/mman.h>
#include <stdlib.h>
#include <limits.h>
#if defined(Q_OS_SYMBIAN)
# include <syslimits.h>
# include <f32file.h>
# include <pathinfo.h>
# include "private/qcore_symbian_p.h"
#endif
#include <errno.h>
#if !defined(QWS) && defined(Q_OS_MAC)
# include <private/qcore_mac_p.h>
#endif
QT_BEGIN_NAMESPACE
#if defined(Q_OS_SYMBIAN)
/*!
\internal
Returns true if supplied path is a relative path
*/
static bool isRelativePathSymbian(const QString& fileName)
{
return !(fileName.startsWith(QLatin1Char('/'))
|| (fileName.length() >= 2
&& ((fileName.at(0).isLetter() && fileName.at(1) == QLatin1Char(':'))
|| (fileName.at(0) == QLatin1Char('/') && fileName.at(1) == QLatin1Char('/')))));
}
#endif
/*!
\internal
Returns the stdlib open string corresponding to a QIODevice::OpenMode.
*/
static inline QByteArray openModeToFopenMode(QIODevice::OpenMode flags, const QString &fileName)
{
QByteArray mode;
if ((flags & QIODevice::ReadOnly) && !(flags & QIODevice::Truncate)) {
mode = "rb";
if (flags & QIODevice::WriteOnly) {
QT_STATBUF statBuf;
if (!fileName.isEmpty()
&& QT_STAT(QFile::encodeName(fileName), &statBuf) == 0
&& (statBuf.st_mode & S_IFMT) == S_IFREG) {
mode += "+";
} else {
mode = "wb+";
}
}
} else if (flags & QIODevice::WriteOnly) {
mode = "wb";
if (flags & QIODevice::ReadOnly)
mode += '+';
}
if (flags & QIODevice::Append) {
mode = "ab";
if (flags & QIODevice::ReadOnly)
mode += '+';
}
#if defined(__GLIBC__) && (__GLIBC__ * 0x100 + __GLIBC_MINOR__) >= 0x0207
// must be glibc >= 2.7
mode += 'e';
#endif
return mode;
}
/*!
\internal
Returns the stdio open flags corresponding to a QIODevice::OpenMode.
*/
static inline int openModeToOpenFlags(QIODevice::OpenMode mode)
{
int oflags = QT_OPEN_RDONLY;
#ifdef QT_LARGEFILE_SUPPORT
oflags |= QT_OPEN_LARGEFILE;
#endif
if ((mode & QFile::ReadWrite) == QFile::ReadWrite) {
oflags = QT_OPEN_RDWR | QT_OPEN_CREAT;
} else if (mode & QFile::WriteOnly) {
oflags = QT_OPEN_WRONLY | QT_OPEN_CREAT;
}
if (mode & QFile::Append) {
oflags |= QT_OPEN_APPEND;
} else if (mode & QFile::WriteOnly) {
if ((mode & QFile::Truncate) || !(mode & QFile::ReadOnly))
oflags |= QT_OPEN_TRUNC;
}
return oflags;
}
/*!
\internal
Sets the file descriptor to close on exec. That is, the file
descriptor is not inherited by child processes.
*/
static inline bool setCloseOnExec(int fd)
{
return fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) != -1;
}
/*!
\internal
*/
void QFSFileEnginePrivate::nativeInitFileName()
{
nativeFilePath = QFile::encodeName(filePath);
}
/*!
\internal
*/
bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode)
{
Q_Q(QFSFileEngine);
if (openMode & QIODevice::Unbuffered) {
int flags = openModeToOpenFlags(openMode);
// Try to open the file in unbuffered mode.
do {
fd = QT_OPEN(nativeFilePath.constData(), flags, 0666);
} while (fd == -1 && errno == EINTR);
// On failure, return and report the error.
if (fd == -1) {
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,
qt_error_string(errno));
return false;
}
QT_STATBUF statBuf;
if (QT_FSTAT(fd, &statBuf) != -1) {
if ((statBuf.st_mode & S_IFMT) == S_IFDIR) {
q->setError(QFile::OpenError, QLatin1String("file to open is a directory"));
QT_CLOSE(fd);
return false;
}
}
// Seek to the end when in Append mode.
if (flags & QFile::Append) {
int ret;
do {
ret = QT_LSEEK(fd, 0, SEEK_END);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,
qt_error_string(int(errno)));
return false;
}
}
fh = 0;
} else {
QByteArray fopenMode = openModeToFopenMode(openMode, filePath);
// Try to open the file in buffered mode.
do {
fh = QT_FOPEN(nativeFilePath.constData(), fopenMode.constData());
} while (!fh && errno == EINTR);
// On failure, return and report the error.
if (!fh) {
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,
qt_error_string(int(errno)));
return false;
}
QT_STATBUF statBuf;
if (QT_FSTAT(fileno(fh), &statBuf) != -1) {
if ((statBuf.st_mode & S_IFMT) == S_IFDIR) {
q->setError(QFile::OpenError, QLatin1String("file to open is a directory"));
fclose(fh);
return false;
}
}
setCloseOnExec(fileno(fh)); // ignore failure
// Seek to the end when in Append mode.
if (openMode & QIODevice::Append) {
int ret;
do {
ret = QT_FSEEK(fh, 0, SEEK_END);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError,
qt_error_string(int(errno)));
return false;
}
}
fd = -1;
}
closeFileHandle = true;
return true;
}
/*!
\internal
*/
bool QFSFileEnginePrivate::nativeClose()
{
return closeFdFh();
}
/*!
\internal
*/
bool QFSFileEnginePrivate::nativeFlush()
{
return fh ? flushFh() : fd != -1;
}
/*!
\internal
*/
qint64 QFSFileEnginePrivate::nativeRead(char *data, qint64 len)
{
Q_Q(QFSFileEngine);
if (fh && nativeIsSequential()) {
size_t readBytes = 0;
int oldFlags = fcntl(QT_FILENO(fh), F_GETFL);
for (int i = 0; i < 2; ++i) {
// Unix: Make the underlying file descriptor non-blocking
if ((oldFlags & O_NONBLOCK) == 0)
fcntl(QT_FILENO(fh), F_SETFL, oldFlags | O_NONBLOCK);
// Cross platform stdlib read
size_t read = 0;
do {
read = fread(data + readBytes, 1, size_t(len - readBytes), fh);
} while (read == 0 && !feof(fh) && errno == EINTR);
if (read > 0) {
readBytes += read;
break;
} else {
if (readBytes)
break;
readBytes = read;
}
// Unix: Restore the blocking state of the underlying socket
if ((oldFlags & O_NONBLOCK) == 0) {
fcntl(QT_FILENO(fh), F_SETFL, oldFlags);
if (readBytes == 0) {
int readByte = 0;
do {
readByte = fgetc(fh);
} while (readByte == -1 && errno == EINTR);
if (readByte != -1) {
*data = uchar(readByte);
readBytes += 1;
} else {
break;
}
}
}
}
// Unix: Restore the blocking state of the underlying socket
if ((oldFlags & O_NONBLOCK) == 0) {
fcntl(QT_FILENO(fh), F_SETFL, oldFlags);
}
if (readBytes == 0 && !feof(fh)) {
// if we didn't read anything and we're not at EOF, it must be an error
q->setError(QFile::ReadError, qt_error_string(int(errno)));
return -1;
}
return readBytes;
}
return readFdFh(data, len);
}
/*!
\internal
*/
qint64 QFSFileEnginePrivate::nativeReadLine(char *data, qint64 maxlen)
{
return readLineFdFh(data, maxlen);
}
/*!
\internal
*/
qint64 QFSFileEnginePrivate::nativeWrite(const char *data, qint64 len)
{
return writeFdFh(data, len);
}
/*!
\internal
*/
qint64 QFSFileEnginePrivate::nativePos() const
{
return posFdFh();
}
/*!
\internal
*/
bool QFSFileEnginePrivate::nativeSeek(qint64 pos)
{
return seekFdFh(pos);
}
/*!
\internal
*/
int QFSFileEnginePrivate::nativeHandle() const
{
return fh ? fileno(fh) : fd;
}
/*!
\internal
*/
bool QFSFileEnginePrivate::nativeIsSequential() const
{
return isSequentialFdFh();
}
bool QFSFileEngine::remove()
{
Q_D(QFSFileEngine);
bool ret = unlink(d->nativeFilePath.constData()) == 0;
if (!ret)
setError(QFile::RemoveError, qt_error_string(errno));
return ret;
}
bool QFSFileEngine::copy(const QString &newName)
{
#if defined(Q_OS_SYMBIAN)
Q_D(QFSFileEngine);
RFs rfs = qt_s60GetRFs();
CFileMan* fm = NULL;
QString oldNative(QDir::toNativeSeparators(d->filePath));
TPtrC oldPtr(qt_QString2TPtrC(oldNative));
QFileInfo fi(newName);
QString absoluteNewName = fi.absoluteFilePath();
QString newNative(QDir::toNativeSeparators(absoluteNewName));
TPtrC newPtr(qt_QString2TPtrC(newNative));
TRAPD (err,
fm = CFileMan::NewL(rfs);
RFile rfile;
err = rfile.Open(rfs, oldPtr, EFileShareReadersOrWriters);
if (err == KErrNone) {
err = fm->Copy(rfile, newPtr);
rfile.Close();
}
) // End TRAP
delete fm;
// ### Add error reporting on failure
return (err == KErrNone);
#else
Q_UNUSED(newName);
// ### Add copy code for Unix here
setError(QFile::UnspecifiedError, QLatin1String("Not implemented!"));
return false;
#endif
}
bool QFSFileEngine::rename(const QString &newName)
{
Q_D(QFSFileEngine);
bool ret = ::rename(d->nativeFilePath.constData(), QFile::encodeName(newName).constData()) == 0;
if (!ret)
setError(QFile::RenameError, qt_error_string(errno));
return ret;
}
bool QFSFileEngine::link(const QString &newName)
{
Q_D(QFSFileEngine);
bool ret = ::symlink(d->nativeFilePath.constData(), QFile::encodeName(newName).constData()) == 0;
if (!ret)
setError(QFile::RenameError, qt_error_string(errno));
return ret;
}
qint64 QFSFileEnginePrivate::nativeSize() const
{
return sizeFdFh();
}
bool QFSFileEngine::mkdir(const QString &name, bool createParentDirectories) const
{
QString dirName = name;
if (createParentDirectories) {
dirName = QDir::cleanPath(dirName);
#if defined(Q_OS_SYMBIAN)
dirName = QDir::toNativeSeparators(dirName);
#endif
for(int oldslash = -1, slash=0; slash != -1; oldslash = slash) {
slash = dirName.indexOf(QDir::separator(), oldslash+1);
if (slash == -1) {
if (oldslash == dirName.length())
break;
slash = dirName.length();
}
if (slash) {
QByteArray chunk = QFile::encodeName(dirName.left(slash));
QT_STATBUF st;
if (QT_STAT(chunk, &st) != -1) {
if ((st.st_mode & S_IFMT) != S_IFDIR)
return false;
} else if (QT_MKDIR(chunk, 0777) != 0) {
return false;
}
}
}
return true;
}
#if defined(Q_OS_DARWIN) // Mac X doesn't support trailing /'s
if (dirName.endsWith(QLatin1Char('/')))
dirName.chop(1);
#endif
return (QT_MKDIR(QFile::encodeName(dirName), 0777) == 0);
}
bool QFSFileEngine::rmdir(const QString &name, bool recurseParentDirectories) const
{
QString dirName = name;
if (recurseParentDirectories) {
dirName = QDir::cleanPath(dirName);
#if defined(Q_OS_SYMBIAN)
dirName = QDir::toNativeSeparators(dirName);
#endif
for(int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) {
QByteArray chunk = QFile::encodeName(dirName.left(slash));
QT_STATBUF st;
if (QT_STAT(chunk, &st) != -1) {
if ((st.st_mode & S_IFMT) != S_IFDIR)
return false;
if (::rmdir(chunk) != 0)
return oldslash != 0;
} else {
return false;
}
slash = dirName.lastIndexOf(QDir::separator(), oldslash-1);
}
return true;
}
return ::rmdir(QFile::encodeName(dirName)) == 0;
}
bool QFSFileEngine::caseSensitive() const
{
#if defined(Q_OS_SYMBIAN)
return false;
#else
return true;
#endif
}
bool QFSFileEngine::setCurrentPath(const QString &path)
{
int r;
r = QT_CHDIR(QFile::encodeName(path));
return r >= 0;
}
QString QFSFileEngine::currentPath(const QString &)
{
QString result;
QT_STATBUF st;
#if defined(Q_OS_SYMBIAN)
char nativeCurrentName[PATH_MAX+1];
if (::getcwd(nativeCurrentName, PATH_MAX))
result = QDir::fromNativeSeparators(QFile::decodeName(QByteArray(nativeCurrentName)));
if (result.isEmpty()) {
# if defined(QT_DEBUG)
qWarning("QFSFileEngine::currentPath: getcwd() failed");
# endif
} else
#endif
if (QT_STAT(".", &st) == 0) {
#if defined(__GLIBC__) && !defined(PATH_MAX)
char *currentName = ::get_current_dir_name();
if (currentName) {
result = QFile::decodeName(QByteArray(currentName));
::free(currentName);
}
#elif !defined(Q_OS_SYMBIAN)
char currentName[PATH_MAX+1];
if (::getcwd(currentName, PATH_MAX))
result = QFile::decodeName(QByteArray(currentName));
# if defined(QT_DEBUG)
if (result.isNull())
qWarning("QFSFileEngine::currentPath: getcwd() failed");
# endif
#endif
} else {
#if defined(Q_OS_SYMBIAN)
// If current dir returned by Open C doesn't exist,
// try to create it (can happen with application private dirs)
// Ignore mkdir failures; we want to be consistent with Open C
// current path regardless.
QT_MKDIR(QFile::encodeName(QLatin1String(nativeCurrentName)), 0777);
#else
# if defined(QT_DEBUG)
qWarning("QFSFileEngine::currentPath: stat(\".\") failed");
# endif
#endif
}
return result;
}
QString QFSFileEngine::homePath()
{
#if defined(Q_OS_SYMBIAN)
QString home = rootPath();
#else
QString home = QFile::decodeName(qgetenv("HOME"));
if (home.isNull())
home = rootPath();
#endif
return home;
}
QString QFSFileEngine::rootPath()
{
#if defined(Q_OS_SYMBIAN)
# ifdef Q_WS_S60
TFileName symbianPath = PathInfo::PhoneMemoryRootPath();
return QDir::cleanPath(QDir::fromNativeSeparators(qt_TDesC2QString(symbianPath)));
# else
# warning No fallback implementation of QFSFileEngine::rootPath()
return QString();
# endif
#else
return QLatin1String("/");
#endif
}
QString QFSFileEngine::tempPath()
{
#if defined(Q_OS_SYMBIAN)
# ifdef Q_WS_S60
TFileName symbianPath = PathInfo::PhoneMemoryRootPath();
QString temp = QDir::fromNativeSeparators(qt_TDesC2QString(symbianPath));
temp += QLatin1String( "temp/");
// Just to verify that folder really exist on hardware
QT_MKDIR(QFile::encodeName(temp), 0777);
# else
# warning No fallback implementation of QFSFileEngine::tempPath()
QString temp;
# endif
#else
QString temp = QFile::decodeName(qgetenv("TMPDIR"));
if (temp.isEmpty())
temp = QLatin1String("/tmp/");
#endif
return temp;
}
QFileInfoList QFSFileEngine::drives()
{
QFileInfoList ret;
#if defined(Q_OS_SYMBIAN)
TDriveList driveList;
RFs rfs = qt_s60GetRFs();
TInt err = rfs.DriveList(driveList);
if (err == KErrNone) {
char driveName[] = "A:/";
for (char i = 0; i < KMaxDrives; i++) {
if (driveList[i]) {
driveName[0] = 'A' + i;
ret.append(QFileInfo(QLatin1String(driveName)));
}
}
} else {
qWarning("QFSFileEngine::drives: Getting drives failed");
}
#else
ret.append(QFileInfo(rootPath()));
#endif
return ret;
}
bool QFSFileEnginePrivate::doStat() const
{
if (!tried_stat) {
tried_stat = true;
could_stat = false;
if (fh && nativeFilePath.isEmpty()) {
// ### actually covers two cases: d->fh and when the file is not open
could_stat = (QT_FSTAT(QT_FILENO(fh), &st) == 0);
} else if (fd == -1) {
// ### actually covers two cases: d->fh and when the file is not open
could_stat = (QT_STAT(nativeFilePath.constData(), &st) == 0);
} else {
could_stat = (QT_FSTAT(fd, &st) == 0);
}
}
return could_stat;
}
bool QFSFileEnginePrivate::isSymlink() const
{
if (need_lstat) {
need_lstat = false;
QT_STATBUF st; // don't clobber our main one
is_link = (QT_LSTAT(nativeFilePath.constData(), &st) == 0) ? S_ISLNK(st.st_mode) : false;
}
return is_link;
}
#if defined(Q_OS_SYMBIAN)
static bool _q_isSymbianHidden(const QString &path, bool isDir)
{
RFs rfs = qt_s60GetRFs();
QFileInfo fi(path);
QString absPath = fi.absoluteFilePath();
if (isDir && !absPath.endsWith(QLatin1Char('/')))
absPath.append(QLatin1Char('/'));
QString native(QDir::toNativeSeparators(absPath));
TPtrC ptr(qt_QString2TPtrC(native));
TUint attributes;
TInt err = rfs.Att(ptr, attributes);
return (err == KErrNone && (attributes & KEntryAttHidden));
}
#endif
#if !defined(QWS) && defined(Q_OS_MAC)
static bool _q_isMacHidden(const QString &path)
{
OSErr err = noErr;
FSRef fsRef;
err = FSPathMakeRefWithOptions(reinterpret_cast<const UInt8 *>(QFile::encodeName(QDir::cleanPath(path)).constData()),
kFSPathMakeRefDoNotFollowLeafSymlink, &fsRef, 0);
if (err != noErr)
return false;
FSCatalogInfo catInfo;
err = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL);
if (err != noErr)
return false;
FileInfo * const fileInfo = reinterpret_cast<FileInfo*>(&catInfo.finderInfo);
bool result = (fileInfo->finderFlags & kIsInvisible);
return result;
}
#endif
/*!
\reimp
*/
QAbstractFileEngine::FileFlags QFSFileEngine::fileFlags(FileFlags type) const
{
Q_D(const QFSFileEngine);
// Force a stat, so that we're guaranteed to get up-to-date results
if (type & Refresh) {
d->tried_stat = 0;
d->need_lstat = 1;
}
QAbstractFileEngine::FileFlags ret = 0;
if (type & FlagsMask)
ret |= LocalDiskFlag;
bool exists = d->doStat();
if (!exists && !d->isSymlink())
return ret;
if (exists && (type & PermsMask)) {
if (d->st.st_mode & S_IRUSR)
ret |= ReadOwnerPerm;
if (d->st.st_mode & S_IWUSR)
ret |= WriteOwnerPerm;
if (d->st.st_mode & S_IXUSR)
ret |= ExeOwnerPerm;
if (d->st.st_mode & S_IRUSR)
ret |= ReadUserPerm;
if (d->st.st_mode & S_IWUSR)
ret |= WriteUserPerm;
if (d->st.st_mode & S_IXUSR)
ret |= ExeUserPerm;
if (d->st.st_mode & S_IRGRP)
ret |= ReadGroupPerm;
if (d->st.st_mode & S_IWGRP)
ret |= WriteGroupPerm;
if (d->st.st_mode & S_IXGRP)
ret |= ExeGroupPerm;
if (d->st.st_mode & S_IROTH)
ret |= ReadOtherPerm;
if (d->st.st_mode & S_IWOTH)
ret |= WriteOtherPerm;
if (d->st.st_mode & S_IXOTH)
ret |= ExeOtherPerm;
}
if (type & TypesMask) {
#if !defined(QWS) && defined(Q_OS_MAC)
bool foundAlias = false;
{
FSRef fref;
if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(),
&fref, NULL) == noErr) {
Boolean isAlias, isFolder;
if (FSIsAliasFile(&fref, &isAlias, &isFolder) == noErr && isAlias) {
foundAlias = true;
ret |= LinkType;
}
}
}
if (!foundAlias)
#endif
{
if ((type & LinkType) && d->isSymlink())
ret |= LinkType;
if (exists && (d->st.st_mode & S_IFMT) == S_IFREG)
ret |= FileType;
else if (exists && (d->st.st_mode & S_IFMT) == S_IFDIR)
ret |= DirectoryType;
#if !defined(QWS) && defined(Q_OS_MAC)
if ((ret & DirectoryType) && (type & BundleType)) {
QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath),
kCFURLPOSIXPathStyle, true);
UInt32 type, creator;
if (CFBundleGetPackageInfoInDirectory(url, &type, &creator))
ret |= BundleType;
}
#endif
}
}
if (type & FlagsMask) {
if (exists)
ret |= ExistsFlag;
#if defined(Q_OS_SYMBIAN)
if (d->filePath == QLatin1String("/")
|| (d->filePath.length() == 3 && d->filePath.at(0).isLetter()
&& d->filePath.at(1) == QLatin1Char(':') && d->filePath.at(2) == QLatin1Char('/'))) {
ret |= RootFlag;
} else {
// In Symbian, all symlinks have hidden attribute for some reason;
// lets make them visible for better compatibility with other platforms.
// If somebody actually wants a hidden link, then they are out of luck.
if (!d->isSymlink() && _q_isSymbianHidden(d->filePath, ret & DirectoryType))
ret |= HiddenFlag;
}
#else
if (d->filePath == QLatin1String("/")) {
ret |= RootFlag;
} else {
QString baseName = fileName(BaseName);
if ((baseName.size() > 1
&& baseName.at(0) == QLatin1Char('.') && baseName.at(1) != QLatin1Char('.'))
# if !defined(QWS) && defined(Q_OS_MAC)
|| _q_isMacHidden(d->filePath)
# endif
) {
ret |= HiddenFlag;
}
}
#endif
}
return ret;
}
#if defined(Q_OS_SYMBIAN)
QString QFSFileEngine::fileName(FileName file) const
{
Q_D(const QFSFileEngine);
const QLatin1Char slashChar('/');
if(file == BaseName) {
int slash = d->filePath.lastIndexOf(slashChar);
if(slash == -1) {
int colon = d->filePath.lastIndexOf(QLatin1Char(':'));
if(colon != -1)
return d->filePath.mid(colon + 1);
return d->filePath;
}
return d->filePath.mid(slash + 1);
} else if(file == PathName) {
if(!d->filePath.size())
return d->filePath;
int slash = d->filePath.lastIndexOf(slashChar);
if(slash == -1) {
if(d->filePath.length() >= 2 && d->filePath.at(1) == QLatin1Char(':'))
return d->filePath.left(2);
return QLatin1String(".");
} else {
if(!slash)
return QLatin1String("/");
if(slash == 2 && d->filePath.length() >= 2 && d->filePath.at(1) == QLatin1Char(':'))
slash++;
return d->filePath.left(slash);
}
} else if(file == AbsoluteName || file == AbsolutePathName) {
QString ret;
if (!isRelativePathSymbian(d->filePath)) {
if (d->filePath.size() > 2 && d->filePath.at(1) == QLatin1Char(':')
&& d->filePath.at(2) != slashChar){
// It's a drive-relative path, so C:a.txt -> C:/currentpath/a.txt,
// or if it's different drive than current, Z:a.txt -> Z:/a.txt
QString currentPath = QDir::currentPath();
if (0 == currentPath.left(1).compare(d->filePath.left(1), Qt::CaseInsensitive))
ret = currentPath + slashChar + d->filePath.mid(2);
else
ret = d->filePath.left(2) + slashChar + d->filePath.mid(2);
} else if (d->filePath.startsWith(slashChar)) {
// It's a absolute path to the current drive, so /a.txt -> C:/a.txt
ret = QDir::currentPath().left(2) + d->filePath;
} else {
ret = d->filePath;
}
} else {
ret = QDir::currentPath() + slashChar + d->filePath;
}
// The path should be absolute at this point.
// From the docs :
// Absolute paths begin with the directory separator "/"
// (optionally preceded by a drive specification under Windows).
if (ret.at(0) != slashChar) {
Q_ASSERT(ret.length() >= 2);
Q_ASSERT(ret.at(0).isLetter());
Q_ASSERT(ret.at(1) == QLatin1Char(':'));
// Force uppercase drive letters.
ret[0] = ret.at(0).toUpper();
}
// Clean up the path
bool isDir = ret.endsWith(slashChar);
ret = QDir::cleanPath(ret);
if (isDir && !ret.endsWith(slashChar))
ret += slashChar;
if (file == AbsolutePathName) {
int slash = ret.lastIndexOf(slashChar);
if (slash < 0)
return ret;
else if (ret.at(0) != slashChar && slash == 2)
return ret.left(3); // include the slash
else
return ret.left(slash > 0 ? slash : 1);
}
return ret;
} else if(file == CanonicalName || file == CanonicalPathName) {
if (!(fileFlags(ExistsFlag) & ExistsFlag))
return QString();
QString ret = QFSFileEnginePrivate::canonicalized(fileName(AbsoluteName));
if (file == CanonicalPathName && !ret.isEmpty()) {
int slash = ret.lastIndexOf(slashChar);
if (slash == -1)
ret = QDir::fromNativeSeparators(QDir::currentPath());
else if (slash == 0)
ret = QLatin1String("/");
ret = ret.left(slash);
}
return ret;
} else if(file == LinkName) {
if (d->isSymlink()) {
char s[PATH_MAX+1];
int len = readlink(d->nativeFilePath.constData(), s, PATH_MAX);
if (len > 0) {
s[len] = '\0';
QString ret = QFile::decodeName(QByteArray(s));
if (isRelativePathSymbian(ret)) {
if (!isRelativePathSymbian(d->filePath)) {
ret.prepend(d->filePath.left(d->filePath.lastIndexOf(slashChar))
+ slashChar);
} else {
ret.prepend(QDir::currentPath() + slashChar);
}
}
ret = QDir::cleanPath(ret);
if (ret.size() > 1 && ret.endsWith(slashChar))
ret.chop(1);
return ret;
}
}
return QString();
} else if(file == BundleName) {
return QString();
}
return d->filePath;
}
#else
QString QFSFileEngine::fileName(FileName file) const
{
Q_D(const QFSFileEngine);
if (file == BundleName) {
#if !defined(QWS) && defined(Q_OS_MAC)
QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath),
kCFURLPOSIXPathStyle, true);
if (CFDictionaryRef dict = CFBundleCopyInfoDictionaryForURL(url)) {
if (CFTypeRef name = (CFTypeRef)CFDictionaryGetValue(dict, kCFBundleNameKey)) {
if (CFGetTypeID(name) == CFStringGetTypeID())
return QCFString::toQString((CFStringRef)name);
}
}
#endif
return QString();
} else if (file == BaseName) {
int slash = d->filePath.lastIndexOf(QLatin1Char('/'));
if (slash != -1)
return d->filePath.mid(slash + 1);
} else if (file == PathName) {
int slash = d->filePath.lastIndexOf(QLatin1Char('/'));
if (slash == -1)
return QLatin1String(".");
else if (!slash)
return QLatin1String("/");
return d->filePath.left(slash);
} else if (file == AbsoluteName || file == AbsolutePathName) {
QString ret;
if (d->filePath.isEmpty() || !d->filePath.startsWith(QLatin1Char('/')))
ret = QDir::currentPath();
if (!d->filePath.isEmpty() && d->filePath != QLatin1String(".")) {
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/')))
ret += QLatin1Char('/');
ret += d->filePath;
}
if (ret == QLatin1String("/"))
return ret;
bool isDir = ret.endsWith(QLatin1Char('/'));
ret = QDir::cleanPath(ret);
if (isDir)
ret += QLatin1Char('/');
if (file == AbsolutePathName) {
int slash = ret.lastIndexOf(QLatin1Char('/'));
if (slash == -1)
return QDir::currentPath();
else if (!slash)
return QLatin1String("/");
return ret.left(slash);
}
return ret;
} else if (file == CanonicalName || file == CanonicalPathName) {
if (!(fileFlags(ExistsFlag) & ExistsFlag))
return QString();
QString ret = QFSFileEnginePrivate::canonicalized(fileName(AbsoluteName));
if (file == CanonicalPathName && !ret.isEmpty()) {
int slash = ret.lastIndexOf(QLatin1Char('/'));
if (slash == -1)
ret = QDir::currentPath();
else if (slash == 0)
ret = QLatin1String("/");
ret = ret.left(slash);
}
return ret;
} else if (file == LinkName) {
if (d->isSymlink()) {
#if defined(__GLIBC__) && !defined(PATH_MAX)
#define PATH_CHUNK_SIZE 256
char *s = 0;
int len = -1;
int size = PATH_CHUNK_SIZE;
while (1) {
s = q_check_ptr((char *) ::realloc(s, size));
len = ::readlink(d->nativeFilePath.constData(), s, size);
if (len < 0) {
::free(s);
break;
}
if (len < size) {
break;
}
size *= 2;
}
#else
char s[PATH_MAX+1];
int len = readlink(d->nativeFilePath.constData(), s, PATH_MAX);
#endif
if (len > 0) {
QString ret;
if (d->doStat() && S_ISDIR(d->st.st_mode) && s[0] != '/') {
QDir parent(d->filePath);
parent.cdUp();
ret = parent.path();
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/')))
ret += QLatin1Char('/');
}
s[len] = '\0';
ret += QFile::decodeName(QByteArray(s));
#if defined(__GLIBC__) && !defined(PATH_MAX)
::free(s);
#endif
if (!ret.startsWith(QLatin1Char('/'))) {
if (d->filePath.startsWith(QLatin1Char('/'))) {
ret.prepend(d->filePath.left(d->filePath.lastIndexOf(QLatin1Char('/')))
+ QLatin1Char('/'));
} else {
ret.prepend(QDir::currentPath() + QLatin1Char('/'));
}
}
ret = QDir::cleanPath(ret);
if (ret.size() > 1 && ret.endsWith(QLatin1Char('/')))
ret.chop(1);
return ret;
}
}
#if !defined(QWS) && defined(Q_OS_MAC)
{
FSRef fref;
if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(), &fref, 0) == noErr) {
Boolean isAlias, isFolder;
if (FSResolveAliasFile(&fref, true, &isFolder, &isAlias) == noErr && isAlias) {
AliasHandle alias;
if (FSNewAlias(0, &fref, &alias) == noErr && alias) {
CFStringRef cfstr;
if (FSCopyAliasInfo(alias, 0, 0, &cfstr, 0, 0) == noErr)
return QCFString::toQString(cfstr);
}
}
}
}
#endif
return QString();
}
return d->filePath;
}
#endif // Q_OS_SYMBIAN
bool QFSFileEngine::isRelativePath() const
{
Q_D(const QFSFileEngine);
#if defined(Q_OS_SYMBIAN)
return isRelativePathSymbian(d->filePath);
#else
return d->filePath.length() ? d->filePath[0] != QLatin1Char('/') : true;
#endif
}
uint QFSFileEngine::ownerId(FileOwner own) const
{
Q_D(const QFSFileEngine);
static const uint nobodyID = (uint) -2;
if (d->doStat()) {
if (own == OwnerUser)
return d->st.st_uid;
else
return d->st.st_gid;
}
return nobodyID;
}
QString QFSFileEngine::owner(FileOwner own) const
{
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
int size_max = sysconf(_SC_GETPW_R_SIZE_MAX);
if (size_max == -1)
size_max = 1024;
QVarLengthArray<char, 1024> buf(size_max);
#endif
if (own == OwnerUser) {
struct passwd *pw = 0;
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
struct passwd entry;
getpwuid_r(ownerId(own), &entry, buf.data(), buf.size(), &pw);
#else
pw = getpwuid(ownerId(own));
#endif
if (pw)
return QFile::decodeName(QByteArray(pw->pw_name));
} else if (own == OwnerGroup) {
#if !defined(Q_OS_SYMBIAN)
struct group *gr = 0;
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
size_max = sysconf(_SC_GETGR_R_SIZE_MAX);
if (size_max == -1)
size_max = 1024;
buf.resize(size_max);
struct group entry;
// Some large systems have more members than the POSIX max size
// Loop over by doubling the buffer size (upper limit 250k)
for (unsigned size = size_max; size < 256000; size += size)
{
buf.resize(size);
// ERANGE indicates that the buffer was too small
if (!getgrgid_r(ownerId(own), &entry, buf.data(), buf.size(), &gr)
|| errno != ERANGE)
break;
}
#else
gr = getgrgid(ownerId(own));
#endif
if (gr)
return QFile::decodeName(QByteArray(gr->gr_name));
#endif
}
return QString();
}
bool QFSFileEngine::setPermissions(uint perms)
{
Q_D(QFSFileEngine);
bool ret = false;
mode_t mode = 0;
if (perms & ReadOwnerPerm)
mode |= S_IRUSR;
if (perms & WriteOwnerPerm)
mode |= S_IWUSR;
if (perms & ExeOwnerPerm)
mode |= S_IXUSR;
if (perms & ReadUserPerm)
mode |= S_IRUSR;
if (perms & WriteUserPerm)
mode |= S_IWUSR;
if (perms & ExeUserPerm)
mode |= S_IXUSR;
if (perms & ReadGroupPerm)
mode |= S_IRGRP;
if (perms & WriteGroupPerm)
mode |= S_IWGRP;
if (perms & ExeGroupPerm)
mode |= S_IXGRP;
if (perms & ReadOtherPerm)
mode |= S_IROTH;
if (perms & WriteOtherPerm)
mode |= S_IWOTH;
if (perms & ExeOtherPerm)
mode |= S_IXOTH;
if (d->fd != -1)
ret = fchmod(d->fd, mode) == 0;
else
ret = ::chmod(d->nativeFilePath.constData(), mode) == 0;
if (!ret)
setError(QFile::PermissionsError, qt_error_string(errno));
return ret;
}
bool QFSFileEngine::setSize(qint64 size)
{
Q_D(QFSFileEngine);
bool ret = false;
if (d->fd != -1)
ret = QT_FTRUNCATE(d->fd, size) == 0;
else if (d->fh)
ret = QT_FTRUNCATE(QT_FILENO(d->fh), size) == 0;
else
ret = QT_TRUNCATE(d->nativeFilePath.constData(), size) == 0;
if (!ret)
setError(QFile::ResizeError, qt_error_string(errno));
return ret;
}
QDateTime QFSFileEngine::fileTime(FileTime time) const
{
Q_D(const QFSFileEngine);
QDateTime ret;
if (d->doStat()) {
if (time == CreationTime)
ret.setTime_t(d->st.st_ctime ? d->st.st_ctime : d->st.st_mtime);
else if (time == ModificationTime)
ret.setTime_t(d->st.st_mtime);
else if (time == AccessTime)
ret.setTime_t(d->st.st_atime);
}
return ret;
}
uchar *QFSFileEnginePrivate::map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
{
Q_Q(QFSFileEngine);
Q_UNUSED(flags);
if (openMode == QIODevice::NotOpen) {
q->setError(QFile::PermissionsError, qt_error_string(int(EACCES)));
return 0;
}
if (offset < 0) {
q->setError(QFile::UnspecifiedError, qt_error_string(int(EINVAL)));
return 0;
}
int access = 0;
if (openMode & QIODevice::ReadOnly) access |= PROT_READ;
if (openMode & QIODevice::WriteOnly) access |= PROT_WRITE;
int pagesSize = getpagesize();
int realOffset = offset / pagesSize;
int extra = offset % pagesSize;
#ifdef Q_OS_SYMBIAN
void *mapAddress;
TRAPD(err, mapAddress = mmap((void*)0, (size_t)size + extra,
access, MAP_SHARED, nativeHandle(), realOffset * pagesSize));
if (err != KErrNone) {
qWarning("OpenC bug: leave from mmap %d", err);
mapAddress = MAP_FAILED;
errno = EINVAL;
}
#else
void *mapAddress = mmap((void*)0, (size_t)size + extra,
access, MAP_SHARED, nativeHandle(), realOffset * pagesSize);
#endif
if (MAP_FAILED != mapAddress) {
uchar *address = extra + static_cast<uchar*>(mapAddress);
maps[address] = QPair<int,int>(extra, size);
return address;
}
switch(errno) {
case EBADF:
q->setError(QFile::PermissionsError, qt_error_string(int(EACCES)));
break;
case ENFILE:
case ENOMEM:
q->setError(QFile::ResourceError, qt_error_string(int(errno)));
break;
case EINVAL:
// size are out of bounds
default:
q->setError(QFile::UnspecifiedError, qt_error_string(int(errno)));
break;
}
return 0;
}
bool QFSFileEnginePrivate::unmap(uchar *ptr)
{
Q_Q(QFSFileEngine);
if (!maps.contains(ptr)) {
q->setError(QFile::PermissionsError, qt_error_string(EACCES));
return false;
}
uchar *start = ptr - maps[ptr].first;
int len = maps[ptr].second;
if (-1 == munmap(start, len)) {
q->setError(QFile::UnspecifiedError, qt_error_string(errno));
return false;
}
maps.remove(ptr);
return true;
}
QT_END_NAMESPACE
#endif // QT_NO_FSFILEENGINE
|