From 2a86bf8ce018cdca3c9e93fb36102ae440d1a114 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Thu, 8 Jan 2015 10:29:12 +0100 Subject: [PATCH] Android: Fix canonical form of nonexistent paths On some Android devices, the realpath() implementation will return the full path even if the path does not exist. This breaks the expectation of the canonical path, which should be empty for nonexistent paths. A few autotests failed due to this. To work around it, we query existence before getting the canonical path. [ChangeLog][Android] Fixed canonical path for nonexistent paths on some devices. Change-Id: I5c1dabb8b8394694bc74d2a91912800aaff6b9e3 Task-number: QTBUG-43705 Reviewed-by: Thiago Macieira --- src/corelib/io/qfilesystemengine_unix.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 11d421591ac..feb86d28952 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -250,6 +250,26 @@ QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry, return QFileSystemEntry(ret); } } + +# elif defined(Q_OS_ANDROID) + // On some Android versions, realpath() will return a path even if it does not exist + // To work around this, we check existence in advance. + if (!data.hasFlags(QFileSystemMetaData::ExistsAttribute)) + fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute); + + if (!data.exists()) { + ret = 0; + errno = ENOENT; + } else { + ret = (char*)malloc(PATH_MAX + 1); + if (realpath(entry.nativeFilePath().constData(), (char*)ret) == 0) { + const int savedErrno = errno; // errno is checked below, and free() might change it + free(ret); + errno = savedErrno; + ret = 0; + } + } + # else # if _POSIX_VERSION >= 200801L ret = realpath(entry.nativeFilePath().constData(), (char*)0);