Commit 7d22c101 authored by Vlad Brovko's avatar Vlad Brovko Committed by Oswald Buddenhagen
Browse files

don't rewrite symlink on save, rewrite target instead



Merge-request: 399
Reviewed-by: default avatarOswald Buddenhagen <oswald.buddenhagen@nokia.com>

Change-Id: If81446a8fd49e9de7611c2f09abd39af2c9c930d
parent db31112e
......@@ -92,6 +92,18 @@ namespace Utils {
\return Whether at least one file in \a filePath has a newer date than \a timeStamp.
*/
/*!
\fn Utils::FileUtils::resolveSymlinks(const QString &filePath)
Recursively resolve possibly present symlinks in \a filePath.
Unlike QFileInfo::canonicalFilePath(), this function will still return the expected target file
even if the symlink is dangling.
\note Maximum recursion depth == 16.
return Symlink target file path.
*/
bool FileUtils::removeRecursively(const QString &filePath, QString *error)
{
QFileInfo fileInfo(filePath);
......@@ -197,6 +209,16 @@ bool FileUtils::isFileNewerThan(const QString &filePath,
return false;
}
QString FileUtils::resolveSymlinks(const QString &path)
{
QFileInfo f(path);
int links = 16;
while (links-- && f.isSymLink())
f.setFile(f.symLinkTarget());
if (links <= 0)
return QString();
return f.filePath();
}
QByteArray FileReader::fetchQrc(const QString &fileName)
......
......@@ -57,6 +57,7 @@ public:
const QString &tgtFilePath, QString *error = 0);
static bool isFileNewerThan(const QString &filePath,
const QDateTime &timeStamp);
static QString resolveSymlinks(const QString &path);
};
class QTCREATOR_UTILS_EXPORT FileReader
......
......@@ -32,6 +32,7 @@
#include "savefile.h"
#include "qtcassert.h"
#include "fileutils.h"
namespace Utils {
......@@ -84,11 +85,12 @@ bool SaveFile::commit()
return false;
}
QString bakname = m_finalFileName + QLatin1Char('~');
QString finalFileName = Utils::FileUtils::resolveSymlinks(m_finalFileName);
QString bakname = finalFileName + QLatin1Char('~');
QFile::remove(bakname); // Kill old backup
QFile::rename(m_finalFileName, bakname); // Backup current file
if (!rename(m_finalFileName)) { // Replace current file
QFile::rename(bakname, m_finalFileName); // Rollback to current file
QFile::rename(finalFileName, bakname); // Backup current file
if (!rename(finalFileName)) { // Replace current file
QFile::rename(bakname, finalFileName); // Rollback to current file
return false;
}
if (!m_backup)
......
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