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
|
#include "FileHandle.h"
#include "FileSystem.h"
#include "CharacterDevice.h"
#include "sys-errno.h"
#include "UnixTypes.h"
#include "TTY.h"
#include <AK/BufferStream.h>
#include <Kernel/Task.h>
FileHandle::FileHandle(RetainPtr<VirtualFileSystem::Node>&& vnode)
: m_vnode(move(vnode))
{
}
FileHandle::~FileHandle()
{
}
#ifndef SERENITY
bool additionWouldOverflow(Unix::off_t a, Unix::off_t b)
{
ASSERT(a > 0);
uint64_t ua = a;
return (ua + b) > maxFileOffset;
}
#endif
int FileHandle::stat(Unix::stat* buffer)
{
if (!m_vnode)
return -EBADF;
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
buffer->st_dev = 0; // FIXME
buffer->st_ino = metadata.inode.index();
buffer->st_mode = metadata.mode;
buffer->st_nlink = metadata.linkCount;
buffer->st_uid = metadata.uid;
buffer->st_gid = metadata.gid;
buffer->st_rdev = 0; // FIXME
buffer->st_size = metadata.size;
buffer->st_blksize = metadata.blockSize;
buffer->st_blocks = metadata.blockCount;
buffer->st_atime = metadata.atime;
buffer->st_mtime = metadata.mtime;
buffer->st_ctime = metadata.ctime;
return 0;
}
Unix::off_t FileHandle::seek(Unix::off_t offset, int whence)
{
if (!m_vnode)
return -EBADF;
// FIXME: The file type should be cached on the vnode.
// It's silly that we have to do a full metadata lookup here.
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
if (metadata.isSocket() || metadata.isFIFO())
return -ESPIPE;
Unix::off_t newOffset;
switch (whence) {
case SEEK_SET:
newOffset = offset;
break;
case SEEK_CUR:
newOffset = m_currentOffset + offset;
#ifndef SERENITY
if (additionWouldOverflow(m_currentOffset, offset))
return -EOVERFLOW;
#endif
if (newOffset < 0)
return -EINVAL;
break;
case SEEK_END:
ASSERT(metadata.size); // FIXME: What do I do?
newOffset = metadata.size;
break;
default:
return -EINVAL;
}
m_currentOffset = newOffset;
return m_currentOffset;
}
Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count)
{
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->read(buffer, count);
}
Unix::ssize_t nread = m_vnode->fileSystem()->readInodeBytes(m_vnode->inode, m_currentOffset, count, buffer, this);
m_currentOffset += nread;
return nread;
}
Unix::ssize_t FileHandle::write(const byte* data, Unix::size_t size)
{
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->write(data, size);
}
// FIXME: Implement non-device writes.
ASSERT_NOT_REACHED();
return -1;
}
bool FileHandle::hasDataAvailableForRead()
{
if (m_vnode->isCharacterDevice())
return m_vnode->characterDevice()->hasDataAvailableForRead();
return true;
}
ByteBuffer FileHandle::readEntireFile()
{
if (m_vnode->isCharacterDevice()) {
auto buffer = ByteBuffer::createUninitialized(1024);
Unix::ssize_t nread = m_vnode->characterDevice()->read(buffer.pointer(), buffer.size());
buffer.trim(nread);
return buffer;
}
return m_vnode->fileSystem()->readEntireInode(m_vnode->inode, this);
}
bool FileHandle::isDirectory() const
{
return m_vnode->metadata().isDirectory();
}
ssize_t FileHandle::get_dir_entries(byte* buffer, Unix::size_t size)
{
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
if (!metadata.isDirectory())
return -ENOTDIR;
// FIXME: Compute the actual size needed.
auto tempBuffer = ByteBuffer::createUninitialized(2048);
BufferStream stream(tempBuffer);
m_vnode->vfs()->enumerateDirectoryInode(m_vnode->inode, [&stream] (auto& entry) {
stream << (dword)entry.inode.index();
stream << (byte)entry.fileType;
stream << (dword)entry.name.length();
stream << entry.name;
return true;
});
if (size < stream.offset())
return -1;
memcpy(buffer, tempBuffer.pointer(), stream.offset());
return stream.offset();
}
bool FileHandle::isTTY() const
{
if (auto* device = m_vnode->characterDevice())
return device->isTTY();
return false;
}
const TTY* FileHandle::tty() const
{
if (auto* device = m_vnode->characterDevice())
return static_cast<const TTY*>(device);
return nullptr;
}
|