use IoError.UNSUPPORTED_OPERATION instead of asserts; improve Path.walk

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2023-07-26 11:33:08 +02:00
committed by Christoffer Lerno
parent 9c503cf6fd
commit 59b077223b
7 changed files with 19 additions and 18 deletions

View File

@@ -25,6 +25,6 @@ macro void! native_chdir(Path path)
};
return IoError.GENERAL_ERROR?;
$default:
unreachable("'getcwd' not available");
return IoError.UNSUPPORTED_OPERATION?;
$endswitch
}

View File

@@ -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?;
}

View File

@@ -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
}

View File

@@ -45,6 +45,6 @@ macro bool! native_mkdir(Path path, MkdirPermissions permissions)
}
};
$default:
unreachable("'mkdir' not available");
return IoError.UNSUPPORTED_OPERATION?;
$endswitch
}

View File

@@ -43,6 +43,6 @@ macro bool! native_rmdir(Path path)
}
};
$default:
unreachable("'rmdir' not available");
return IoError.UNSUPPORTED_OPERATION?;
$endswitch
}

View File

@@ -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?;
}

View File

@@ -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;
}