2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-04-19 17:57:05 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2023-01-11 20:30:30 +00:00
|
|
|
* Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-05-26 11:52:44 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2022-12-05 17:45:19 +00:00
|
|
|
#include <AK/Platform.h>
|
2023-01-11 20:30:30 +00:00
|
|
|
#include <AK/String.h>
|
2020-04-19 17:57:05 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2022-12-15 12:18:15 +00:00
|
|
|
#include <LibCore/SessionManagement.h>
|
2020-04-19 17:57:05 +00:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <pwd.h>
|
2019-05-25 23:58:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-27 19:17:23 +00:00
|
|
|
#if defined(AK_OS_HAIKU)
|
|
|
|
# include <FindDirectory.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-19 17:57:05 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::home_directory()
|
2019-05-26 17:14:03 +00:00
|
|
|
{
|
|
|
|
if (auto* home_env = getenv("HOME"))
|
2020-05-26 11:52:44 +00:00
|
|
|
return LexicalPath::canonicalized_path(home_env);
|
2019-05-26 17:14:03 +00:00
|
|
|
|
2019-08-03 06:32:07 +00:00
|
|
|
auto* pwd = getpwuid(getuid());
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = pwd ? pwd->pw_dir : "/";
|
2019-08-03 06:32:07 +00:00
|
|
|
endpwent();
|
2020-05-26 11:52:44 +00:00
|
|
|
return LexicalPath::canonicalized_path(path);
|
2020-04-19 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::desktop_directory()
|
2020-04-19 17:57:05 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/Desktop"sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
2020-04-19 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::documents_directory()
|
2022-09-23 14:34:06 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
|
|
|
builder.append("/Documents"sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
2022-09-23 14:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::downloads_directory()
|
2020-05-05 21:56:57 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/Downloads"sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
2020-05-05 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::config_directory()
|
2020-08-05 15:18:59 +00:00
|
|
|
{
|
2022-12-05 17:45:19 +00:00
|
|
|
if (auto* config_directory = getenv("XDG_CONFIG_HOME"))
|
|
|
|
return LexicalPath::canonicalized_path(config_directory);
|
|
|
|
|
2020-08-05 15:18:59 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-12-05 17:45:19 +00:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
builder.append("/Library/Preferences"sv);
|
2023-08-27 19:17:23 +00:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/config/settings"sv);
|
2022-12-05 17:45:19 +00:00
|
|
|
#else
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/.config"sv);
|
2022-12-05 17:45:19 +00:00
|
|
|
#endif
|
2022-12-06 01:12:49 +00:00
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
2020-08-05 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:46:53 +00:00
|
|
|
DeprecatedString StandardPaths::data_directory()
|
|
|
|
{
|
|
|
|
if (auto* data_directory = getenv("XDG_DATA_HOME"))
|
|
|
|
return LexicalPath::canonicalized_path(data_directory);
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
|
|
|
#if defined(AK_OS_SERENITY)
|
|
|
|
builder.append("/.data"sv);
|
|
|
|
#elif defined(AK_OS_MACOS)
|
|
|
|
builder.append("/Library/Application Support"sv);
|
2023-08-27 19:17:23 +00:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/config/non-packaged/data"sv);
|
2022-12-05 17:46:53 +00:00
|
|
|
#else
|
|
|
|
builder.append("/.local/share"sv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:15:17 +00:00
|
|
|
ErrorOr<DeprecatedString> StandardPaths::runtime_directory()
|
|
|
|
{
|
|
|
|
if (auto* data_directory = getenv("XDG_RUNTIME_DIR"))
|
|
|
|
return LexicalPath::canonicalized_path(data_directory);
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
#if defined(AK_OS_SERENITY)
|
2022-12-15 12:18:15 +00:00
|
|
|
auto sid = TRY(Core::SessionManagement::root_session_id());
|
2022-12-09 16:15:17 +00:00
|
|
|
builder.appendff("/tmp/session/{}", sid);
|
|
|
|
#elif defined(AK_OS_MACOS)
|
|
|
|
builder.append(home_directory());
|
|
|
|
builder.append("/Library/Application Support"sv);
|
2023-08-27 19:17:23 +00:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/boot/system/var/shared_memory"sv);
|
2022-12-09 16:15:17 +00:00
|
|
|
#else
|
|
|
|
auto uid = getuid();
|
|
|
|
builder.appendff("/run/user/{}", uid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LexicalPath::canonicalized_path(builder.to_deprecated_string());
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString StandardPaths::tempfile_directory()
|
2020-04-19 17:57:05 +00:00
|
|
|
{
|
|
|
|
return "/tmp";
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:30:30 +00:00
|
|
|
ErrorOr<Vector<String>> StandardPaths::font_directories()
|
|
|
|
{
|
2023-08-27 19:17:23 +00:00
|
|
|
#if defined(AK_OS_HAIKU)
|
|
|
|
Vector<String> paths_vector;
|
|
|
|
char** paths;
|
|
|
|
size_t paths_count;
|
|
|
|
if (find_paths(B_FIND_PATH_FONTS_DIRECTORY, NULL, &paths, &paths_count) == B_OK) {
|
|
|
|
for (size_t i = 0; i < paths_count; ++i) {
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(paths[i], strlen(paths[i]));
|
|
|
|
paths_vector.append(TRY(builder.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths_vector;
|
|
|
|
#else
|
2023-01-11 20:30:30 +00:00
|
|
|
return Vector { {
|
2023-08-27 19:17:23 +00:00
|
|
|
# if defined(AK_OS_SERENITY)
|
2023-08-07 09:12:38 +00:00
|
|
|
"/res/fonts"_string,
|
2023-08-27 19:17:23 +00:00
|
|
|
# elif defined(AK_OS_MACOS)
|
2023-08-07 09:12:38 +00:00
|
|
|
"/System/Library/Fonts"_string,
|
|
|
|
"/Library/Fonts"_string,
|
2023-01-11 20:30:30 +00:00
|
|
|
TRY(String::formatted("{}/Library/Fonts"sv, home_directory())),
|
2023-08-27 19:17:23 +00:00
|
|
|
# else
|
2023-08-07 09:12:38 +00:00
|
|
|
"/usr/share/fonts"_string,
|
|
|
|
"/usr/local/share/fonts"_string,
|
2023-01-11 20:30:30 +00:00
|
|
|
TRY(String::formatted("{}/.local/share/fonts"sv, home_directory())),
|
2023-08-27 19:17:23 +00:00
|
|
|
# endif
|
2023-01-11 20:30:30 +00:00
|
|
|
} };
|
2023-08-27 19:17:23 +00:00
|
|
|
#endif
|
2023-01-11 20:30:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:58:22 +00:00
|
|
|
}
|