Add renameOverwrite() to QAbstractFileEngine.

QFSFileEngine::rename() on Windows doesn't overwrite the existing destination.
Keep that unchanged (it's the desired behavior in QFile::rename), and
provide cross-platform rename-overwrite behavior in the new method.

This is needed by QSaveFile.

Change-Id: I5e753d289d8a53692530a48a1783d62e26169cdc
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2013-01-14 09:34:08 +01:00 committed by The Qt Project
parent 18c916517f
commit 7e7b65c370
7 changed files with 49 additions and 0 deletions

View File

@ -495,6 +495,24 @@ bool QAbstractFileEngine::rename(const QString &newName)
return false;
}
/*!
\since 5.1
Requests that the file be renamed to \a newName in the file
system. If the new name already exists, it must be overwritten.
If the operation succeeds, returns true; otherwise returns
false.
This virtual function must be reimplemented by all subclasses.
\sa setFileName()
*/
bool QAbstractFileEngine::renameOverwrite(const QString &newName)
{
Q_UNUSED(newName);
return false;
}
/*!
Creates a link from the file currently specified by fileName() to
\a newName. What a link is depends on the underlying filesystem

View File

@ -131,6 +131,7 @@ public:
virtual bool remove();
virtual bool copy(const QString &newName);
virtual bool rename(const QString &newName);
virtual bool renameOverwrite(const QString &newName);
virtual bool link(const QString &newName);
virtual bool mkdir(const QString &dirName, bool createParentDirectories) const;
virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const;

View File

@ -908,6 +908,11 @@ bool QFSFileEngine::supportsExtension(Extension extension) const
\reimp
*/
/*! \fn bool QFSFileEngine::renameOverwrite(const QString &newName)
\reimp
*/
/*! \fn bool QFSFileEngine::rmdir(const QString &name, bool recurseParentDirectories) const
\reimp
*/

View File

@ -88,6 +88,7 @@ public:
bool remove();
bool copy(const QString &newName);
bool rename(const QString &newName);
bool renameOverwrite(const QString &newName);
bool link(const QString &newName);
bool mkdir(const QString &dirName, bool createParentDirectories) const;
bool rmdir(const QString &dirName, bool recurseParentDirectories) const;

View File

@ -388,6 +388,12 @@ bool QFSFileEngine::copy(const QString &newName)
return ret;
}
bool QFSFileEngine::renameOverwrite(const QString &newName)
{
// On Unix, rename() overwrites.
return rename(newName);
}
bool QFSFileEngine::rename(const QString &newName)
{
Q_D(QFSFileEngine);

View File

@ -507,6 +507,17 @@ bool QFSFileEngine::rename(const QString &newName)
return ret;
}
bool QFSFileEngine::renameOverwrite(const QString &newName)
{
Q_D(QFSFileEngine);
bool ret = ::MoveFileEx((wchar_t*)d->fileEntry.nativeFilePath().utf16(),
(wchar_t*)QFileSystemEntry(newName).nativeFilePath().utf16(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0;
if (!ret)
setError(QFile::RenameError, QSystemError(::GetLastError(), QSystemError::NativeError).toString());
return ret;
}
bool QFSFileEngine::mkdir(const QString &name, bool createParentDirectories) const
{
return QFileSystemEngine::createDirectory(QFileSystemEntry(name), createParentDirectories);

View File

@ -242,6 +242,7 @@ public:
bool open(QIODevice::OpenMode flags);
bool remove();
bool rename(const QString &newName);
bool renameOverwrite(const QString &newName);
bool close();
bool filePathIsTemplate;
@ -398,6 +399,12 @@ bool QTemporaryFileEngine::rename(const QString &newName)
return QFSFileEngine::rename(newName);
}
bool QTemporaryFileEngine::renameOverwrite(const QString &newName)
{
QFSFileEngine::close();
return QFSFileEngine::renameOverwrite(newName);
}
bool QTemporaryFileEngine::close()
{
// Don't close the file, just seek to the front.