Skip to content

Commit 11edca1

Browse files
authored
os: propagate POSIX error code from os.ls/1 and os.rmdir_all/1 on UNIX-like systems (#25392)
1 parent 6762c9a commit 11edca1

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎vlib/os/os.v‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,29 @@ pub fn sigint_to_signal_name(si int) string {
226226

227227
// rmdir_all recursively removes the specified directory.
228228
pub fn rmdir_all(path string) ! {
229-
mut ret_err := ''
229+
mut err_msg := ''
230+
mut err_code := -1
230231
items := ls(path)!
231232
for item in items {
232233
fullpath := join_path_single(path, item)
233234
if is_dir(fullpath) && !is_link(fullpath) {
234-
rmdir_all(fullpath) or { ret_err = err.msg() }
235+
rmdir_all(fullpath) or {
236+
err_msg = err.msg()
237+
err_code = err.code()
238+
}
235239
} else {
236-
rm(fullpath) or { ret_err = err.msg() }
240+
rm(fullpath) or {
241+
err_msg = err.msg()
242+
err_code = err.code()
243+
}
237244
}
238245
}
239-
rmdir(path) or { ret_err = err.msg() }
240-
if ret_err.len > 0 {
241-
return error(ret_err)
246+
rmdir(path) or {
247+
err_msg = err.msg()
248+
err_code = err.code()
249+
}
250+
if err_msg != '' {
251+
return error_with_code(err_msg, err_code)
242252
}
243253
}
244254

‎vlib/os/os_nix.c.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,9 @@ pub fn ls(path string) ![]string {
282282
mut res := []string{cap: 50}
283283
dir := unsafe { C.opendir(&char(path.str)) }
284284
if isnil(dir) {
285-
return error('ls() couldnt open dir "${path}"')
285+
return error_posix(msg: 'ls() couldnt open dir "${path}"')
286286
}
287287
mut ent := &C.dirent(unsafe { nil })
288-
// mut ent := &C.dirent{!}
289288
for {
290289
ent = C.readdir(dir)
291290
if isnil(ent) {

0 commit comments

Comments
 (0)