Skip to content

Commit

Permalink
Add utility to check whether a path is child of
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Jun 25, 2021
1 parent 9117275 commit 7981553
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/common/filesystembase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions src/common/filesystembase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/** @} */
Expand Down
10 changes: 6 additions & 4 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}();

Expand Down

0 comments on commit 7981553

Please sign in to comment.