From ad893943cfe0434624a1e807ed19e95286e362cc Mon Sep 17 00:00:00 2001 From: David Faure Date: Mon, 14 Jan 2013 09:41:35 +0100 Subject: Add syncToDisk() to QAbstractFileEngine. This is needed by QSaveFile. Change-Id: I07ebdfd832c0be65c26f0aed1bb7852ac33135ca Reviewed-by: Thiago Macieira --- src/corelib/io/qabstractfileengine.cpp | 13 +++++++++++++ src/corelib/io/qabstractfileengine_p.h | 1 + src/corelib/io/qfsfileengine.cpp | 11 +++++++++++ src/corelib/io/qfsfileengine_p.h | 2 ++ src/corelib/io/qfsfileengine_unix.cpp | 17 +++++++++++++++++ src/corelib/io/qfsfileengine_win.cpp | 18 ++++++++++++++---- 6 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qabstractfileengine.cpp b/src/corelib/io/qabstractfileengine.cpp index b12dc47c52..e046119fe2 100644 --- a/src/corelib/io/qabstractfileengine.cpp +++ b/src/corelib/io/qabstractfileengine.cpp @@ -399,6 +399,19 @@ bool QAbstractFileEngine::close() 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 false. diff --git a/src/corelib/io/qabstractfileengine_p.h b/src/corelib/io/qabstractfileengine_p.h index 2cbd927968..5106c01be9 100644 --- a/src/corelib/io/qabstractfileengine_p.h +++ b/src/corelib/io/qabstractfileengine_p.h @@ -124,6 +124,7 @@ public: virtual bool open(QIODevice::OpenMode openMode); virtual bool close(); virtual bool flush(); + virtual bool syncToDisk(); virtual qint64 size() const; virtual qint64 pos() const; virtual bool seek(qint64 pos); diff --git a/src/corelib/io/qfsfileengine.cpp b/src/corelib/io/qfsfileengine.cpp index 1bc3a7da2e..11cbda14ca 100644 --- a/src/corelib/io/qfsfileengine.cpp +++ b/src/corelib/io/qfsfileengine.cpp @@ -418,6 +418,17 @@ bool QFSFileEngine::flush() return d->nativeFlush(); } +/*! + \reimp +*/ +bool QFSFileEngine::syncToDisk() +{ + Q_D(QFSFileEngine); + if ((d->openMode & QIODevice::WriteOnly) == 0) + return true; + return d->nativeSyncToDisk(); +} + /*! \internal */ diff --git a/src/corelib/io/qfsfileengine_p.h b/src/corelib/io/qfsfileengine_p.h index cf70032134..5d3a6a3767 100644 --- a/src/corelib/io/qfsfileengine_p.h +++ b/src/corelib/io/qfsfileengine_p.h @@ -81,6 +81,7 @@ public: bool open(QIODevice::OpenMode flags, FILE *fh); bool close(); bool flush(); + bool syncToDisk(); qint64 size() const; qint64 pos() const; bool seek(qint64); @@ -150,6 +151,7 @@ public: bool nativeClose(); bool closeFdFh(); bool nativeFlush(); + bool nativeSyncToDisk(); bool flushFh(); qint64 nativeSize() const; #ifndef Q_OS_WIN diff --git a/src/corelib/io/qfsfileengine_unix.cpp b/src/corelib/io/qfsfileengine_unix.cpp index 8f622d77cd..05b55525b6 100644 --- a/src/corelib/io/qfsfileengine_unix.cpp +++ b/src/corelib/io/qfsfileengine_unix.cpp @@ -256,6 +256,23 @@ bool QFSFileEnginePrivate::nativeFlush() 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 */ diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp index 86e9bf971d..756e884bd1 100644 --- a/src/corelib/io/qfsfileengine_win.cpp +++ b/src/corelib/io/qfsfileengine_win.cpp @@ -198,13 +198,23 @@ bool QFSFileEnginePrivate::nativeFlush() return true; } - // Windows native mode; flushing is - // 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. + // Windows native mode; flushing is unnecessary. 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 */ -- cgit v1.2.3