From 270934441c935247e160fdd2332509b31c84315b Mon Sep 17 00:00:00 2001 From: Simon Ernst Date: Thu, 17 Oct 2019 13:30:05 +0200 Subject: [PATCH] os: make ls() return an optional --- vlib/compiler/cgen.v | 2 +- vlib/compiler/main.v | 2 +- vlib/compiler/msvc.v | 4 ++-- vlib/os/os.v | 2 +- vlib/os/os_nix.v | 6 ++---- vlib/os/os_test.v | 2 +- vlib/os/os_win.v | 5 ++--- vlib/vweb/vweb.v | 2 +- 8 files changed, 11 insertions(+), 14 deletions(-) diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index c0fdcdfaab..07d44b9012 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -250,7 +250,7 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) { } println('$obj_path not found, building it...') parent := os.dir(obj_path) - files := os.ls(parent) + files := os.ls(parent) or { panic(err) } mut cfiles := '' for file in files { if file.ends_with('.c') { diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 36bfd93fd4..5b0a5b35e1 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -499,7 +499,7 @@ pub fn (v &V) v_files_from_dir(dir string) []string { } else if !os.dir_exists(dir) { verror("$dir isn't a directory") } - mut files := os.ls(dir) + mut files := os.ls(dir) or { panic(err) } if v.pref.is_verbose { println('v_files_from_dir ("$dir")') } diff --git a/vlib/compiler/msvc.v b/vlib/compiler/msvc.v index 7491bb57ec..5a7d9a6538 100644 --- a/vlib/compiler/msvc.v +++ b/vlib/compiler/msvc.v @@ -100,7 +100,7 @@ fn find_windows_kit_root(host_arch string) ?WindowsKit { // println(kit_lib) - files := os.ls(kit_lib) + files := os.ls(kit_lib) or { panic(err) } mut highest_path := '' mut highest_int := 0 for f in files { @@ -395,7 +395,7 @@ fn build_thirdparty_obj_file_with_msvc(path string, moduleflags []CFlag) { println('$obj_path not found, building it (with msvc)...') parent := os.dir(obj_path) - files := os.ls(parent) + files := os.ls(parent) or { panic(err) } mut cfiles := '' for file in files { diff --git a/vlib/os/os.v b/vlib/os/os.v index c22c677a18..1c58b7995f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -788,7 +788,7 @@ pub fn walk_ext(path, ext string) []string { if !os.is_dir(path) { return []string } - mut files := os.ls(path) + mut files := os.ls(path) or { panic(err) } mut res := []string for i, file in files { if file.starts_with('.') { diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index ecfe769a22..4abce85b37 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -25,13 +25,11 @@ pub fn get_error_msg(code int) string { return tos(_ptr_text, vstrlen(_ptr_text)) } -pub fn ls(path string) []string { +pub fn ls(path string) ?[]string { mut res := []string dir := C.opendir(path.str) if isnil(dir) { - println('ls() couldnt open dir "$path"') - print_c_errno() - return res + return error('ls() couldnt open dir "$path"') } mut ent := &C.dirent{!} for { diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 9dbf1d6279..f66322a86c 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -37,7 +37,7 @@ fn test_create_and_delete_folder() { folder := './test1' os.mkdir(folder) - folder_contents := os.ls(folder) + folder_contents := os.ls(folder) or { panic(err) } assert folder_contents.len == 0 os.rmdir(folder) diff --git a/vlib/os/os_win.v b/vlib/os/os_win.v index cadfebd0a6..36348c5762 100644 --- a/vlib/os/os_win.v +++ b/vlib/os/os_win.v @@ -51,7 +51,7 @@ fn init_os_args(argc int, argv &byteptr) []string { } -pub fn ls(path string) []string { +pub fn ls(path string) ?[]string { mut find_file_data := Win32finddata{} mut dir_files := []string // We can also check if the handle is valid. but using dir_exists instead @@ -61,8 +61,7 @@ pub fn ls(path string) []string { // } // C.FindClose(h_find_dir) if !dir_exists(path) { - println('ls() couldnt open dir "$path" (does not exist).') - return dir_files + return error('ls() couldnt open dir "$path"') } // NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR) // we need to add files to path eg. c:\windows\*.dll or :\windows\* diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index c0e2b766aa..7a75f0828e 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -194,7 +194,7 @@ fn (ctx mut Context) parse_form(s string) { } fn (ctx mut Context) scan_static_directory(directory_path, mount_path string) { - files := os.ls(directory_path) + files := os.ls(directory_path) or { panic(err) } if files.len > 0 { for file in files { mut ext := ''