diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2021-06-25 11:30:26 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2021-06-25 15:34:10 +0300 |
commit | 79815538e2eeed946b8933b3aecd73c69d6a12a3 (patch) | |
tree | 6f9780487b1cf3c7cdab47fb7ce27d1d1ece817a /src/common | |
parent | 9117275c67ca311152b4f16749c6fd32c3d40fe9 (diff) |
Add utility to check whether a path is child of
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/filesystembase.cpp | 9 | ||||
-rw-r--r-- | src/common/filesystembase.h | 2 | ||||
-rw-r--r-- | src/common/utility.cpp | 10 |
3 files changed, 17 insertions, 4 deletions
diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index a27e46b3a..a4f99ccaa 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -535,6 +535,15 @@ QString FileSystem::pathtoUNC(const QString &_str) } #endif +bool FileSystem::isChildPathOf(QStringView child, QStringView parent) +{ + static const auto Casing = Utility::fsCasePreserving() ? Qt::CaseInsensitive : Qt::CaseSensitive; + // ignore additional / in the assert + Q_ASSERT(parent.startsWith(QFileInfo(parent.toString()).canonicalFilePath(), Casing)); + Q_ASSERT(child.startsWith(QFileInfo(child.toString()).canonicalFilePath(), Casing)); + return child.startsWith(parent, Casing); +} + } // namespace OCC #include "moc_filesystembase.cpp" diff --git a/src/common/filesystembase.h b/src/common/filesystembase.h index b8c2389ff..a7556240d 100644 --- a/src/common/filesystembase.h +++ b/src/common/filesystembase.h @@ -163,6 +163,8 @@ namespace FileSystem { * Returns whether the file is a junction (windows only) */ bool OCSYNC_EXPORT isJunction(const QString &filename); + + bool OCSYNC_EXPORT isChildPathOf(QStringView child, QStringView parent); } /** @} */ diff --git a/src/common/utility.cpp b/src/common/utility.cpp index 225d01e3d..08576c81e 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -263,10 +263,12 @@ void Utility::usleep(int usec) } // This can be overriden from the tests -OCSYNC_EXPORT bool fsCasePreserving_override = []()-> bool { - QByteArray env = qgetenv("OWNCLOUD_TEST_CASE_PRESERVING"); - if (!env.isEmpty()) - return env.toInt(); +OCSYNC_EXPORT bool fsCasePreserving_override = []() -> bool { + static bool ok = false; + static int env = qEnvironmentVariableIntValue("OWNCLOUD_TEST_CASE_PRESERVING", &ok); + if (ok) { + return env; + } return Utility::isWindows() || Utility::isMac(); }(); |