mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:57:35 +00:00
Kernel/SysFS: Add Symbolic link functionality to the filesystem
This will be used later on to help connecting a node at /sys/dev/block/ that represents a Storage device to a directory in /sys/devices/storage/ with details on that device in that directory.
This commit is contained in:
parent
7e88bbe550
commit
ecc29bb52e
4 changed files with 104 additions and 0 deletions
|
@ -131,6 +131,31 @@ ErrorOr<void> SysFSInode::truncate(u64 size)
|
||||||
return m_associated_component->truncate(size);
|
return m_associated_component->truncate(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<SysFSLinkInode>> SysFSLinkInode::try_create(SysFS const& sysfs, SysFSComponent const& component)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSLinkInode(sysfs, component));
|
||||||
|
}
|
||||||
|
|
||||||
|
SysFSLinkInode::SysFSLinkInode(SysFS const& fs, SysFSComponent const& component)
|
||||||
|
: SysFSInode(fs, component)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SysFSLinkInode::~SysFSLinkInode() = default;
|
||||||
|
|
||||||
|
InodeMetadata SysFSLinkInode::metadata() const
|
||||||
|
{
|
||||||
|
// NOTE: No locking required as m_associated_component or its component index will never change during our lifetime.
|
||||||
|
InodeMetadata metadata;
|
||||||
|
metadata.inode = { fsid(), m_associated_component->component_index() };
|
||||||
|
metadata.mode = S_IFLNK | S_IRUSR | S_IRGRP | S_IROTH | S_IXOTH;
|
||||||
|
metadata.uid = 0;
|
||||||
|
metadata.gid = 0;
|
||||||
|
metadata.size = 0;
|
||||||
|
metadata.mtime = TimeManagement::boot_time();
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<SysFSDirectoryInode>> SysFSDirectoryInode::try_create(SysFS const& sysfs, SysFSComponent const& component)
|
ErrorOr<NonnullRefPtr<SysFSDirectoryInode>> SysFSDirectoryInode::try_create(SysFS const& sysfs, SysFSComponent const& component)
|
||||||
{
|
{
|
||||||
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSDirectoryInode(sysfs, component));
|
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSDirectoryInode(sysfs, component));
|
||||||
|
|
|
@ -62,6 +62,19 @@ protected:
|
||||||
NonnullRefPtr<SysFSComponent> m_associated_component;
|
NonnullRefPtr<SysFSComponent> m_associated_component;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SysFSLinkInode : public SysFSInode {
|
||||||
|
friend class SysFS;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullRefPtr<SysFSLinkInode>> try_create(SysFS const&, SysFSComponent const&);
|
||||||
|
virtual ~SysFSLinkInode() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
SysFSLinkInode(SysFS const&, SysFSComponent const&);
|
||||||
|
// ^Inode
|
||||||
|
virtual InodeMetadata metadata() const override;
|
||||||
|
};
|
||||||
|
|
||||||
class SysFSDirectoryInode : public SysFSInode {
|
class SysFSDirectoryInode : public SysFSInode {
|
||||||
friend class SysFS;
|
friend class SysFS;
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,53 @@ mode_t SysFSComponent::permissions() const
|
||||||
return S_IRUSR | S_IRGRP | S_IROTH;
|
return S_IRUSR | S_IRGRP | S_IROTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<size_t> SysFSSymbolicLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
|
||||||
|
{
|
||||||
|
auto blob = TRY(try_to_generate_buffer());
|
||||||
|
|
||||||
|
if ((size_t)offset >= blob->size())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
|
||||||
|
TRY(buffer.write(blob->data() + offset, nread));
|
||||||
|
return nread;
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<KBuffer>> SysFSSymbolicLink::try_to_generate_buffer() const
|
||||||
|
{
|
||||||
|
auto return_path_to_mount_point = TRY(try_generate_return_path_to_mount_point());
|
||||||
|
if (!m_pointed_component)
|
||||||
|
return Error::from_errno(EIO);
|
||||||
|
auto pointed_component_base_name = MUST(KString::try_create(m_pointed_component->name()));
|
||||||
|
auto pointed_component_relative_path = MUST(m_pointed_component->relative_path(move(pointed_component_base_name), 0));
|
||||||
|
auto full_return_and_target_path = TRY(KString::formatted("{}{}", return_path_to_mount_point->view(), pointed_component_relative_path->view()));
|
||||||
|
return KBuffer::try_create_with_bytes("SysFSSymbolicLink"sv, full_return_and_target_path->view().bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
static ErrorOr<NonnullOwnPtr<KString>> generate_return_path_to_mount_point(NonnullOwnPtr<KString> current_path, size_t remaining_hop)
|
||||||
|
{
|
||||||
|
if (remaining_hop == 0)
|
||||||
|
return current_path;
|
||||||
|
auto new_path = TRY(KString::formatted("../{}"sv, current_path->view()));
|
||||||
|
remaining_hop--;
|
||||||
|
return generate_return_path_to_mount_point(move(new_path), remaining_hop);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<KString>> SysFSSymbolicLink::try_generate_return_path_to_mount_point() const
|
||||||
|
{
|
||||||
|
auto hops_from_mountpoint = TRY(relative_path_hops_count_from_mountpoint());
|
||||||
|
if (hops_from_mountpoint == 0)
|
||||||
|
return KString::try_create("./"sv);
|
||||||
|
auto start_return_path = TRY(KString::try_create("./"sv));
|
||||||
|
return generate_return_path_to_mount_point(move(start_return_path), hops_from_mountpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
SysFSSymbolicLink::SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component)
|
||||||
|
: SysFSComponent(parent_directory)
|
||||||
|
, m_pointed_component(pointed_component)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<void> SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
|
ErrorOr<void> SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
|
||||||
{
|
{
|
||||||
MutexLocker locker(SysFSComponentRegistry::the().get_lock());
|
MutexLocker locker(SysFSComponentRegistry::the().get_lock());
|
||||||
|
@ -91,6 +138,11 @@ ErrorOr<NonnullRefPtr<SysFSInode>> SysFSDirectory::to_inode(SysFS const& sysfs_i
|
||||||
return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this));
|
return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<SysFSInode>> SysFSSymbolicLink::to_inode(SysFS const& sysfs_instance) const
|
||||||
|
{
|
||||||
|
return TRY(SysFSLinkInode::try_create(sysfs_instance, *this));
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<SysFSInode>> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
|
ErrorOr<NonnullRefPtr<SysFSInode>> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
|
||||||
{
|
{
|
||||||
return SysFSInode::try_create(sysfs_instance, *this);
|
return SysFSInode::try_create(sysfs_instance, *this);
|
||||||
|
|
|
@ -56,6 +56,20 @@ private:
|
||||||
InodeIndex m_component_index {};
|
InodeIndex m_component_index {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SysFSSymbolicLink : public SysFSComponent {
|
||||||
|
public:
|
||||||
|
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override final;
|
||||||
|
virtual ErrorOr<NonnullRefPtr<SysFSInode>> to_inode(SysFS const& sysfs_instance) const override final;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ErrorOr<NonnullOwnPtr<KString>> try_generate_return_path_to_mount_point() const;
|
||||||
|
ErrorOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
|
||||||
|
|
||||||
|
explicit SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component);
|
||||||
|
|
||||||
|
RefPtr<SysFSComponent> m_pointed_component;
|
||||||
|
};
|
||||||
|
|
||||||
class SysFSDirectory : public SysFSComponent {
|
class SysFSDirectory : public SysFSComponent {
|
||||||
public:
|
public:
|
||||||
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
|
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue