Truly independent web browser
LibCore: Read the XDG user directories from user-dirs.dirs
sideshowbarker2763a4ef83a7
diff --git a/Libraries/LibCore/StandardPaths.cpp b/Libraries/LibCore/StandardPaths.cppindex 9b6c68c5f10..0a295e4d832 100644--- a/Libraries/LibCore/StandardPaths.cpp+++ b/Libraries/LibCore/StandardPaths.cpp@@ -12,6 +12,7 @@#include <AK/StringBuilder.h>#include <AK/StringUtils.h>#include <LibCore/Environment.h>+#include <LibCore/File.h>#include <LibCore/StandardPaths.h>#include <LibCore/System.h>#include <stdlib.h>@@ -31,6 +32,71 @@ static Optional<StringView> get_environment_if_not_empty(StringView name)return maybe_value;}+#if !defined(AK_OS_WINDOWS)+// https://www.freedesktop.org/wiki/Software/xdg-user-dirs/+// Desktop environments keep the user's directories in the user-dirs.dirs file, under names in the user's language, as+// shell assignments like XDG_DOWNLOAD_DIR="$HOME/Downloads": Each path is absolute, or relative to $HOME.+static Optional<ByteString> user_directory_from_user_dirs_file(StringView name)+{+ // https://specifications.freedesktop.org/basedir-spec/latest/+ // If an implementation encounters a relative path in any of these variables it should consider the path invalid+ // and ignore it.+ ByteString path;+ if (auto config_home = get_environment_if_not_empty("XDG_CONFIG_HOME"sv); config_home.has_value() && config_home->starts_with('/'))+ path = ByteString::formatted("{}/user-dirs.dirs", *config_home);+ else+ path = ByteString::formatted("{}/.config/user-dirs.dirs", StandardPaths::home_directory());++ auto file = File::open(path, File::OpenMode::Read);+ if (file.is_error())+ return {};+ auto contents = file.value()->read_until_eof();+ if (contents.is_error())+ return {};++ // NB: As when a shell runs the file, a later assignment of the same name replaces an earlier one.+ Optional<ByteString> directory;+ for (auto line : StringView { contents.value() }.lines()) {+ line = line.trim_whitespace();+ if (!line.starts_with(name))+ continue;+ auto value = line.substring_view(name.length()).trim_whitespace(TrimMode::Left);+ if (!value.starts_with('='))+ continue;+ value = value.substring_view(1).trim_whitespace(TrimMode::Left);+ if (!value.starts_with('"'))+ continue;+ value = value.substring_view(1);++ StringBuilder builder;+ if (value.starts_with("$HOME"sv)) {+ builder.append(StandardPaths::home_directory());+ value = value.substring_view(5);+ } else if (!value.starts_with('/')) {+ continue;+ }+ // As in a double-quoted shell string, a backslash escapes only $, `, " and \; before others, it's literal.+ // The string ends at the first unescaped quote, after which only a comment can follow.+ Optional<size_t> closing_quote;+ for (size_t i = 0; i < value.length(); ++i) {+ if (value[i] == '"') {+ closing_quote = i;+ break;+ }+ if (value[i] == '\\' && i + 1 < value.length() && "$`\"\\"sv.contains(value[i + 1]))+ ++i;+ builder.append(value[i]);+ }+ if (!closing_quote.has_value())+ continue;+ if (auto rest = value.substring_view(*closing_quote + 1).trim_whitespace(); !rest.is_empty() && !rest.starts_with('#'))+ continue;+ directory = LexicalPath::canonicalized_path(builder.to_byte_string());+ }+ return directory;+}+#endif+ByteString StandardPaths::home_directory(){#if defined(AK_OS_WINDOWS)@@ -51,6 +117,8 @@ ByteString StandardPaths::desktop_directory()#if !defined(AK_OS_WINDOWS)if (auto desktop_directory = get_environment_if_not_empty("XDG_DESKTOP_DIR"sv); desktop_directory.has_value())return LexicalPath::canonicalized_path(*desktop_directory);+ if (auto desktop_directory = user_directory_from_user_dirs_file("XDG_DESKTOP_DIR"sv); desktop_directory.has_value())+ return desktop_directory.release_value();#endifStringBuilder builder;@@ -64,6 +132,8 @@ ByteString StandardPaths::documents_directory()#if !defined(AK_OS_WINDOWS)if (auto documents_directory = get_environment_if_not_empty("XDG_DOCUMENTS_DIR"sv); documents_directory.has_value())return LexicalPath::canonicalized_path(*documents_directory);+ if (auto documents_directory = user_directory_from_user_dirs_file("XDG_DOCUMENTS_DIR"sv); documents_directory.has_value())+ return documents_directory.release_value();#endifStringBuilder builder;@@ -77,6 +147,8 @@ ByteString StandardPaths::downloads_directory()#if !defined(AK_OS_WINDOWS)if (auto downloads_directory = get_environment_if_not_empty("XDG_DOWNLOAD_DIR"sv); downloads_directory.has_value())return LexicalPath::canonicalized_path(*downloads_directory);+ if (auto downloads_directory = user_directory_from_user_dirs_file("XDG_DOWNLOAD_DIR"sv); downloads_directory.has_value())+ return downloads_directory.release_value();#endifStringBuilder builder;@@ -90,6 +162,8 @@ ByteString StandardPaths::music_directory()#if !defined(AK_OS_WINDOWS)if (auto music_directory = get_environment_if_not_empty("XDG_MUSIC_DIR"sv); music_directory.has_value())return LexicalPath::canonicalized_path(*music_directory);+ if (auto music_directory = user_directory_from_user_dirs_file("XDG_MUSIC_DIR"sv); music_directory.has_value())+ return music_directory.release_value();#endifStringBuilder builder;@@ -103,6 +177,8 @@ ByteString StandardPaths::pictures_directory()#if !defined(AK_OS_WINDOWS)if (auto pictures_directory = get_environment_if_not_empty("XDG_PICTURES_DIR"sv); pictures_directory.has_value())return LexicalPath::canonicalized_path(*pictures_directory);+ if (auto pictures_directory = user_directory_from_user_dirs_file("XDG_PICTURES_DIR"sv); pictures_directory.has_value())+ return pictures_directory.release_value();#endifStringBuilder builder;@@ -116,6 +192,8 @@ ByteString StandardPaths::videos_directory()#if !defined(AK_OS_WINDOWS)if (auto videos_directory = get_environment_if_not_empty("XDG_VIDEOS_DIR"sv); videos_directory.has_value())return LexicalPath::canonicalized_path(*videos_directory);+ if (auto videos_directory = user_directory_from_user_dirs_file("XDG_VIDEOS_DIR"sv); videos_directory.has_value())+ return videos_directory.release_value();#endifStringBuilder builder;diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txtindex 4b1efffbeca..3ed97d7bf90 100644--- a/Tests/LibCore/CMakeLists.txt+++ b/Tests/LibCore/CMakeLists.txt@@ -25,6 +25,7 @@ if (APPLE OR LINUX)list(APPEND TEST_SOURCESTestLibCoreFutex.cppTestLibCoreProcess.cpp+ TestLibCoreStandardPaths.cpp)endif()diff --git a/Tests/LibCore/TestLibCoreStandardPaths.cpp b/Tests/LibCore/TestLibCoreStandardPaths.cppnew file mode 100644index 00000000000..e5e7777754d--- /dev/null+++ b/Tests/LibCore/TestLibCoreStandardPaths.cpp@@ -0,0 +1,126 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/ByteBuffer.h>+#include <AK/ByteString.h>+#include <AK/LexicalPath.h>+#include <LibCore/Directory.h>+#include <LibCore/Environment.h>+#include <LibCore/StandardPaths.h>+#include <LibFileSystem/FileSystem.h>+#include <LibTest/TestCase.h>+#include <stdlib.h>++static ByteString make_home_with_user_dirs_file(StringView contents)+{+ // mkdtemp() creates a private directory atomically, under a name nobody could have set up beforehand.+ auto template_path = LexicalPath::join(Core::StandardPaths::tempfile_directory(), "test-libcore-standard-paths-XXXXXX"sv).string();+ auto buffer = MUST(ByteBuffer::copy(template_path.bytes()));+ buffer.append('\0');+ auto* created = mkdtemp(reinterpret_cast<char*>(buffer.data()));+ VERIFY(created);+ ByteString home { created };++ auto config = MUST(Core::Directory::create(LexicalPath::join(home, ".config"sv), Core::Directory::CreateDirectories::Yes));+ auto file = MUST(config.open("user-dirs.dirs"sv, Core::File::OpenMode::Write));+ MUST(file->write_until_depleted(contents.bytes()));++ MUST(Core::Environment::set("HOME"sv, home, Core::Environment::Overwrite::Yes));+ MUST(Core::Environment::unset("XDG_CONFIG_HOME"sv));+ MUST(Core::Environment::unset("XDG_DESKTOP_DIR"sv));+ MUST(Core::Environment::unset("XDG_DOWNLOAD_DIR"sv));+ MUST(Core::Environment::unset("XDG_DOCUMENTS_DIR"sv));+ MUST(Core::Environment::unset("XDG_MUSIC_DIR"sv));+ MUST(Core::Environment::unset("XDG_PICTURES_DIR"sv));+ return home;+}++TEST_CASE(user_dirs_file_sets_user_directories)+{+ auto home = make_home_with_user_dirs_file(R"~~~(# This file is written by xdg-user-dirs-update+XDG_DESKTOP_DIR="$HOME/Bureau"+XDG_DOWNLOAD_DIR="$HOME/T\"el\\echargements"+XDG_DOCUMENTS_DIR="/srv/documents"+XDG_PICTURES_DIR="$HOME/a\bc"+XDG_MUSIC_DIR=relative/music+XDG_DOWNLOAD_DIRECTORY="$HOME/Wrong"+)~~~"sv);++ EXPECT_EQ(Core::StandardPaths::desktop_directory(), LexicalPath::join(home, "Bureau"sv).string());+ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "T\"el\\echargements"sv).string());+ EXPECT_EQ(Core::StandardPaths::documents_directory(), "/srv/documents"sv);+ EXPECT_EQ(Core::StandardPaths::pictures_directory(), LexicalPath::join(home, "a\\bc"sv).string());++ // An unquoted or relative path isn't a valid entry, so the default directory stands in.+ EXPECT_EQ(Core::StandardPaths::music_directory(), LexicalPath::join(home, "Music"sv).string());++ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}++TEST_CASE(last_valid_assignment_in_user_dirs_file_wins)+{+ auto home = make_home_with_user_dirs_file(R"~~~(XDG_DOWNLOAD_DIR="$HOME/Old"+XDG_DOWNLOAD_DIR="$HOME/New"+XDG_DOWNLOAD_DIR=not-quoted+)~~~"sv);++ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "New"sv).string());++ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}++TEST_CASE(user_dirs_file_entry_can_end_in_a_comment)+{+ auto home = make_home_with_user_dirs_file(R"~~~(XDG_DOWNLOAD_DIR="$HOME/Hamtningar" # preferred folder+XDG_MUSIC_DIR="$HOME/Musik" trailing-text+)~~~"sv);++ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "Hamtningar"sv).string());++ // Anything but a comment after the closing quote makes the entry invalid.+ EXPECT_EQ(Core::StandardPaths::music_directory(), LexicalPath::join(home, "Music"sv).string());++ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}++TEST_CASE(relative_xdg_config_home_is_ignored)+{+ auto home = make_home_with_user_dirs_file("XDG_DOWNLOAD_DIR=\"$HOME/Hamtningar\"\n"sv);+ MUST(Core::Environment::set("XDG_CONFIG_HOME"sv, "relative/config"sv, Core::Environment::Overwrite::Yes));++ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "Hamtningar"sv).string());++ MUST(Core::Environment::unset("XDG_CONFIG_HOME"sv));+ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}++TEST_CASE(environment_variable_takes_precedence_over_user_dirs_file)+{+ auto home = make_home_with_user_dirs_file("XDG_DOWNLOAD_DIR=\"$HOME/Hamtningar\"\n"sv);+ MUST(Core::Environment::set("XDG_DOWNLOAD_DIR"sv, "/srv/downloads"sv, Core::Environment::Overwrite::Yes));++ EXPECT_EQ(Core::StandardPaths::downloads_directory(), "/srv/downloads"sv);++ MUST(Core::Environment::unset("XDG_DOWNLOAD_DIR"sv));+ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "Hamtningar"sv).string());++ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}++TEST_CASE(user_dirs_file_in_xdg_config_home)+{+ auto home = make_home_with_user_dirs_file(""sv);+ auto config_home = LexicalPath::join(home, "elsewhere"sv).string();+ auto config = MUST(Core::Directory::create(config_home, Core::Directory::CreateDirectories::Yes));+ auto file = MUST(config.open("user-dirs.dirs"sv, Core::File::OpenMode::Write));+ MUST(file->write_until_depleted("XDG_DOWNLOAD_DIR=\"$HOME/Scaricati\"\n"sv.bytes()));+ MUST(Core::Environment::set("XDG_CONFIG_HOME"sv, config_home, Core::Environment::Overwrite::Yes));++ EXPECT_EQ(Core::StandardPaths::downloads_directory(), LexicalPath::join(home, "Scaricati"sv).string());++ MUST(Core::Environment::unset("XDG_CONFIG_HOME"sv));+ MUST(FileSystem::remove(home, FileSystem::RecursionMode::Allowed));+}