Commit 0fe61a2d authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Avoid truncation of file when renaming with illegal characters on Windows.

parent 1e3aee16
......@@ -40,7 +40,7 @@ set(CPACK_PACKAGE_VERSION_MAJOR 2)
set(CPACK_PACKAGE_VERSION_MINOR 0)
set(CPACK_PACKAGE_VERSION_PATCH 1)
#set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
set(CPACK_PACKAGE_VERSION "git20120129")
set(CPACK_PACKAGE_VERSION "git20120216")
set(RELEASE_YEAR 2012)
if (WITH_GCC_PCH)
......
......@@ -28,6 +28,7 @@
#include <QFileInfo>
#include <QDir>
#include "qtcompatmac.h"
#include "saferename.h"
/**
* Constructor.
......@@ -123,7 +124,7 @@ bool DirRenamer::renameDirectory(
}
return false;
}
if (QDir().rename(olddir, newdir) && QFileInfo(newdir).isDir()) {
if (Utils::safeRename(olddir, newdir) && QFileInfo(newdir).isDir()) {
return true;
} else {
if (errorMsg) {
......@@ -166,7 +167,7 @@ bool DirRenamer::renameFile(const QString& oldfn, const QString& newfn,
}
return false;
}
if (QDir().rename(oldfn, newfn) && QFileInfo(newfn).isFile()) {
if (Utils::safeRename(oldfn, newfn) && QFileInfo(newfn).isFile()) {
return true;
} else {
if (errorMsg) {
......
......@@ -32,6 +32,7 @@
#include "configstore.h"
#include "genres.h"
#include "modeliterator.h"
#include "saferename.h"
QList<const TaggedFile::Resolver*> TaggedFile::s_resolvers;
......@@ -518,12 +519,12 @@ bool TaggedFile::renameFile(const QString& fnOld, const QString& fnNew) const
// insensitive filesystems (e.g. Windows).
QString temp_filename(fnNew);
temp_filename.append("_CASE");
if (!QDir(m_dirname).rename(fnOld, temp_filename)) {
if (!Utils::safeRename(m_dirname, fnOld, temp_filename)) {
qDebug("rename(%s, %s) failed", fnOld.toLatin1().data(),
temp_filename.toLatin1().data());
return false;
}
if (!QDir(m_dirname).rename(temp_filename, fnNew)) {
if (!Utils::safeRename(m_dirname, temp_filename, fnNew)) {
qDebug("rename(%s, %s) failed", temp_filename.toLatin1().data(),
fnNew.toLatin1().data());
return false;
......@@ -532,7 +533,7 @@ bool TaggedFile::renameFile(const QString& fnOld, const QString& fnNew) const
qDebug("rename(%s, %s): %s already exists", fnOld.toLatin1().data(),
fnNew.toLatin1().data(), fnNew.toLatin1().data());
return false;
} else if (!QDir(m_dirname).rename(fnOld, fnNew)) {
} else if (!Utils::safeRename(m_dirname, fnOld, fnNew)) {
qDebug("rename(%s, %s) failed", fnOld.toLatin1().data(),
fnNew.toLatin1().data());
return false;
......
set(utils_SRCS
utils/movetotrash.cpp
utils/saferename.cpp
)
set(utils_MOC_HDRS)
/**
* \file saferename.cpp
* Safely rename a file.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 16 Feb 2012
*
* Copyright (C) 2012 Urs Fleisch
*
* This file is part of Kid3.
*
* Kid3 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Kid3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "saferename.h"
#include <QDir>
#ifdef Q_OS_WIN32
bool Utils::hasIllegalFileNameCharacters(const QString& fileName)
{
static const char illegalChars[] = "<>:\"|?*";
for (const char* chPtr = illegalChars; *chPtr; ++chPtr) {
if (fileName.contains(*chPtr)) {
return true;
}
}
return false;
}
#else
bool Utils::hasIllegalCharacters(const QString&)
{
return false;
}
#endif
bool Utils::safeRename(const QString& oldName, const QString& newName)
{
if (hasIllegalFileNameCharacters(newName))
return false;
return QDir().rename(oldName, newName);
}
bool Utils::safeRename(const QString& dirPath,
const QString& oldName, const QString& newName)
{
if (hasIllegalFileNameCharacters(newName))
return false;
return QDir(dirPath).rename(oldName, newName);
}
/**
* \file saferename.h
* Safely rename a file.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 16 Feb 2012
*
* Copyright (C) 2012 Urs Fleisch
*
* This file is part of Kid3.
*
* Kid3 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Kid3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SAFERENAME_H
#define SAFERENAME_H
class QString;
namespace Utils {
/**
* Check if file name has illegal characters.
*
* @param fileName file name
*
* @return true if file name contains illegal characters.
*/
bool hasIllegalFileNameCharacters(const QString& fileName);
/**
* Rename a file.
* Renames the file using QDir::rename() if @a newName does not contain
* illegal characters.
*
* @param oldName old file name
* @param newName new file name
*
* @return true if ok.
*/
bool safeRename(const QString& oldName, const QString& newName);
/**
* Rename a file.
* Renames the file using QDir::rename() if @a newName does not contain
* illegal characters.
*
* @param dirPath directory path
* @param oldName old file name
* @param newName new file name
*
* @return true if ok.
*/
bool safeRename(const QString& dirPath,
const QString& oldName, const QString& newName);
}
#endif // SAFERENAME_H
......@@ -96,6 +96,7 @@
#include "dirrenamer.h"
#include "movetotrash.h"
#include "qtcompatmac.h"
#include "saferename.h"
#ifdef HAVE_PHONON
#include "audioplayer.h"
#endif
......@@ -1970,7 +1971,7 @@ void Kid3MainWindow::renameFile()
FileProxyModel::releaseTaggedFileOfIndex(index);
}
QString newPath = dirName + '/' + newFileName;
if (!QDir().rename(absFilename, newPath)) {
if (!Utils::safeRename(absFilename, newPath)) {
QMessageBox::warning(
0, i18n("File Error"),
i18n("Error while renaming:\n") +
......
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