From 41f578aa1874ad83fc27181185ec7aed00b99351 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 22 Jun 2023 20:10:09 +0200 Subject: [PATCH] kernel/ext2: Add support for symbolic links --- kernel/src/fs/ext2/Inode.cpp | 27 ++++++++++++++++++++++++++- kernel/src/fs/ext2/Inode.h | 5 +++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/kernel/src/fs/ext2/Inode.cpp b/kernel/src/fs/ext2/Inode.cpp index da6bd70b..53c063f9 100644 --- a/kernel/src/fs/ext2/Inode.cpp +++ b/kernel/src/fs/ext2/Inode.cpp @@ -1,5 +1,5 @@ #include "fs/ext2/Inode.h" -#include +#include namespace Ext2 { @@ -162,4 +162,29 @@ namespace Ext2 return m_entries[index]; } + + Result Inode::readlink() + { + check(m_type == VFS::InodeType::Symlink); + + if (!m_link.is_empty()) return m_link.view(); + + const usize length = size(); + + if (length < 60) + { + // The symlink location is stored inline within the inode's data blocks. + m_link = TRY(String::from_string_view( + StringView::from_fixed_size_cstring((char*)&m_raw_inode.direct_pointers[0], length))); + + return m_link.view(); + } + + Buffer buf = TRY(Buffer::create_sized(length)); + TRY(read(buf.data(), 0, length)); + + m_link = TRY(String::from_string_view(StringView::from_fixed_size_cstring((char*)buf.data(), length))); + + return m_link.view(); + } } diff --git a/kernel/src/fs/ext2/Inode.h b/kernel/src/fs/ext2/Inode.h index 2084cebd..d1359795 100644 --- a/kernel/src/fs/ext2/Inode.h +++ b/kernel/src/fs/ext2/Inode.h @@ -1,5 +1,6 @@ #pragma once #include "fs/ext2/FileSystem.h" +#include #define EXT2_FIFO 0x1000 #define EXT2_CHR 0x2000 @@ -125,6 +126,8 @@ namespace Ext2 return false; } + Result readlink() override; + Result lazy_initialize_dir() const; // FIXME: Implement readlink() and device numbers. @@ -138,6 +141,8 @@ namespace Ext2 FileSystem* m_fs; ino_t m_inum; + String m_link; + mutable Vector m_entries; mutable bool m_dir_already_lazily_initialized { false };