From 23c86b29948f01e06984f82bf33716b880a75d96 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Mon, 30 Mar 2015 13:46:51 +0100
Subject: [PATCH 6/9] Don't remove files that live outside of path

If a file is symlinked outside of path in QInstaller::removeFiles
then it gets removed regardless. This will cause e.g. users' HOME
directories to get removed on MSYS2.
---
 src/libs/installer/fileutils.cpp | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/libs/installer/fileutils.cpp b/src/libs/installer/fileutils.cpp
index 04a24ad..2ccca71 100644
--- a/src/libs/installer/fileutils.cpp
+++ b/src/libs/installer/fileutils.cpp
@@ -178,8 +178,30 @@ void QInstaller::removeFiles(const QString &path, bool ignoreErrors)
     const QFileInfoList entries = QDir(path).entryInfoList(QDir::AllEntries | QDir::Hidden);
     foreach (const QFileInfo &fi, entries) {
         if (fi.isSymLink() || fi.isFile()) {
+            // Don't remove symlinks that point outside of path
+            // as this can delete for example, the HOME folder.
+            QFileInfo toRoot(fi);
+            QString prevToRoot;
+            bool symLinksOutsidePath = false;
+            do
+            {
+                if (toRoot.isSymLink()) {
+                    QString symLinkTarget = toRoot.symLinkTarget();
+                    if (symLinkTarget.indexOf(path) != 0) {
+                        symLinksOutsidePath = true;
+                        break;
+                    }
+                }
+                prevToRoot = toRoot.absoluteFilePath();
+                toRoot = QFileInfo(toRoot.dir().absolutePath());
+            } while (toRoot.exists() && toRoot.dir().absolutePath() != prevToRoot);
             QString filePath = fi.filePath();
             QFile f(filePath);
+            if (symLinksOutsidePath) {
+                qDebug() << "Not removing " << f.fileName()
+                         << " as it symlinks outside path " << path;
+                continue;
+            }
             bool ok = f.remove();
             if (!ok) { //ReadOnly can prevent removing in Windows. Change permission and try again.
                 const QFile::Permissions permissions = f.permissions();
-- 
2.7.0
