From 3a6ccf7f31ea73f10c5835dfca420866e2ead03e Mon Sep 17 00:00:00 2001 From: Don Alfons Nisnoni Date: Sun, 24 Nov 2019 00:55:18 +0800 Subject: [PATCH] os: mkdir() error handling --- tools/gen_vc.v | 2 +- tools/vcreate.v | 2 +- tools/vpm.v | 2 +- vlib/compiler/cc.v | 2 +- vlib/compiler/main.v | 8 ++++---- vlib/compiler/module_header.v | 4 +--- vlib/compiler/vtmp.v | 2 +- vlib/os/os.v | 4 ++-- vlib/os/os_nix.v | 14 ++++++++------ vlib/os/os_test.v | 8 ++++---- vlib/os/os_windows.v | 14 +++++++------- vlib/vweb/assets/assets.v | 2 +- 12 files changed, 32 insertions(+), 32 deletions(-) diff --git a/tools/gen_vc.v b/tools/gen_vc.v index 25912f7c67..267b054300 100644 --- a/tools/gen_vc.v +++ b/tools/gen_vc.v @@ -196,7 +196,7 @@ fn (gen_vc mut GenVC) generate() { // check if gen_vc dir exists if !os.dir_exists(gen_vc.options.work_dir) { // try create - os.mkdir(gen_vc.options.work_dir) + os.mkdir(gen_vc.options.work_dir) or { panic(err) } // still dosen't exist... we have a problem if !os.dir_exists(gen_vc.options.work_dir) { gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir') diff --git a/tools/vcreate.v b/tools/vcreate.v index 7c5c345d20..3537383654 100644 --- a/tools/vcreate.v +++ b/tools/vcreate.v @@ -48,7 +48,7 @@ fn main() { c.description = os.get_line() println('Initialising ...') if (os.is_dir(c.name)) { cerror('folder already exists') exit(3) } - os.mkdir(c.name) + os.mkdir(c.name) or { panic(err) } c.write_vmod() c.write_main() println('Complete !') diff --git a/tools/vpm.v b/tools/vpm.v index a036c80b1a..1241fd1f62 100644 --- a/tools/vpm.v +++ b/tools/vpm.v @@ -154,7 +154,7 @@ fn ensure_vmodules_dir_exist() { home_vmodules := get_vmodules_dir_path() if !os.dir_exists( home_vmodules ) { println('Creating $home_vmodules/ ...') - os.mkdir(home_vmodules) + os.mkdir(home_vmodules) or { panic(err) } } } diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index ddecef9e51..8aceec585c 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -108,7 +108,7 @@ fn (v mut V) cc() { } if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) { // TODO tcc bug, needs an empty libtcc1.a fila - //os.mkdir('/var/tmp/tcc/lib/tcc/') + //os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) } //os.create('/var/tmp/tcc/lib/tcc/libtcc1.a') v.pref.ccompiler = tcc_path a << '-m64' diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 74f9f38990..4e078b7e51 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -843,8 +843,8 @@ pub fn (v &V) log(s string) { pub fn new_v(args[]string) &V { // Create modules dirs if they are missing if !os.dir_exists(v_modules_path) { - os.mkdir(v_modules_path) - os.mkdir('$v_modules_path${os.path_separator}cache') + os.mkdir(v_modules_path) or { panic(err) } + os.mkdir('$v_modules_path${os.path_separator}cache') or { panic(err) } } // Location of all vlib files @@ -903,7 +903,7 @@ pub fn new_v(args[]string) &V { // Cross compiling? Use separate dirs for each os /* if target_os != os.user_os() { - os.mkdir('$TmpPath/vlib/$target_os') + os.mkdir('$TmpPath/vlib/$target_os') or { panic(err) } out_name = '$TmpPath/vlib/$target_os/${base}.o' println('target_os=$target_os user_os=${os.user_os()}') println('!Cross compiling $out_name') @@ -939,7 +939,7 @@ pub fn new_v(args[]string) &V { d := out_name.all_before_last(os.path_separator) if !os.dir_exists(d) { println('creating a new directory "$d"') - os.mkdir(d) + os.mkdir(d) or { panic(err) } } } mut _os := OS.mac diff --git a/vlib/compiler/module_header.v b/vlib/compiler/module_header.v index 6c9ac8f28a..454c2ab780 100644 --- a/vlib/compiler/module_header.v +++ b/vlib/compiler/module_header.v @@ -36,7 +36,7 @@ fn generate_vh(mod string) { pdir := dir.all_before_last(os.path_separator) if !os.dir_exists(pdir) { os.mkdir_all(pdir) - // os.mkdir(os.realpath(dir)) + // os.mkdir(os.realpath(dir)) or { panic(err) } } out := os.create(path) or { panic(err) } mod_path := mod.replace("\\", "/") @@ -169,5 +169,3 @@ fn (g mut VhGen) generate_type() { //g.i = old //g.i-- } - - diff --git a/vlib/compiler/vtmp.v b/vlib/compiler/vtmp.v index 2505fe8d24..1011a47dec 100644 --- a/vlib/compiler/vtmp.v +++ b/vlib/compiler/vtmp.v @@ -10,7 +10,7 @@ import filepath pub fn get_vtmp_folder() string { vtmp := filepath.join(os.tmpdir(),'v') if !os.dir_exists( vtmp ) { - os.mkdir(vtmp) + os.mkdir(vtmp) or { panic(err) } } return vtmp } diff --git a/vlib/os/os.v b/vlib/os/os.v index 7959c18aa1..f307a37031 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -198,7 +198,7 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{ sp := filepath.join(source_path, file) dp := filepath.join(dest_path, file) if os.is_dir(sp) { - os.mkdir(dp) + os.mkdir(dp) or { panic(err) } } cp_r(sp, dp, overwrite) or { os.rmdir(dp) @@ -991,7 +991,7 @@ pub fn mkdir_all(path string) { for subdir in path.split(os.path_separator) { p += subdir + os.path_separator if !os.dir_exists(p) { - os.mkdir(p) + os.mkdir(p) or { panic(err) } } } } diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index bf318ea4b2..31aab766cd 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -61,12 +61,14 @@ pub fn dir_exists(path string) bool { } // mkdir creates a new directory with the specified path. -pub fn mkdir(path string) { - //$if linux { - //C.syscall(83, path.str, 511) // sys_mkdir - //} $else { - C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO - //} +pub fn mkdir(path string) ?bool { + if path == '.' { return true } + apath := os.realpath( path ) + r := int(C.mkdir(apath.str, 511)) + if r == -1 { + return error(get_error_msg(C.errno)) + } + return true } // exec starts the specified command, waits for it to complete, and returns its output. diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index b43e65c60c..cd6e335d75 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -75,7 +75,7 @@ fn test_write_and_read_bytes() { fn test_create_and_delete_folder() { folder := './test1' - os.mkdir(folder) + os.mkdir(folder) or { panic(err) } assert os.dir_exists(folder) folder_contents := os.ls(folder) or { panic(err) } @@ -106,7 +106,7 @@ fn walk_callback(file string) { fn test_walk() { folder := 'test_walk' - os.mkdir(folder) + os.mkdir(folder) or { panic(err) } file1 := folder+os.path_separator+'test1' @@ -137,12 +137,12 @@ fn test_cp_r() { //fileX -> dir/fileX // NB: clean up of the files happens inside the cleanup_leftovers function os.write_file('ex1.txt', 'wow!') - os.mkdir('ex') + os.mkdir('ex') or { panic(err) } os.cp_r('ex1.txt', 'ex', false) or { panic(err) } old := os.read_file('ex1.txt') or { panic(err) } new := os.read_file('ex/ex1.txt') or { panic(err) } assert old == new - os.mkdir('ex/ex2') + os.mkdir('ex/ex2') or { panic(err) } os.write_file('ex2.txt', 'great!') os.cp_r('ex2.txt', 'ex/ex2', false) or { panic(err) } old2 := os.read_file('ex2.txt') or { panic(err) } diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index 16d5641417..cd14592dd5 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -131,14 +131,14 @@ pub fn dir_exists(path string) bool { } // mkdir creates a new directory with the specified path. -pub fn mkdir(path string) { - _path := path.replace('/', '\\') - // Windows doesnt recursively create the folders - // so we need to help it out here - if _path.last_index('\\') != -1 { - mkdir(_path.all_before_last('\\')) +pub fn mkdir(path string) ?bool { + if path == '.' { return true } + apath := os.realpath( path ) + r := int(C.CreateDirectory(apath.to_wide(), 0)) + if r == 0 { + return error('mkdir failed for "$apath", because CreateDirectory returned ' + get_error_msg(int(C.GetLastError()))) } - C.CreateDirectory(_path.to_wide(), 0) + return true } // Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019 diff --git a/vlib/vweb/assets/assets.v b/vlib/vweb/assets/assets.v index 28bb46587b..07d56e4813 100644 --- a/vlib/vweb/assets/assets.v +++ b/vlib/vweb/assets/assets.v @@ -103,7 +103,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string { return out } if !os.dir_exists(am.cache_dir) { - os.mkdir(am.cache_dir) + os.mkdir(am.cache_dir) or { panic(err) } } file := os.create(out_file) or { panic(err)