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
|
//===--- Implementation of the Linux specialization of File ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "file.h"
#include "hdr/stdio_macros.h"
#include "hdr/types/off_t.h"
#include "src/__support/CPP/new.h"
#include "src/__support/File/file.h"
#include "src/__support/File/linux/lseekImpl.h"
#include "src/__support/OSUtil/fcntl.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/libc_errno.h" // For error macros
#include "src/__support/macros/config.h"
#include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
#include <sys/stat.h> // For S_IS*, S_IF*, and S_IR* flags.
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
FileIOResult linux_file_write(File *f, const void *data, size_t size) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_write, lf->get_fd(), data, size);
if (ret < 0) {
return {0, -ret};
}
return ret;
}
FileIOResult linux_file_read(File *f, void *buf, size_t size) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_read, lf->get_fd(), buf, size);
if (ret < 0) {
return {0, -ret};
}
return ret;
}
ErrorOr<off_t> linux_file_seek(File *f, off_t offset, int whence) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
auto result = internal::lseekimpl(lf->get_fd(), offset, whence);
if (!result.has_value())
return result.error();
return result.value();
}
int linux_file_close(File *f) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_close, lf->get_fd());
if (ret < 0) {
return -ret;
}
delete lf;
return 0;
}
ErrorOr<File *> openfile(const char *path, const char *mode) {
using ModeFlags = File::ModeFlags;
auto modeflags = File::mode_flags(mode);
if (modeflags == 0) {
// return {nullptr, EINVAL};
return Error(EINVAL);
}
long open_flags = 0;
if (modeflags & ModeFlags(File::OpenMode::APPEND)) {
open_flags = O_CREAT | O_APPEND;
if (modeflags & ModeFlags(File::OpenMode::PLUS))
open_flags |= O_RDWR;
else
open_flags |= O_WRONLY;
} else if (modeflags & ModeFlags(File::OpenMode::WRITE)) {
open_flags = O_CREAT | O_TRUNC;
if (modeflags & ModeFlags(File::OpenMode::PLUS))
open_flags |= O_RDWR;
else
open_flags |= O_WRONLY;
} else {
if (modeflags & ModeFlags(File::OpenMode::PLUS))
open_flags |= O_RDWR;
else
open_flags |= O_RDONLY;
}
// File created will have 0666 permissions.
constexpr long OPEN_MODE =
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#ifdef SYS_open
int fd =
LIBC_NAMESPACE::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
#elif defined(SYS_openat)
int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, AT_FDCWD, path,
open_flags, OPEN_MODE);
#else
#error "open and openat syscalls not available."
#endif
if (fd < 0)
return Error(-fd);
uint8_t *buffer;
{
AllocChecker ac;
buffer = new (ac) uint8_t[File::DEFAULT_BUFFER_SIZE];
if (!ac)
return Error(ENOMEM);
}
AllocChecker ac;
auto *file = new (ac)
LinuxFile(fd, buffer, File::DEFAULT_BUFFER_SIZE, _IOFBF, true, modeflags);
if (!ac)
return Error(ENOMEM);
return file;
}
ErrorOr<LinuxFile *> create_file_from_fd(int fd, const char *mode) {
using ModeFlags = File::ModeFlags;
ModeFlags modeflags = File::mode_flags(mode);
if (modeflags == 0) {
return Error(EINVAL);
}
auto result = internal::fcntl(fd, F_GETFL);
if (!result.has_value()) {
return Error(EBADF);
}
int fd_flags = result.value();
using OpenMode = File::OpenMode;
if (((fd_flags & O_ACCMODE) == O_RDONLY &&
!(modeflags & static_cast<ModeFlags>(OpenMode::READ))) ||
((fd_flags & O_ACCMODE) == O_WRONLY &&
!(modeflags & static_cast<ModeFlags>(OpenMode::WRITE)))) {
return Error(EINVAL);
}
bool do_seek = false;
if ((modeflags & static_cast<ModeFlags>(OpenMode::APPEND)) &&
!(fd_flags & O_APPEND)) {
do_seek = true;
if (!internal::fcntl(fd, F_SETFL,
reinterpret_cast<void *>(fd_flags | O_APPEND))
.has_value()) {
return Error(EBADF);
}
}
uint8_t *buffer;
{
AllocChecker ac;
buffer = new (ac) uint8_t[File::DEFAULT_BUFFER_SIZE];
if (!ac) {
return Error(ENOMEM);
}
}
AllocChecker ac;
auto *file = new (ac)
LinuxFile(fd, buffer, File::DEFAULT_BUFFER_SIZE, _IOFBF, true, modeflags);
if (!ac) {
return Error(ENOMEM);
}
if (do_seek) {
auto result = file->seek(0, SEEK_END);
if (!result.has_value()) {
free(file);
return Error(result.error());
}
}
return file;
}
int get_fileno(File *f) {
auto *lf = reinterpret_cast<LinuxFile *>(f);
return lf->get_fd();
}
} // namespace LIBC_NAMESPACE_DECL
|