2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-04-19 19:57:05 +02: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 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2022-12-05 12:45:19 -05:00
|
|
|
#include <AK/Platform.h>
|
2023-01-11 20:30:30 +00:00
|
|
|
#include <AK/String.h>
|
2020-04-19 19:57:05 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2022-12-15 07:18:15 -05:00
|
|
|
#include <LibCore/SessionManagement.h>
|
2020-04-19 19:57:05 +02:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2023-12-28 11:29:14 +01:00
|
|
|
#include <LibCore/System.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <pwd.h>
|
2019-05-25 16:58:22 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-27 21:17:23 +02:00
|
|
|
#if defined(AK_OS_HAIKU)
|
|
|
|
# include <FindDirectory.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-19 19:57:05 +02:00
|
|
|
namespace Core {
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::home_directory()
|
2019-05-26 10:14:03 -07:00
|
|
|
{
|
|
|
|
if (auto* home_env = getenv("HOME"))
|
2020-05-26 14:52:44 +03:00
|
|
|
return LexicalPath::canonicalized_path(home_env);
|
2019-05-26 10:14:03 -07:00
|
|
|
|
2019-08-03 08:32:07 +02:00
|
|
|
auto* pwd = getpwuid(getuid());
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString path = pwd ? pwd->pw_dir : "/";
|
2019-08-03 08:32:07 +02:00
|
|
|
endpwent();
|
2020-05-26 14:52:44 +03:00
|
|
|
return LexicalPath::canonicalized_path(path);
|
2020-04-19 19:57:05 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::desktop_directory()
|
2020-04-19 19:57:05 +02:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/Desktop"sv);
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2020-04-19 19:57:05 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::documents_directory()
|
2022-09-23 10:34:06 -04:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
|
|
|
builder.append("/Documents"sv);
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2022-09-23 10:34:06 -04:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::downloads_directory()
|
2020-05-05 23:56:57 +02:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/Downloads"sv);
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2020-05-05 23:56:57 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::config_directory()
|
2020-08-05 17:18:59 +02:00
|
|
|
{
|
2022-12-05 12:45:19 -05:00
|
|
|
if (auto* config_directory = getenv("XDG_CONFIG_HOME"))
|
|
|
|
return LexicalPath::canonicalized_path(config_directory);
|
|
|
|
|
2020-08-05 17:18:59 +02:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(home_directory());
|
2022-12-05 12:45:19 -05:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
builder.append("/Library/Preferences"sv);
|
2023-08-27 21:17:23 +02:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/config/settings"sv);
|
2022-12-05 12:45:19 -05:00
|
|
|
#else
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("/.config"sv);
|
2022-12-05 12:45:19 -05:00
|
|
|
#endif
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2020-08-05 17:18:59 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::data_directory()
|
2022-12-05 12:46:53 -05:00
|
|
|
{
|
|
|
|
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 21:17:23 +02:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/config/non-packaged/data"sv);
|
2022-12-05 12:46:53 -05:00
|
|
|
#else
|
|
|
|
builder.append("/.local/share"sv);
|
|
|
|
#endif
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2022-12-05 12:46:53 -05:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ErrorOr<ByteString> StandardPaths::runtime_directory()
|
2022-12-09 11:15:17 -05:00
|
|
|
{
|
|
|
|
if (auto* data_directory = getenv("XDG_RUNTIME_DIR"))
|
|
|
|
return LexicalPath::canonicalized_path(data_directory);
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
#if defined(AK_OS_SERENITY)
|
2022-12-15 07:18:15 -05:00
|
|
|
auto sid = TRY(Core::SessionManagement::root_session_id());
|
2022-12-09 11:15:17 -05:00
|
|
|
builder.appendff("/tmp/session/{}", sid);
|
|
|
|
#elif defined(AK_OS_MACOS)
|
|
|
|
builder.append(home_directory());
|
|
|
|
builder.append("/Library/Application Support"sv);
|
2023-08-27 21:17:23 +02:00
|
|
|
#elif defined(AK_OS_HAIKU)
|
|
|
|
builder.append("/boot/system/var/shared_memory"sv);
|
2023-12-28 11:29:14 +01:00
|
|
|
#elif defined(AK_OS_LINUX)
|
2022-12-09 11:15:17 -05:00
|
|
|
auto uid = getuid();
|
|
|
|
builder.appendff("/run/user/{}", uid);
|
2023-12-28 11:29:14 +01:00
|
|
|
#else
|
|
|
|
// Just create a directory in /tmp that's owned by us with 0700
|
|
|
|
auto uid = getuid();
|
|
|
|
builder.appendff("/tmp/runtime_{}", uid);
|
|
|
|
auto error_or_stat = System::stat(builder.string_view());
|
|
|
|
if (error_or_stat.is_error()) {
|
|
|
|
MUST(System::mkdir(builder.string_view(), 0700));
|
|
|
|
} else {
|
|
|
|
auto stat = error_or_stat.release_value();
|
|
|
|
VERIFY(S_ISDIR(stat.st_mode));
|
|
|
|
if ((stat.st_mode & 0777) != 0700)
|
|
|
|
warnln("{} has unexpected mode flags {}", builder.string_view(), stat.st_mode);
|
|
|
|
}
|
2022-12-09 11:15:17 -05:00
|
|
|
#endif
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
return LexicalPath::canonicalized_path(builder.to_byte_string());
|
2022-12-09 11:15:17 -05:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString StandardPaths::tempfile_directory()
|
2020-04-19 19:57:05 +02:00
|
|
|
{
|
|
|
|
return "/tmp";
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:30:30 +00:00
|
|
|
ErrorOr<Vector<String>> StandardPaths::font_directories()
|
|
|
|
{
|
2023-08-27 21:17:23 +02: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 21:17:23 +02:00
|
|
|
# if defined(AK_OS_SERENITY)
|
2023-08-07 11:12:38 +02:00
|
|
|
"/res/fonts"_string,
|
2023-08-27 21:17:23 +02:00
|
|
|
# elif defined(AK_OS_MACOS)
|
2023-08-07 11:12:38 +02: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 21:17:23 +02:00
|
|
|
# else
|
2023-08-07 11:12:38 +02: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 21:17:23 +02:00
|
|
|
# endif
|
2023-01-11 20:30:30 +00:00
|
|
|
} };
|
2023-08-27 21:17:23 +02:00
|
|
|
#endif
|
2023-01-11 20:30:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 16:58:22 -07:00
|
|
|
}
|