From 793f0ddec2a1ac04360adf468197fe76f75051d9 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Fri, 18 Aug 2017 12:35:27 +0200 Subject: Win: Handle installation of read-only target files correctly When the source file is read-only then it would copy the file with that attribute set when it is installed. However this will cause a problem if it is installed a second time. Therefore the read-only attribute needs to be manually reset before installing and again before touching the file. Once the process is done then it is set back to be read-only to preserve the state of the original. Change-Id: I1c01f418ef3c9bd434acd2c2b8ee695544d7bb35 Reviewed-by: Oswald Buddenhagen --- qmake/main.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'qmake') diff --git a/qmake/main.cpp b/qmake/main.cpp index 13b18d018a..85709dc9bf 100644 --- a/qmake/main.cpp +++ b/qmake/main.cpp @@ -245,9 +245,15 @@ static int doLink(int argc, char **argv) static int installFile(const QString &source, const QString &target, bool exe = false) { QFile sourceFile(source); - - QFile::remove(target); - QDir::root().mkpath(QFileInfo(target).absolutePath()); + QFile targetFile(target); + if (targetFile.exists()) { +#ifdef Q_OS_WIN + targetFile.setPermissions(targetFile.permissions() | QFile::WriteUser); +#endif + QFile::remove(target); + } else { + QDir::root().mkpath(QFileInfo(target).absolutePath()); + } if (!sourceFile.copy(target)) { fprintf(stderr, "Error copying %s to %s: %s\n", source.toLatin1().constData(), qPrintable(target), qPrintable(sourceFile.errorString())); @@ -255,7 +261,6 @@ static int installFile(const QString &source, const QString &target, bool exe = } if (exe) { - QFile targetFile(target); if (!targetFile.setPermissions(sourceFile.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeUser | QFileDevice::ExeGroup | QFileDevice::ExeOther)) { fprintf(stderr, "Error setting execute permissions on %s: %s\n", @@ -266,10 +271,20 @@ static int installFile(const QString &source, const QString &target, bool exe = // Copy file times QString error; +#ifdef Q_OS_WIN + const QFile::Permissions permissions = targetFile.permissions(); + const bool readOnly = !(permissions & QFile::WriteUser); + if (readOnly) + targetFile.setPermissions(permissions | QFile::WriteUser); +#endif if (!IoUtils::touchFile(target, sourceFile.fileName(), &error)) { fprintf(stderr, "%s", qPrintable(error)); return 3; } +#ifdef Q_OS_WIN + if (readOnly) + targetFile.setPermissions(permissions); +#endif return 0; } -- cgit v1.2.3