Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/HashTable.h>
28#include <AK/StringBuilder.h>
29#include <AK/Vector.h>
30#include <Kernel/FileSystem/Custody.h>
31#include <Kernel/FileSystem/Inode.h>
32#include <Kernel/Lock.h>
33
34namespace Kernel {
35
36static Lockable<InlineLinkedList<Custody>>& all_custodies()
37{
38 static Lockable<InlineLinkedList<Custody>>* list;
39 if (!list)
40 list = new Lockable<InlineLinkedList<Custody>>;
41 return *list;
42}
43
44Custody* Custody::get_if_cached(Custody* parent, const StringView& name)
45{
46 LOCKER(all_custodies().lock());
47 for (auto& custody : all_custodies().resource()) {
48 if (custody.is_deleted())
49 continue;
50 if (custody.is_mounted_on())
51 continue;
52 if (custody.parent() == parent && custody.name() == name)
53 return &custody;
54 }
55 return nullptr;
56}
57
58NonnullRefPtr<Custody> Custody::get_or_create(Custody* parent, const StringView& name, Inode& inode, int mount_flags)
59{
60 if (RefPtr<Custody> cached_custody = get_if_cached(parent, name)) {
61 if (&cached_custody->inode() != &inode) {
62 dbg() << "WTF! Cached custody for name '" << name << "' has inode=" << cached_custody->inode().identifier() << ", new inode=" << inode.identifier();
63 }
64 ASSERT(&cached_custody->inode() == &inode);
65 return *cached_custody;
66 }
67 return create(parent, name, inode, mount_flags);
68}
69
70Custody::Custody(Custody* parent, const StringView& name, Inode& inode, int mount_flags)
71 : m_parent(parent)
72 , m_name(name)
73 , m_inode(inode)
74 , m_mount_flags(mount_flags)
75{
76 LOCKER(all_custodies().lock());
77 all_custodies().resource().append(this);
78}
79
80Custody::~Custody()
81{
82 LOCKER(all_custodies().lock());
83 all_custodies().resource().remove(this);
84}
85
86String Custody::absolute_path() const
87{
88 if (!parent())
89 return "/";
90 Vector<const Custody*, 32> custody_chain;
91 for (auto* custody = this; custody; custody = custody->parent())
92 custody_chain.append(custody);
93 StringBuilder builder;
94 for (int i = custody_chain.size() - 2; i >= 0; --i) {
95 builder.append('/');
96 builder.append(custody_chain[i]->name().characters());
97 }
98 return builder.to_string();
99}
100
101void Custody::did_delete(Badge<VFS>)
102{
103 m_deleted = true;
104}
105
106void Custody::did_mount_on(Badge<VFS>)
107{
108 m_mounted_on = true;
109}
110
111void Custody::did_rename(Badge<VFS>, const String& name)
112{
113 m_name = name;
114}
115
116}