serenity/Kernel/FileSystem/FileSystemSpecificOption.cpp
Liav A. cb10f70394 Kernel: Change internal handling of filesystem-specific options
Instead of using a raw `KBuffer` and letting each implementation to
populating the specific flags on its own, we change things so we only
let each FileSystem implementation to validate the flag and its value
but then store it in a HashMap which its key is the flag name and
the value is a special new class called `FileSystemSpecificOption`
which wraps around `AK::Variant<...>`.

This approach has multiple advantages over the previous:
- It allows runtime inspection of what the user has set on a `MountFile`
  description for a specific filesystem.
- It ensures accidental overriding of filesystem specific option that
  was already set is not possible
- It removes ugly casting of a `KBuffer` contents to a strongly-typed
  values. Instead, a strongly-typed `AK::Variant` is used which ensures
  we always get a value without doing any casting.

Please note that we have removed support for ASCII string-oriented flags
as there were no actual use cases, and supporting such type would make
`FileSystemSpecificOption` more complicated unnecessarily for now.
2024-08-03 20:35:06 +02:00

107 lines
3 KiB
C++

/*
* Copyright (c) 2024, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/FileSystemSpecificOption.h>
namespace Kernel {
Optional<u64> parse_unsigned_filesystem_specific_option(FileSystemSpecificOptions const& filesystem_specific_options, StringView name)
{
Optional<u64> maybe_value;
auto maybe_flag = filesystem_specific_options.get(name);
if (maybe_flag.has_value()) {
maybe_flag.value()->property_value().visit(
[&](unsigned value) {
maybe_value = value;
},
[&](NonnullOwnPtr<KString>) {
VERIFY_NOT_REACHED();
},
[&](bool) {
VERIFY_NOT_REACHED();
},
[&](signed) {
VERIFY_NOT_REACHED();
});
}
return maybe_value;
}
Optional<i64> parse_signed_filesystem_specific_option(FileSystemSpecificOptions const& filesystem_specific_options, StringView name)
{
Optional<i64> maybe_value;
auto maybe_flag = filesystem_specific_options.get(name);
if (maybe_flag.has_value()) {
maybe_flag.value()->property_value().visit(
[&](unsigned) {
VERIFY_NOT_REACHED();
},
[&](NonnullOwnPtr<KString>) {
VERIFY_NOT_REACHED();
},
[&](bool) {
VERIFY_NOT_REACHED();
},
[&](signed value) {
maybe_value = value;
});
}
return maybe_value;
}
Optional<bool> parse_bool_filesystem_specific_option(FileSystemSpecificOptions const& filesystem_specific_options, StringView name)
{
Optional<bool> maybe_value;
auto maybe_flag = filesystem_specific_options.get(name);
if (maybe_flag.has_value()) {
maybe_flag.value()->property_value().visit(
[&](unsigned) {
VERIFY_NOT_REACHED();
},
[&](NonnullOwnPtr<KString>) {
VERIFY_NOT_REACHED();
},
[&](bool value) {
maybe_value = value;
},
[&](signed) {
VERIFY_NOT_REACHED();
});
}
return maybe_value;
}
ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> FileSystemSpecificOption::create_as_unsigned(unsigned value)
{
return adopt_nonnull_own_or_enomem(new (nothrow) FileSystemSpecificOption(value));
}
ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> FileSystemSpecificOption::create_as_signed(signed value)
{
return adopt_nonnull_own_or_enomem(new (nothrow) FileSystemSpecificOption(value));
}
ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> FileSystemSpecificOption::create_as_boolean(bool value)
{
return adopt_nonnull_own_or_enomem(new (nothrow) FileSystemSpecificOption(value));
}
FileSystemSpecificOption::FileSystemSpecificOption(unsigned value)
: m_value(value)
{
}
FileSystemSpecificOption::FileSystemSpecificOption(signed value)
: m_value(value)
{
}
FileSystemSpecificOption::FileSystemSpecificOption(bool value)
: m_value(value)
{
}
}