From 692ca8ac83244429893ee4c247d6bc6815301246 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 26 Jun 2020 15:01:10 +0200 Subject: [PATCH] os: make all rm functions return an optional --- vlib/os/os.v | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index 8cec21f19b..dcd3a95098 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -643,11 +643,17 @@ pub fn rm(path string) ? { // C.unlink(path.cstr()) } // rmdir removes a specified directory. -pub fn rmdir(path string) { +pub fn rmdir(path string) ? { $if !windows { - C.rmdir(path.str) + rc := C.rmdir(path.str) + if rc == -1 { + return error(posix_get_error_msg(C.errno)) + } } $else { - C.RemoveDirectory(path.to_wide()) + rc := C.RemoveDirectory(path.to_wide()) + if rc == -1 { + return error('Failed to remove "$path"') + } } } @@ -657,7 +663,8 @@ pub fn rmdir_recursive(path string) { rmdir_all(path) } -pub fn rmdir_all(path string) { +pub fn rmdir_all(path string) ? { + mut ret_err := '' items := os.ls(path) or { return } @@ -665,9 +672,12 @@ pub fn rmdir_all(path string) { if os.is_dir(os.join_path(path, item)) { rmdir_all(os.join_path(path, item)) } - os.rm(os.join_path(path, item)) + os.rm(os.join_path(path, item)) or { ret_err = err } + } + os.rmdir(path) or { ret_err = err } + if ret_err.len > 0 { + return error(ret_err) } - os.rmdir(path) } pub fn is_dir_empty(path string) bool {