/* * Copyright (c) 2022-2024, Undefine * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Kernel { struct FATEntryLocation { BlockBasedFileSystem::BlockIndex block; u32 entry; }; class FATInode final : public Inode { friend FATFS; public: virtual ~FATInode() override = default; static ErrorOr> create(FATFS&, FATEntry, FATEntryLocation inode_metadata_location, Vector const& = {}); FATFS& fs() { return static_cast(Inode::fs()); } FATFS const& fs() const { return static_cast(Inode::fs()); } private: FATInode(FATFS&, FATEntry, FATEntryLocation inode_metadata_location, NonnullOwnPtr filename); static constexpr u8 end_entry_byte = 0x00; static constexpr u8 unused_entry_byte = 0xE5; static constexpr u8 lfn_entry_character_termination = 0x00; static constexpr u8 lfn_entry_unused_byte = 0xFF; static constexpr u8 normal_filename_length = 8; static constexpr u8 normal_extension_length = 3; static constexpr size_t lfn_entry_characters_part_1_length = 5; static constexpr size_t lfn_entry_characters_part_2_length = 6; static constexpr size_t lfn_entry_characters_part_3_length = 2; static constexpr size_t characters_per_lfn_entry = lfn_entry_characters_part_1_length + lfn_entry_characters_part_2_length + lfn_entry_characters_part_3_length; static constexpr u8 last_lfn_entry_mask = 0x40; static constexpr size_t max_filename_length = 255; static ErrorOr> compute_filename(FATEntry&, Vector const& = {}); static StringView byte_terminated_string(StringView, u8); static u8 lfn_entry_checksum(FATEntry const& entry); static void create_83_filename_for(FATEntry& entry, StringView name); static ErrorOr> create_lfn_entries(StringView name, u8 checksum); ErrorOr> compute_cluster_list(FATFS&, u32 first_cluster); ErrorOr> get_block_list(); ErrorOr> read_block_list(); ErrorOr> traverse(Function(RefPtr)> callback); u32 first_cluster() const; // This overload of `first_cluster` does not rely on the base Inode // already being created to determine the FAT version. It is used // during FATInode creation (create()). u32 first_cluster(FATVersion const version) const; ErrorOr allocate_and_add_cluster_to_chain(); ErrorOr remove_last_cluster_from_chain(); ErrorOr> allocate_entries(u32 count); ErrorOr resize(u64 size); // ^Inode virtual ErrorOr write_bytes_locked(off_t, size_t, UserOrKernelBuffer const& data, OpenFileDescription*) override; virtual ErrorOr read_bytes_locked(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; virtual ErrorOr traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const override; virtual ErrorOr> lookup(StringView name) override; virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; virtual ErrorOr add_child(Inode&, StringView name, mode_t) override; virtual ErrorOr remove_child(StringView name) override; virtual ErrorOr replace_child(StringView name, Inode& child) override; virtual ErrorOr chmod(mode_t) override; virtual ErrorOr chown(UserID, GroupID) override; virtual ErrorOr truncate_locked(u64) override; virtual ErrorOr flush_metadata() override; virtual ErrorOr update_timestamps(Optional atime, Optional ctime, Optional mtime) override; Vector m_cluster_list; FATEntry m_entry; FATEntryLocation m_inode_metadata_location; NonnullOwnPtr m_filename; }; }