2023-05-20 23:04:43 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
2023-10-04 13:33:54 +03:00
|
|
|
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2023-05-20 23:04:43 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "InstalledPort.h"
|
|
|
|
#include <AK/Function.h>
|
2023-10-04 13:33:54 +03:00
|
|
|
#include <AK/StringUtils.h>
|
2023-05-20 23:04:43 +03:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
2023-10-04 13:33:54 +03:00
|
|
|
Optional<InstalledPort::Type> InstalledPort::type_from_string(StringView type)
|
|
|
|
{
|
|
|
|
if (type == "auto"sv)
|
|
|
|
return Type::Auto;
|
|
|
|
if (type == "manual"sv)
|
|
|
|
return Type::Manual;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-05-20 23:04:43 +03:00
|
|
|
ErrorOr<HashMap<String, InstalledPort>> InstalledPort::read_ports_database()
|
|
|
|
{
|
2023-10-04 13:02:47 +03:00
|
|
|
auto file = TRY(Core::File::open(ports_database, Core::File::OpenMode::Read));
|
2023-05-20 23:04:43 +03:00
|
|
|
auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
|
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(PAGE_SIZE));
|
|
|
|
|
|
|
|
HashMap<String, InstalledPort> ports;
|
|
|
|
while (TRY(buffered_file->can_read_line())) {
|
|
|
|
auto line = TRY(buffered_file->read_line(buffer));
|
2023-10-04 13:33:54 +03:00
|
|
|
if (line.is_empty())
|
2023-05-20 23:04:43 +03:00
|
|
|
continue;
|
2023-10-04 13:33:54 +03:00
|
|
|
|
|
|
|
auto parts = line.split_view(' ');
|
|
|
|
if (parts.size() < 2) {
|
|
|
|
dbgln("Invalid database entry {} (only {} parts)", line, parts.size());
|
|
|
|
// FIXME: Skip over invalid entries instead?
|
|
|
|
return Error::from_string_view("Database entry too short"sv);
|
|
|
|
}
|
2023-12-21 13:32:33 +03:00
|
|
|
auto install_type_string = parts[0];
|
|
|
|
auto port_name = TRY(String::from_utf8(parts[1]));
|
2023-10-04 13:33:54 +03:00
|
|
|
|
2023-12-21 13:32:33 +03:00
|
|
|
if (auto maybe_type = type_from_string(install_type_string); maybe_type.has_value()) {
|
2023-10-04 13:33:54 +03:00
|
|
|
auto const type = maybe_type.release_value();
|
|
|
|
if (parts.size() < 3)
|
|
|
|
return Error::from_string_view("Port is missing a version specification"sv);
|
|
|
|
|
2023-12-21 13:32:33 +03:00
|
|
|
ports.ensure(port_name, [=] { return InstalledPort { port_name, MUST(String::from_utf8(parts[2])), type }; });
|
|
|
|
} else if (install_type_string == "dependency"sv) {
|
2023-10-04 13:33:54 +03:00
|
|
|
Vector<String> dependencies;
|
2023-12-21 13:32:33 +03:00
|
|
|
TRY(dependencies.try_ensure_capacity(parts.size() - 2));
|
|
|
|
for (auto const& dependency : parts.span().slice(2)) {
|
|
|
|
dependencies.unchecked_append(TRY(String::from_utf8(dependency)));
|
|
|
|
}
|
2023-10-04 13:33:54 +03:00
|
|
|
// Assume the port as automatically installed if the "dependency" line occurs before the "manual"/"auto" line.
|
|
|
|
// This is fine since these entries override the port type in any case.
|
2023-12-21 13:32:33 +03:00
|
|
|
auto& port = ports.ensure(port_name, [&] { return InstalledPort { port_name, {}, Type::Auto }; });
|
2023-10-04 13:33:54 +03:00
|
|
|
port.m_dependencies = move(dependencies);
|
2023-05-20 23:04:43 +03:00
|
|
|
} else {
|
|
|
|
return Error::from_string_literal("Unknown installed port type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ports;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> InstalledPort::for_each_by_type(HashMap<String, InstalledPort>& ports_database, InstalledPort::Type type, Function<ErrorOr<void>(InstalledPort const&)> callback)
|
|
|
|
{
|
|
|
|
for (auto& port : ports_database) {
|
|
|
|
if (type == port.value.type())
|
|
|
|
TRY(callback(port.value));
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|