Skip to content

Commit

Permalink
Refactor validateDataPath loops to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
scribblemaniac committed Jun 6, 2024
1 parent fb2f6fb commit a82bd7b
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions core_lib/src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,41 +150,37 @@ QString validateDataPath(QString filePath, QString dataDirPath)
// File does not exist, use absolute path and attempt to resolve symlinks again for each parent directory
fi.setFile(fi.absoluteFilePath());
QDir ancestor(fi.absoluteFilePath());
while (true) {
if (ancestor == dataDir)
while (ancestor != dataDir) {
if (ancestor.isRoot())
{
// Found data dir in parents
return fi.absoluteFilePath();
// Reached root directory without finding data dir
return QString();
}
QDir newAncestor = QFileInfo(ancestor.absolutePath()).dir();
if (newAncestor.exists())
{
// Resolve directory symlinks
newAncestor.setPath(newAncestor.canonicalPath());
}
if (ancestor == newAncestor)
{
// Reached root directory without finding data dir
return QString();
}
ancestor = newAncestor;
};
}
// One of the parent directories of filePath matches dataDir
return fi.absoluteFilePath();
}
else
{
// File exists and all symlinks have been resolved in canonicalPath
// File exists and all symlinks have been resolved in canonicalPath so no further attempts to resolve symlinks are necessary
fi.setFile(canonicalPath);
QDir ancestor = fi.dir();
while (ancestor != dataDir)
{
QDir newAncestor = QFileInfo(ancestor.absolutePath()).dir();
if (ancestor == newAncestor)
{
if (ancestor.isRoot()) {
// Data dir was not found in ancestors of the src path
return QString();
}
ancestor = newAncestor;
ancestor = QFileInfo(ancestor.absolutePath()).dir();
}
// One of the parent directories of filePath matches dataDir
return fi.absoluteFilePath();
}
}

0 comments on commit a82bd7b

Please sign in to comment.