From 59b077223b9886d73c87bdae169a81e241e57da5 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Wed, 26 Jul 2023 11:33:08 +0200 Subject: [PATCH] use IoError.UNSUPPORTED_OPERATION instead of asserts; improve Path.walk Signed-off-by: Pierre Curto --- lib/std/io/os/chdir.c3 | 2 +- lib/std/io/os/file_nolibc.c3 | 14 +++++++------- lib/std/io/os/fileinfo.c3 | 2 +- lib/std/io/os/mkdir.c3 | 2 +- lib/std/io/os/rmdir.c3 | 2 +- lib/std/io/os/temp_directory.c3 | 2 +- lib/std/io/path.c3 | 13 +++++++------ 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/std/io/os/chdir.c3 b/lib/std/io/os/chdir.c3 index fa7596364..57ce74b07 100644 --- a/lib/std/io/os/chdir.c3 +++ b/lib/std/io/os/chdir.c3 @@ -25,6 +25,6 @@ macro void! native_chdir(Path path) }; return IoError.GENERAL_ERROR?; $default: - unreachable("'getcwd' not available"); + return IoError.UNSUPPORTED_OPERATION?; $endswitch } diff --git a/lib/std/io/os/file_nolibc.c3 b/lib/std/io/os/file_nolibc.c3 index 329928c12..c395365d6 100644 --- a/lib/std/io/os/file_nolibc.c3 +++ b/lib/std/io/os/file_nolibc.c3 @@ -26,7 +26,7 @@ RemoveFn native_remove_fn @weak @if(!$defined(native_remove_fn)); fn void*! native_fopen(String filename, String mode) @inline { if (native_fopen_fn) return native_fopen_fn(filename, mode); - unreachable("Tried to call fopen without support."); + return IoError.UNSUPPORTED_OPERATION?; } /** @@ -37,7 +37,7 @@ fn void*! native_fopen(String filename, String mode) @inline fn void! native_remove(String filename) @inline { if (native_remove_fn) return native_remove_fn(filename); - unreachable("Tried to call remove without support."); + return IoError.UNSUPPORTED_OPERATION?; } /** @@ -47,29 +47,29 @@ fn void! native_remove(String filename) @inline fn void*! native_freopen(void* file, String filename, String mode) @inline { if (native_freopen_fn) return native_freopen_fn(file, filename, mode); - unreachable("Tried to call freopen without support."); + return IoError.UNSUPPORTED_OPERATION?; } fn void! native_fseek(void* file, isz offset, Seek seek_mode) @inline { if (native_fseek_fn) return native_fseek_fn(file, offset, seek_mode); - unreachable("Tried to call fseek without support."); + return IoError.UNSUPPORTED_OPERATION?; } fn usz! native_ftell(CFile file) @inline { if (native_ftell_fn) return native_ftell_fn(file); - unreachable("Tried to call ftell without support."); + return IoError.UNSUPPORTED_OPERATION?; } fn usz! native_fwrite(CFile file, char[] buffer) @inline { if (native_fwrite_fn) return native_fwrite_fn(file, buffer); - unreachable("Tried to call fwrite without support."); + return IoError.UNSUPPORTED_OPERATION?; } fn usz! native_fread(CFile file, char[] buffer) @inline { if (native_fread_fn) return native_fread_fn(file, buffer); - unreachable("Tried to call fread without support."); + return IoError.UNSUPPORTED_OPERATION?; } diff --git a/lib/std/io/os/fileinfo.c3 b/lib/std/io/os/fileinfo.c3 index d8a73163a..21502e39a 100644 --- a/lib/std/io/os/fileinfo.c3 +++ b/lib/std/io/os/fileinfo.c3 @@ -85,7 +85,7 @@ fn bool native_file_or_dir_exists(String path) return posix::access(path.zstr_tcopy(), 0 /* F_OK */) != -1; }; $default: - unreachable("Not supported"); + unreachable("Not supported"); $endswitch } diff --git a/lib/std/io/os/mkdir.c3 b/lib/std/io/os/mkdir.c3 index 717e364b8..7e4f8928e 100644 --- a/lib/std/io/os/mkdir.c3 +++ b/lib/std/io/os/mkdir.c3 @@ -45,6 +45,6 @@ macro bool! native_mkdir(Path path, MkdirPermissions permissions) } }; $default: - unreachable("'mkdir' not available"); + return IoError.UNSUPPORTED_OPERATION?; $endswitch } \ No newline at end of file diff --git a/lib/std/io/os/rmdir.c3 b/lib/std/io/os/rmdir.c3 index 911c291ff..fc3c81e0d 100644 --- a/lib/std/io/os/rmdir.c3 +++ b/lib/std/io/os/rmdir.c3 @@ -43,6 +43,6 @@ macro bool! native_rmdir(Path path) } }; $default: - unreachable("'rmdir' not available"); + return IoError.UNSUPPORTED_OPERATION?; $endswitch } diff --git a/lib/std/io/os/temp_directory.c3 b/lib/std/io/os/temp_directory.c3 index c2821aadf..226fb10cf 100644 --- a/lib/std/io/os/temp_directory.c3 +++ b/lib/std/io/os/temp_directory.c3 @@ -26,5 +26,5 @@ module std::io::os @if(env::NO_LIBC); macro Path! native_temp_directory(Allocator* using = mem::heap()) { - unreachable("Not available"); + return IoError.UNSUPPORTED_OPERATION?; } diff --git a/lib/std/io/path.c3 b/lib/std/io/path.c3 index 90c6a01ac..cd43765f9 100644 --- a/lib/std/io/path.c3 +++ b/lib/std/io/path.c3 @@ -63,7 +63,7 @@ fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String $if $defined(os::native_ls): return os::native_ls(dir, no_dirs, no_symlinks, mask, using); $else - unreachable("'ls' is not available."); + return IoError.UNSUPPORTED_OPERATION?; $endif } @@ -101,7 +101,7 @@ fn void! rmtree(Path path) $if $defined(os::native_rmtree): return os::native_rmtree(path); $else - assert(false, "rmtree is not available"); + return IoError.UNSUPPORTED_OPERATION?; $endif } @@ -407,9 +407,9 @@ fn String Path.root_directory(self) return path_str; } -def PathWalker = fn bool(Path); +def PathWalker = fn bool(Path, bool is_dir, void*); -fn bool! Path.walk(self, PathWalker w, Allocator* using = mem::heap()) +fn bool! Path.walk(self, PathWalker w, void* data, Allocator* using = mem::heap()) { Path abs = self.absolute(using)!; defer abs.free(); @@ -418,8 +418,9 @@ fn bool! Path.walk(self, PathWalker w, Allocator* using = mem::heap()) { if (f.as_str() == "." || f.as_str() == "..") continue; f = abs.append(f.as_str(), using)!; - if (w(f)) return true; - if (is_dir(f) && f.walk(w, using)!) return true; + bool is_directory = is_dir(f); + if (w(f, is_directory, data)) return true; + if (is_directory && f.walk(w, data, using)!) return true; } return false; }