2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-05-30 18:58:59 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2020-02-16 02:15:33 +01:00
|
|
|
#include <AK/Vector.h>
|
2019-05-30 17:46:08 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-05-28 11:23:00 +02:00
|
|
|
KResultOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
|
2021-05-28 11:21:00 +02:00
|
|
|
{
|
|
|
|
|
auto name_kstring = KString::try_create(name);
|
|
|
|
|
if (!name_kstring)
|
|
|
|
|
return ENOMEM;
|
|
|
|
|
auto custody = adopt_ref_if_nonnull(new Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
|
|
|
|
|
if (!custody)
|
|
|
|
|
return ENOMEM;
|
|
|
|
|
return custody.release_nonnull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
|
2019-05-30 17:46:08 +02:00
|
|
|
: m_parent(parent)
|
2021-05-28 11:21:00 +02:00
|
|
|
, m_name(move(name))
|
2019-05-30 17:46:08 +02:00
|
|
|
, m_inode(inode)
|
2020-01-11 18:25:26 +03:00
|
|
|
, m_mount_flags(mount_flags)
|
2019-05-30 17:46:08 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Custody::~Custody()
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-05-30 18:58:59 +02:00
|
|
|
|
|
|
|
|
String Custody::absolute_path() const
|
|
|
|
|
{
|
2020-01-10 23:09:58 +01:00
|
|
|
if (!parent())
|
|
|
|
|
return "/";
|
2019-05-30 18:58:59 +02:00
|
|
|
Vector<const Custody*, 32> custody_chain;
|
|
|
|
|
for (auto* custody = this; custody; custody = custody->parent())
|
|
|
|
|
custody_chain.append(custody);
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
for (int i = custody_chain.size() - 2; i >= 0; --i) {
|
|
|
|
|
builder.append('/');
|
2021-05-28 11:21:00 +02:00
|
|
|
builder.append(custody_chain[i]->name());
|
2019-05-30 18:58:59 +02:00
|
|
|
}
|
|
|
|
|
return builder.to_string();
|
|
|
|
|
}
|
2019-05-31 15:22:52 +02:00
|
|
|
|
2020-05-28 17:56:25 +03:00
|
|
|
bool Custody::is_readonly() const
|
|
|
|
|
{
|
|
|
|
|
if (m_mount_flags & MS_RDONLY)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return m_inode->fs().is_readonly();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|