2018-11-18 13:57:41 +00:00
|
|
|
#include <AK/FileSystemPath.h>
|
2018-10-28 11:20:25 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-04-03 10:36:40 +00:00
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2019-05-30 15:46:08 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-12-31 00:22:00 +00:00
|
|
|
#include <Kernel/FileSystem/DiskBackedFileSystem.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-06-07 17:29:34 +00:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
//#define VFS_DEBUG
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
static VFS* s_the;
|
2019-12-24 09:39:21 +00:00
|
|
|
static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
|
2018-10-18 08:27:07 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS& VFS::the()
|
2018-10-18 08:27:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-22 09:15:16 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: Constructing VFS\n");
|
2018-10-22 09:15:16 +00:00
|
|
|
#endif
|
2018-10-18 08:27:07 +00:00
|
|
|
s_the = this;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::~VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-30 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 22:28:43 +00:00
|
|
|
InodeIdentifier VFS::root_inode_id() const
|
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
ASSERT(m_root_inode);
|
|
|
|
return m_root_inode->identifier();
|
2018-11-18 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:03:50 +00:00
|
|
|
KResult VFS::mount(NonnullRefPtr<FS>&& file_system, Custody& mount_point)
|
|
|
|
{
|
|
|
|
auto& inode = mount_point.inode();
|
|
|
|
dbg() << "VFS: Mounting " << file_system->class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ")";
|
|
|
|
// FIXME: check that this is not already a mount point
|
|
|
|
auto mount = make<Mount>(mount_point, move(file_system));
|
|
|
|
m_mounts.append(move(mount));
|
|
|
|
mount_point.did_mount_on({});
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
KResult VFS::mount(NonnullRefPtr<FS>&& file_system, StringView path)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-08-11 13:56:39 +00:00
|
|
|
LOCKER(m_lock);
|
2019-05-31 13:30:09 +00:00
|
|
|
auto result = resolve_path(path, root_custody());
|
2019-05-30 19:01:16 +00:00
|
|
|
if (result.is_error()) {
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS: mount can't resolve mount point '" << path << "'";
|
2019-08-02 17:03:50 +00:00
|
|
|
return result.error();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2019-08-02 17:03:50 +00:00
|
|
|
return mount(move(file_system), result.value());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 12:24:50 +00:00
|
|
|
KResult VFS::unmount(InodeIdentifier guest_inode_id)
|
2019-08-11 13:56:39 +00:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
2019-08-17 12:24:50 +00:00
|
|
|
dbg() << "VFS: unmount called with inode " << guest_inode_id;
|
|
|
|
|
|
|
|
for (int i = 0; i < m_mounts.size(); ++i) {
|
|
|
|
auto& mount = m_mounts.at(i);
|
|
|
|
if (mount.guest() == guest_inode_id) {
|
|
|
|
auto result = mount.guest_fs().prepare_to_unmount();
|
|
|
|
if (result.is_error()) {
|
|
|
|
dbg() << "VFS: Failed to unmount!";
|
|
|
|
return result;
|
2019-08-11 13:56:39 +00:00
|
|
|
}
|
2019-08-17 12:24:50 +00:00
|
|
|
dbg() << "VFS: found fs " << mount.guest_fs().fsid() << " at mount index " << i << "! Unmounting...";
|
2019-08-11 13:56:39 +00:00
|
|
|
m_mounts.remove(i);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 12:24:50 +00:00
|
|
|
dbg() << "VFS: Nothing mounted on inode " << guest_inode_id;
|
2019-08-11 13:56:39 +00:00
|
|
|
return KResult(-ENODEV);
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
bool VFS::mount_root(NonnullRefPtr<FS>&& file_system)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
if (m_root_inode) {
|
2018-11-15 14:10:12 +00:00
|
|
|
kprintf("VFS: mount_root can't mount another root\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-30 19:29:26 +00:00
|
|
|
auto mount = make<Mount>(nullptr, move(file_system));
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
auto root_inode_id = mount->guest().fs()->root_inode();
|
|
|
|
auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
|
|
|
|
if (!root_inode->is_directory()) {
|
|
|
|
kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
m_root_inode = move(root_inode);
|
2019-12-31 00:22:00 +00:00
|
|
|
char device_name[32];
|
|
|
|
if (m_root_inode->fs().is_disk_backed()) {
|
|
|
|
auto& device = static_cast<DiskBackedFS&>(m_root_inode->fs()).device();
|
|
|
|
sprintf(device_name, "%d,%d", device.major(), device.minor());
|
|
|
|
} else {
|
|
|
|
sprintf(device_name, "not-a-disk");
|
|
|
|
}
|
|
|
|
kprintf("VFS: mounted root on %s (%s)\n", m_root_inode->fs().class_name(), device_name);
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-17 09:40:58 +00:00
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
2019-07-24 07:15:33 +00:00
|
|
|
if (mount.host() == inode)
|
|
|
|
return &mount;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
2019-07-24 07:15:33 +00:00
|
|
|
if (mount.guest() == inode)
|
|
|
|
return &mount;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::is_vfs_root(InodeIdentifier inode) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
return inode == root_inode_id();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-06-07 09:43:58 +00:00
|
|
|
dir_inode.traverse_as_directory([&](const FS::DirectoryEntry& entry) {
|
2019-01-31 16:31:23 +00:00
|
|
|
InodeIdentifier resolved_inode;
|
2018-11-15 14:10:12 +00:00
|
|
|
if (auto mount = find_mount_for_host(entry.inode))
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = mount->guest();
|
2018-10-10 09:53:07 +00:00
|
|
|
else
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = entry.inode;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-02 23:20:00 +00:00
|
|
|
if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
|
2018-11-15 14:10:12 +00:00
|
|
|
auto mount = find_mount_for_guest(entry.inode);
|
2018-10-10 09:53:07 +00:00
|
|
|
ASSERT(mount);
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = mount->host();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
|
2019-02-21 15:37:41 +00:00
|
|
|
{
|
2019-03-06 21:14:31 +00:00
|
|
|
auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
|
|
|
|
if (descriptor_or_error.is_error())
|
|
|
|
return descriptor_or_error.error();
|
|
|
|
auto& inode = *descriptor_or_error.value()->inode();
|
2019-02-25 19:47:56 +00:00
|
|
|
if (inode.fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
2019-03-23 21:03:17 +00:00
|
|
|
if (inode.metadata().uid != current->process().euid())
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EACCES);
|
2019-03-06 21:14:31 +00:00
|
|
|
|
|
|
|
int error = inode.set_atime(atime);
|
2019-02-21 15:37:41 +00:00
|
|
|
if (error)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(error);
|
2019-02-21 15:37:41 +00:00
|
|
|
error = inode.set_mtime(mtime);
|
|
|
|
if (error)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(error);
|
|
|
|
return KSuccess;
|
2019-02-21 15:37:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:23:23 +00:00
|
|
|
KResultOr<InodeMetadata> VFS::lookup_metadata(StringView path, Custody& base, int options)
|
2019-02-21 15:09:12 +00:00
|
|
|
{
|
2019-05-31 13:30:09 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base, nullptr, options);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
2019-08-02 17:23:23 +00:00
|
|
|
return custody_or_error.value()->inode().metadata();
|
2019-02-21 15:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options, mode_t mode, Custody& base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-06-09 17:52:03 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base, &parent_custody, options);
|
2019-02-14 13:38:30 +00:00
|
|
|
if (options & O_CREAT) {
|
2019-06-09 17:52:03 +00:00
|
|
|
if (!parent_custody)
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (custody_or_error.is_error()) {
|
|
|
|
if (custody_or_error.error() != -ENOENT)
|
|
|
|
return custody_or_error.error();
|
|
|
|
return create(path, options, mode, *parent_custody);
|
|
|
|
}
|
2019-03-06 21:14:31 +00:00
|
|
|
if (options & O_EXCL)
|
|
|
|
return KResult(-EEXIST);
|
2019-01-21 23:58:13 +00:00
|
|
|
}
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
2019-02-21 15:09:12 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
auto metadata = inode.metadata();
|
2019-03-27 15:42:30 +00:00
|
|
|
|
|
|
|
bool should_truncate_file = false;
|
2019-02-21 15:09:12 +00:00
|
|
|
|
2019-02-21 14:45:31 +00:00
|
|
|
// NOTE: Read permission is a bit weird, since O_RDONLY == 0,
|
|
|
|
// so we check if (NOT write_only OR read_and_write)
|
|
|
|
if (!(options & O_WRONLY) || (options & O_RDWR)) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!metadata.may_read(current->process()))
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-EACCES);
|
2019-02-21 14:45:31 +00:00
|
|
|
}
|
|
|
|
if ((options & O_WRONLY) || (options & O_RDWR)) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!metadata.may_write(current->process()))
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
if (metadata.is_directory())
|
|
|
|
return KResult(-EISDIR);
|
2019-03-27 15:42:30 +00:00
|
|
|
should_truncate_file = options & O_TRUNC;
|
2019-02-21 14:45:31 +00:00
|
|
|
}
|
2019-02-21 15:09:12 +00:00
|
|
|
|
|
|
|
if (metadata.is_device()) {
|
2019-08-18 11:48:15 +00:00
|
|
|
auto device = Device::get_device(metadata.major_device, metadata.minor_device);
|
|
|
|
if (device == nullptr) {
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-ENODEV);
|
2019-01-16 11:57:07 +00:00
|
|
|
}
|
2019-08-18 11:48:15 +00:00
|
|
|
auto descriptor_or_error = device->open(options);
|
2019-03-20 01:55:12 +00:00
|
|
|
if (descriptor_or_error.is_error())
|
|
|
|
return descriptor_or_error.error();
|
2019-05-31 13:44:04 +00:00
|
|
|
descriptor_or_error.value()->set_original_inode({}, inode);
|
2019-03-20 01:55:12 +00:00
|
|
|
return descriptor_or_error;
|
2019-01-16 11:57:07 +00:00
|
|
|
}
|
2019-03-27 15:42:30 +00:00
|
|
|
if (should_truncate_file)
|
2019-05-30 16:58:59 +00:00
|
|
|
inode.truncate(0);
|
2019-06-07 07:36:51 +00:00
|
|
|
return FileDescription::create(custody);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
|
2019-05-03 20:59:58 +00:00
|
|
|
{
|
|
|
|
if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto existing_file_or_error = resolve_path(path, base, &parent_custody);
|
2019-05-03 20:59:58 +00:00
|
|
|
if (!existing_file_or_error.is_error())
|
|
|
|
return KResult(-EEXIST);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_custody)
|
2019-05-03 20:59:58 +00:00
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (existing_file_or_error.error() != -ENOENT)
|
|
|
|
return existing_file_or_error.error();
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-05-03 20:59:58 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
|
|
|
|
FileSystemPath p(path);
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
|
2019-05-03 20:59:58 +00:00
|
|
|
int error;
|
2019-05-30 16:58:59 +00:00
|
|
|
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, error);
|
2019-05-03 20:59:58 +00:00
|
|
|
if (!new_file)
|
|
|
|
return KResult(error);
|
|
|
|
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-03-06 21:14:31 +00:00
|
|
|
(void)options;
|
2019-01-23 03:29:56 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
|
2019-01-23 03:29:56 +00:00
|
|
|
// Turn it into a regular file. (This feels rather hackish.)
|
|
|
|
mode |= 0100000;
|
|
|
|
}
|
|
|
|
|
2019-06-09 17:52:03 +00:00
|
|
|
auto& parent_inode = parent_custody.inode();
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-EACCES);
|
2019-02-21 12:26:40 +00:00
|
|
|
FileSystemPath p(path);
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
|
2019-03-06 21:14:31 +00:00
|
|
|
int error;
|
2019-05-30 16:58:59 +00:00
|
|
|
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, error);
|
2019-01-21 23:58:13 +00:00
|
|
|
if (!new_file)
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(error);
|
2019-01-21 23:58:13 +00:00
|
|
|
|
2019-06-09 17:52:03 +00:00
|
|
|
auto new_custody = Custody::create(&parent_custody, p.basename(), *new_file);
|
2019-06-07 07:36:51 +00:00
|
|
|
return FileDescription::create(*new_custody);
|
2018-10-15 22:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
|
2018-10-15 22:35:03 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto result = resolve_path(path, base, &parent_custody);
|
2019-02-25 19:47:56 +00:00
|
|
|
if (!result.is_error())
|
|
|
|
return KResult(-EEXIST);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_custody)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (result.error() != -ENOENT)
|
|
|
|
return result.error();
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EACCES);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-02-21 12:26:40 +00:00
|
|
|
FileSystemPath p(path);
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
|
2019-02-25 19:47:56 +00:00
|
|
|
int error;
|
2019-05-30 16:58:59 +00:00
|
|
|
auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, error);
|
2019-02-25 19:47:56 +00:00
|
|
|
if (new_dir)
|
|
|
|
return KSuccess;
|
|
|
|
return KResult(error);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::access(StringView path, int mode, Custody& base)
|
2019-02-26 14:57:59 +00:00
|
|
|
{
|
2019-05-31 13:30:09 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
auto metadata = inode.metadata();
|
2019-02-26 14:57:59 +00:00
|
|
|
if (mode & R_OK) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!metadata.may_read(current->process()))
|
2019-02-26 14:57:59 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
if (mode & W_OK) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!metadata.may_write(current->process()))
|
2019-02-26 14:57:59 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
if (mode & X_OK) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!metadata.may_execute(current->process()))
|
2019-02-26 14:57:59 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
|
2019-03-01 22:54:07 +00:00
|
|
|
{
|
2019-05-31 13:30:09 +00:00
|
|
|
auto inode_or_error = resolve_path(path, base);
|
2019-03-01 22:54:07 +00:00
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& custody = *inode_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
if (!inode.is_directory())
|
2019-03-01 22:54:07 +00:00
|
|
|
return KResult(-ENOTDIR);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!inode.metadata().may_execute(current->process()))
|
2019-03-01 22:54:07 +00:00
|
|
|
return KResult(-EACCES);
|
2019-05-30 16:58:59 +00:00
|
|
|
return custody;
|
2019-03-01 22:54:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:36:38 +00:00
|
|
|
KResult VFS::chmod(Inode& inode, mode_t mode)
|
2019-01-29 03:55:08 +00:00
|
|
|
{
|
2019-03-01 09:39:19 +00:00
|
|
|
if (inode.fs().is_readonly())
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EROFS);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
if (current->process().euid() != inode.metadata().uid && !current->process().is_superuser())
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EPERM);
|
2019-01-29 03:55:08 +00:00
|
|
|
|
|
|
|
// Only change the permission bits.
|
2019-03-01 09:39:19 +00:00
|
|
|
mode = (inode.mode() & ~04777u) | (mode & 04777u);
|
|
|
|
return inode.chmod(mode);
|
|
|
|
}
|
2019-01-29 03:55:08 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
|
2019-03-01 09:39:19 +00:00
|
|
|
{
|
2019-05-31 13:30:09 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
2019-06-02 10:36:38 +00:00
|
|
|
return chmod(inode, mode);
|
2019-02-25 19:47:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
|
2019-04-07 21:35:26 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> old_parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (old_custody_or_error.is_error())
|
|
|
|
return old_custody_or_error.error();
|
|
|
|
auto& old_custody = *old_custody_or_error.value();
|
|
|
|
auto& old_inode = old_custody.inode();
|
2019-04-07 21:35:26 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> new_parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (new_custody_or_error.is_error()) {
|
|
|
|
if (new_custody_or_error.error() != -ENOENT)
|
|
|
|
return new_custody_or_error.error();
|
2019-04-07 21:35:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& old_parent_inode = old_parent_custody->inode();
|
|
|
|
auto& new_parent_inode = new_parent_custody->inode();
|
|
|
|
|
|
|
|
if (!new_parent_inode.metadata().may_write(current->process()))
|
2019-04-07 21:35:26 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!old_parent_inode.metadata().may_write(current->process()))
|
2019-04-07 21:35:26 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (old_parent_inode.metadata().is_sticky()) {
|
|
|
|
if (!current->process().is_superuser() && old_inode.metadata().uid != current->process().euid())
|
2019-04-28 20:54:30 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
|
2019-05-31 13:22:52 +00:00
|
|
|
auto new_basename = FileSystemPath(new_path).basename();
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!new_custody_or_error.is_error()) {
|
|
|
|
auto& new_custody = *new_custody_or_error.value();
|
|
|
|
auto& new_inode = new_custody.inode();
|
2019-04-07 21:35:26 +00:00
|
|
|
// FIXME: Is this really correct? Check what other systems do.
|
2019-05-30 16:58:59 +00:00
|
|
|
if (&new_inode == &old_inode)
|
2019-04-07 21:35:26 +00:00
|
|
|
return KSuccess;
|
2019-05-30 16:58:59 +00:00
|
|
|
if (new_parent_inode.metadata().is_sticky()) {
|
|
|
|
if (!current->process().is_superuser() && new_inode.metadata().uid != current->process().euid())
|
2019-04-28 21:34:33 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
2019-05-30 16:58:59 +00:00
|
|
|
if (new_inode.is_directory() && !old_inode.is_directory())
|
2019-04-07 21:35:26 +00:00
|
|
|
return KResult(-EISDIR);
|
2019-05-31 13:22:52 +00:00
|
|
|
auto result = new_parent_inode.remove_child(new_basename);
|
2019-04-07 21:35:26 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-05-31 13:22:52 +00:00
|
|
|
new_custody.did_delete({});
|
2019-04-07 21:35:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 15:41:33 +00:00
|
|
|
auto result = new_parent_inode.add_child(old_inode.identifier(), new_basename, old_inode.mode());
|
2019-04-07 21:35:26 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
result = old_parent_inode.remove_child(FileSystemPath(old_path).basename());
|
2019-04-07 21:35:26 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-05-31 13:22:52 +00:00
|
|
|
old_custody.did_rename({}, new_basename);
|
2019-04-07 21:35:26 +00:00
|
|
|
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-06-02 10:30:24 +00:00
|
|
|
KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
|
2019-02-27 11:32:53 +00:00
|
|
|
{
|
2019-05-30 16:58:59 +00:00
|
|
|
if (inode.fs().is_readonly())
|
2019-02-27 11:32:53 +00:00
|
|
|
return KResult(-EROFS);
|
|
|
|
|
2019-06-02 08:31:25 +00:00
|
|
|
auto metadata = inode.metadata();
|
|
|
|
|
|
|
|
if (current->process().euid() != metadata.uid && !current->process().is_superuser())
|
2019-02-27 11:32:53 +00:00
|
|
|
return KResult(-EPERM);
|
|
|
|
|
2019-06-02 08:31:25 +00:00
|
|
|
uid_t new_uid = metadata.uid;
|
|
|
|
gid_t new_gid = metadata.gid;
|
2019-02-27 11:32:53 +00:00
|
|
|
|
|
|
|
if (a_uid != (uid_t)-1) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (current->process().euid() != a_uid && !current->process().is_superuser())
|
2019-02-27 11:32:53 +00:00
|
|
|
return KResult(-EPERM);
|
|
|
|
new_uid = a_uid;
|
|
|
|
}
|
|
|
|
if (a_gid != (gid_t)-1) {
|
2019-03-23 21:03:17 +00:00
|
|
|
if (!current->process().in_group(a_gid) && !current->process().is_superuser())
|
2019-02-27 11:32:53 +00:00
|
|
|
return KResult(-EPERM);
|
|
|
|
new_gid = a_gid;
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
|
2019-05-30 16:58:59 +00:00
|
|
|
return inode.chown(new_uid, new_gid);
|
2019-02-27 11:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:30:24 +00:00
|
|
|
KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
|
|
|
|
{
|
|
|
|
auto custody_or_error = resolve_path(path, base);
|
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
return chown(inode, a_uid, a_gid);
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
|
2019-02-21 12:26:40 +00:00
|
|
|
{
|
2019-05-31 13:30:09 +00:00
|
|
|
auto old_custody_or_error = resolve_path(old_path, base);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (old_custody_or_error.is_error())
|
|
|
|
return old_custody_or_error.error();
|
|
|
|
auto& old_custody = *old_custody_or_error.value();
|
|
|
|
auto& old_inode = old_custody.inode();
|
2019-02-21 12:26:40 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!new_custody_or_error.is_error())
|
2019-02-27 14:31:26 +00:00
|
|
|
return KResult(-EEXIST);
|
2019-01-22 06:03:44 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_custody)
|
2019-02-27 14:31:26 +00:00
|
|
|
return KResult(-ENOENT);
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
|
|
|
|
if (parent_inode.fsid() != old_inode.fsid())
|
2019-02-27 14:31:26 +00:00
|
|
|
return KResult(-EXDEV);
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (parent_inode.fs().is_readonly())
|
2019-02-27 14:31:26 +00:00
|
|
|
return KResult(-EROFS);
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-02-27 14:31:26 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
|
2019-05-31 15:41:33 +00:00
|
|
|
return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), old_inode.mode());
|
2019-02-21 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::unlink(StringView path, Custody& base)
|
2019-02-21 12:26:40 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base, &parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return custody_or_error.error();
|
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
2019-01-23 04:35:42 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (inode.is_directory())
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-EISDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-EACCES);
|
2019-01-22 06:03:44 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (parent_inode.metadata().is_sticky()) {
|
|
|
|
if (!current->process().is_superuser() && inode.metadata().uid != current->process().euid())
|
2019-04-28 20:54:30 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
|
2019-05-31 13:22:52 +00:00
|
|
|
auto result = parent_inode.remove_child(FileSystemPath(path).basename());
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
custody.did_delete({});
|
|
|
|
return KSuccess;
|
2019-01-22 06:03:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
|
2019-03-02 00:50:34 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!existing_custody_or_error.is_error())
|
2019-03-02 00:50:34 +00:00
|
|
|
return KResult(-EEXIST);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!parent_custody)
|
2019-03-02 00:50:34 +00:00
|
|
|
return KResult(-ENOENT);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (existing_custody_or_error.error() != -ENOENT)
|
|
|
|
return existing_custody_or_error.error();
|
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-03-02 00:50:34 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
|
|
|
|
FileSystemPath p(linkpath);
|
2019-07-08 13:38:44 +00:00
|
|
|
dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
|
2019-03-02 00:50:34 +00:00
|
|
|
int error;
|
2019-05-30 16:58:59 +00:00
|
|
|
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, error);
|
2019-03-02 00:50:34 +00:00
|
|
|
if (!new_file)
|
|
|
|
return KResult(error);
|
2019-07-08 13:38:44 +00:00
|
|
|
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
|
2019-03-02 00:50:34 +00:00
|
|
|
if (nwritten < 0)
|
|
|
|
return KResult(nwritten);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
KResult VFS::rmdir(StringView path, Custody& base)
|
2019-01-28 03:16:01 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Custody> parent_custody;
|
2019-05-31 13:30:09 +00:00
|
|
|
auto custody_or_error = resolve_path(path, base, &parent_custody);
|
2019-05-30 16:58:59 +00:00
|
|
|
if (custody_or_error.is_error())
|
|
|
|
return KResult(custody_or_error.error());
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& custody = *custody_or_error.value();
|
|
|
|
auto& inode = custody.inode();
|
|
|
|
if (inode.fs().is_readonly())
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-EROFS);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
|
|
|
// FIXME: We should return EINVAL if the last component of the path is "."
|
|
|
|
// FIXME: We should return ENOTEMPTY if the last component of the path is ".."
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (!inode.is_directory())
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-ENOTDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto& parent_inode = parent_custody->inode();
|
|
|
|
|
|
|
|
if (!parent_inode.metadata().may_write(current->process()))
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-EACCES);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (inode.directory_entry_count() != 2)
|
2019-02-27 13:11:25 +00:00
|
|
|
return KResult(-ENOTEMPTY);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
auto result = inode.remove_child(".");
|
2019-02-27 13:11:25 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
result = inode.remove_child("..");
|
2019-02-27 13:11:25 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
return parent_inode.remove_child(FileSystemPath(path).basename());
|
2019-01-28 03:16:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
|
2018-10-28 11:20:25 +00:00
|
|
|
{
|
2018-12-02 23:20:00 +00:00
|
|
|
if (!inode_id.is_valid())
|
2018-11-13 22:44:54 +00:00
|
|
|
return nullptr;
|
2018-12-02 23:20:00 +00:00
|
|
|
return inode_id.fs()->get_inode(inode_id);
|
2018-11-13 22:44:54 +00:00
|
|
|
}
|
2019-01-31 05:13:55 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
VFS::Mount::Mount(RefPtr<Custody>&& host_custody, NonnullRefPtr<FS>&& guest_fs)
|
2019-05-30 19:29:26 +00:00
|
|
|
: m_guest(guest_fs->root_inode())
|
2018-11-15 14:36:35 +00:00
|
|
|
, m_guest_fs(move(guest_fs))
|
2019-05-30 19:29:26 +00:00
|
|
|
, m_host_custody(move(host_custody))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String VFS::Mount::absolute_path() const
|
|
|
|
{
|
|
|
|
if (!m_host_custody)
|
|
|
|
return "/";
|
|
|
|
return m_host_custody->absolute_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
InodeIdentifier VFS::Mount::host() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-05-30 19:29:26 +00:00
|
|
|
if (!m_host_custody)
|
2019-06-07 09:43:58 +00:00
|
|
|
return {};
|
2019-05-30 19:29:26 +00:00
|
|
|
return m_host_custody->inode().identifier();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
void VFS::for_each_mount(Function<void(const Mount&)> callback) const
|
2018-10-26 16:43:25 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
2019-07-24 07:15:33 +00:00
|
|
|
callback(mount);
|
2018-10-26 16:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 23:39:29 +00:00
|
|
|
|
|
|
|
void VFS::sync()
|
|
|
|
{
|
|
|
|
FS::sync();
|
|
|
|
}
|
2019-05-30 15:46:08 +00:00
|
|
|
|
|
|
|
Custody& VFS::root_custody()
|
|
|
|
{
|
|
|
|
if (!m_root_custody)
|
2019-05-31 13:32:19 +00:00
|
|
|
m_root_custody = Custody::create(nullptr, "", *m_root_inode);
|
2019-05-30 15:46:08 +00:00
|
|
|
return *m_root_custody;
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:39:21 +00:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* parent_custody, int options, int symlink_recursion_level)
|
2019-05-30 15:46:08 +00:00
|
|
|
{
|
2019-08-25 16:18:51 +00:00
|
|
|
// FIXME: resolve_path currently doesn't deal with .. and . . If path is ../. and base is /home/anon, it returns
|
|
|
|
// /home/anon/../. instead of /home .
|
2019-12-24 09:39:21 +00:00
|
|
|
if (symlink_recursion_level >= symlink_recursion_limit)
|
|
|
|
return KResult(-ELOOP);
|
2019-08-25 16:18:51 +00:00
|
|
|
|
2019-05-30 15:46:08 +00:00
|
|
|
if (path.is_empty())
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
|
2019-09-20 21:45:16 +00:00
|
|
|
auto parts = path.split_view('/', true);
|
2019-05-30 15:46:08 +00:00
|
|
|
InodeIdentifier crumb_id;
|
|
|
|
|
2019-06-27 11:44:26 +00:00
|
|
|
NonnullRefPtrVector<Custody, 32> custody_chain;
|
2019-05-30 15:46:08 +00:00
|
|
|
|
|
|
|
if (path[0] == '/') {
|
2019-05-30 16:58:59 +00:00
|
|
|
custody_chain.append(root_custody());
|
2019-05-30 15:46:08 +00:00
|
|
|
crumb_id = root_inode_id();
|
|
|
|
} else {
|
|
|
|
for (auto* custody = &base; custody; custody = custody->parent()) {
|
|
|
|
// FIXME: Prepending here is not efficient! Fix this.
|
|
|
|
custody_chain.prepend(*custody);
|
|
|
|
}
|
|
|
|
crumb_id = base.inode().identifier();
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:58:59 +00:00
|
|
|
if (parent_custody)
|
|
|
|
*parent_custody = custody_chain.last();
|
|
|
|
|
2019-05-30 15:46:08 +00:00
|
|
|
for (int i = 0; i < parts.size(); ++i) {
|
|
|
|
bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
|
|
|
|
auto crumb_inode = get_inode(crumb_id);
|
|
|
|
if (!crumb_inode)
|
|
|
|
return KResult(-EIO);
|
|
|
|
auto metadata = crumb_inode->metadata();
|
|
|
|
if (!metadata.is_directory())
|
|
|
|
return KResult(-ENOTDIR);
|
|
|
|
if (!metadata.may_execute(current->process()))
|
|
|
|
return KResult(-EACCES);
|
2019-06-13 13:33:01 +00:00
|
|
|
|
|
|
|
auto& part = parts[i];
|
|
|
|
if (part.is_empty())
|
2019-09-20 21:45:16 +00:00
|
|
|
continue;
|
2019-06-13 13:33:01 +00:00
|
|
|
|
2019-06-27 11:44:26 +00:00
|
|
|
auto& current_parent = custody_chain.last();
|
2019-05-30 15:46:08 +00:00
|
|
|
crumb_id = crumb_inode->lookup(part);
|
|
|
|
if (!crumb_id.is_valid())
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (auto mount = find_mount_for_host(crumb_id))
|
|
|
|
crumb_id = mount->guest();
|
|
|
|
if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
|
|
|
|
auto mount = find_mount_for_guest(crumb_id);
|
|
|
|
auto dir_inode = get_inode(mount->host());
|
|
|
|
ASSERT(dir_inode);
|
|
|
|
crumb_id = dir_inode->lookup("..");
|
|
|
|
}
|
|
|
|
crumb_inode = get_inode(crumb_id);
|
|
|
|
ASSERT(crumb_inode);
|
2019-05-31 04:42:49 +00:00
|
|
|
|
2019-06-27 11:44:26 +00:00
|
|
|
custody_chain.append(Custody::get_or_create(&custody_chain.last(), part, *crumb_inode));
|
2019-05-31 04:42:49 +00:00
|
|
|
|
2019-05-30 15:46:08 +00:00
|
|
|
metadata = crumb_inode->metadata();
|
2019-05-30 16:58:59 +00:00
|
|
|
if (metadata.is_directory()) {
|
|
|
|
if (i != parts.size() - 1) {
|
|
|
|
if (parent_custody)
|
|
|
|
*parent_custody = custody_chain.last();
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 15:46:08 +00:00
|
|
|
if (metadata.is_symlink()) {
|
|
|
|
if (i == parts.size() - 1) {
|
|
|
|
if (options & O_NOFOLLOW)
|
|
|
|
return KResult(-ELOOP);
|
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
|
|
|
return custody_chain.last();
|
|
|
|
}
|
2019-05-31 04:42:49 +00:00
|
|
|
auto symlink_contents = crumb_inode->read_entire();
|
|
|
|
if (!symlink_contents)
|
2019-05-30 15:46:08 +00:00
|
|
|
return KResult(-ENOENT);
|
2019-05-31 04:42:49 +00:00
|
|
|
|
2019-12-24 09:39:21 +00:00
|
|
|
auto symlink_path = StringView(symlink_contents.data(), symlink_contents.size());
|
|
|
|
auto symlink_target = resolve_path(symlink_path, current_parent, parent_custody, options, symlink_recursion_level + 1);
|
2019-06-12 13:36:05 +00:00
|
|
|
|
|
|
|
if (symlink_target.is_error())
|
|
|
|
return symlink_target;
|
|
|
|
|
|
|
|
bool have_more_parts = i + 1 < parts.size();
|
|
|
|
if (i + 1 == parts.size() - 1 && parts[i + 1].is_empty())
|
|
|
|
have_more_parts = false;
|
|
|
|
|
|
|
|
if (!have_more_parts)
|
|
|
|
return symlink_target;
|
|
|
|
|
2019-06-13 13:30:55 +00:00
|
|
|
StringView remaining_path = path.substring_view_starting_from_substring(parts[i + 1]);
|
2019-12-24 09:39:21 +00:00
|
|
|
return resolve_path(remaining_path, *symlink_target.value(), parent_custody, options, symlink_recursion_level + 1);
|
2019-05-30 15:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return custody_chain.last();
|
|
|
|
}
|