Commit 368defd7 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Close file handles before renaming directory.

parent 9430e210
......@@ -29,6 +29,8 @@
#include <QDir>
#include "qtcompatmac.h"
#include "saferename.h"
#include "fileproxymodel.h"
#include "modeliterator.h"
/**
* Constructor.
......@@ -105,13 +107,15 @@ bool DirRenamer::createDirectory(const QString& dir,
*
* @param olddir old directory name
* @param newdir new directory name
* @param index model index of item to rename
* @param errorMsg if not NULL and an error occurred, a message is
* appended here, otherwise it is not touched
*
* @return true if rename successful.
*/
bool DirRenamer::renameDirectory(
const QString& olddir, const QString& newdir, QString* errorMsg) const
const QString& olddir, const QString& newdir,
const QPersistentModelIndex& index, QString* errorMsg) const
{
if (QFileInfo(newdir).exists()) {
if (errorMsg) {
......@@ -125,6 +129,10 @@ bool DirRenamer::renameDirectory(
}
return false;
}
if (index.isValid()) {
// The directory must be closed before renaming on Windows.
TaggedFileIterator::closeFileHandles(index);
}
if (Utils::safeRename(olddir, newdir) && QFileInfo(newdir).isDir()) {
return true;
} else {
......@@ -147,11 +155,12 @@ bool DirRenamer::renameDirectory(
* @param newfn new file name
* @param errorMsg if not NULL and an error occurred, a message is
* appended here, otherwise it is not touched
* @param index model index of item to rename
*
* @return true if rename successful or newfn already exists.
*/
bool DirRenamer::renameFile(const QString& oldfn, const QString& newfn,
QString* errorMsg) const
const QPersistentModelIndex& index, QString* errorMsg) const
{
if (QFileInfo(newfn).isFile()) {
return true;
......@@ -168,6 +177,10 @@ bool DirRenamer::renameFile(const QString& oldfn, const QString& newfn,
}
return false;
}
if (TaggedFile* taggedFile = FileProxyModel::getTaggedFileOfIndex(index)) {
// The file must be closed before renaming on Windows.
taggedFile->closeFileHandle();
}
if (Utils::safeRename(oldfn, newfn) && QFileInfo(newfn).isFile()) {
return true;
} else {
......@@ -229,8 +242,10 @@ void DirRenamer::clearActions()
* @param type type of action
* @param src source file or directory name
* @param dest destination file or directory name
* @param index model index of item to rename
*/
void DirRenamer::addAction(RenameAction::Type type, const QString& src, const QString& dest)
void DirRenamer::addAction(RenameAction::Type type, const QString& src, const QString& dest,
const QPersistentModelIndex& index)
{
// do not add an action if the source or destination is already in an action
for (RenameActionList::const_iterator it = m_actions.begin();
......@@ -242,7 +257,7 @@ void DirRenamer::addAction(RenameAction::Type type, const QString& src, const QS
}
}
m_actions.push_back(RenameAction(type, src, dest));
m_actions.push_back(RenameAction(type, src, dest, index));
}
/**
......@@ -356,7 +371,8 @@ void DirRenamer::scheduleAction(TaggedFile* taggedFile)
if (!createDir) {
addAction(RenameAction::RenameFile,
dirWithFiles + '/' + taggedFile->getFilename(),
currentDirname + newPart + '/' + taggedFile->getFilename());
currentDirname + newPart + '/' + taggedFile->getFilename(),
taggedFile->getIndex());
}
currentDirname = currentDirname + newPart;
}
......@@ -379,10 +395,12 @@ void DirRenamer::scheduleAction(TaggedFile* taggedFile)
// directory already exists => move files
addAction(RenameAction::RenameFile,
currentDirname + '/' + taggedFile->getFilename(),
parentWithNewPart + '/' + taggedFile->getFilename());
parentWithNewPart + '/' + taggedFile->getFilename(),
taggedFile->getIndex());
currentDirname = parentWithNewPart;
} else {
addAction(RenameAction::RenameDirectory, currentDirname, parentWithNewPart);
addAction(RenameAction::RenameDirectory, currentDirname, parentWithNewPart,
taggedFile->getIndex().parent());
currentDirname = parentWithNewPart;
}
} else {
......@@ -411,14 +429,15 @@ void DirRenamer::performActions(QString* errorMsg)
createDirectory((*it).m_dest, errorMsg);
break;
case RenameAction::RenameDirectory:
if (renameDirectory((*it).m_src, (*it).m_dest, errorMsg)) {
if (renameDirectory((*it).m_src, (*it).m_dest, (*it).m_index,
errorMsg)) {
if ((*it).m_src == m_dirName) {
m_dirName = (*it).m_dest;
}
}
break;
case RenameAction::RenameFile:
renameFile((*it).m_src, (*it).m_dest, errorMsg);
renameFile((*it).m_src, (*it).m_dest, (*it).m_index, errorMsg);
break;
case RenameAction::ReportError:
default:
......
......@@ -161,9 +161,11 @@ private:
* @param type type of action
* @param src source file or directory name
* @param dest destination file or directory name
* @param index model index of item to rename
*/
RenameAction(Type type, const QString& src, const QString& dest) :
m_type(type), m_src(src), m_dest(dest) {}
RenameAction(Type type, const QString& src, const QString& dest,
const QPersistentModelIndex& index) :
m_type(type), m_src(src), m_dest(dest), m_index(index) {}
/**
* Constructor.
......@@ -187,6 +189,7 @@ private:
Type m_type; /**< type of action */
QString m_src; /**< source file or directory name */
QString m_dest; /**< destination file or directory name */
QPersistentModelIndex m_index; /**< model index of item to rename */
};
/** List of rename actions. */
......@@ -210,11 +213,13 @@ private:
* @param newdir new directory name
* @param errorMsg if not NULL and an error occurred, a message is
* appended here, otherwise it is not touched
* @param index model index of item to rename
*
* @return true if rename successful.
*/
bool renameDirectory(
const QString& olddir, const QString& newdir, QString* errorMsg) const;
const QString& olddir, const QString& newdir,
const QPersistentModelIndex& index, QString *errorMsg) const;
/**
* Rename a file.
......@@ -223,11 +228,12 @@ private:
* @param newfn new file name
* @param errorMsg if not NULL and an error occurred, a message is
* appended here, otherwise it is not touched
* @param index model index of item to rename
*
* @return true if rename successful or newfn already exists.
*/
bool renameFile(const QString& oldfn, const QString& newfn,
QString* errorMsg) const;
const QPersistentModelIndex& index, QString* errorMsg) const;
/**
* Add a rename action.
......@@ -235,9 +241,11 @@ private:
* @param type type of action
* @param src source file or directory name
* @param dest destination file or directory name
* @param index model index of item to rename
*/
void addAction(RenameAction::Type type, const QString& src,
const QString& dest);
const QString& dest,
const QPersistentModelIndex& index = QPersistentModelIndex());
/**
* Add a rename action.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment