1010#include < stdexcept>
1111#include < string>
1212#include < sys/unistd.h>
13+ #include < system_error>
1314#include < unordered_map>
1415
1516namespace 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
143144ssize_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
152153ssize_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