/* * Copyright (c) 2021, sin-ack * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include namespace Kernel { ErrorOr ISO9660Inode::read_bytes_locked(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const { VERIFY(m_inode_lock.is_locked()); u32 data_length = LittleEndian { m_record.data_length.little }; u32 extent_location = LittleEndian { m_record.extent_location.little }; if (static_cast(offset) >= data_length) return 0; auto block = TRY(KBuffer::try_create_with_size("ISO9660FS: Inode read buffer"sv, fs().m_logical_block_size)); auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data()); size_t total_bytes = min(size, data_length - offset); size_t nread = 0; size_t blocks_already_read = offset / fs().m_logical_block_size; size_t initial_offset = offset % fs().m_logical_block_size; auto current_block_index = BlockBasedFileSystem::BlockIndex { extent_location + blocks_already_read }; while (nread != total_bytes) { size_t bytes_to_read = min(total_bytes - nread, fs().logical_block_size() - initial_offset); auto buffer_offset = buffer.offset(nread); dbgln_if(ISO9660_VERY_DEBUG, "ISO9660Inode::read_bytes: Reading {} bytes into buffer offset {}/{}, logical block index: {}", bytes_to_read, nread, total_bytes, current_block_index.value()); TRY(const_cast(fs()).raw_read(current_block_index, block_buffer)); TRY(buffer_offset.write(block->data() + initial_offset, bytes_to_read)); nread += bytes_to_read; initial_offset = 0; current_block_index = BlockBasedFileSystem::BlockIndex { current_block_index.value() + 1 }; } return nread; } InodeMetadata ISO9660Inode::metadata() const { return m_metadata; } ErrorOr ISO9660Inode::traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)> visitor) const { Array file_identifier_buffer; ErrorOr result; return fs().visit_directory_record(m_record, [&](ISO::DirectoryRecordHeader const* record) { StringView filename = get_normalized_filename(*record, file_identifier_buffer); dbgln_if(ISO9660_VERY_DEBUG, "traverse_as_directory(): Found {}", filename); InodeIdentifier id { fsid(), get_inode_index(*record, filename) }; auto entry = FileSystem::DirectoryEntryView(filename, id, static_cast(record->file_flags)); result = visitor(entry); if (result.is_error()) return RecursionDecision::Break; return RecursionDecision::Continue; }); } ErrorOr ISO9660Inode::replace_child(StringView, Inode&) { return EROFS; } ErrorOr> ISO9660Inode::lookup(StringView name) { RefPtr inode; Array file_identifier_buffer; TRY(fs().visit_directory_record(m_record, [&](ISO::DirectoryRecordHeader const* record) { StringView filename = get_normalized_filename(*record, file_identifier_buffer); if (filename == name) { auto maybe_inode = ISO9660Inode::try_create_from_directory_record(fs(), *record, filename); if (maybe_inode.is_error()) { // FIXME: The Inode API does not handle allocation failures very // well... we can't return a ErrorOr from here. It // would be nice if we could return a ErrorOr(Or) from // any place where allocation may happen. dbgln("Could not allocate inode for lookup!"); } else { inode = maybe_inode.release_value(); } return RecursionDecision::Break; } return RecursionDecision::Continue; })); if (!inode) return ENOENT; return inode.release_nonnull(); } ErrorOr ISO9660Inode::flush_metadata() { return {}; } ErrorOr ISO9660Inode::write_bytes_locked(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; } ErrorOr> ISO9660Inode::create_child(StringView, mode_t, dev_t, UserID, GroupID) { return EROFS; } ErrorOr ISO9660Inode::add_child(Inode&, StringView, mode_t) { return EROFS; } ErrorOr ISO9660Inode::remove_child(StringView) { return EROFS; } ErrorOr ISO9660Inode::chmod(mode_t) { return EROFS; } ErrorOr ISO9660Inode::chown(UserID, GroupID) { return EROFS; } ErrorOr ISO9660Inode::truncate(u64) { return EROFS; } ErrorOr ISO9660Inode::update_timestamps(Optional