From 33138900de506f3c71803b084272d0422c75ae43 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 29 Aug 2020 21:25:01 +0300 Subject: [PATCH] FileSystem: Convert file types to DT_* types at a later stage A change introduced in 5e01234 made it the resposibility of each filesystem to have the file types returned from 'traverse_as_directory' match up with the DT_* types. However, this caused corruption of the Ext2FS file format because the Ext2FS uses 'traverse_as_directory' internally when manipulating the file system. The result was a mixture between EXT2_FT_* and DT_* file types in the internal Ext2FS structures. Starting with this commit, the conversion from internal filesystem file types to the user facing DT_* types happens at a later stage, in the 'FileDescription::get_dir_entries' function which is directly used by sys$get_dir_entries. --- Kernel/FileSystem/Ext2FileSystem.cpp | 6 +++--- Kernel/FileSystem/Ext2FileSystem.h | 2 ++ Kernel/FileSystem/FileDescription.cpp | 4 ++-- Kernel/FileSystem/FileSystem.h | 5 ++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index e18e8049c7..2d93e734fe 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -855,7 +855,7 @@ ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const u8* data, Fi return nwritten; } -u8 Ext2FSInode::file_type_for_directory_entry(const ext2_dir_entry_2& entry) +u8 Ext2FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { switch (entry.file_type) { case EXT2_FT_REG_FILE: @@ -869,7 +869,7 @@ u8 Ext2FSInode::file_type_for_directory_entry(const ext2_dir_entry_2& entry) case EXT2_FT_FIFO: return DT_FIFO; case EXT2_FT_SOCK: - return EXT2_FT_SOCK; + return DT_SOCK; case EXT2_FT_SYMLINK: return DT_LNK; default: @@ -899,7 +899,7 @@ KResult Ext2FSInode::traverse_as_directory(Functioninode << ", name_len: " << entry->name_len << ", rec_len: " << entry->rec_len << ", file_type: " << entry->file_type << ", name: " << String(entry->name, entry->name_len); #endif - if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, file_type_for_directory_entry(*entry) })) + if (!callback({ { entry->name, entry->name_len }, { fsid(), entry->inode }, entry->file_type })) break; } entry = (ext2_dir_entry_2*)((char*)entry + entry->rec_len); diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index dee084813a..bdaa3255ca 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -110,6 +110,8 @@ public: virtual bool supports_watchers() const override { return true; } + virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const override; + private: typedef unsigned BlockIndex; typedef unsigned GroupIndex; diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index ce0ddd0715..09a286b215 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -188,9 +188,9 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size) auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate); BufferStream stream(temp_buffer); - KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream](auto& entry) { + KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream, this](auto& entry) { stream << (u32)entry.inode.index(); - stream << (u8)entry.file_type; + stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry); stream << (u32)entry.name.length(); stream << entry.name; return true; diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index 0cdd6d2b5c..19fea4bafd 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -77,12 +77,15 @@ public: u8 file_type { 0 }; }; - virtual void flush_writes() { } + virtual void flush_writes() {} size_t block_size() const { return m_block_size; } virtual bool is_file_backed() const { return false; } + // Converts file types that are used internally by the filesystem to DT_* types + virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { return entry.file_type; } + protected: FS();