From 37fcf98d4280068e15a40fd0ce5bcc55f47c8dd3 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 29 Jan 2023 21:52:51 +0200 Subject: [PATCH] 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. Reviewed-by: Thiago Macieira Change-Id: If894751ba68b24ccc214f9a4bb2099be3f0e4349 (cherry picked from commit ee515dd842d79fa4543568ed82bd7c949923e438) (cherry picked from commit 668560f8bd13bd1478c1837ef0dc0eb33f6445f9) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qstandardpaths_unix.cpp | 45 +++++++++++++++----------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp index 038cdd3b8da..07325ae9c89 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/ - 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())); - } + // 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")); + + QStringList dirs = dirsList(xdgDataDirsEnv); + if (dirs.isEmpty()) + dirs = QStringList{u"/usr/local/share"_s, u"/usr/share"_s}; - // 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; }