Skip to content

Commit 2b7851d

Browse files
committed
refactor: ♻️ Reimplement all fs stubs, except _close
1 parent e0e2b45 commit 2b7851d

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/system/vfs/vfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ssize_t _read(int file, void* buf, size_t len) {
133133
}
134134
return file_table[file].driver->read_r(r, file_table[file].arg, buf, len);
135135
}*/
136-
136+
/*
137137
int _close(int file) {
138138
struct _reent* r = _REENT;
139139
// NOTE: newlib automatically closes all open files for a given task when
@@ -190,4 +190,4 @@ int32_t fdctl(int file, const uint32_t action, void* const extra_arg) {
190190
return -1;
191191
}
192192
return file_table[file].driver->ctl(file_table[file].arg, action, extra_arg);
193-
}
193+
}*/

src/system/vfs/vfs.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdexcept>
1111
#include <string>
1212
#include <sys/unistd.h>
13+
#include <system_error>
1314
#include <unordered_map>
1415

1516
namespace zest::fs {
@@ -56,10 +57,10 @@ static auto with_fd(int file, std::invocable<zest::fs::FileEntry> auto func) {
5657
zest::fs::FileEntry file_entry;
5758
try {
5859
file_entry = fd_data.at(file).value();
59-
} catch (std::bad_optional_access) {
60+
} catch (std::bad_optional_access&) {
6061
r->_errno = EBADF;
6162
return ReturnType{-1};
62-
} catch (std::out_of_range) {
63+
} catch (std::out_of_range&) {
6364
r->_errno = EBADF;
6465
return ReturnType{-1};
6566
}
@@ -141,7 +142,7 @@ char* getcwd(char* buf, size_t size) {
141142
}
142143

143144
ssize_t _write(int file, void* buf, size_t len) {
144-
return with_fd(file, [=](zest::fs::FileEntry file_entry) {
145+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
145146
return file_entry.driver->write(
146147
file_entry.data,
147148
std::span<std::byte>{static_cast<std::byte*>(buf), len}
@@ -150,11 +151,45 @@ ssize_t _write(int file, void* buf, size_t len) {
150151
}
151152

152153
ssize_t _read(int file, void* buf, size_t len) {
153-
return with_fd(file, [=](zest::fs::FileEntry file_entry) {
154+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
154155
return file_entry.driver->read(
155156
file_entry.data,
156157
std::span<std::byte>{static_cast<std::byte*>(buf), len}
157158
);
158159
});
159160
}
161+
162+
int _close(int file) {
163+
// TODO: implement
164+
(void)file;
165+
return 0;
166+
}
167+
168+
int _fstat(int file, struct stat* st) {
169+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
170+
return file_entry.driver->fstat(file_entry.data, st).transform([]() {
171+
return 0;
172+
});
173+
});
174+
}
175+
176+
off_t _lseek(int file, off_t ptr, int dir) {
177+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
178+
return file_entry.driver->lseek(file_entry.data, ptr, dir);
179+
});
180+
}
181+
182+
int _isatty(int file) {
183+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
184+
bool is_atty = file_entry.driver->isatty(file_entry.data);
185+
return std::expected<int, std::error_condition>{is_atty};
186+
});
187+
}
188+
189+
int32_t fdctl(int file, uint32_t action, void* extra_arg) {
190+
return with_fd(file, [&](zest::fs::FileEntry file_entry) {
191+
int ret_val = file_entry.driver->ctl(file_entry.data, action, extra_arg);
192+
return std::expected<int, std::error_condition>{ret_val};
193+
});
194+
}
160195
}

0 commit comments

Comments
 (0)