Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Nate Graham
Kid3
Commits
0fe61a2d
Commit
0fe61a2d
authored
Feb 16, 2012
by
Urs Fleisch
Browse files
Avoid truncation of file when renaming with illegal characters on Windows.
parent
1e3aee16
Changes
7
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
0fe61a2d
...
...
@@ -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
"git20120
129
"
)
set
(
CPACK_PACKAGE_VERSION
"git20120
216
"
)
set
(
RELEASE_YEAR 2012
)
if
(
WITH_GCC_PCH
)
...
...
src/core/model/dirrenamer.cpp
View file @
0fe61a2d
...
...
@@ -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
().
r
ename
(
olddir
,
newdir
)
&&
QFileInfo
(
newdir
).
isDir
())
{
if
(
Utils
::
safeR
ename
(
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
().
r
ename
(
oldfn
,
newfn
)
&&
QFileInfo
(
newfn
).
isFile
())
{
if
(
Utils
::
safeR
ename
(
oldfn
,
newfn
)
&&
QFileInfo
(
newfn
).
isFile
())
{
return
true
;
}
else
{
if
(
errorMsg
)
{
...
...
src/core/tags/taggedfile.cpp
View file @
0fe61a2d
...
...
@@ -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
;
...
...
src/core/utils/Sources.cmake
View file @
0fe61a2d
set
(
utils_SRCS
utils/movetotrash.cpp
utils/saferename.cpp
)
set
(
utils_MOC_HDRS
)
src/core/utils/saferename.cpp
0 → 100644
View file @
0fe61a2d
/**
* \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
);
}
src/core/utils/saferename.h
0 → 100644
View file @
0fe61a2d
/**
* \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
src/gui/forms/kid3mainwindow.cpp
View file @
0fe61a2d
...
...
@@ -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
().
r
ename
(
absFilename
,
newPath
))
{
if
(
!
Utils
::
safeR
ename
(
absFilename
,
newPath
))
{
QMessageBox
::
warning
(
0
,
i18n
(
"File Error"
),
i18n
(
"Error while renaming:
\n
"
)
+
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment