aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/fileutils_mac.mm
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@theqtcompany.com>2015-09-01 15:59:27 +0200
committerEike Ziller <eike.ziller@theqtcompany.com>2015-09-10 11:46:43 +0000
commitc87d3e3f4e7f5b74cdea71115f298d4b01936ec9 (patch)
tree8b918131856f661930935318fdc5fbbbaafcb1ea /src/libs/utils/fileutils_mac.mm
parentedfe837192012857d8c1ba070e7847d01dd047a0 (diff)
Normalize file paths on OS X
On a case insensitive file system, we want to e.g. open 'foo.H' when switching between header and source from 'foo.cpp' if that is how the file appears on the file system. Since there doesn't seem to be a way to normalize the full path without resolving symlinks, do it for each path component in turn. Also add a header for the mac specific utility functions. Task-number: QTCREATORBUG-13507 Change-Id: I6cf51fed698d12ac56fa1ec051da1b893bb0b065 Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com> Reviewed-by: Morten Johan Sørvig <morten.sorvig@theqtcompany.com> Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
Diffstat (limited to 'src/libs/utils/fileutils_mac.mm')
-rw-r--r--src/libs/utils/fileutils_mac.mm39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/libs/utils/fileutils_mac.mm b/src/libs/utils/fileutils_mac.mm
index 1c149bbf67..27c0f127b8 100644
--- a/src/libs/utils/fileutils_mac.mm
+++ b/src/libs/utils/fileutils_mac.mm
@@ -28,8 +28,13 @@
**
****************************************************************************/
+#include "fileutils_mac.h"
+
#include "autoreleasepool.h"
+#include "qtcassert.h"
+#include <QDir>
+#include <QFileInfo>
#include <QUrl>
#include <Foundation/NSURL.h>
@@ -47,5 +52,39 @@ QUrl filePathUrl(const QUrl &url)
return ret;
}
+QString normalizePathName(const QString &filePath)
+{
+ AutoreleasePool pool; Q_UNUSED(pool)
+
+ // NSURL getResourceValue returns values based on the cleaned path so we need to work on that.
+ // It also returns the disk name for "/" and "/.." and errors on "" and relative paths,
+ // so avoid that
+
+ // we cannot know the normalized name for relative paths
+ if (QFileInfo(filePath).isRelative())
+ return filePath;
+
+ QString result;
+ QString path = QDir::cleanPath(filePath);
+ // avoid empty paths and paths like "/../foo" or "/.."
+ if (path.isEmpty() || path.contains(QLatin1String("/../")) || path.endsWith(QLatin1String("/..")))
+ return filePath;
+
+ while (path != QLatin1String("/") /*be defensive->*/&& path != QLatin1String(".") && !path.isEmpty()) {
+ QFileInfo info(path);
+ NSURL *nsurl = [NSURL fileURLWithPath:path.toNSString()];
+ NSString *out;
+ QString component;
+ if ([nsurl getResourceValue:(NSString **)&out forKey:NSURLNameKey error:nil])
+ component = QString::fromNSString(out);
+ else // e.g. if the full path does not exist
+ component = info.fileName();
+ result.prepend(QLatin1Char('/') + component);
+ path = info.path();
+ }
+ QTC_ASSERT(path == QLatin1String("/"), return filePath);
+ return result;
+}
+
} // Internal
} // Utils