2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-09-12 14:06:24 +00:00
|
|
|
* Copyright (c) 2021, networkException <networkexception@serenityos.org>
|
2022-02-03 16:52:24 +00:00
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@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-04-09 10:35:21 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
2022-04-09 10:35:21 +00:00
|
|
|
#include <LibCore/Directory.h>
|
2020-04-19 17:57:05 +00:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2022-04-09 10:35:21 +00:00
|
|
|
#include <LibCore/System.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <pwd.h>
|
2022-04-09 10:35:21 +00:00
|
|
|
#include <sys/types.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_lib(DeprecatedString const& lib_name, AllowWriting allow_altering)
|
2020-08-17 14:43:15 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString directory_name = DeprecatedString::formatted("{}/lib", StandardPaths::config_directory());
|
2022-04-09 10:35:21 +00:00
|
|
|
auto directory = TRY(Directory::create(directory_name, Directory::CreateDirectories::Yes));
|
2022-12-04 18:02:33 +00:00
|
|
|
auto path = DeprecatedString::formatted("{}/{}.ini", directory, lib_name);
|
2022-02-06 13:58:29 +00:00
|
|
|
return ConfigFile::open(path, allow_altering);
|
2020-08-17 14:43:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_app(DeprecatedString const& app_name, AllowWriting allow_altering)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2022-04-09 10:35:21 +00:00
|
|
|
auto directory = TRY(Directory::create(StandardPaths::config_directory(), Directory::CreateDirectories::Yes));
|
2022-12-04 18:02:33 +00:00
|
|
|
auto path = DeprecatedString::formatted("{}/{}.ini", directory, app_name);
|
2022-02-06 13:58:29 +00:00
|
|
|
return ConfigFile::open(path, allow_altering);
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_system(DeprecatedString const& app_name, AllowWriting allow_altering)
|
2019-05-25 04:10:23 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
auto path = DeprecatedString::formatted("/etc/{}.ini", app_name);
|
2022-02-06 13:58:29 +00:00
|
|
|
return ConfigFile::open(path, allow_altering);
|
2019-05-25 04:10:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, AllowWriting allow_altering)
|
2019-11-11 08:39:01 +00:00
|
|
|
{
|
2023-02-09 02:02:46 +00:00
|
|
|
auto maybe_file = File::open(filename, allow_altering == AllowWriting::Yes ? File::OpenMode::ReadWrite : File::OpenMode::Read);
|
2023-05-03 22:45:18 +00:00
|
|
|
OwnPtr<InputBufferedFile> buffered_file;
|
2022-02-06 15:27:17 +00:00
|
|
|
if (maybe_file.is_error()) {
|
|
|
|
// If we attempted to open a read-only file that does not exist, we ignore the error, making it appear
|
|
|
|
// the same as if we had opened an empty file. This behavior is a little weird, but is required by
|
|
|
|
// user code, which does not check the config file exists before opening.
|
|
|
|
if (!(allow_altering == AllowWriting::No && maybe_file.error().code() == ENOENT))
|
2023-02-09 18:26:53 +00:00
|
|
|
return maybe_file.release_error();
|
2022-02-06 15:27:17 +00:00
|
|
|
} else {
|
2023-05-03 22:45:18 +00:00
|
|
|
buffered_file = TRY(InputBufferedFile::create(maybe_file.release_value()));
|
2022-02-06 13:58:29 +00:00
|
|
|
}
|
2022-02-06 15:27:17 +00:00
|
|
|
|
|
|
|
auto config_file = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ConfigFile(filename, move(buffered_file))));
|
|
|
|
TRY(config_file->reparse());
|
|
|
|
return config_file;
|
2019-11-11 08:39:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, int fd)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(File::adopt_fd(fd, File::OpenMode::ReadWrite));
|
2022-09-14 14:03:04 +00:00
|
|
|
return open(filename, move(file));
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, NonnullOwnPtr<Core::File> file)
|
2022-09-14 14:03:04 +00:00
|
|
|
{
|
2023-05-03 22:45:18 +00:00
|
|
|
auto buffered_file = TRY(InputBufferedFile::create(move(file)));
|
2022-02-06 15:27:17 +00:00
|
|
|
|
|
|
|
auto config_file = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ConfigFile(filename, move(buffered_file))));
|
|
|
|
TRY(config_file->reparse());
|
|
|
|
return config_file;
|
2021-08-18 23:23:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 22:45:18 +00:00
|
|
|
ConfigFile::ConfigFile(DeprecatedString const& filename, OwnPtr<InputBufferedFile> open_file)
|
2022-02-06 15:27:17 +00:00
|
|
|
: m_filename(filename)
|
|
|
|
, m_file(move(open_file))
|
2021-08-18 23:23:31 +00:00
|
|
|
{
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
ConfigFile::~ConfigFile()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2022-02-06 14:26:33 +00:00
|
|
|
MUST(sync());
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 15:27:17 +00:00
|
|
|
ErrorOr<void> ConfigFile::reparse()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
m_groups.clear();
|
2022-02-06 15:27:17 +00:00
|
|
|
if (!m_file)
|
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, DeprecatedString>* current_group = nullptr;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-02-06 15:27:17 +00:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
|
|
|
while (TRY(m_file->can_read_line())) {
|
2022-04-15 13:52:33 +00:00
|
|
|
auto line = TRY(m_file->read_line(buffer));
|
2022-02-03 16:52:24 +00:00
|
|
|
size_t i = 0;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-02-03 16:52:24 +00:00
|
|
|
while (i < line.length() && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n'))
|
|
|
|
++i;
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-02-03 16:52:24 +00:00
|
|
|
if (i >= line.length())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (line[i]) {
|
|
|
|
case '#': // Comment, skip entire line.
|
|
|
|
case ';': // -||-
|
2019-05-28 09:53:16 +00:00
|
|
|
continue;
|
|
|
|
case '[': { // Start of new group.
|
|
|
|
StringBuilder builder;
|
2022-02-03 16:52:24 +00:00
|
|
|
++i; // Skip the '['
|
|
|
|
while (i < line.length() && (line[i] != ']')) {
|
|
|
|
builder.append(line[i]);
|
|
|
|
++i;
|
|
|
|
}
|
2022-12-06 01:12:49 +00:00
|
|
|
current_group = &m_groups.ensure(builder.to_deprecated_string());
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-03 16:52:24 +00:00
|
|
|
default: { // Start of key
|
2019-05-28 09:53:16 +00:00
|
|
|
StringBuilder key_builder;
|
|
|
|
StringBuilder value_builder;
|
2022-02-03 16:52:24 +00:00
|
|
|
while (i < line.length() && (line[i] != '=')) {
|
|
|
|
key_builder.append(line[i]);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++i; // Skip the '='
|
|
|
|
while (i < line.length() && (line[i] != '\n')) {
|
|
|
|
value_builder.append(line[i]);
|
|
|
|
++i;
|
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
if (!current_group) {
|
|
|
|
// We're not in a group yet, create one with the name ""...
|
|
|
|
current_group = &m_groups.ensure("");
|
|
|
|
}
|
2022-12-06 01:12:49 +00:00
|
|
|
auto value_string = value_builder.to_deprecated_string();
|
|
|
|
current_group->set(key_builder.to_deprecated_string(), value_string.trim_whitespace(TrimMode::Right));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 15:27:17 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
Optional<DeprecatedString> ConfigFile::read_entry_optional(const AK::DeprecatedString& group, const AK::DeprecatedString& key) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2023-10-10 11:30:58 +00:00
|
|
|
if (!has_key(group, key))
|
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
auto it = m_groups.find(group);
|
|
|
|
auto jt = it->value.find(key);
|
|
|
|
return jt->value;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
bool ConfigFile::read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
2021-11-04 21:28:05 +00:00
|
|
|
auto value = read_entry(group, key, default_value ? "true" : "false");
|
2023-03-10 07:48:54 +00:00
|
|
|
return value == "1" || value.equals_ignoring_ascii_case("true"sv);
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void ConfigFile::write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
m_groups.ensure(group).ensure(key) = value;
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void ConfigFile::write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value)
|
2019-07-01 05:54:30 +00:00
|
|
|
{
|
2021-11-04 21:28:05 +00:00
|
|
|
write_entry(group, key, value ? "true" : "false");
|
2019-07-01 05:54:30 +00:00
|
|
|
}
|
2022-02-06 15:27:17 +00:00
|
|
|
|
2022-02-06 14:26:33 +00:00
|
|
|
ErrorOr<void> ConfigFile::sync()
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
if (!m_dirty)
|
2022-02-06 14:26:33 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2022-02-06 15:27:17 +00:00
|
|
|
if (!m_file)
|
|
|
|
return Error::from_errno(ENOENT);
|
|
|
|
|
|
|
|
TRY(m_file->truncate(0));
|
2023-01-22 04:09:11 +00:00
|
|
|
TRY(m_file->seek(0, SeekMode::SetPosition));
|
2019-04-15 00:22:08 +00:00
|
|
|
|
|
|
|
for (auto& it : m_groups) {
|
2023-03-01 16:24:50 +00:00
|
|
|
TRY(m_file->write_until_depleted(DeprecatedString::formatted("[{}]\n", it.key).bytes()));
|
2019-04-15 00:22:08 +00:00
|
|
|
for (auto& jt : it.value)
|
2023-03-01 16:24:50 +00:00
|
|
|
TRY(m_file->write_until_depleted(DeprecatedString::formatted("{}={}\n", jt.key, jt.value).bytes()));
|
|
|
|
TRY(m_file->write_until_depleted("\n"sv.bytes()));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = false;
|
2022-02-06 14:26:33 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void ConfigFile::dump() const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
for (auto& it : m_groups) {
|
2020-10-15 11:21:23 +00:00
|
|
|
outln("[{}]", it.key);
|
2019-04-15 00:22:08 +00:00
|
|
|
for (auto& jt : it.value)
|
2020-10-15 11:21:23 +00:00
|
|
|
outln("{}={}", jt.key, jt.value);
|
|
|
|
outln();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> ConfigFile::groups() const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
return m_groups.keys();
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> ConfigFile::keys(DeprecatedString const& group) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
return it->value.keys();
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
bool ConfigFile::has_key(DeprecatedString const& group, DeprecatedString const& key) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-15 00:22:08 +00:00
|
|
|
return it->value.contains(key);
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
bool ConfigFile::has_group(DeprecatedString const& group) const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
return m_groups.contains(group);
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void ConfigFile::add_group(DeprecatedString const& group)
|
2022-09-23 13:45:05 +00:00
|
|
|
{
|
|
|
|
m_groups.ensure(group);
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void ConfigFile::remove_group(DeprecatedString const& group)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
m_groups.remove(group);
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void ConfigFile::remove_entry(DeprecatedString const& group, DeprecatedString const& key)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = m_groups.find(group);
|
|
|
|
if (it == m_groups.end())
|
|
|
|
return;
|
|
|
|
it->value.remove(key);
|
2019-05-28 09:53:16 +00:00
|
|
|
m_dirty = true;
|
2019-04-15 00:22:08 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|