blob: 733e6cef5d93456b7aff3154e9d6b9c59f271791 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/Plan9FS/FileSystem.h>
#include <Kernel/FileSystem/Plan9FS/Inode.h>
#include <Kernel/FileSystem/Plan9FS/Message.h>
namespace Kernel {
Plan9FSMessage& Plan9FSMessage::operator<<(u8 number)
{
return append_number(number);
}
Plan9FSMessage& Plan9FSMessage::operator<<(u16 number)
{
return append_number(number);
}
Plan9FSMessage& Plan9FSMessage::operator<<(u32 number)
{
return append_number(number);
}
Plan9FSMessage& Plan9FSMessage::operator<<(u64 number)
{
return append_number(number);
}
Plan9FSMessage& Plan9FSMessage::operator<<(StringView string)
{
*this << static_cast<u16>(string.length());
// FIXME: Handle append failure.
(void)m_builder.append(string);
return *this;
}
void Plan9FSMessage::append_data(StringView data)
{
*this << static_cast<u32>(data.length());
// FIXME: Handle append failure.
(void)m_builder.append(data);
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(u8& number)
{
return read_number(number);
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(u16& number)
{
return read_number(number);
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(u32& number)
{
return read_number(number);
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(u64& number)
{
return read_number(number);
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(Plan9FSQIdentifier& qid)
{
return *this >> qid.type >> qid.version >> qid.path;
}
Plan9FSMessage::Decoder& Plan9FSMessage::Decoder::operator>>(StringView& string)
{
u16 length;
*this >> length;
VERIFY(length <= m_data.length());
string = m_data.substring_view(0, length);
m_data = m_data.substring_view_starting_after_substring(string);
return *this;
}
StringView Plan9FSMessage::Decoder::read_data()
{
u32 length;
*this >> length;
VERIFY(length <= m_data.length());
auto data = m_data.substring_view(0, length);
m_data = m_data.substring_view_starting_after_substring(data);
return data;
}
Plan9FSMessage::Plan9FSMessage(Plan9FS& fs, Type type)
: m_builder(KBufferBuilder::try_create().release_value()) // FIXME: Don't assume KBufferBuilder allocation success.
, m_tag(fs.allocate_tag())
, m_type(type)
, m_have_been_built(false)
{
u32 size_placeholder = 0;
*this << size_placeholder << (u8)type << m_tag;
}
Plan9FSMessage::Plan9FSMessage(NonnullOwnPtr<KBuffer>&& buffer)
: m_built { move(buffer), Decoder({ buffer->bytes() }) }
, m_have_been_built(true)
{
u32 size;
u8 raw_type;
*this >> size >> raw_type >> m_tag;
m_type = (Type)raw_type;
}
Plan9FSMessage::~Plan9FSMessage()
{
if (m_have_been_built) {
m_built.buffer.~NonnullOwnPtr<KBuffer>();
m_built.decoder.~Decoder();
} else {
m_builder.~KBufferBuilder();
}
}
Plan9FSMessage& Plan9FSMessage::operator=(Plan9FSMessage&& message)
{
m_tag = message.m_tag;
m_type = message.m_type;
if (m_have_been_built) {
m_built.buffer.~NonnullOwnPtr<KBuffer>();
m_built.decoder.~Decoder();
} else {
m_builder.~KBufferBuilder();
}
m_have_been_built = message.m_have_been_built;
if (m_have_been_built) {
new (&m_built.buffer) NonnullOwnPtr<KBuffer>(move(message.m_built.buffer));
new (&m_built.decoder) Decoder(move(message.m_built.decoder));
} else {
new (&m_builder) KBufferBuilder(move(message.m_builder));
}
return *this;
}
KBuffer const& Plan9FSMessage::build()
{
VERIFY(!m_have_been_built);
auto tmp_buffer = m_builder.build();
// FIXME: We should not assume success here.
VERIFY(tmp_buffer);
m_have_been_built = true;
m_builder.~KBufferBuilder();
new (&m_built.buffer) NonnullOwnPtr<KBuffer>(tmp_buffer.release_nonnull());
new (&m_built.decoder) Decoder({ m_built.buffer->data(), m_built.buffer->size() });
u32* size = reinterpret_cast<u32*>(m_built.buffer->data());
*size = m_built.buffer->size();
return *m_built.buffer;
}
}
|