Files
serenity/Kernel/FileSystem/FileSystemSpecificOption.h
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

48 lines
1.7 KiB
C++

/*
* Copyright (c) 2024, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/FixedStringBuffer.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <AK/Variant.h>
#include <Kernel/API/FileSystem/MountSpecificFlags.h>
#include <Kernel/Library/KString.h>
#include <Kernel/UnixTypes.h>
namespace Kernel {
class FileSystemSpecificOption {
public:
static ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> create_as_unsigned(unsigned);
static ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> create_as_signed(signed);
static ErrorOr<NonnullOwnPtr<FileSystemSpecificOption>> create_as_boolean(bool);
Variant<unsigned, signed, bool> const& property_value() const { return m_value; }
private:
explicit FileSystemSpecificOption(unsigned);
explicit FileSystemSpecificOption(signed);
explicit FileSystemSpecificOption(bool);
Variant<unsigned, signed, bool> const m_value;
};
// NOTE: It's OK to use a StringView as a key because we are storing the actual string
// in the FileSystemSpecificOption object.
using FileSystemSpecificOptions = HashMap<NonnullOwnPtr<KString>, NonnullOwnPtr<FileSystemSpecificOption>>;
Optional<u64> parse_unsigned_filesystem_specific_option(FileSystemSpecificOptions const&, StringView name);
Optional<i64> parse_signed_filesystem_specific_option(FileSystemSpecificOptions const&, StringView name);
Optional<bool> parse_bool_filesystem_specific_option(FileSystemSpecificOptions const&, StringView name);
ErrorOr<OwnPtr<KString>> parse_string_filesystem_specific_option(FileSystemSpecificOptions const&, StringView name);
}