Add syncToDisk() to QAbstractFileEngine.

This is needed by QSaveFile.

Change-Id: I07ebdfd832c0be65c26f0aed1bb7852ac33135ca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2013-01-14 09:41:35 +01:00 committed by The Qt Project
parent 7e7b65c370
commit ad893943cf
6 changed files with 58 additions and 4 deletions

View File

@ -399,6 +399,19 @@ bool QAbstractFileEngine::close()
return false; return false;
} }
/*!
\since 5.1
Flushes and syncs the file to disk.
Returns true if successful; otherwise returns false.
The default implementation always returns false.
*/
bool QAbstractFileEngine::syncToDisk()
{
return false;
}
/*! /*!
Flushes the open file, returning true if successful; otherwise returns Flushes the open file, returning true if successful; otherwise returns
false. false.

View File

@ -124,6 +124,7 @@ public:
virtual bool open(QIODevice::OpenMode openMode); virtual bool open(QIODevice::OpenMode openMode);
virtual bool close(); virtual bool close();
virtual bool flush(); virtual bool flush();
virtual bool syncToDisk();
virtual qint64 size() const; virtual qint64 size() const;
virtual qint64 pos() const; virtual qint64 pos() const;
virtual bool seek(qint64 pos); virtual bool seek(qint64 pos);

View File

@ -418,6 +418,17 @@ bool QFSFileEngine::flush()
return d->nativeFlush(); return d->nativeFlush();
} }
/*!
\reimp
*/
bool QFSFileEngine::syncToDisk()
{
Q_D(QFSFileEngine);
if ((d->openMode & QIODevice::WriteOnly) == 0)
return true;
return d->nativeSyncToDisk();
}
/*! /*!
\internal \internal
*/ */

View File

@ -81,6 +81,7 @@ public:
bool open(QIODevice::OpenMode flags, FILE *fh); bool open(QIODevice::OpenMode flags, FILE *fh);
bool close(); bool close();
bool flush(); bool flush();
bool syncToDisk();
qint64 size() const; qint64 size() const;
qint64 pos() const; qint64 pos() const;
bool seek(qint64); bool seek(qint64);
@ -150,6 +151,7 @@ public:
bool nativeClose(); bool nativeClose();
bool closeFdFh(); bool closeFdFh();
bool nativeFlush(); bool nativeFlush();
bool nativeSyncToDisk();
bool flushFh(); bool flushFh();
qint64 nativeSize() const; qint64 nativeSize() const;
#ifndef Q_OS_WIN #ifndef Q_OS_WIN

View File

@ -256,6 +256,23 @@ bool QFSFileEnginePrivate::nativeFlush()
return fh ? flushFh() : fd != -1; return fh ? flushFh() : fd != -1;
} }
/*!
\internal
\since 5.1
*/
bool QFSFileEnginePrivate::nativeSyncToDisk()
{
Q_Q(QFSFileEngine);
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
const int ret = fdatasync(nativeHandle());
#else
const int ret = fsync(nativeHandle());
#endif
if (ret != 0)
q->setError(QFile::WriteError, qt_error_string(errno));
return ret == 0;
}
/*! /*!
\internal \internal
*/ */

View File

@ -198,13 +198,23 @@ bool QFSFileEnginePrivate::nativeFlush()
return true; return true;
} }
// Windows native mode; flushing is // Windows native mode; flushing is unnecessary.
// unnecessary. FlushFileBuffers(), the equivalent of sync() or
// fsync() on Unix, does a low-level flush to the disk, and we
// don't expose an API for this.
return true; return true;
} }
/*
\internal
\since 5.1
*/
bool QFSFileEnginePrivate::nativeSyncToDisk()
{
if (fh || fd != -1) {
// stdlib / stdio mode. No API available.
return false;
}
return FlushFileBuffers(fileHandle);
}
/* /*
\internal \internal
*/ */