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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#include <AK/BufferStream.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FIFO.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/InodeFile.h>
#include <Kernel/FileSystem/SharedMemory.h>
#include <Kernel/Net/Socket.h>
#include <Kernel/Process.h>
#include <Kernel/TTY/MasterPTY.h>
#include <Kernel/TTY/TTY.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/VM/MemoryManager.h>
#include <LibC/errno_numbers.h>
NonnullRefPtr<FileDescription> FileDescription::create(RefPtr<Custody>&& custody)
{
auto description = adopt(*new FileDescription(InodeFile::create(custody->inode())));
description->m_custody = move(custody);
return description;
}
NonnullRefPtr<FileDescription> FileDescription::create(RefPtr<File> file, SocketRole role)
{
return adopt(*new FileDescription(move(file), role));
}
FileDescription::FileDescription(RefPtr<File>&& file, SocketRole role)
: m_file(move(file))
{
if (m_file->is_inode())
m_inode = static_cast<InodeFile&>(*m_file).inode();
set_socket_role(role);
}
FileDescription::~FileDescription()
{
if (is_socket())
socket()->detach(*this);
if (is_fifo())
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
m_file->close();
m_file = nullptr;
m_inode = nullptr;
}
void FileDescription::set_socket_role(SocketRole role)
{
if (role == m_socket_role)
return;
ASSERT(is_socket());
if (m_socket_role != SocketRole::None)
socket()->detach(*this);
m_socket_role = role;
socket()->attach(*this);
}
NonnullRefPtr<FileDescription> FileDescription::clone()
{
RefPtr<FileDescription> description;
if (is_fifo()) {
description = fifo()->open_direction(m_fifo_direction);
} else {
description = FileDescription::create(m_file, m_socket_role);
description->m_custody = m_custody;
description->m_inode = m_inode;
}
ASSERT(description);
description->m_current_offset = m_current_offset;
description->m_is_blocking = m_is_blocking;
description->m_should_append = m_should_append;
description->m_file_flags = m_file_flags;
return *description;
}
KResult FileDescription::fstat(stat& buffer)
{
ASSERT(!is_fifo());
if (!m_inode)
return KResult(-EBADF);
return metadata().stat(buffer);
}
KResult FileDescription::fchmod(mode_t mode)
{
if (!m_inode)
return KResult(-EBADF);
return VFS::the().chmod(*m_inode, mode);
}
off_t FileDescription::seek(off_t offset, int whence)
{
if (!m_file->is_seekable())
return -EINVAL;
auto metadata = this->metadata();
if (!metadata.is_valid())
return -EIO;
if (metadata.is_socket() || metadata.is_fifo())
return -ESPIPE;
off_t new_offset;
switch (whence) {
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = m_current_offset + offset;
break;
case SEEK_END:
new_offset = metadata.size;
break;
default:
return -EINVAL;
}
if (new_offset < 0)
return -EINVAL;
// FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
m_current_offset = new_offset;
return m_current_offset;
}
ssize_t FileDescription::read(u8* buffer, ssize_t count)
{
int nread = m_file->read(*this, buffer, count);
if (m_file->is_seekable())
m_current_offset += nread;
return nread;
}
ssize_t FileDescription::write(const u8* data, ssize_t size)
{
int nwritten = m_file->write(*this, data, size);
if (m_file->is_seekable())
m_current_offset += nwritten;
return nwritten;
}
bool FileDescription::can_write()
{
return m_file->can_write(*this);
}
bool FileDescription::can_read()
{
return m_file->can_read(*this);
}
ByteBuffer FileDescription::read_entire_file()
{
// HACK ALERT: (This entire function)
ASSERT(m_file->is_inode());
ASSERT(m_inode);
return m_inode->read_entire(this);
}
bool FileDescription::is_directory() const
{
ASSERT(!is_fifo());
return metadata().is_directory();
}
ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
{
auto metadata = this->metadata();
if (!metadata.is_valid())
return -EIO;
if (!metadata.is_directory())
return -ENOTDIR;
int size_to_allocate = max(PAGE_SIZE, metadata.size);
auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
BufferStream stream(temp_buffer);
VFS::the().traverse_directory_inode(*m_inode, [&stream](auto& entry) {
stream << (u32)entry.inode.index();
stream << (u8)entry.file_type;
stream << (u32)entry.name_length;
stream << entry.name;
return true;
});
stream.snip();
if (size < temp_buffer.size())
return -1;
memcpy(buffer, temp_buffer.pointer(), temp_buffer.size());
return stream.offset();
}
bool FileDescription::is_device() const
{
return m_file->is_device();
}
bool FileDescription::is_tty() const
{
return m_file->is_tty();
}
const TTY* FileDescription::tty() const
{
if (!is_tty())
return nullptr;
return static_cast<const TTY*>(m_file.ptr());
}
TTY* FileDescription::tty()
{
if (!is_tty())
return nullptr;
return static_cast<TTY*>(m_file.ptr());
}
bool FileDescription::is_master_pty() const
{
return m_file->is_master_pty();
}
const MasterPTY* FileDescription::master_pty() const
{
if (!is_master_pty())
return nullptr;
return static_cast<const MasterPTY*>(m_file.ptr());
}
MasterPTY* FileDescription::master_pty()
{
if (!is_master_pty())
return nullptr;
return static_cast<MasterPTY*>(m_file.ptr());
}
int FileDescription::close()
{
return 0;
}
String FileDescription::absolute_path() const
{
if (m_custody)
return m_custody->absolute_path();
dbgprintf("FileDescription::absolute_path() for FD without custody, File type: %s\n", m_file->class_name());
return m_file->absolute_path(*this);
}
InodeMetadata FileDescription::metadata() const
{
if (m_inode)
return m_inode->metadata();
return {};
}
KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot)
{
return m_file->mmap(process, *this, vaddr, offset, size, prot);
}
KResult FileDescription::truncate(off_t length)
{
return m_file->truncate(length);
}
bool FileDescription::is_shared_memory() const
{
return m_file->is_shared_memory();
}
SharedMemory* FileDescription::shared_memory()
{
if (!is_shared_memory())
return nullptr;
return static_cast<SharedMemory*>(m_file.ptr());
}
const SharedMemory* FileDescription::shared_memory() const
{
if (!is_shared_memory())
return nullptr;
return static_cast<const SharedMemory*>(m_file.ptr());
}
bool FileDescription::is_fifo() const
{
return m_file->is_fifo();
}
FIFO* FileDescription::fifo()
{
if (!is_fifo())
return nullptr;
return static_cast<FIFO*>(m_file.ptr());
}
bool FileDescription::is_socket() const
{
return m_file->is_socket();
}
Socket* FileDescription::socket()
{
if (!is_socket())
return nullptr;
return static_cast<Socket*>(m_file.ptr());
}
const Socket* FileDescription::socket() const
{
if (!is_socket())
return nullptr;
return static_cast<const Socket*>(m_file.ptr());
}
void FileDescription::set_file_flags(u32 flags)
{
m_is_blocking = !(flags & O_NONBLOCK);
m_should_append = flags & O_APPEND;
m_file_flags = flags;
}
KResult FileDescription::chown(uid_t uid, gid_t gid)
{
if (!m_inode)
return KResult(-EINVAL);
return VFS::the().chown(*m_inode, uid, gid);
}
|