author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 03 May 2010 13:17:34 +0300 | |
changeset 19 | fcece45ef507 |
parent 18 | 2f34d5167611 |
child 22 | 79de32ba3296 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
#include "qabstractfileengine.h" |
|
44 |
#include "private/qfsfileengine_p.h" |
|
45 |
#include "private/qcore_unix_p.h" |
|
46 |
||
47 |
#ifndef QT_NO_FSFILEENGINE |
|
48 |
||
49 |
#include "qfile.h" |
|
50 |
#include "qdir.h" |
|
51 |
#include "qdatetime.h" |
|
52 |
#include "qvarlengtharray.h" |
|
53 |
||
54 |
#include <sys/mman.h> |
|
55 |
#include <stdlib.h> |
|
56 |
#include <limits.h> |
|
57 |
#if defined(Q_OS_SYMBIAN) |
|
58 |
# include <syslimits.h> |
|
59 |
# include <f32file.h> |
|
60 |
# include <pathinfo.h> |
|
61 |
# include "private/qcore_symbian_p.h" |
|
62 |
#endif |
|
63 |
#include <errno.h> |
|
64 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
65 |
# include <private/qcore_mac_p.h> |
|
66 |
#endif |
|
67 |
||
68 |
QT_BEGIN_NAMESPACE |
|
69 |
||
70 |
||
71 |
#if defined(Q_OS_SYMBIAN) |
|
72 |
/*! |
|
73 |
\internal |
|
74 |
||
75 |
Returns true if supplied path is a relative path |
|
76 |
*/ |
|
77 |
static bool isRelativePathSymbian(const QString& fileName) |
|
78 |
{ |
|
79 |
return !(fileName.startsWith(QLatin1Char('/')) |
|
80 |
|| (fileName.length() >= 2 |
|
81 |
&& ((fileName.at(0).isLetter() && fileName.at(1) == QLatin1Char(':')) |
|
82 |
|| (fileName.at(0) == QLatin1Char('/') && fileName.at(1) == QLatin1Char('/'))))); |
|
83 |
} |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
84 |
|
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
85 |
/*! |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
86 |
\internal |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
87 |
convert symbian error code to the one suitable for setError. |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
88 |
example usage: setSymbianError(err, QFile::CopyError, QLatin1String("copy error")) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
89 |
*/ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
90 |
void QFSFileEnginePrivate::setSymbianError(int symbianError, QFile::FileError defaultError, QString defaultString) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
91 |
{ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
92 |
Q_Q(QFSFileEngine); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
93 |
switch (symbianError) { |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
94 |
case KErrNone: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
95 |
q->setError(QFile::NoError, QLatin1String("")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
96 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
97 |
case KErrAccessDenied: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
98 |
q->setError(QFile::PermissionsError, QLatin1String("access denied")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
99 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
100 |
case KErrPermissionDenied: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
101 |
q->setError(QFile::PermissionsError, QLatin1String("permission denied")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
102 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
103 |
case KErrAbort: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
104 |
q->setError(QFile::AbortError, QLatin1String("aborted")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
105 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
106 |
case KErrCancel: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
107 |
q->setError(QFile::AbortError, QLatin1String("cancelled")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
108 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
109 |
case KErrTimedOut: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
110 |
q->setError(QFile::TimeOutError, QLatin1String("timed out")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
111 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
112 |
default: |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
113 |
q->setError(defaultError, defaultString); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
114 |
break; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
115 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
116 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
117 |
|
0 | 118 |
#endif |
119 |
||
120 |
/*! |
|
121 |
\internal |
|
122 |
||
123 |
Returns the stdlib open string corresponding to a QIODevice::OpenMode. |
|
124 |
*/ |
|
125 |
static inline QByteArray openModeToFopenMode(QIODevice::OpenMode flags, const QString &fileName) |
|
126 |
{ |
|
127 |
QByteArray mode; |
|
128 |
if ((flags & QIODevice::ReadOnly) && !(flags & QIODevice::Truncate)) { |
|
129 |
mode = "rb"; |
|
130 |
if (flags & QIODevice::WriteOnly) { |
|
131 |
QT_STATBUF statBuf; |
|
132 |
if (!fileName.isEmpty() |
|
133 |
&& QT_STAT(QFile::encodeName(fileName), &statBuf) == 0 |
|
134 |
&& (statBuf.st_mode & S_IFMT) == S_IFREG) { |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
135 |
mode += '+'; |
0 | 136 |
} else { |
137 |
mode = "wb+"; |
|
138 |
} |
|
139 |
} |
|
140 |
} else if (flags & QIODevice::WriteOnly) { |
|
141 |
mode = "wb"; |
|
142 |
if (flags & QIODevice::ReadOnly) |
|
143 |
mode += '+'; |
|
144 |
} |
|
145 |
if (flags & QIODevice::Append) { |
|
146 |
mode = "ab"; |
|
147 |
if (flags & QIODevice::ReadOnly) |
|
148 |
mode += '+'; |
|
149 |
} |
|
150 |
||
151 |
#if defined(__GLIBC__) && (__GLIBC__ * 0x100 + __GLIBC_MINOR__) >= 0x0207 |
|
152 |
// must be glibc >= 2.7 |
|
153 |
mode += 'e'; |
|
154 |
#endif |
|
155 |
||
156 |
return mode; |
|
157 |
} |
|
158 |
||
159 |
/*! |
|
160 |
\internal |
|
161 |
||
162 |
Returns the stdio open flags corresponding to a QIODevice::OpenMode. |
|
163 |
*/ |
|
164 |
static inline int openModeToOpenFlags(QIODevice::OpenMode mode) |
|
165 |
{ |
|
166 |
int oflags = QT_OPEN_RDONLY; |
|
167 |
#ifdef QT_LARGEFILE_SUPPORT |
|
168 |
oflags |= QT_OPEN_LARGEFILE; |
|
169 |
#endif |
|
170 |
||
171 |
if ((mode & QFile::ReadWrite) == QFile::ReadWrite) { |
|
172 |
oflags = QT_OPEN_RDWR | QT_OPEN_CREAT; |
|
173 |
} else if (mode & QFile::WriteOnly) { |
|
174 |
oflags = QT_OPEN_WRONLY | QT_OPEN_CREAT; |
|
175 |
} |
|
176 |
||
177 |
if (mode & QFile::Append) { |
|
178 |
oflags |= QT_OPEN_APPEND; |
|
179 |
} else if (mode & QFile::WriteOnly) { |
|
180 |
if ((mode & QFile::Truncate) || !(mode & QFile::ReadOnly)) |
|
181 |
oflags |= QT_OPEN_TRUNC; |
|
182 |
} |
|
183 |
||
184 |
return oflags; |
|
185 |
} |
|
186 |
||
187 |
/*! |
|
188 |
\internal |
|
189 |
||
190 |
Sets the file descriptor to close on exec. That is, the file |
|
191 |
descriptor is not inherited by child processes. |
|
192 |
*/ |
|
193 |
static inline bool setCloseOnExec(int fd) |
|
194 |
{ |
|
195 |
return fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) != -1; |
|
196 |
} |
|
197 |
||
198 |
/*! |
|
199 |
\internal |
|
200 |
*/ |
|
201 |
void QFSFileEnginePrivate::nativeInitFileName() |
|
202 |
{ |
|
203 |
nativeFilePath = QFile::encodeName(filePath); |
|
204 |
} |
|
205 |
||
206 |
/*! |
|
207 |
\internal |
|
208 |
*/ |
|
209 |
bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode) |
|
210 |
{ |
|
211 |
Q_Q(QFSFileEngine); |
|
212 |
||
213 |
if (openMode & QIODevice::Unbuffered) { |
|
214 |
int flags = openModeToOpenFlags(openMode); |
|
215 |
||
216 |
// Try to open the file in unbuffered mode. |
|
217 |
do { |
|
218 |
fd = QT_OPEN(nativeFilePath.constData(), flags, 0666); |
|
219 |
} while (fd == -1 && errno == EINTR); |
|
220 |
||
221 |
// On failure, return and report the error. |
|
222 |
if (fd == -1) { |
|
223 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
|
224 |
qt_error_string(errno)); |
|
225 |
return false; |
|
226 |
} |
|
227 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
if (!(openMode & QIODevice::WriteOnly)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
// we don't need this check if we tried to open for writing because then |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
// we had received EISDIR anyway. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
QT_STATBUF statBuf; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
if (QT_FSTAT(fd, &statBuf) != -1) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
if ((statBuf.st_mode & S_IFMT) == S_IFDIR) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
q->setError(QFile::OpenError, QLatin1String("file to open is a directory")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
QT_CLOSE(fd); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
} |
0 | 238 |
} |
239 |
} |
|
240 |
||
241 |
// Seek to the end when in Append mode. |
|
242 |
if (flags & QFile::Append) { |
|
243 |
int ret; |
|
244 |
do { |
|
245 |
ret = QT_LSEEK(fd, 0, SEEK_END); |
|
246 |
} while (ret == -1 && errno == EINTR); |
|
247 |
||
248 |
if (ret == -1) { |
|
249 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
|
250 |
qt_error_string(int(errno))); |
|
251 |
return false; |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
fh = 0; |
|
256 |
} else { |
|
257 |
QByteArray fopenMode = openModeToFopenMode(openMode, filePath); |
|
258 |
||
259 |
// Try to open the file in buffered mode. |
|
260 |
do { |
|
261 |
fh = QT_FOPEN(nativeFilePath.constData(), fopenMode.constData()); |
|
262 |
} while (!fh && errno == EINTR); |
|
263 |
||
264 |
// On failure, return and report the error. |
|
265 |
if (!fh) { |
|
266 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
|
267 |
qt_error_string(int(errno))); |
|
268 |
return false; |
|
269 |
} |
|
270 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
271 |
if (!(openMode & QIODevice::WriteOnly)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
// we don't need this check if we tried to open for writing because then |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
// we had received EISDIR anyway. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
QT_STATBUF statBuf; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
if (QT_FSTAT(fileno(fh), &statBuf) != -1) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
if ((statBuf.st_mode & S_IFMT) == S_IFDIR) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
q->setError(QFile::OpenError, QLatin1String("file to open is a directory")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
fclose(fh); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
} |
0 | 281 |
} |
282 |
} |
|
283 |
||
284 |
setCloseOnExec(fileno(fh)); // ignore failure |
|
285 |
||
286 |
// Seek to the end when in Append mode. |
|
287 |
if (openMode & QIODevice::Append) { |
|
288 |
int ret; |
|
289 |
do { |
|
290 |
ret = QT_FSEEK(fh, 0, SEEK_END); |
|
291 |
} while (ret == -1 && errno == EINTR); |
|
292 |
||
293 |
if (ret == -1) { |
|
294 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
|
295 |
qt_error_string(int(errno))); |
|
296 |
return false; |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
fd = -1; |
|
301 |
} |
|
302 |
||
303 |
closeFileHandle = true; |
|
304 |
return true; |
|
305 |
} |
|
306 |
||
307 |
/*! |
|
308 |
\internal |
|
309 |
*/ |
|
310 |
bool QFSFileEnginePrivate::nativeClose() |
|
311 |
{ |
|
312 |
return closeFdFh(); |
|
313 |
} |
|
314 |
||
315 |
/*! |
|
316 |
\internal |
|
317 |
||
318 |
*/ |
|
319 |
bool QFSFileEnginePrivate::nativeFlush() |
|
320 |
{ |
|
321 |
return fh ? flushFh() : fd != -1; |
|
322 |
} |
|
323 |
||
324 |
/*! |
|
325 |
\internal |
|
326 |
*/ |
|
327 |
qint64 QFSFileEnginePrivate::nativeRead(char *data, qint64 len) |
|
328 |
{ |
|
329 |
Q_Q(QFSFileEngine); |
|
330 |
||
331 |
if (fh && nativeIsSequential()) { |
|
332 |
size_t readBytes = 0; |
|
333 |
int oldFlags = fcntl(QT_FILENO(fh), F_GETFL); |
|
334 |
for (int i = 0; i < 2; ++i) { |
|
335 |
// Unix: Make the underlying file descriptor non-blocking |
|
336 |
if ((oldFlags & O_NONBLOCK) == 0) |
|
337 |
fcntl(QT_FILENO(fh), F_SETFL, oldFlags | O_NONBLOCK); |
|
338 |
||
339 |
// Cross platform stdlib read |
|
340 |
size_t read = 0; |
|
341 |
do { |
|
342 |
read = fread(data + readBytes, 1, size_t(len - readBytes), fh); |
|
343 |
} while (read == 0 && !feof(fh) && errno == EINTR); |
|
344 |
if (read > 0) { |
|
345 |
readBytes += read; |
|
346 |
break; |
|
347 |
} else { |
|
348 |
if (readBytes) |
|
349 |
break; |
|
350 |
readBytes = read; |
|
351 |
} |
|
352 |
||
353 |
// Unix: Restore the blocking state of the underlying socket |
|
354 |
if ((oldFlags & O_NONBLOCK) == 0) { |
|
355 |
fcntl(QT_FILENO(fh), F_SETFL, oldFlags); |
|
356 |
if (readBytes == 0) { |
|
357 |
int readByte = 0; |
|
358 |
do { |
|
359 |
readByte = fgetc(fh); |
|
360 |
} while (readByte == -1 && errno == EINTR); |
|
361 |
if (readByte != -1) { |
|
362 |
*data = uchar(readByte); |
|
363 |
readBytes += 1; |
|
364 |
} else { |
|
365 |
break; |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
} |
|
370 |
// Unix: Restore the blocking state of the underlying socket |
|
371 |
if ((oldFlags & O_NONBLOCK) == 0) { |
|
372 |
fcntl(QT_FILENO(fh), F_SETFL, oldFlags); |
|
373 |
} |
|
374 |
if (readBytes == 0 && !feof(fh)) { |
|
375 |
// if we didn't read anything and we're not at EOF, it must be an error |
|
376 |
q->setError(QFile::ReadError, qt_error_string(int(errno))); |
|
377 |
return -1; |
|
378 |
} |
|
379 |
return readBytes; |
|
380 |
} |
|
381 |
||
382 |
return readFdFh(data, len); |
|
383 |
} |
|
384 |
||
385 |
/*! |
|
386 |
\internal |
|
387 |
*/ |
|
388 |
qint64 QFSFileEnginePrivate::nativeReadLine(char *data, qint64 maxlen) |
|
389 |
{ |
|
390 |
return readLineFdFh(data, maxlen); |
|
391 |
} |
|
392 |
||
393 |
/*! |
|
394 |
\internal |
|
395 |
*/ |
|
396 |
qint64 QFSFileEnginePrivate::nativeWrite(const char *data, qint64 len) |
|
397 |
{ |
|
398 |
return writeFdFh(data, len); |
|
399 |
} |
|
400 |
||
401 |
/*! |
|
402 |
\internal |
|
403 |
*/ |
|
404 |
qint64 QFSFileEnginePrivate::nativePos() const |
|
405 |
{ |
|
406 |
return posFdFh(); |
|
407 |
} |
|
408 |
||
409 |
/*! |
|
410 |
\internal |
|
411 |
*/ |
|
412 |
bool QFSFileEnginePrivate::nativeSeek(qint64 pos) |
|
413 |
{ |
|
414 |
return seekFdFh(pos); |
|
415 |
} |
|
416 |
||
417 |
/*! |
|
418 |
\internal |
|
419 |
*/ |
|
420 |
int QFSFileEnginePrivate::nativeHandle() const |
|
421 |
{ |
|
422 |
return fh ? fileno(fh) : fd; |
|
423 |
} |
|
424 |
||
425 |
/*! |
|
426 |
\internal |
|
427 |
*/ |
|
428 |
bool QFSFileEnginePrivate::nativeIsSequential() const |
|
429 |
{ |
|
430 |
return isSequentialFdFh(); |
|
431 |
} |
|
432 |
||
433 |
bool QFSFileEngine::remove() |
|
434 |
{ |
|
435 |
Q_D(QFSFileEngine); |
|
436 |
bool ret = unlink(d->nativeFilePath.constData()) == 0; |
|
437 |
if (!ret) |
|
438 |
setError(QFile::RemoveError, qt_error_string(errno)); |
|
439 |
return ret; |
|
440 |
} |
|
441 |
||
442 |
bool QFSFileEngine::copy(const QString &newName) |
|
443 |
{ |
|
444 |
#if defined(Q_OS_SYMBIAN) |
|
445 |
Q_D(QFSFileEngine); |
|
446 |
RFs rfs = qt_s60GetRFs(); |
|
447 |
CFileMan* fm = NULL; |
|
448 |
QString oldNative(QDir::toNativeSeparators(d->filePath)); |
|
449 |
TPtrC oldPtr(qt_QString2TPtrC(oldNative)); |
|
450 |
QFileInfo fi(newName); |
|
451 |
QString absoluteNewName = fi.absoluteFilePath(); |
|
452 |
QString newNative(QDir::toNativeSeparators(absoluteNewName)); |
|
453 |
TPtrC newPtr(qt_QString2TPtrC(newNative)); |
|
454 |
TRAPD (err, |
|
455 |
fm = CFileMan::NewL(rfs); |
|
456 |
RFile rfile; |
|
457 |
err = rfile.Open(rfs, oldPtr, EFileShareReadersOrWriters); |
|
458 |
if (err == KErrNone) { |
|
459 |
err = fm->Copy(rfile, newPtr); |
|
460 |
rfile.Close(); |
|
461 |
} |
|
462 |
) // End TRAP |
|
463 |
delete fm; |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
464 |
if (err == KErrNone) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
465 |
return true; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
466 |
d->setSymbianError(err, QFile::CopyError, QLatin1String("copy error")); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
467 |
return false; |
0 | 468 |
#else |
469 |
Q_UNUSED(newName); |
|
470 |
// ### Add copy code for Unix here |
|
471 |
setError(QFile::UnspecifiedError, QLatin1String("Not implemented!")); |
|
472 |
return false; |
|
473 |
#endif |
|
474 |
} |
|
475 |
||
476 |
bool QFSFileEngine::rename(const QString &newName) |
|
477 |
{ |
|
478 |
Q_D(QFSFileEngine); |
|
479 |
bool ret = ::rename(d->nativeFilePath.constData(), QFile::encodeName(newName).constData()) == 0; |
|
480 |
if (!ret) |
|
481 |
setError(QFile::RenameError, qt_error_string(errno)); |
|
482 |
return ret; |
|
483 |
} |
|
484 |
||
485 |
bool QFSFileEngine::link(const QString &newName) |
|
486 |
{ |
|
487 |
Q_D(QFSFileEngine); |
|
488 |
bool ret = ::symlink(d->nativeFilePath.constData(), QFile::encodeName(newName).constData()) == 0; |
|
489 |
if (!ret) |
|
490 |
setError(QFile::RenameError, qt_error_string(errno)); |
|
491 |
return ret; |
|
492 |
} |
|
493 |
||
494 |
qint64 QFSFileEnginePrivate::nativeSize() const |
|
495 |
{ |
|
496 |
return sizeFdFh(); |
|
497 |
} |
|
498 |
||
499 |
bool QFSFileEngine::mkdir(const QString &name, bool createParentDirectories) const |
|
500 |
{ |
|
501 |
QString dirName = name; |
|
502 |
if (createParentDirectories) { |
|
503 |
dirName = QDir::cleanPath(dirName); |
|
504 |
#if defined(Q_OS_SYMBIAN) |
|
505 |
dirName = QDir::toNativeSeparators(dirName); |
|
506 |
#endif |
|
507 |
for(int oldslash = -1, slash=0; slash != -1; oldslash = slash) { |
|
508 |
slash = dirName.indexOf(QDir::separator(), oldslash+1); |
|
509 |
if (slash == -1) { |
|
510 |
if (oldslash == dirName.length()) |
|
511 |
break; |
|
512 |
slash = dirName.length(); |
|
513 |
} |
|
514 |
if (slash) { |
|
515 |
QByteArray chunk = QFile::encodeName(dirName.left(slash)); |
|
516 |
QT_STATBUF st; |
|
517 |
if (QT_STAT(chunk, &st) != -1) { |
|
518 |
if ((st.st_mode & S_IFMT) != S_IFDIR) |
|
519 |
return false; |
|
520 |
} else if (QT_MKDIR(chunk, 0777) != 0) { |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
521 |
//QTP: workaround for QT-3141 |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
522 |
if (errno != EEXIST) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
523 |
return false; |
0 | 524 |
} |
525 |
} |
|
526 |
} |
|
527 |
return true; |
|
528 |
} |
|
529 |
#if defined(Q_OS_DARWIN) // Mac X doesn't support trailing /'s |
|
530 |
if (dirName.endsWith(QLatin1Char('/'))) |
|
531 |
dirName.chop(1); |
|
532 |
#endif |
|
533 |
return (QT_MKDIR(QFile::encodeName(dirName), 0777) == 0); |
|
534 |
} |
|
535 |
||
536 |
bool QFSFileEngine::rmdir(const QString &name, bool recurseParentDirectories) const |
|
537 |
{ |
|
538 |
QString dirName = name; |
|
539 |
if (recurseParentDirectories) { |
|
540 |
dirName = QDir::cleanPath(dirName); |
|
541 |
#if defined(Q_OS_SYMBIAN) |
|
542 |
dirName = QDir::toNativeSeparators(dirName); |
|
543 |
#endif |
|
544 |
for(int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) { |
|
545 |
QByteArray chunk = QFile::encodeName(dirName.left(slash)); |
|
546 |
QT_STATBUF st; |
|
547 |
if (QT_STAT(chunk, &st) != -1) { |
|
548 |
if ((st.st_mode & S_IFMT) != S_IFDIR) |
|
549 |
return false; |
|
550 |
if (::rmdir(chunk) != 0) |
|
551 |
return oldslash != 0; |
|
552 |
} else { |
|
553 |
return false; |
|
554 |
} |
|
555 |
slash = dirName.lastIndexOf(QDir::separator(), oldslash-1); |
|
556 |
} |
|
557 |
return true; |
|
558 |
} |
|
559 |
return ::rmdir(QFile::encodeName(dirName)) == 0; |
|
560 |
} |
|
561 |
||
562 |
bool QFSFileEngine::caseSensitive() const |
|
563 |
{ |
|
564 |
#if defined(Q_OS_SYMBIAN) |
|
565 |
return false; |
|
566 |
#else |
|
567 |
return true; |
|
568 |
#endif |
|
569 |
} |
|
570 |
||
571 |
bool QFSFileEngine::setCurrentPath(const QString &path) |
|
572 |
{ |
|
573 |
int r; |
|
574 |
r = QT_CHDIR(QFile::encodeName(path)); |
|
575 |
return r >= 0; |
|
576 |
} |
|
577 |
||
578 |
QString QFSFileEngine::currentPath(const QString &) |
|
579 |
{ |
|
580 |
QString result; |
|
581 |
QT_STATBUF st; |
|
582 |
#if defined(Q_OS_SYMBIAN) |
|
583 |
char nativeCurrentName[PATH_MAX+1]; |
|
584 |
if (::getcwd(nativeCurrentName, PATH_MAX)) |
|
585 |
result = QDir::fromNativeSeparators(QFile::decodeName(QByteArray(nativeCurrentName))); |
|
586 |
if (result.isEmpty()) { |
|
587 |
# if defined(QT_DEBUG) |
|
588 |
qWarning("QFSFileEngine::currentPath: getcwd() failed"); |
|
589 |
# endif |
|
590 |
} else |
|
591 |
#endif |
|
592 |
if (QT_STAT(".", &st) == 0) { |
|
593 |
#if defined(__GLIBC__) && !defined(PATH_MAX) |
|
594 |
char *currentName = ::get_current_dir_name(); |
|
595 |
if (currentName) { |
|
596 |
result = QFile::decodeName(QByteArray(currentName)); |
|
597 |
::free(currentName); |
|
598 |
} |
|
599 |
#elif !defined(Q_OS_SYMBIAN) |
|
600 |
char currentName[PATH_MAX+1]; |
|
601 |
if (::getcwd(currentName, PATH_MAX)) |
|
602 |
result = QFile::decodeName(QByteArray(currentName)); |
|
603 |
# if defined(QT_DEBUG) |
|
604 |
if (result.isNull()) |
|
605 |
qWarning("QFSFileEngine::currentPath: getcwd() failed"); |
|
606 |
# endif |
|
607 |
#endif |
|
608 |
} else { |
|
609 |
#if defined(Q_OS_SYMBIAN) |
|
610 |
// If current dir returned by Open C doesn't exist, |
|
611 |
// try to create it (can happen with application private dirs) |
|
612 |
// Ignore mkdir failures; we want to be consistent with Open C |
|
613 |
// current path regardless. |
|
614 |
QT_MKDIR(QFile::encodeName(QLatin1String(nativeCurrentName)), 0777); |
|
615 |
#else |
|
616 |
# if defined(QT_DEBUG) |
|
617 |
qWarning("QFSFileEngine::currentPath: stat(\".\") failed"); |
|
618 |
# endif |
|
619 |
#endif |
|
620 |
} |
|
621 |
return result; |
|
622 |
} |
|
623 |
||
624 |
QString QFSFileEngine::homePath() |
|
625 |
{ |
|
626 |
#if defined(Q_OS_SYMBIAN) |
|
627 |
QString home = rootPath(); |
|
628 |
#else |
|
629 |
QString home = QFile::decodeName(qgetenv("HOME")); |
|
630 |
if (home.isNull()) |
|
631 |
home = rootPath(); |
|
632 |
#endif |
|
633 |
return home; |
|
634 |
} |
|
635 |
||
636 |
QString QFSFileEngine::rootPath() |
|
637 |
{ |
|
638 |
#if defined(Q_OS_SYMBIAN) |
|
639 |
# ifdef Q_WS_S60 |
|
640 |
TFileName symbianPath = PathInfo::PhoneMemoryRootPath(); |
|
641 |
return QDir::cleanPath(QDir::fromNativeSeparators(qt_TDesC2QString(symbianPath))); |
|
642 |
# else |
|
643 |
# warning No fallback implementation of QFSFileEngine::rootPath() |
|
644 |
return QString(); |
|
645 |
# endif |
|
646 |
#else |
|
647 |
return QLatin1String("/"); |
|
648 |
#endif |
|
649 |
} |
|
650 |
||
651 |
QString QFSFileEngine::tempPath() |
|
652 |
{ |
|
653 |
#if defined(Q_OS_SYMBIAN) |
|
654 |
# ifdef Q_WS_S60 |
|
655 |
TFileName symbianPath = PathInfo::PhoneMemoryRootPath(); |
|
656 |
QString temp = QDir::fromNativeSeparators(qt_TDesC2QString(symbianPath)); |
|
657 |
temp += QLatin1String( "temp/"); |
|
658 |
||
659 |
// Just to verify that folder really exist on hardware |
|
660 |
QT_MKDIR(QFile::encodeName(temp), 0777); |
|
661 |
# else |
|
662 |
# warning No fallback implementation of QFSFileEngine::tempPath() |
|
663 |
QString temp; |
|
664 |
# endif |
|
665 |
#else |
|
666 |
QString temp = QFile::decodeName(qgetenv("TMPDIR")); |
|
667 |
if (temp.isEmpty()) |
|
668 |
temp = QLatin1String("/tmp/"); |
|
669 |
#endif |
|
670 |
return temp; |
|
671 |
} |
|
672 |
||
673 |
QFileInfoList QFSFileEngine::drives() |
|
674 |
{ |
|
675 |
QFileInfoList ret; |
|
676 |
#if defined(Q_OS_SYMBIAN) |
|
677 |
TDriveList driveList; |
|
678 |
RFs rfs = qt_s60GetRFs(); |
|
679 |
TInt err = rfs.DriveList(driveList); |
|
680 |
if (err == KErrNone) { |
|
681 |
char driveName[] = "A:/"; |
|
682 |
||
683 |
for (char i = 0; i < KMaxDrives; i++) { |
|
684 |
if (driveList[i]) { |
|
685 |
driveName[0] = 'A' + i; |
|
686 |
ret.append(QFileInfo(QLatin1String(driveName))); |
|
687 |
} |
|
688 |
} |
|
689 |
} else { |
|
690 |
qWarning("QFSFileEngine::drives: Getting drives failed"); |
|
691 |
} |
|
692 |
#else |
|
693 |
ret.append(QFileInfo(rootPath())); |
|
694 |
#endif |
|
695 |
return ret; |
|
696 |
} |
|
697 |
||
698 |
bool QFSFileEnginePrivate::doStat() const |
|
699 |
{ |
|
700 |
if (!tried_stat) { |
|
701 |
tried_stat = true; |
|
702 |
could_stat = false; |
|
703 |
||
704 |
if (fh && nativeFilePath.isEmpty()) { |
|
705 |
// ### actually covers two cases: d->fh and when the file is not open |
|
706 |
could_stat = (QT_FSTAT(QT_FILENO(fh), &st) == 0); |
|
707 |
} else if (fd == -1) { |
|
708 |
// ### actually covers two cases: d->fh and when the file is not open |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
709 |
#if defined(Q_OS_SYMBIAN) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
710 |
// Optimisation for Symbian where fileFlags() calls both doStat() and isSymlink(), but rarely on real links. |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
711 |
// When the filename is not a link, lstat will return the same info as stat, but this also removes |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
712 |
// any need for a further call to lstat to check if the file is a link. |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
713 |
need_lstat = false; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
714 |
could_stat = (QT_LSTAT(nativeFilePath.constData(), &st) == 0); |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
715 |
is_link = could_stat ? S_ISLNK(st.st_mode) : false; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
716 |
// if it turns out this was a link, we can call stat too. |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
717 |
if (is_link) |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
718 |
#endif |
0 | 719 |
could_stat = (QT_STAT(nativeFilePath.constData(), &st) == 0); |
720 |
} else { |
|
721 |
could_stat = (QT_FSTAT(fd, &st) == 0); |
|
722 |
} |
|
723 |
} |
|
724 |
return could_stat; |
|
725 |
} |
|
726 |
||
727 |
bool QFSFileEnginePrivate::isSymlink() const |
|
728 |
{ |
|
729 |
if (need_lstat) { |
|
730 |
need_lstat = false; |
|
731 |
||
732 |
QT_STATBUF st; // don't clobber our main one |
|
733 |
is_link = (QT_LSTAT(nativeFilePath.constData(), &st) == 0) ? S_ISLNK(st.st_mode) : false; |
|
734 |
} |
|
735 |
return is_link; |
|
736 |
} |
|
737 |
||
738 |
#if defined(Q_OS_SYMBIAN) |
|
739 |
static bool _q_isSymbianHidden(const QString &path, bool isDir) |
|
740 |
{ |
|
741 |
RFs rfs = qt_s60GetRFs(); |
|
742 |
QFileInfo fi(path); |
|
743 |
QString absPath = fi.absoluteFilePath(); |
|
744 |
if (isDir && !absPath.endsWith(QLatin1Char('/'))) |
|
745 |
absPath.append(QLatin1Char('/')); |
|
746 |
QString native(QDir::toNativeSeparators(absPath)); |
|
747 |
TPtrC ptr(qt_QString2TPtrC(native)); |
|
748 |
TUint attributes; |
|
749 |
TInt err = rfs.Att(ptr, attributes); |
|
750 |
return (err == KErrNone && (attributes & KEntryAttHidden)); |
|
751 |
} |
|
752 |
#endif |
|
753 |
||
754 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
755 |
static bool _q_isMacHidden(const QString &path) |
|
756 |
{ |
|
757 |
OSErr err = noErr; |
|
758 |
||
759 |
FSRef fsRef; |
|
760 |
||
761 |
err = FSPathMakeRefWithOptions(reinterpret_cast<const UInt8 *>(QFile::encodeName(QDir::cleanPath(path)).constData()), |
|
762 |
kFSPathMakeRefDoNotFollowLeafSymlink, &fsRef, 0); |
|
763 |
if (err != noErr) |
|
764 |
return false; |
|
765 |
||
766 |
FSCatalogInfo catInfo; |
|
767 |
err = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL); |
|
768 |
if (err != noErr) |
|
769 |
return false; |
|
770 |
||
771 |
FileInfo * const fileInfo = reinterpret_cast<FileInfo*>(&catInfo.finderInfo); |
|
772 |
bool result = (fileInfo->finderFlags & kIsInvisible); |
|
773 |
return result; |
|
774 |
} |
|
775 |
#endif |
|
776 |
||
777 |
/*! |
|
778 |
\reimp |
|
779 |
*/ |
|
780 |
QAbstractFileEngine::FileFlags QFSFileEngine::fileFlags(FileFlags type) const |
|
781 |
{ |
|
782 |
Q_D(const QFSFileEngine); |
|
783 |
// Force a stat, so that we're guaranteed to get up-to-date results |
|
784 |
if (type & Refresh) { |
|
785 |
d->tried_stat = 0; |
|
786 |
d->need_lstat = 1; |
|
787 |
} |
|
788 |
||
789 |
QAbstractFileEngine::FileFlags ret = 0; |
|
790 |
if (type & FlagsMask) |
|
791 |
ret |= LocalDiskFlag; |
|
792 |
bool exists = d->doStat(); |
|
793 |
if (!exists && !d->isSymlink()) |
|
794 |
return ret; |
|
795 |
||
796 |
if (exists && (type & PermsMask)) { |
|
797 |
if (d->st.st_mode & S_IRUSR) |
|
798 |
ret |= ReadOwnerPerm; |
|
799 |
if (d->st.st_mode & S_IWUSR) |
|
800 |
ret |= WriteOwnerPerm; |
|
801 |
if (d->st.st_mode & S_IXUSR) |
|
802 |
ret |= ExeOwnerPerm; |
|
803 |
if (d->st.st_mode & S_IRUSR) |
|
804 |
ret |= ReadUserPerm; |
|
805 |
if (d->st.st_mode & S_IWUSR) |
|
806 |
ret |= WriteUserPerm; |
|
807 |
if (d->st.st_mode & S_IXUSR) |
|
808 |
ret |= ExeUserPerm; |
|
809 |
if (d->st.st_mode & S_IRGRP) |
|
810 |
ret |= ReadGroupPerm; |
|
811 |
if (d->st.st_mode & S_IWGRP) |
|
812 |
ret |= WriteGroupPerm; |
|
813 |
if (d->st.st_mode & S_IXGRP) |
|
814 |
ret |= ExeGroupPerm; |
|
815 |
if (d->st.st_mode & S_IROTH) |
|
816 |
ret |= ReadOtherPerm; |
|
817 |
if (d->st.st_mode & S_IWOTH) |
|
818 |
ret |= WriteOtherPerm; |
|
819 |
if (d->st.st_mode & S_IXOTH) |
|
820 |
ret |= ExeOtherPerm; |
|
821 |
} |
|
822 |
if (type & TypesMask) { |
|
823 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
824 |
bool foundAlias = false; |
|
825 |
{ |
|
826 |
FSRef fref; |
|
827 |
if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(), |
|
828 |
&fref, NULL) == noErr) { |
|
829 |
Boolean isAlias, isFolder; |
|
830 |
if (FSIsAliasFile(&fref, &isAlias, &isFolder) == noErr && isAlias) { |
|
831 |
foundAlias = true; |
|
832 |
ret |= LinkType; |
|
833 |
} |
|
834 |
} |
|
835 |
} |
|
836 |
if (!foundAlias) |
|
837 |
#endif |
|
838 |
{ |
|
839 |
if ((type & LinkType) && d->isSymlink()) |
|
840 |
ret |= LinkType; |
|
841 |
if (exists && (d->st.st_mode & S_IFMT) == S_IFREG) |
|
842 |
ret |= FileType; |
|
843 |
else if (exists && (d->st.st_mode & S_IFMT) == S_IFDIR) |
|
844 |
ret |= DirectoryType; |
|
845 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
846 |
if ((ret & DirectoryType) && (type & BundleType)) { |
|
847 |
QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath), |
|
848 |
kCFURLPOSIXPathStyle, true); |
|
849 |
UInt32 type, creator; |
|
850 |
if (CFBundleGetPackageInfoInDirectory(url, &type, &creator)) |
|
851 |
ret |= BundleType; |
|
852 |
} |
|
853 |
#endif |
|
854 |
} |
|
855 |
} |
|
856 |
if (type & FlagsMask) { |
|
857 |
if (exists) |
|
858 |
ret |= ExistsFlag; |
|
859 |
#if defined(Q_OS_SYMBIAN) |
|
860 |
if (d->filePath == QLatin1String("/") |
|
861 |
|| (d->filePath.length() == 3 && d->filePath.at(0).isLetter() |
|
862 |
&& d->filePath.at(1) == QLatin1Char(':') && d->filePath.at(2) == QLatin1Char('/'))) { |
|
863 |
ret |= RootFlag; |
|
864 |
} else { |
|
865 |
// In Symbian, all symlinks have hidden attribute for some reason; |
|
866 |
// lets make them visible for better compatibility with other platforms. |
|
867 |
// If somebody actually wants a hidden link, then they are out of luck. |
|
868 |
if (!d->isSymlink() && _q_isSymbianHidden(d->filePath, ret & DirectoryType)) |
|
869 |
ret |= HiddenFlag; |
|
870 |
} |
|
871 |
#else |
|
872 |
if (d->filePath == QLatin1String("/")) { |
|
873 |
ret |= RootFlag; |
|
874 |
} else { |
|
875 |
QString baseName = fileName(BaseName); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
876 |
if ((baseName.size() > 0 && baseName.at(0) == QLatin1Char('.')) |
0 | 877 |
# if !defined(QWS) && defined(Q_OS_MAC) |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
878 |
|| _q_isMacHidden(d->filePath) |
0 | 879 |
# endif |
880 |
) { |
|
881 |
ret |= HiddenFlag; |
|
882 |
} |
|
883 |
} |
|
884 |
#endif |
|
885 |
} |
|
886 |
return ret; |
|
887 |
} |
|
888 |
||
889 |
#if defined(Q_OS_SYMBIAN) |
|
890 |
QString QFSFileEngine::fileName(FileName file) const |
|
891 |
{ |
|
892 |
Q_D(const QFSFileEngine); |
|
893 |
const QLatin1Char slashChar('/'); |
|
894 |
if(file == BaseName) { |
|
895 |
int slash = d->filePath.lastIndexOf(slashChar); |
|
896 |
if(slash == -1) { |
|
897 |
int colon = d->filePath.lastIndexOf(QLatin1Char(':')); |
|
898 |
if(colon != -1) |
|
899 |
return d->filePath.mid(colon + 1); |
|
900 |
return d->filePath; |
|
901 |
} |
|
902 |
return d->filePath.mid(slash + 1); |
|
903 |
} else if(file == PathName) { |
|
904 |
if(!d->filePath.size()) |
|
905 |
return d->filePath; |
|
906 |
||
907 |
int slash = d->filePath.lastIndexOf(slashChar); |
|
908 |
if(slash == -1) { |
|
909 |
if(d->filePath.length() >= 2 && d->filePath.at(1) == QLatin1Char(':')) |
|
910 |
return d->filePath.left(2); |
|
911 |
return QLatin1String("."); |
|
912 |
} else { |
|
913 |
if(!slash) |
|
914 |
return QLatin1String("/"); |
|
915 |
if(slash == 2 && d->filePath.length() >= 2 && d->filePath.at(1) == QLatin1Char(':')) |
|
916 |
slash++; |
|
917 |
return d->filePath.left(slash); |
|
918 |
} |
|
919 |
} else if(file == AbsoluteName || file == AbsolutePathName) { |
|
920 |
QString ret; |
|
921 |
if (!isRelativePathSymbian(d->filePath)) { |
|
922 |
if (d->filePath.size() > 2 && d->filePath.at(1) == QLatin1Char(':') |
|
923 |
&& d->filePath.at(2) != slashChar){ |
|
924 |
// It's a drive-relative path, so C:a.txt -> C:/currentpath/a.txt, |
|
925 |
// or if it's different drive than current, Z:a.txt -> Z:/a.txt |
|
926 |
QString currentPath = QDir::currentPath(); |
|
927 |
if (0 == currentPath.left(1).compare(d->filePath.left(1), Qt::CaseInsensitive)) |
|
928 |
ret = currentPath + slashChar + d->filePath.mid(2); |
|
929 |
else |
|
930 |
ret = d->filePath.left(2) + slashChar + d->filePath.mid(2); |
|
931 |
} else if (d->filePath.startsWith(slashChar)) { |
|
932 |
// It's a absolute path to the current drive, so /a.txt -> C:/a.txt |
|
933 |
ret = QDir::currentPath().left(2) + d->filePath; |
|
934 |
} else { |
|
935 |
ret = d->filePath; |
|
936 |
} |
|
937 |
} else { |
|
938 |
ret = QDir::currentPath() + slashChar + d->filePath; |
|
939 |
} |
|
940 |
||
941 |
// The path should be absolute at this point. |
|
942 |
// From the docs : |
|
943 |
// Absolute paths begin with the directory separator "/" |
|
944 |
// (optionally preceded by a drive specification under Windows). |
|
945 |
if (ret.at(0) != slashChar) { |
|
946 |
Q_ASSERT(ret.length() >= 2); |
|
947 |
Q_ASSERT(ret.at(0).isLetter()); |
|
948 |
Q_ASSERT(ret.at(1) == QLatin1Char(':')); |
|
949 |
||
950 |
// Force uppercase drive letters. |
|
951 |
ret[0] = ret.at(0).toUpper(); |
|
952 |
} |
|
953 |
||
954 |
// Clean up the path |
|
955 |
bool isDir = ret.endsWith(slashChar); |
|
956 |
ret = QDir::cleanPath(ret); |
|
957 |
if (isDir && !ret.endsWith(slashChar)) |
|
958 |
ret += slashChar; |
|
959 |
||
960 |
if (file == AbsolutePathName) { |
|
961 |
int slash = ret.lastIndexOf(slashChar); |
|
962 |
if (slash < 0) |
|
963 |
return ret; |
|
964 |
else if (ret.at(0) != slashChar && slash == 2) |
|
965 |
return ret.left(3); // include the slash |
|
966 |
else |
|
967 |
return ret.left(slash > 0 ? slash : 1); |
|
968 |
} |
|
969 |
return ret; |
|
970 |
} else if(file == CanonicalName || file == CanonicalPathName) { |
|
971 |
if (!(fileFlags(ExistsFlag) & ExistsFlag)) |
|
972 |
return QString(); |
|
973 |
||
974 |
QString ret = QFSFileEnginePrivate::canonicalized(fileName(AbsoluteName)); |
|
975 |
if (file == CanonicalPathName && !ret.isEmpty()) { |
|
976 |
int slash = ret.lastIndexOf(slashChar); |
|
977 |
if (slash == -1) |
|
978 |
ret = QDir::fromNativeSeparators(QDir::currentPath()); |
|
979 |
else if (slash == 0) |
|
980 |
ret = QLatin1String("/"); |
|
981 |
ret = ret.left(slash); |
|
982 |
} |
|
983 |
return ret; |
|
984 |
} else if(file == LinkName) { |
|
985 |
if (d->isSymlink()) { |
|
986 |
char s[PATH_MAX+1]; |
|
987 |
int len = readlink(d->nativeFilePath.constData(), s, PATH_MAX); |
|
988 |
if (len > 0) { |
|
989 |
s[len] = '\0'; |
|
990 |
QString ret = QFile::decodeName(QByteArray(s)); |
|
991 |
||
992 |
if (isRelativePathSymbian(ret)) { |
|
993 |
if (!isRelativePathSymbian(d->filePath)) { |
|
994 |
ret.prepend(d->filePath.left(d->filePath.lastIndexOf(slashChar)) |
|
995 |
+ slashChar); |
|
996 |
} else { |
|
997 |
ret.prepend(QDir::currentPath() + slashChar); |
|
998 |
} |
|
999 |
} |
|
1000 |
ret = QDir::cleanPath(ret); |
|
1001 |
if (ret.size() > 1 && ret.endsWith(slashChar)) |
|
1002 |
ret.chop(1); |
|
1003 |
return ret; |
|
1004 |
} |
|
1005 |
} |
|
1006 |
return QString(); |
|
1007 |
} else if(file == BundleName) { |
|
1008 |
return QString(); |
|
1009 |
} |
|
1010 |
return d->filePath; |
|
1011 |
} |
|
1012 |
||
1013 |
#else |
|
1014 |
||
1015 |
QString QFSFileEngine::fileName(FileName file) const |
|
1016 |
{ |
|
1017 |
Q_D(const QFSFileEngine); |
|
1018 |
if (file == BundleName) { |
|
1019 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
1020 |
QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath), |
|
1021 |
kCFURLPOSIXPathStyle, true); |
|
1022 |
if (CFDictionaryRef dict = CFBundleCopyInfoDictionaryForURL(url)) { |
|
1023 |
if (CFTypeRef name = (CFTypeRef)CFDictionaryGetValue(dict, kCFBundleNameKey)) { |
|
1024 |
if (CFGetTypeID(name) == CFStringGetTypeID()) |
|
1025 |
return QCFString::toQString((CFStringRef)name); |
|
1026 |
} |
|
1027 |
} |
|
1028 |
#endif |
|
1029 |
return QString(); |
|
1030 |
} else if (file == BaseName) { |
|
1031 |
int slash = d->filePath.lastIndexOf(QLatin1Char('/')); |
|
1032 |
if (slash != -1) |
|
1033 |
return d->filePath.mid(slash + 1); |
|
1034 |
} else if (file == PathName) { |
|
1035 |
int slash = d->filePath.lastIndexOf(QLatin1Char('/')); |
|
1036 |
if (slash == -1) |
|
1037 |
return QLatin1String("."); |
|
1038 |
else if (!slash) |
|
1039 |
return QLatin1String("/"); |
|
1040 |
return d->filePath.left(slash); |
|
1041 |
} else if (file == AbsoluteName || file == AbsolutePathName) { |
|
1042 |
QString ret; |
|
1043 |
if (d->filePath.isEmpty() || !d->filePath.startsWith(QLatin1Char('/'))) |
|
1044 |
ret = QDir::currentPath(); |
|
1045 |
if (!d->filePath.isEmpty() && d->filePath != QLatin1String(".")) { |
|
1046 |
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) |
|
1047 |
ret += QLatin1Char('/'); |
|
1048 |
ret += d->filePath; |
|
1049 |
} |
|
1050 |
if (ret == QLatin1String("/")) |
|
1051 |
return ret; |
|
1052 |
bool isDir = ret.endsWith(QLatin1Char('/')); |
|
1053 |
ret = QDir::cleanPath(ret); |
|
1054 |
if (isDir) |
|
1055 |
ret += QLatin1Char('/'); |
|
1056 |
if (file == AbsolutePathName) { |
|
1057 |
int slash = ret.lastIndexOf(QLatin1Char('/')); |
|
1058 |
if (slash == -1) |
|
1059 |
return QDir::currentPath(); |
|
1060 |
else if (!slash) |
|
1061 |
return QLatin1String("/"); |
|
1062 |
return ret.left(slash); |
|
1063 |
} |
|
1064 |
return ret; |
|
1065 |
} else if (file == CanonicalName || file == CanonicalPathName) { |
|
1066 |
if (!(fileFlags(ExistsFlag) & ExistsFlag)) |
|
1067 |
return QString(); |
|
1068 |
||
1069 |
QString ret = QFSFileEnginePrivate::canonicalized(fileName(AbsoluteName)); |
|
1070 |
if (file == CanonicalPathName && !ret.isEmpty()) { |
|
1071 |
int slash = ret.lastIndexOf(QLatin1Char('/')); |
|
1072 |
if (slash == -1) |
|
1073 |
ret = QDir::currentPath(); |
|
1074 |
else if (slash == 0) |
|
1075 |
ret = QLatin1String("/"); |
|
1076 |
ret = ret.left(slash); |
|
1077 |
} |
|
1078 |
return ret; |
|
1079 |
} else if (file == LinkName) { |
|
1080 |
if (d->isSymlink()) { |
|
1081 |
#if defined(__GLIBC__) && !defined(PATH_MAX) |
|
1082 |
#define PATH_CHUNK_SIZE 256 |
|
1083 |
char *s = 0; |
|
1084 |
int len = -1; |
|
1085 |
int size = PATH_CHUNK_SIZE; |
|
1086 |
||
1087 |
while (1) { |
|
1088 |
s = q_check_ptr((char *) ::realloc(s, size)); |
|
1089 |
len = ::readlink(d->nativeFilePath.constData(), s, size); |
|
1090 |
if (len < 0) { |
|
1091 |
::free(s); |
|
1092 |
break; |
|
1093 |
} |
|
1094 |
if (len < size) { |
|
1095 |
break; |
|
1096 |
} |
|
1097 |
size *= 2; |
|
1098 |
} |
|
1099 |
#else |
|
1100 |
char s[PATH_MAX+1]; |
|
1101 |
int len = readlink(d->nativeFilePath.constData(), s, PATH_MAX); |
|
1102 |
#endif |
|
1103 |
if (len > 0) { |
|
1104 |
QString ret; |
|
1105 |
if (d->doStat() && S_ISDIR(d->st.st_mode) && s[0] != '/') { |
|
1106 |
QDir parent(d->filePath); |
|
1107 |
parent.cdUp(); |
|
1108 |
ret = parent.path(); |
|
1109 |
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) |
|
1110 |
ret += QLatin1Char('/'); |
|
1111 |
} |
|
1112 |
s[len] = '\0'; |
|
1113 |
ret += QFile::decodeName(QByteArray(s)); |
|
1114 |
#if defined(__GLIBC__) && !defined(PATH_MAX) |
|
1115 |
::free(s); |
|
1116 |
#endif |
|
1117 |
||
1118 |
if (!ret.startsWith(QLatin1Char('/'))) { |
|
1119 |
if (d->filePath.startsWith(QLatin1Char('/'))) { |
|
1120 |
ret.prepend(d->filePath.left(d->filePath.lastIndexOf(QLatin1Char('/'))) |
|
1121 |
+ QLatin1Char('/')); |
|
1122 |
} else { |
|
1123 |
ret.prepend(QDir::currentPath() + QLatin1Char('/')); |
|
1124 |
} |
|
1125 |
} |
|
1126 |
ret = QDir::cleanPath(ret); |
|
1127 |
if (ret.size() > 1 && ret.endsWith(QLatin1Char('/'))) |
|
1128 |
ret.chop(1); |
|
1129 |
return ret; |
|
1130 |
} |
|
1131 |
} |
|
1132 |
#if !defined(QWS) && defined(Q_OS_MAC) |
|
1133 |
{ |
|
1134 |
FSRef fref; |
|
1135 |
if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(), &fref, 0) == noErr) { |
|
1136 |
Boolean isAlias, isFolder; |
|
1137 |
if (FSResolveAliasFile(&fref, true, &isFolder, &isAlias) == noErr && isAlias) { |
|
1138 |
AliasHandle alias; |
|
1139 |
if (FSNewAlias(0, &fref, &alias) == noErr && alias) { |
|
1140 |
CFStringRef cfstr; |
|
1141 |
if (FSCopyAliasInfo(alias, 0, 0, &cfstr, 0, 0) == noErr) |
|
1142 |
return QCFString::toQString(cfstr); |
|
1143 |
} |
|
1144 |
} |
|
1145 |
} |
|
1146 |
} |
|
1147 |
#endif |
|
1148 |
return QString(); |
|
1149 |
} |
|
1150 |
return d->filePath; |
|
1151 |
} |
|
1152 |
#endif // Q_OS_SYMBIAN |
|
1153 |
||
1154 |
bool QFSFileEngine::isRelativePath() const |
|
1155 |
{ |
|
1156 |
Q_D(const QFSFileEngine); |
|
1157 |
#if defined(Q_OS_SYMBIAN) |
|
1158 |
return isRelativePathSymbian(d->filePath); |
|
1159 |
#else |
|
1160 |
return d->filePath.length() ? d->filePath[0] != QLatin1Char('/') : true; |
|
1161 |
#endif |
|
1162 |
} |
|
1163 |
||
1164 |
uint QFSFileEngine::ownerId(FileOwner own) const |
|
1165 |
{ |
|
1166 |
Q_D(const QFSFileEngine); |
|
1167 |
static const uint nobodyID = (uint) -2; |
|
1168 |
if (d->doStat()) { |
|
1169 |
if (own == OwnerUser) |
|
1170 |
return d->st.st_uid; |
|
1171 |
else |
|
1172 |
return d->st.st_gid; |
|
1173 |
} |
|
1174 |
return nobodyID; |
|
1175 |
} |
|
1176 |
||
1177 |
QString QFSFileEngine::owner(FileOwner own) const |
|
1178 |
{ |
|
1179 |
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) |
|
1180 |
int size_max = sysconf(_SC_GETPW_R_SIZE_MAX); |
|
1181 |
if (size_max == -1) |
|
1182 |
size_max = 1024; |
|
1183 |
QVarLengthArray<char, 1024> buf(size_max); |
|
1184 |
#endif |
|
1185 |
||
1186 |
if (own == OwnerUser) { |
|
1187 |
struct passwd *pw = 0; |
|
1188 |
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) |
|
1189 |
struct passwd entry; |
|
1190 |
getpwuid_r(ownerId(own), &entry, buf.data(), buf.size(), &pw); |
|
1191 |
#else |
|
1192 |
pw = getpwuid(ownerId(own)); |
|
1193 |
#endif |
|
1194 |
if (pw) |
|
1195 |
return QFile::decodeName(QByteArray(pw->pw_name)); |
|
1196 |
} else if (own == OwnerGroup) { |
|
1197 |
#if !defined(Q_OS_SYMBIAN) |
|
1198 |
struct group *gr = 0; |
|
1199 |
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) |
|
1200 |
size_max = sysconf(_SC_GETGR_R_SIZE_MAX); |
|
1201 |
if (size_max == -1) |
|
1202 |
size_max = 1024; |
|
1203 |
buf.resize(size_max); |
|
1204 |
struct group entry; |
|
1205 |
// Some large systems have more members than the POSIX max size |
|
1206 |
// Loop over by doubling the buffer size (upper limit 250k) |
|
1207 |
for (unsigned size = size_max; size < 256000; size += size) |
|
1208 |
{ |
|
1209 |
buf.resize(size); |
|
1210 |
// ERANGE indicates that the buffer was too small |
|
1211 |
if (!getgrgid_r(ownerId(own), &entry, buf.data(), buf.size(), &gr) |
|
1212 |
|| errno != ERANGE) |
|
1213 |
break; |
|
1214 |
} |
|
1215 |
#else |
|
1216 |
gr = getgrgid(ownerId(own)); |
|
1217 |
#endif |
|
1218 |
if (gr) |
|
1219 |
return QFile::decodeName(QByteArray(gr->gr_name)); |
|
1220 |
#endif |
|
1221 |
} |
|
1222 |
return QString(); |
|
1223 |
} |
|
1224 |
||
1225 |
bool QFSFileEngine::setPermissions(uint perms) |
|
1226 |
{ |
|
1227 |
Q_D(QFSFileEngine); |
|
1228 |
bool ret = false; |
|
1229 |
mode_t mode = 0; |
|
1230 |
if (perms & ReadOwnerPerm) |
|
1231 |
mode |= S_IRUSR; |
|
1232 |
if (perms & WriteOwnerPerm) |
|
1233 |
mode |= S_IWUSR; |
|
1234 |
if (perms & ExeOwnerPerm) |
|
1235 |
mode |= S_IXUSR; |
|
1236 |
if (perms & ReadUserPerm) |
|
1237 |
mode |= S_IRUSR; |
|
1238 |
if (perms & WriteUserPerm) |
|
1239 |
mode |= S_IWUSR; |
|
1240 |
if (perms & ExeUserPerm) |
|
1241 |
mode |= S_IXUSR; |
|
1242 |
if (perms & ReadGroupPerm) |
|
1243 |
mode |= S_IRGRP; |
|
1244 |
if (perms & WriteGroupPerm) |
|
1245 |
mode |= S_IWGRP; |
|
1246 |
if (perms & ExeGroupPerm) |
|
1247 |
mode |= S_IXGRP; |
|
1248 |
if (perms & ReadOtherPerm) |
|
1249 |
mode |= S_IROTH; |
|
1250 |
if (perms & WriteOtherPerm) |
|
1251 |
mode |= S_IWOTH; |
|
1252 |
if (perms & ExeOtherPerm) |
|
1253 |
mode |= S_IXOTH; |
|
1254 |
if (d->fd != -1) |
|
1255 |
ret = fchmod(d->fd, mode) == 0; |
|
1256 |
else |
|
1257 |
ret = ::chmod(d->nativeFilePath.constData(), mode) == 0; |
|
1258 |
if (!ret) |
|
1259 |
setError(QFile::PermissionsError, qt_error_string(errno)); |
|
1260 |
return ret; |
|
1261 |
} |
|
1262 |
||
1263 |
bool QFSFileEngine::setSize(qint64 size) |
|
1264 |
{ |
|
1265 |
Q_D(QFSFileEngine); |
|
1266 |
bool ret = false; |
|
1267 |
if (d->fd != -1) |
|
1268 |
ret = QT_FTRUNCATE(d->fd, size) == 0; |
|
1269 |
else if (d->fh) |
|
1270 |
ret = QT_FTRUNCATE(QT_FILENO(d->fh), size) == 0; |
|
1271 |
else |
|
1272 |
ret = QT_TRUNCATE(d->nativeFilePath.constData(), size) == 0; |
|
1273 |
if (!ret) |
|
1274 |
setError(QFile::ResizeError, qt_error_string(errno)); |
|
1275 |
return ret; |
|
1276 |
} |
|
1277 |
||
1278 |
QDateTime QFSFileEngine::fileTime(FileTime time) const |
|
1279 |
{ |
|
1280 |
Q_D(const QFSFileEngine); |
|
1281 |
QDateTime ret; |
|
1282 |
if (d->doStat()) { |
|
1283 |
if (time == CreationTime) |
|
1284 |
ret.setTime_t(d->st.st_ctime ? d->st.st_ctime : d->st.st_mtime); |
|
1285 |
else if (time == ModificationTime) |
|
1286 |
ret.setTime_t(d->st.st_mtime); |
|
1287 |
else if (time == AccessTime) |
|
1288 |
ret.setTime_t(d->st.st_atime); |
|
1289 |
} |
|
1290 |
return ret; |
|
1291 |
} |
|
1292 |
||
1293 |
uchar *QFSFileEnginePrivate::map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags) |
|
1294 |
{ |
|
1295 |
Q_Q(QFSFileEngine); |
|
1296 |
Q_UNUSED(flags); |
|
1297 |
if (openMode == QIODevice::NotOpen) { |
|
1298 |
q->setError(QFile::PermissionsError, qt_error_string(int(EACCES))); |
|
1299 |
return 0; |
|
1300 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1301 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1302 |
if (offset < 0 || offset != qint64(QT_OFF_T(offset)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1303 |
|| size < 0 || quint64(size) > quint64(size_t(-1))) { |
0 | 1304 |
q->setError(QFile::UnspecifiedError, qt_error_string(int(EINVAL))); |
1305 |
return 0; |
|
1306 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1307 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1308 |
// If we know the mapping will extend beyond EOF, fail early to avoid |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1309 |
// undefined behavior. Otherwise, let mmap have its say. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1310 |
if (doStat() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1311 |
&& (QT_OFF_T(size) > st.st_size - QT_OFF_T(offset))) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1312 |
qWarning("QFSFileEngine::map: Mapping a file beyond its size is not portable"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1313 |
|
0 | 1314 |
int access = 0; |
1315 |
if (openMode & QIODevice::ReadOnly) access |= PROT_READ; |
|
1316 |
if (openMode & QIODevice::WriteOnly) access |= PROT_WRITE; |
|
1317 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1318 |
int pageSize = getpagesize(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1319 |
int extra = offset % pageSize; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1320 |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1321 |
if (quint64(size + extra) > quint64((size_t)-1)) { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1322 |
q->setError(QFile::UnspecifiedError, qt_error_string(int(EINVAL))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1323 |
return 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1324 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1325 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1326 |
size_t realSize = (size_t)size + extra; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1327 |
QT_OFF_T realOffset = QT_OFF_T(offset); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1328 |
realOffset &= ~(QT_OFF_T(pageSize - 1)); |
0 | 1329 |
|
1330 |
#ifdef Q_OS_SYMBIAN |
|
1331 |
void *mapAddress; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1332 |
TRAPD(err, mapAddress = QT_MMAP((void*)0, realSize, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1333 |
access, MAP_SHARED, nativeHandle(), realOffset)); |
0 | 1334 |
if (err != KErrNone) { |
1335 |
qWarning("OpenC bug: leave from mmap %d", err); |
|
1336 |
mapAddress = MAP_FAILED; |
|
1337 |
errno = EINVAL; |
|
1338 |
} |
|
1339 |
#else |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1340 |
void *mapAddress = QT_MMAP((void*)0, realSize, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1341 |
access, MAP_SHARED, nativeHandle(), realOffset); |
0 | 1342 |
#endif |
1343 |
if (MAP_FAILED != mapAddress) { |
|
1344 |
uchar *address = extra + static_cast<uchar*>(mapAddress); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1345 |
maps[address] = QPair<int,size_t>(extra, realSize); |
0 | 1346 |
return address; |
1347 |
} |
|
1348 |
||
1349 |
switch(errno) { |
|
1350 |
case EBADF: |
|
1351 |
q->setError(QFile::PermissionsError, qt_error_string(int(EACCES))); |
|
1352 |
break; |
|
1353 |
case ENFILE: |
|
1354 |
case ENOMEM: |
|
1355 |
q->setError(QFile::ResourceError, qt_error_string(int(errno))); |
|
1356 |
break; |
|
1357 |
case EINVAL: |
|
1358 |
// size are out of bounds |
|
1359 |
default: |
|
1360 |
q->setError(QFile::UnspecifiedError, qt_error_string(int(errno))); |
|
1361 |
break; |
|
1362 |
} |
|
1363 |
return 0; |
|
1364 |
} |
|
1365 |
||
1366 |
bool QFSFileEnginePrivate::unmap(uchar *ptr) |
|
1367 |
{ |
|
1368 |
Q_Q(QFSFileEngine); |
|
1369 |
if (!maps.contains(ptr)) { |
|
1370 |
q->setError(QFile::PermissionsError, qt_error_string(EACCES)); |
|
1371 |
return false; |
|
1372 |
} |
|
1373 |
||
1374 |
uchar *start = ptr - maps[ptr].first; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1375 |
size_t len = maps[ptr].second; |
0 | 1376 |
if (-1 == munmap(start, len)) { |
1377 |
q->setError(QFile::UnspecifiedError, qt_error_string(errno)); |
|
1378 |
return false; |
|
1379 |
} |
|
1380 |
maps.remove(ptr); |
|
1381 |
return true; |
|
1382 |
} |
|
1383 |
||
1384 |
QT_END_NAMESPACE |
|
1385 |
||
1386 |
#endif // QT_NO_FSFILEENGINE |