From ee515dd842d79fa4543568ed82bd7c949923e438 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 29 Jan 2023 21:52:51 +0200 Subject: QStandardPaths/Unix: fix logic in xdgDataDirs() function This method correctly ignores relative paths (as per the XDG basedir spec), but checking the list of dirs is empty should be moved to after splitting the env var, because even if the env var is not empty, if the paths in it are all relative the resulting list will be empty. Drive-by change: Split some code to a static helper, which will be used in xdgConfigDirs() too. Change-Id: If894751ba68b24ccc214f9a4bb2099be3f0e4349 Reviewed-by: Thiago Macieira --- src/corelib/io/qstandardpaths_unix.cpp | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp index 038cdd3b8d..07325ae9c8 100644 --- a/src/corelib/io/qstandardpaths_unix.cpp +++ b/src/corelib/io/qstandardpaths_unix.cpp @@ -326,29 +326,36 @@ QString QStandardPaths::writableLocation(StandardLocation type) return path; } -static QStringList xdgDataDirs() +static QStringList dirsList(const QString &xdgEnvVar) { QStringList dirs; + // http://standards.freedesktop.org/basedir-spec/latest/ + // Normalize paths, skip relative paths (the spec says relative paths + // should be ignored) + for (const auto dir : qTokenize(xdgEnvVar, u':')) + if (dir.startsWith(u'/')) + dirs.push_back(QDir::cleanPath(dir.toString())); + + // Remove duplicates from the list, there's no use for duplicated + // paths in XDG_DATA_DIRS - if it's not found in the given + // directory the first time, it won't be there the second time. + // Plus duplicate paths causes problems for example for mimetypes, + // where duplicate paths here lead to duplicated mime types returned + // for a file, eg "text/plain,text/plain" instead of "text/plain" + dirs.removeDuplicates(); + + return dirs; +} + +static QStringList xdgDataDirs() +{ // http://standards.freedesktop.org/basedir-spec/latest/ QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS")); - if (xdgDataDirsEnv.isEmpty()) { - dirs.append(QString::fromLatin1("/usr/local/share")); - dirs.append(QString::fromLatin1("/usr/share")); - } else { - // Normalize paths, skip relative paths - for (const auto dir : qTokenize(xdgDataDirsEnv, u':')) { - if (dir.startsWith(u'/')) - dirs.push_back(QDir::cleanPath(dir.toString())); - } - // Remove duplicates from the list, there's no use for duplicated - // paths in XDG_DATA_DIRS - if it's not found in the given - // directory the first time, it won't be there the second time. - // Plus duplicate paths causes problems for example for mimetypes, - // where duplicate paths here lead to duplicated mime types returned - // for a file, eg "text/plain,text/plain" instead of "text/plain" - dirs.removeDuplicates(); - } + QStringList dirs = dirsList(xdgDataDirsEnv); + if (dirs.isEmpty()) + dirs = QStringList{u"/usr/local/share"_s, u"/usr/share"_s}; + return dirs; } -- cgit v1.2.3