author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 "qfsfileengine_p.h" |
|
43 |
#include "qfsfileengine_iterator_p.h" |
|
44 |
#include "qdatetime.h" |
|
45 |
#include "qdiriterator.h" |
|
46 |
#include "qset.h" |
|
47 |
#include <QtCore/qdebug.h> |
|
48 |
||
49 |
#ifndef QT_NO_FSFILEENGINE |
|
50 |
||
51 |
#if !defined(Q_OS_WINCE) |
|
52 |
#include <errno.h> |
|
53 |
#endif |
|
54 |
#if defined(Q_OS_UNIX) |
|
55 |
#include "private/qcore_unix_p.h" |
|
56 |
#endif |
|
57 |
#include <stdio.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
#include <stdlib.h> |
0 | 59 |
|
60 |
QT_BEGIN_NAMESPACE |
|
61 |
||
62 |
#ifdef Q_OS_WIN |
|
63 |
# ifndef S_ISREG |
|
64 |
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
|
65 |
# endif |
|
66 |
# ifndef S_ISCHR |
|
67 |
# define S_ISCHR(x) (((x) & S_IFMT) == S_IFCHR) |
|
68 |
# endif |
|
69 |
# ifndef S_ISFIFO |
|
70 |
# define S_ISFIFO(x) false |
|
71 |
# endif |
|
72 |
# ifndef S_ISSOCK |
|
73 |
# define S_ISSOCK(x) false |
|
74 |
# endif |
|
75 |
# ifndef INVALID_FILE_ATTRIBUTES |
|
76 |
# define INVALID_FILE_ATTRIBUTES (DWORD (-1)) |
|
77 |
# endif |
|
78 |
#endif |
|
79 |
||
80 |
/*! \class QFSFileEngine |
|
81 |
\brief The QFSFileEngine class implements Qt's default file engine. |
|
82 |
\since 4.1 |
|
83 |
||
84 |
This class is part of the file engine framework in Qt. If you only want to |
|
85 |
access files or directories, use QFile, QFileInfo or QDir instead. |
|
86 |
||
87 |
QFSFileEngine is the default file engine for accessing regular files. It |
|
88 |
is provided for convenience; by subclassing this class, you can alter its |
|
89 |
behavior slightly, without having to write a complete QAbstractFileEngine |
|
90 |
subclass. To install your custom file engine, you must also subclass |
|
91 |
QAbstractFileEngineHandler and create an instance of your handler. |
|
92 |
||
93 |
It can also be useful to create a QFSFileEngine object directly if you |
|
94 |
need to use the local file system inside QAbstractFileEngine::create(), in |
|
95 |
order to avoid recursion (as higher-level classes tend to call |
|
96 |
QAbstractFileEngine::create()). |
|
97 |
*/ |
|
98 |
||
99 |
//**************** QFSFileEnginePrivate |
|
100 |
QFSFileEnginePrivate::QFSFileEnginePrivate() : QAbstractFileEnginePrivate() |
|
101 |
{ |
|
102 |
init(); |
|
103 |
} |
|
104 |
||
105 |
/*! |
|
106 |
\internal |
|
107 |
*/ |
|
108 |
void QFSFileEnginePrivate::init() |
|
109 |
{ |
|
110 |
is_sequential = 0; |
|
111 |
tried_stat = 0; |
|
112 |
#if !defined(Q_OS_WINCE) |
|
113 |
need_lstat = 1; |
|
114 |
is_link = 0; |
|
115 |
#endif |
|
116 |
openMode = QIODevice::NotOpen; |
|
117 |
fd = -1; |
|
118 |
fh = 0; |
|
119 |
lastIOCommand = IOFlushCommand; |
|
120 |
lastFlushFailed = false; |
|
121 |
closeFileHandle = false; |
|
122 |
#ifdef Q_OS_WIN |
|
123 |
fileAttrib = INVALID_FILE_ATTRIBUTES; |
|
124 |
fileHandle = INVALID_HANDLE_VALUE; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
mapHandle = INVALID_HANDLE_VALUE; |
0 | 126 |
cachedFd = -1; |
127 |
#endif |
|
128 |
} |
|
129 |
||
130 |
/*! |
|
131 |
\internal |
|
132 |
||
133 |
Returns the canonicalized form of \a path (i.e., with all symlinks |
|
134 |
resolved, and all redundant path elements removed. |
|
135 |
*/ |
|
136 |
QString QFSFileEnginePrivate::canonicalized(const QString &path) |
|
137 |
{ |
|
138 |
if (path.isEmpty()) |
|
139 |
return path; |
|
140 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
// FIXME let's see if this stuff works, then we might be able to remove some of the other code. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
if (path.size() == 1 && path.at(0) == QLatin1Char('/')) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
return path; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
// Mac OS X 10.5.x doesn't support the realpath(X,0) extenstion we use here. |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
147 |
#if defined(Q_OS_LINUX) || defined(Q_OS_SYMBIAN) |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
148 |
// ... but Linux with uClibc does not have it |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
149 |
#if !defined(__UCLIBC__) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
char *ret = realpath(path.toLocal8Bit().constData(), (char*)0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
if (ret) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
QString canonicalPath = QDir::cleanPath(QString::fromLocal8Bit(ret)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
free(ret); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
return canonicalPath; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
#endif |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
157 |
#endif |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
|
0 | 159 |
QFileInfo fi; |
160 |
const QChar slash(QLatin1Char('/')); |
|
161 |
QString tmpPath = path; |
|
162 |
int separatorPos = 0; |
|
163 |
QSet<QString> nonSymlinks; |
|
164 |
QSet<QString> known; |
|
165 |
||
166 |
known.insert(path); |
|
167 |
do { |
|
168 |
#ifdef Q_OS_WIN |
|
169 |
// UNC, skip past the first two elements |
|
170 |
if (separatorPos == 0 && tmpPath.startsWith(QLatin1String("//"))) |
|
171 |
separatorPos = tmpPath.indexOf(slash, 2); |
|
172 |
if (separatorPos != -1) |
|
173 |
#endif |
|
174 |
separatorPos = tmpPath.indexOf(slash, separatorPos + 1); |
|
175 |
QString prefix = separatorPos == -1 ? tmpPath : tmpPath.left(separatorPos); |
|
176 |
if ( |
|
177 |
#ifdef Q_OS_SYMBIAN |
|
178 |
// Symbian doesn't support directory symlinks, so do not check for link unless we |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
// are handling the last path element. This not only slightly improves performance, |
0 | 180 |
// but also saves us from lot of unnecessary platform security check failures |
181 |
// when dealing with files under *:/private directories. |
|
182 |
separatorPos == -1 && |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
#endif |
0 | 184 |
!nonSymlinks.contains(prefix)) { |
185 |
fi.setFile(prefix); |
|
186 |
if (fi.isSymLink()) { |
|
187 |
QString target = fi.symLinkTarget(); |
|
188 |
if (separatorPos != -1) { |
|
189 |
if (fi.isDir() && !target.endsWith(slash)) |
|
190 |
target.append(slash); |
|
191 |
target.append(tmpPath.mid(separatorPos)); |
|
192 |
} |
|
193 |
tmpPath = QDir::cleanPath(target); |
|
194 |
separatorPos = 0; |
|
195 |
||
196 |
if (known.contains(tmpPath)) |
|
197 |
return QString(); |
|
198 |
known.insert(tmpPath); |
|
199 |
} else { |
|
200 |
nonSymlinks.insert(prefix); |
|
201 |
} |
|
202 |
} |
|
203 |
} while (separatorPos != -1); |
|
204 |
||
205 |
return QDir::cleanPath(tmpPath); |
|
206 |
} |
|
207 |
||
208 |
/*! |
|
209 |
Constructs a QFSFileEngine for the file name \a file. |
|
210 |
*/ |
|
211 |
QFSFileEngine::QFSFileEngine(const QString &file) : QAbstractFileEngine(*new QFSFileEnginePrivate) |
|
212 |
{ |
|
213 |
Q_D(QFSFileEngine); |
|
214 |
d->filePath = QDir::fromNativeSeparators(file); |
|
215 |
d->nativeInitFileName(); |
|
216 |
} |
|
217 |
||
218 |
/*! |
|
219 |
Constructs a QFSFileEngine. |
|
220 |
*/ |
|
221 |
QFSFileEngine::QFSFileEngine() : QAbstractFileEngine(*new QFSFileEnginePrivate) |
|
222 |
{ |
|
223 |
} |
|
224 |
||
225 |
/*! |
|
226 |
\internal |
|
227 |
*/ |
|
228 |
QFSFileEngine::QFSFileEngine(QFSFileEnginePrivate &dd) |
|
229 |
: QAbstractFileEngine(dd) |
|
230 |
{ |
|
231 |
} |
|
232 |
||
233 |
/*! |
|
234 |
Destructs the QFSFileEngine. |
|
235 |
*/ |
|
236 |
QFSFileEngine::~QFSFileEngine() |
|
237 |
{ |
|
238 |
Q_D(QFSFileEngine); |
|
239 |
if (d->closeFileHandle) { |
|
240 |
if (d->fh) { |
|
241 |
int ret; |
|
242 |
do { |
|
243 |
ret = fclose(d->fh); |
|
244 |
} while (ret == EOF && errno == EINTR); |
|
245 |
} else if (d->fd != -1) { |
|
246 |
int ret; |
|
247 |
do { |
|
248 |
ret = QT_CLOSE(d->fd); |
|
249 |
} while (ret == -1 && errno == EINTR); |
|
250 |
} |
|
251 |
} |
|
252 |
QList<uchar*> keys = d->maps.keys(); |
|
253 |
for (int i = 0; i < keys.count(); ++i) |
|
254 |
unmap(keys.at(i)); |
|
255 |
} |
|
256 |
||
257 |
/*! |
|
258 |
\reimp |
|
259 |
*/ |
|
260 |
void QFSFileEngine::setFileName(const QString &file) |
|
261 |
{ |
|
262 |
Q_D(QFSFileEngine); |
|
263 |
d->init(); |
|
264 |
d->filePath = QDir::fromNativeSeparators(file); |
|
265 |
d->nativeInitFileName(); |
|
266 |
} |
|
267 |
||
268 |
/*! |
|
269 |
\reimp |
|
270 |
*/ |
|
271 |
bool QFSFileEngine::open(QIODevice::OpenMode openMode) |
|
272 |
{ |
|
273 |
Q_D(QFSFileEngine); |
|
274 |
if (d->filePath.isEmpty()) { |
|
275 |
qWarning("QFSFileEngine::open: No file name specified"); |
|
276 |
setError(QFile::OpenError, QLatin1String("No file name specified")); |
|
277 |
return false; |
|
278 |
} |
|
279 |
||
280 |
// Append implies WriteOnly. |
|
281 |
if (openMode & QFile::Append) |
|
282 |
openMode |= QFile::WriteOnly; |
|
283 |
||
284 |
// WriteOnly implies Truncate if neither ReadOnly nor Append are sent. |
|
285 |
if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append))) |
|
286 |
openMode |= QFile::Truncate; |
|
287 |
||
288 |
d->openMode = openMode; |
|
289 |
d->lastFlushFailed = false; |
|
290 |
d->tried_stat = 0; |
|
291 |
d->fh = 0; |
|
292 |
d->fd = -1; |
|
293 |
||
294 |
return d->nativeOpen(openMode); |
|
295 |
} |
|
296 |
||
297 |
/*! |
|
298 |
Opens the file handle \a fh in \a openMode mode. Returns true on |
|
299 |
success; otherwise returns false. |
|
300 |
*/ |
|
301 |
bool QFSFileEngine::open(QIODevice::OpenMode openMode, FILE *fh) |
|
302 |
{ |
|
303 |
Q_D(QFSFileEngine); |
|
304 |
||
305 |
// Append implies WriteOnly. |
|
306 |
if (openMode & QFile::Append) |
|
307 |
openMode |= QFile::WriteOnly; |
|
308 |
||
309 |
// WriteOnly implies Truncate if neither ReadOnly nor Append are sent. |
|
310 |
if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append))) |
|
311 |
openMode |= QFile::Truncate; |
|
312 |
||
313 |
d->openMode = openMode; |
|
314 |
d->lastFlushFailed = false; |
|
315 |
d->closeFileHandle = false; |
|
316 |
d->nativeFilePath.clear(); |
|
317 |
d->filePath.clear(); |
|
318 |
d->tried_stat = 0; |
|
319 |
d->fd = -1; |
|
320 |
||
321 |
return d->openFh(openMode, fh); |
|
322 |
} |
|
323 |
||
324 |
/*! |
|
325 |
Opens the file handle \a fh using the open mode \a flags. |
|
326 |
*/ |
|
327 |
bool QFSFileEnginePrivate::openFh(QIODevice::OpenMode openMode, FILE *fh) |
|
328 |
{ |
|
329 |
Q_Q(QFSFileEngine); |
|
330 |
this->fh = fh; |
|
331 |
fd = -1; |
|
332 |
||
333 |
// Seek to the end when in Append mode. |
|
334 |
if (openMode & QIODevice::Append) { |
|
335 |
int ret; |
|
336 |
do { |
|
337 |
ret = QT_FSEEK(fh, 0, SEEK_END); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
} while (ret != 0 && errno == EINTR); |
0 | 339 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
if (ret != 0) { |
0 | 341 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
342 |
qt_error_string(int(errno))); |
|
343 |
||
344 |
this->openMode = QIODevice::NotOpen; |
|
345 |
this->fh = 0; |
|
346 |
||
347 |
return false; |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
return true; |
|
352 |
} |
|
353 |
||
354 |
/*! |
|
355 |
Opens the file descriptor \a fd in \a openMode mode. Returns true |
|
356 |
on success; otherwise returns false. |
|
357 |
*/ |
|
358 |
bool QFSFileEngine::open(QIODevice::OpenMode openMode, int fd) |
|
359 |
{ |
|
360 |
Q_D(QFSFileEngine); |
|
361 |
||
362 |
// Append implies WriteOnly. |
|
363 |
if (openMode & QFile::Append) |
|
364 |
openMode |= QFile::WriteOnly; |
|
365 |
||
366 |
// WriteOnly implies Truncate if neither ReadOnly nor Append are sent. |
|
367 |
if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | QFile::Append))) |
|
368 |
openMode |= QFile::Truncate; |
|
369 |
||
370 |
d->openMode = openMode; |
|
371 |
d->lastFlushFailed = false; |
|
372 |
d->closeFileHandle = false; |
|
373 |
d->nativeFilePath.clear(); |
|
374 |
d->filePath.clear(); |
|
375 |
d->fh = 0; |
|
376 |
d->fd = -1; |
|
377 |
d->tried_stat = 0; |
|
378 |
||
379 |
return d->openFd(openMode, fd); |
|
380 |
} |
|
381 |
||
382 |
||
383 |
/*! |
|
384 |
Opens the file descriptor \a fd to the file engine, using the open mode \a |
|
385 |
flags. |
|
386 |
*/ |
|
387 |
bool QFSFileEnginePrivate::openFd(QIODevice::OpenMode openMode, int fd) |
|
388 |
{ |
|
389 |
Q_Q(QFSFileEngine); |
|
390 |
this->fd = fd; |
|
391 |
fh = 0; |
|
392 |
||
393 |
// Seek to the end when in Append mode. |
|
394 |
if (openMode & QFile::Append) { |
|
395 |
int ret; |
|
396 |
do { |
|
397 |
ret = QT_LSEEK(fd, 0, SEEK_END); |
|
398 |
} while (ret == -1 && errno == EINTR); |
|
399 |
||
400 |
if (ret == -1) { |
|
401 |
q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, |
|
402 |
qt_error_string(int(errno))); |
|
403 |
||
404 |
this->openMode = QIODevice::NotOpen; |
|
405 |
this->fd = -1; |
|
406 |
||
407 |
return false; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
return true; |
|
412 |
} |
|
413 |
||
414 |
/*! |
|
415 |
\reimp |
|
416 |
*/ |
|
417 |
bool QFSFileEngine::close() |
|
418 |
{ |
|
419 |
Q_D(QFSFileEngine); |
|
420 |
d->openMode = QIODevice::NotOpen; |
|
421 |
return d->nativeClose(); |
|
422 |
} |
|
423 |
||
424 |
/*! |
|
425 |
\internal |
|
426 |
*/ |
|
427 |
bool QFSFileEnginePrivate::closeFdFh() |
|
428 |
{ |
|
429 |
Q_Q(QFSFileEngine); |
|
430 |
if (fd == -1 && !fh) |
|
431 |
return false; |
|
432 |
||
433 |
// Flush the file if it's buffered, and if the last flush didn't fail. |
|
434 |
bool flushed = !fh || (!lastFlushFailed && q->flush()); |
|
435 |
bool closed = true; |
|
436 |
tried_stat = 0; |
|
437 |
||
438 |
// Close the file if we created the handle. |
|
439 |
if (closeFileHandle) { |
|
440 |
int ret; |
|
441 |
do { |
|
442 |
if (fh) { |
|
443 |
// Close buffered file. |
|
444 |
ret = fclose(fh) != 0 ? -1 : 0; |
|
445 |
} else { |
|
446 |
// Close unbuffered file. |
|
447 |
ret = QT_CLOSE(fd); |
|
448 |
} |
|
449 |
} while (ret == -1 && errno == EINTR); |
|
450 |
||
451 |
// We must reset these guys regardless; calling close again after a |
|
452 |
// failed close causes crashes on some systems. |
|
453 |
fh = 0; |
|
454 |
fd = -1; |
|
455 |
closed = (ret == 0); |
|
456 |
} |
|
457 |
||
458 |
// Report errors. |
|
459 |
if (!flushed || !closed) { |
|
460 |
if (flushed) { |
|
461 |
// If not flushed, we want the flush error to fall through. |
|
462 |
q->setError(QFile::UnspecifiedError, qt_error_string(errno)); |
|
463 |
} |
|
464 |
return false; |
|
465 |
} |
|
466 |
||
467 |
return true; |
|
468 |
} |
|
469 |
||
470 |
/*! |
|
471 |
\reimp |
|
472 |
*/ |
|
473 |
bool QFSFileEngine::flush() |
|
474 |
{ |
|
475 |
Q_D(QFSFileEngine); |
|
476 |
if ((d->openMode & QIODevice::WriteOnly) == 0) { |
|
477 |
// Nothing in the write buffers, so flush succeeds in doing |
|
478 |
// nothing. |
|
479 |
return true; |
|
480 |
} |
|
481 |
return d->nativeFlush(); |
|
482 |
} |
|
483 |
||
484 |
/*! |
|
485 |
\internal |
|
486 |
*/ |
|
487 |
bool QFSFileEnginePrivate::flushFh() |
|
488 |
{ |
|
489 |
Q_Q(QFSFileEngine); |
|
490 |
||
491 |
// Never try to flush again if the last flush failed. Otherwise you can |
|
492 |
// get crashes on some systems (AIX). |
|
493 |
if (lastFlushFailed) |
|
494 |
return false; |
|
495 |
||
496 |
int ret = fflush(fh); |
|
497 |
||
498 |
lastFlushFailed = (ret != 0); |
|
499 |
lastIOCommand = QFSFileEnginePrivate::IOFlushCommand; |
|
500 |
||
501 |
if (ret != 0) { |
|
502 |
q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, |
|
503 |
qt_error_string(errno)); |
|
504 |
return false; |
|
505 |
} |
|
506 |
return true; |
|
507 |
} |
|
508 |
||
509 |
/*! |
|
510 |
\reimp |
|
511 |
*/ |
|
512 |
qint64 QFSFileEngine::size() const |
|
513 |
{ |
|
514 |
Q_D(const QFSFileEngine); |
|
515 |
return d->nativeSize(); |
|
516 |
} |
|
517 |
||
518 |
/*! |
|
519 |
\internal |
|
520 |
*/ |
|
521 |
qint64 QFSFileEnginePrivate::sizeFdFh() const |
|
522 |
{ |
|
523 |
Q_Q(const QFSFileEngine); |
|
524 |
// ### Fix this function, it should not stat unless the file is closed. |
|
525 |
QT_STATBUF st; |
|
526 |
int ret = 0; |
|
527 |
const_cast<QFSFileEngine *>(q)->flush(); |
|
528 |
if (fh && nativeFilePath.isEmpty()) { |
|
529 |
// Buffered stdlib mode. |
|
530 |
// ### This should really be an ftell |
|
531 |
ret = QT_FSTAT(QT_FILENO(fh), &st); |
|
532 |
} else if (fd == -1) { |
|
533 |
// Stateless stat. |
|
534 |
ret = QT_STAT(nativeFilePath.constData(), &st); |
|
535 |
} else { |
|
536 |
// Unbuffered stdio mode. |
|
537 |
ret = QT_FSTAT(fd, &st); |
|
538 |
} |
|
539 |
if (ret == -1) |
|
540 |
return 0; |
|
541 |
return st.st_size; |
|
542 |
} |
|
543 |
||
544 |
/*! |
|
545 |
\reimp |
|
546 |
*/ |
|
547 |
qint64 QFSFileEngine::pos() const |
|
548 |
{ |
|
549 |
Q_D(const QFSFileEngine); |
|
550 |
return d->nativePos(); |
|
551 |
} |
|
552 |
||
553 |
/*! |
|
554 |
\internal |
|
555 |
*/ |
|
556 |
qint64 QFSFileEnginePrivate::posFdFh() const |
|
557 |
{ |
|
558 |
if (fh) |
|
559 |
return qint64(QT_FTELL(fh)); |
|
560 |
return QT_LSEEK(fd, 0, SEEK_CUR); |
|
561 |
} |
|
562 |
||
563 |
/*! |
|
564 |
\reimp |
|
565 |
*/ |
|
566 |
bool QFSFileEngine::seek(qint64 pos) |
|
567 |
{ |
|
568 |
Q_D(QFSFileEngine); |
|
569 |
return d->nativeSeek(pos); |
|
570 |
} |
|
571 |
||
572 |
/*! |
|
573 |
\internal |
|
574 |
*/ |
|
575 |
bool QFSFileEnginePrivate::seekFdFh(qint64 pos) |
|
576 |
{ |
|
577 |
Q_Q(QFSFileEngine); |
|
578 |
||
579 |
// On Windows' stdlib implementation, the results of calling fread and |
|
580 |
// fwrite are undefined if not called either in sequence, or if preceded |
|
581 |
// with a call to fflush(). |
|
582 |
if (lastIOCommand != QFSFileEnginePrivate::IOFlushCommand && !q->flush()) |
|
583 |
return false; |
|
584 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
if (pos < 0 || pos != qint64(QT_OFF_T(pos))) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
586 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
587 |
|
0 | 588 |
if (fh) { |
589 |
// Buffered stdlib mode. |
|
590 |
int ret; |
|
591 |
do { |
|
592 |
ret = QT_FSEEK(fh, QT_OFF_T(pos), SEEK_SET); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
593 |
} while (ret != 0 && errno == EINTR); |
0 | 594 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
595 |
if (ret != 0) { |
0 | 596 |
q->setError(QFile::ReadError, qt_error_string(int(errno))); |
597 |
return false; |
|
598 |
} |
|
599 |
} else { |
|
600 |
// Unbuffered stdio mode. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
601 |
if (QT_LSEEK(fd, QT_OFF_T(pos), SEEK_SET) == -1) { |
0 | 602 |
qWarning() << "QFile::at: Cannot set file position" << pos; |
603 |
q->setError(QFile::PositionError, qt_error_string(errno)); |
|
604 |
return false; |
|
605 |
} |
|
606 |
} |
|
607 |
return true; |
|
608 |
} |
|
609 |
||
610 |
/*! |
|
611 |
\reimp |
|
612 |
*/ |
|
613 |
int QFSFileEngine::handle() const |
|
614 |
{ |
|
615 |
Q_D(const QFSFileEngine); |
|
616 |
return d->nativeHandle(); |
|
617 |
} |
|
618 |
||
619 |
/*! |
|
620 |
\reimp |
|
621 |
*/ |
|
622 |
qint64 QFSFileEngine::read(char *data, qint64 maxlen) |
|
623 |
{ |
|
624 |
Q_D(QFSFileEngine); |
|
625 |
||
626 |
// On Windows' stdlib implementation, the results of calling fread and |
|
627 |
// fwrite are undefined if not called either in sequence, or if preceded |
|
628 |
// with a call to fflush(). |
|
629 |
if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) { |
|
630 |
flush(); |
|
631 |
d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand; |
|
632 |
} |
|
633 |
||
634 |
return d->nativeRead(data, maxlen); |
|
635 |
} |
|
636 |
||
637 |
/*! |
|
638 |
\internal |
|
639 |
*/ |
|
640 |
qint64 QFSFileEnginePrivate::readFdFh(char *data, qint64 len) |
|
641 |
{ |
|
642 |
Q_Q(QFSFileEngine); |
|
643 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
644 |
if (len < 0 || len != qint64(size_t(len))) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
645 |
q->setError(QFile::ReadError, qt_error_string(EINVAL)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
646 |
return -1; |
0 | 647 |
} |
648 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
649 |
qint64 readBytes = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
650 |
bool eof = false; |
0 | 651 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
652 |
if (fh) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
653 |
// Buffered stdlib mode. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
654 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
655 |
size_t result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
656 |
bool retry = true; |
0 | 657 |
do { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
658 |
result = fread(data + readBytes, 1, size_t(len - readBytes), fh); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
659 |
eof = feof(fh); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
660 |
if (retry && eof && result == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
661 |
// On Mac OS, this is needed, e.g., if a file was written to |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
662 |
// through another stream since our last read. See test |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
663 |
// tst_QFile::appendAndRead |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
664 |
QT_FSEEK(fh, QT_FTELL(fh), SEEK_SET); // re-sync stream. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
665 |
retry = false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
666 |
continue; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
667 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
668 |
readBytes += result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
669 |
} while (!eof && (result == 0 ? errno == EINTR : readBytes < len)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
670 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
671 |
} else if (fd != -1) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
672 |
// Unbuffered stdio mode. |
0 | 673 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
674 |
#ifdef Q_OS_WIN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
675 |
int result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
676 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
677 |
ssize_t result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
678 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
679 |
do { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
680 |
result = QT_READ(fd, data + readBytes, size_t(len - readBytes)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
681 |
} while ((result == -1 && errno == EINTR) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
682 |
|| (result > 0 && (readBytes += result) < len)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
683 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
684 |
eof = !(result == -1); |
0 | 685 |
} |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
686 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
687 |
if (!eof && readBytes == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
688 |
readBytes = -1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
689 |
q->setError(QFile::ReadError, qt_error_string(errno)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
690 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
691 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
692 |
return readBytes; |
0 | 693 |
} |
694 |
||
695 |
/*! |
|
696 |
\reimp |
|
697 |
*/ |
|
698 |
qint64 QFSFileEngine::readLine(char *data, qint64 maxlen) |
|
699 |
{ |
|
700 |
Q_D(QFSFileEngine); |
|
701 |
||
702 |
// On Windows' stdlib implementation, the results of calling fread and |
|
703 |
// fwrite are undefined if not called either in sequence, or if preceded |
|
704 |
// with a call to fflush(). |
|
705 |
if (d->lastIOCommand != QFSFileEnginePrivate::IOReadCommand) { |
|
706 |
flush(); |
|
707 |
d->lastIOCommand = QFSFileEnginePrivate::IOReadCommand; |
|
708 |
} |
|
709 |
||
710 |
return d->nativeReadLine(data, maxlen); |
|
711 |
} |
|
712 |
||
713 |
/*! |
|
714 |
\internal |
|
715 |
*/ |
|
716 |
qint64 QFSFileEnginePrivate::readLineFdFh(char *data, qint64 maxlen) |
|
717 |
{ |
|
718 |
Q_Q(QFSFileEngine); |
|
719 |
if (!fh) |
|
720 |
return q->QAbstractFileEngine::readLine(data, maxlen); |
|
721 |
||
722 |
QT_OFF_T oldPos = 0; |
|
723 |
#ifdef Q_OS_WIN |
|
724 |
bool seq = q->isSequential(); |
|
725 |
if (!seq) |
|
726 |
#endif |
|
727 |
oldPos = QT_FTELL(fh); |
|
728 |
||
729 |
// QIODevice::readLine() passes maxlen - 1 to QFile::readLineData() |
|
730 |
// because it has made space for the '\0' at the end of data. But fgets |
|
731 |
// does the same, so we'd get two '\0' at the end - passing maxlen + 1 |
|
732 |
// solves this. |
|
733 |
if (!fgets(data, int(maxlen + 1), fh)) { |
|
734 |
if (!feof(fh)) |
|
735 |
q->setError(QFile::ReadError, qt_error_string(int(errno))); |
|
736 |
return -1; // error |
|
737 |
} |
|
738 |
||
739 |
#ifdef Q_OS_WIN |
|
740 |
if (seq) |
|
741 |
return qstrlen(data); |
|
742 |
#endif |
|
743 |
||
744 |
qint64 lineLength = QT_FTELL(fh) - oldPos; |
|
745 |
return lineLength > 0 ? lineLength : qstrlen(data); |
|
746 |
} |
|
747 |
||
748 |
/*! |
|
749 |
\reimp |
|
750 |
*/ |
|
751 |
qint64 QFSFileEngine::write(const char *data, qint64 len) |
|
752 |
{ |
|
753 |
Q_D(QFSFileEngine); |
|
754 |
||
755 |
// On Windows' stdlib implementation, the results of calling fread and |
|
756 |
// fwrite are undefined if not called either in sequence, or if preceded |
|
757 |
// with a call to fflush(). |
|
758 |
if (d->lastIOCommand != QFSFileEnginePrivate::IOWriteCommand) { |
|
759 |
flush(); |
|
760 |
d->lastIOCommand = QFSFileEnginePrivate::IOWriteCommand; |
|
761 |
} |
|
762 |
||
763 |
return d->nativeWrite(data, len); |
|
764 |
} |
|
765 |
||
766 |
/*! |
|
767 |
\internal |
|
768 |
*/ |
|
769 |
qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len) |
|
770 |
{ |
|
771 |
Q_Q(QFSFileEngine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
772 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
773 |
if (len < 0 || len != qint64(size_t(len))) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
774 |
q->setError(QFile::WriteError, qt_error_string(EINVAL)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
775 |
return -1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
776 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
777 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
778 |
qint64 writtenBytes = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
779 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
780 |
if (fh) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
781 |
// Buffered stdlib mode. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
782 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
783 |
size_t result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
784 |
do { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
785 |
result = fwrite(data + writtenBytes, 1, size_t(len - writtenBytes), fh); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
786 |
writtenBytes += result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
787 |
} while (result == 0 ? errno == EINTR : writtenBytes < len); |
0 | 788 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
789 |
} else if (fd != -1) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
790 |
// Unbuffered stdio mode. |
0 | 791 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
792 |
#ifdef Q_OS_WIN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
793 |
int result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
794 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
795 |
ssize_t result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
796 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
797 |
do { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
798 |
result = QT_WRITE(fd, data + writtenBytes, size_t(len - writtenBytes)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
799 |
} while ((result == -1 && errno == EINTR) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
800 |
|| (result > 0 && (writtenBytes += result) < len)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
801 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
802 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
803 |
if (len && writtenBytes == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
804 |
writtenBytes = -1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
805 |
q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
806 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
807 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
808 |
return writtenBytes; |
0 | 809 |
} |
810 |
||
811 |
/*! |
|
812 |
\internal |
|
813 |
*/ |
|
814 |
QAbstractFileEngine::Iterator *QFSFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames) |
|
815 |
{ |
|
816 |
return new QFSFileEngineIterator(filters, filterNames); |
|
817 |
} |
|
818 |
||
819 |
/*! |
|
820 |
\internal |
|
821 |
*/ |
|
822 |
QAbstractFileEngine::Iterator *QFSFileEngine::endEntryList() |
|
823 |
{ |
|
824 |
return 0; |
|
825 |
} |
|
826 |
||
827 |
/*! |
|
828 |
\internal |
|
829 |
*/ |
|
830 |
QStringList QFSFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const |
|
831 |
{ |
|
832 |
return QAbstractFileEngine::entryList(filters, filterNames); |
|
833 |
} |
|
834 |
||
835 |
/*! |
|
836 |
\reimp |
|
837 |
*/ |
|
838 |
bool QFSFileEngine::isSequential() const |
|
839 |
{ |
|
840 |
Q_D(const QFSFileEngine); |
|
841 |
if (d->is_sequential == 0) |
|
842 |
d->is_sequential = d->nativeIsSequential() ? 1 : 2; |
|
843 |
return d->is_sequential == 1; |
|
844 |
} |
|
845 |
||
846 |
/*! |
|
847 |
\internal |
|
848 |
*/ |
|
849 |
bool QFSFileEnginePrivate::isSequentialFdFh() const |
|
850 |
{ |
|
851 |
if (!tried_stat) |
|
852 |
doStat(); |
|
853 |
if (could_stat) { |
|
854 |
#ifdef Q_OS_UNIX |
|
855 |
return (st.st_mode & S_IFMT) != S_IFREG; |
|
856 |
// ### WINDOWS! |
|
857 |
#endif |
|
858 |
} |
|
859 |
return true; |
|
860 |
} |
|
861 |
||
862 |
/*! |
|
863 |
\reimp |
|
864 |
*/ |
|
865 |
bool QFSFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output) |
|
866 |
{ |
|
867 |
Q_D(QFSFileEngine); |
|
868 |
if (extension == AtEndExtension && d->fh && isSequential()) |
|
869 |
return feof(d->fh); |
|
870 |
||
871 |
if (extension == MapExtension) { |
|
872 |
const MapExtensionOption *options = (MapExtensionOption*)(option); |
|
873 |
MapExtensionReturn *returnValue = static_cast<MapExtensionReturn*>(output); |
|
874 |
returnValue->address = d->map(options->offset, options->size, options->flags); |
|
875 |
return (returnValue->address != 0); |
|
876 |
} |
|
877 |
if (extension == UnMapExtension) { |
|
878 |
UnMapExtensionOption *options = (UnMapExtensionOption*)option; |
|
879 |
return d->unmap(options->address); |
|
880 |
} |
|
881 |
||
882 |
return false; |
|
883 |
} |
|
884 |
||
885 |
/*! |
|
886 |
\reimp |
|
887 |
*/ |
|
888 |
bool QFSFileEngine::supportsExtension(Extension extension) const |
|
889 |
{ |
|
890 |
Q_D(const QFSFileEngine); |
|
891 |
if (extension == AtEndExtension && d->fh && isSequential()) |
|
892 |
return true; |
|
893 |
if (extension == FastReadLineExtension && d->fh) |
|
894 |
return true; |
|
895 |
if (extension == FastReadLineExtension && d->fd != -1 && isSequential()) |
|
896 |
return true; |
|
897 |
if (extension == UnMapExtension || extension == MapExtension) |
|
898 |
return true; |
|
899 |
return false; |
|
900 |
} |
|
901 |
||
902 |
/*! \fn bool QFSFileEngine::caseSensitive() const |
|
903 |
Returns true for Windows, false for Unix. |
|
904 |
*/ |
|
905 |
||
906 |
/*! \fn bool QFSFileEngine::copy(const QString ©Name) |
|
907 |
||
908 |
For windows, copy the file to file \a copyName. |
|
909 |
||
910 |
Not implemented for Unix. |
|
911 |
*/ |
|
912 |
||
913 |
/*! \fn QString QFSFileEngine::currentPath(const QString &fileName) |
|
914 |
For Unix, returns the current working directory for the file |
|
915 |
engine. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
916 |
|
0 | 917 |
For Windows, returns the canonicalized form of the current path used |
918 |
by the file engine for the drive specified by \a fileName. On |
|
919 |
Windows, each drive has its own current directory, so a different |
|
920 |
path is returned for file names that include different drive names |
|
921 |
(e.g. A: or C:). |
|
922 |
||
923 |
\sa setCurrentPath() |
|
924 |
*/ |
|
925 |
||
926 |
/*! \fn QFileInfoList QFSFileEngine::drives() |
|
927 |
For Windows, returns the list of drives in the file system as a list |
|
928 |
of QFileInfo objects. On unix, Mac OS X and Windows CE, only the |
|
929 |
root path is returned. On Windows, this function returns all drives |
|
930 |
(A:\, C:\, D:\, etc.). |
|
931 |
||
932 |
For Unix, the list contains just the root path "/". |
|
933 |
*/ |
|
934 |
||
935 |
/*! \fn QString QFSFileEngine::fileName(FileName file) const |
|
936 |
\reimp |
|
937 |
*/ |
|
938 |
||
939 |
/*! \fn QDateTime QFSFileEngine::fileTime(FileTime time) const |
|
940 |
\reimp |
|
941 |
*/ |
|
942 |
||
943 |
/*! \fn QString QFSFileEngine::homePath() |
|
944 |
Returns the home path of the current user. |
|
945 |
||
946 |
\sa rootPath() |
|
947 |
*/ |
|
948 |
||
949 |
/*! \fn bool QFSFileEngine::isRelativePath() const |
|
950 |
\reimp |
|
951 |
*/ |
|
952 |
||
953 |
/*! \fn bool QFSFileEngine::link(const QString &newName) |
|
954 |
||
955 |
Creates a link from the file currently specified by fileName() to |
|
956 |
\a newName. What a link is depends on the underlying filesystem |
|
957 |
(be it a shortcut on Windows or a symbolic link on Unix). Returns |
|
958 |
true if successful; otherwise returns false. |
|
959 |
*/ |
|
960 |
||
961 |
/*! \fn bool QFSFileEngine::mkdir(const QString &name, bool createParentDirectories) const |
|
962 |
\reimp |
|
963 |
*/ |
|
964 |
||
965 |
/*! \fn uint QFSFileEngine::ownerId(FileOwner own) const |
|
966 |
In Unix, if stat() is successful, the \c uid is returned if |
|
967 |
\a own is the owner. Otherwise the \c gid is returned. If stat() |
|
968 |
is unsuccessful, -2 is reuturned. |
|
969 |
||
970 |
For Windows, -2 is always returned. |
|
971 |
*/ |
|
972 |
||
973 |
/*! \fn QString QFSFileEngine::owner(FileOwner own) const |
|
974 |
\reimp |
|
975 |
*/ |
|
976 |
||
977 |
/*! \fn bool QFSFileEngine::remove() |
|
978 |
\reimp |
|
979 |
*/ |
|
980 |
||
981 |
/*! \fn bool QFSFileEngine::rename(const QString &newName) |
|
982 |
\reimp |
|
983 |
*/ |
|
984 |
||
985 |
/*! \fn bool QFSFileEngine::rmdir(const QString &name, bool recurseParentDirectories) const |
|
986 |
\reimp |
|
987 |
*/ |
|
988 |
||
989 |
/*! \fn QString QFSFileEngine::rootPath() |
|
990 |
Returns the root path. |
|
991 |
||
992 |
\sa homePath() |
|
993 |
*/ |
|
994 |
||
995 |
/*! \fn bool QFSFileEngine::setCurrentPath(const QString &path) |
|
996 |
Sets the current path (e.g., for QDir), to \a path. Returns true if the |
|
997 |
new path exists; otherwise this function does nothing, and returns false. |
|
998 |
||
999 |
\sa currentPath() |
|
1000 |
*/ |
|
1001 |
||
1002 |
/*! \fn bool QFSFileEngine::setPermissions(uint perms) |
|
1003 |
\reimp |
|
1004 |
*/ |
|
1005 |
||
1006 |
/*! \fn bool QFSFileEngine::setSize(qint64 size) |
|
1007 |
\reimp |
|
1008 |
*/ |
|
1009 |
||
1010 |
/*! \fn QString QFSFileEngine::tempPath() |
|
1011 |
Returns the temporary path (i.e., a path in which it is safe |
|
1012 |
to store temporary files). |
|
1013 |
*/ |
|
1014 |
||
1015 |
QT_END_NAMESPACE |
|
1016 |
||
1017 |
#endif // QT_NO_FSFILEENGINE |