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
|
#include "FileHandle.h"
#include "FileSystem.h"
#include "CharacterDevice.h"
#include "sys-errno.h"
#include "UnixTypes.h"
#include <AK/BufferStream.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)
{
Locker locker(VirtualFileSystem::lock());
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)
{
Locker locker(VirtualFileSystem::lock());
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:
// FIXME: Implement!
notImplemented();
newOffset = 0;
break;
default:
return -EINVAL;
}
m_currentOffset = newOffset;
return m_currentOffset;
}
Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count)
{
Locker locker(VirtualFileSystem::lock());
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);
m_currentOffset += nread;
return nread;
}
ByteBuffer FileHandle::readEntireFile()
{
Locker locker(VirtualFileSystem::lock());
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);
}
ssize_t FileHandle::get_dir_entries(byte* buffer, size_t size)
{
Locker locker(VirtualFileSystem::lock());
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] (const FileSystem::DirectoryEntry& entry) {
stream << (dword)entry.inode.index();
stream << (byte)entry.fileType;
stream << (dword)entry.name.length();
stream << entry.name;
return true;
});
memcpy(buffer, tempBuffer.pointer(), stream.offset());
return stream.offset();
}
|