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
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <fcntl.h>
#include <unistd.h>
namespace Core {
ErrorOr<NonnullOwnPtr<File>> File::open(StringView filename, OpenMode mode, mode_t permissions)
{
auto file = TRY(adopt_nonnull_own_or_enomem(new (nothrow) File(mode)));
TRY(file->open_path(filename, permissions));
return file;
}
ErrorOr<NonnullOwnPtr<File>> File::adopt_fd(int fd, OpenMode mode, ShouldCloseFileDescriptor should_close_file_descriptor)
{
if (fd < 0) {
return Error::from_errno(EBADF);
}
if (!has_any_flag(mode, OpenMode::ReadWrite)) {
dbgln("Core::File::adopt_fd: Attempting to adopt a file with neither Read nor Write specified in mode");
return Error::from_errno(EINVAL);
}
auto file = TRY(adopt_nonnull_own_or_enomem(new (nothrow) File(mode, should_close_file_descriptor)));
file->m_fd = fd;
return file;
}
ErrorOr<NonnullOwnPtr<File>> File::standard_input()
{
return File::adopt_fd(STDIN_FILENO, OpenMode::Read, ShouldCloseFileDescriptor::No);
}
ErrorOr<NonnullOwnPtr<File>> File::standard_output()
{
return File::adopt_fd(STDOUT_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No);
}
ErrorOr<NonnullOwnPtr<File>> File::standard_error()
{
return File::adopt_fd(STDERR_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No);
}
ErrorOr<NonnullOwnPtr<File>> File::open_file_or_standard_stream(StringView filename, OpenMode mode)
{
if (!filename.is_empty() && filename != "-"sv)
return File::open(filename, mode);
switch (mode) {
case OpenMode::Read:
return standard_input();
case OpenMode::Write:
return standard_output();
default:
VERIFY_NOT_REACHED();
}
}
int File::open_mode_to_options(OpenMode mode)
{
int flags = 0;
if (has_flag(mode, OpenMode::ReadWrite)) {
flags |= O_RDWR | O_CREAT;
} else if (has_flag(mode, OpenMode::Read)) {
flags |= O_RDONLY;
} else if (has_flag(mode, OpenMode::Write)) {
flags |= O_WRONLY | O_CREAT;
bool should_truncate = !has_any_flag(mode, OpenMode::Append | OpenMode::MustBeNew);
if (should_truncate)
flags |= O_TRUNC;
}
if (has_flag(mode, OpenMode::Append))
flags |= O_APPEND;
if (has_flag(mode, OpenMode::Truncate))
flags |= O_TRUNC;
if (has_flag(mode, OpenMode::MustBeNew))
flags |= O_EXCL;
if (!has_flag(mode, OpenMode::KeepOnExec))
flags |= O_CLOEXEC;
if (!has_flag(mode, OpenMode::Nonblocking))
flags |= O_NONBLOCK;
return flags;
}
ErrorOr<void> File::open_path(StringView filename, mode_t permissions)
{
VERIFY(m_fd == -1);
auto flags = open_mode_to_options(m_mode);
m_fd = TRY(System::open(filename, flags, permissions));
return {};
}
ErrorOr<Bytes> File::read_some(Bytes buffer)
{
if (!has_flag(m_mode, OpenMode::Read)) {
// NOTE: POSIX says that if the fd is not open for reading, the call
// will return EBADF. Since we already know whether we can or
// can't read the file, let's avoid a syscall.
return Error::from_errno(EBADF);
}
ssize_t nread = TRY(System::read(m_fd, buffer));
m_last_read_was_eof = nread == 0;
return buffer.trim(nread);
}
ErrorOr<ByteBuffer> File::read_until_eof(size_t block_size)
{
// Note: This is used as a heuristic, it's not valid for devices or virtual files.
auto const potential_file_size = TRY(System::fstat(m_fd)).st_size;
return read_until_eof_impl(block_size, potential_file_size);
}
ErrorOr<size_t> File::write_some(ReadonlyBytes buffer)
{
if (!has_flag(m_mode, OpenMode::Write)) {
// NOTE: Same deal as Read.
return Error::from_errno(EBADF);
}
return TRY(System::write(m_fd, buffer));
}
bool File::is_eof() const { return m_last_read_was_eof; }
bool File::is_open() const { return m_fd >= 0; }
void File::close()
{
if (!is_open()) {
return;
}
// NOTE: The closing of the file can be interrupted by a signal, in which
// case EINTR will be returned by the close syscall. So let's try closing
// the file until we aren't interrupted by rude signals. :^)
ErrorOr<void> result;
do {
result = System::close(m_fd);
} while (result.is_error() && result.error().code() == EINTR);
VERIFY(!result.is_error());
m_fd = -1;
}
ErrorOr<size_t> File::seek(i64 offset, SeekMode mode)
{
int syscall_mode;
switch (mode) {
case SeekMode::SetPosition:
syscall_mode = SEEK_SET;
break;
case SeekMode::FromCurrentPosition:
syscall_mode = SEEK_CUR;
break;
case SeekMode::FromEndPosition:
syscall_mode = SEEK_END;
break;
default:
VERIFY_NOT_REACHED();
}
size_t seek_result = TRY(System::lseek(m_fd, offset, syscall_mode));
m_last_read_was_eof = false;
return seek_result;
}
ErrorOr<void> File::truncate(size_t length)
{
if (length > static_cast<size_t>(NumericLimits<off_t>::max()))
return Error::from_string_literal("Length is larger than the maximum supported length");
return System::ftruncate(m_fd, length);
}
}
|