From 74bb5ae17a4a0af8dda913ddb1ca9d8d4a590136 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 30 Jun 2022 12:49:47 +0200 Subject: [PATCH] os: add an optional "mode" parameter to os.mkdir and os.mkdir_all (#14887) --- cmd/tools/vcheck-md.v | 2 +- vlib/os/os.v | 9 +++++++-- vlib/os/os_js.js.v | 2 +- vlib/os/os_nix.c.v | 6 +++--- vlib/os/os_test.v | 2 +- vlib/os/os_windows.c.v | 2 +- vlib/v/util/util.v | 2 +- vlib/v/vcache/vcache.v | 3 +-- 8 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/tools/vcheck-md.v b/cmd/tools/vcheck-md.v index 7265457f01..9e51c3ee88 100644 --- a/cmd/tools/vcheck-md.v +++ b/cmd/tools/vcheck-md.v @@ -58,7 +58,7 @@ fn main() { if term_colors { os.setenv('VCOLORS', 'always', true) } - os.mkdir_all(vcheckfolder) or {} + os.mkdir_all(vcheckfolder, mode: 0o700) or {} // keep directory private defer { os.rmdir_all(vcheckfolder) or {} } diff --git a/vlib/os/os.v b/vlib/os/os.v index 24a884cf29..891bf18c66 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -622,8 +622,13 @@ pub fn log(s string) { println('os.log: ' + s) } +[params] +pub struct MkdirParams { + mode u32 = 0o777 // note that the actual mode is affected by the process's umask +} + // mkdir_all will create a valid full path of all directories given in `path`. -pub fn mkdir_all(opath string) ? { +pub fn mkdir_all(opath string, params MkdirParams) ? { path := opath.replace('/', path_separator) mut p := if path.starts_with(path_separator) { path_separator } else { '' } path_parts := path.trim_left(path_separator).split(path_separator) @@ -632,7 +637,7 @@ pub fn mkdir_all(opath string) ? { if exists(p) && is_dir(p) { continue } - mkdir(p) or { return error('folder: $p, error: $err') } + mkdir(p, params) or { return error('folder: $p, error: $err') } } } diff --git a/vlib/os/os_js.js.v b/vlib/os/os_js.js.v index 803e15ed89..ad14be843c 100644 --- a/vlib/os/os_js.js.v +++ b/vlib/os/os_js.js.v @@ -1,6 +1,6 @@ module os -pub fn mkdir(path string) ?bool { +pub fn mkdir(path string, params MkdirParams) ?bool { $if js_node { if path == '.' { return true diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 81544f31f6..b359889114 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -296,7 +296,7 @@ pub fn is_dir(path string) bool { */ // mkdir creates a new directory with the specified path. -pub fn mkdir(path string) ?bool { +pub fn mkdir(path string, params MkdirParams) ?bool { if path == '.' { return true } @@ -313,7 +313,7 @@ pub fn mkdir(path string) ?bool { /* $if linux { $if !android { - ret := C.syscall(sys_mkdir, apath.str, 511) + ret := C.syscall(sys_mkdir, apath.str, params.mode) if ret == -1 { return error(posix_get_error_msg(C.errno)) } @@ -321,7 +321,7 @@ pub fn mkdir(path string) ?bool { } } */ - r := unsafe { C.mkdir(&char(apath.str), 511) } + r := unsafe { C.mkdir(&char(apath.str), params.mode) } if r == -1 { return error(posix_get_error_msg(C.errno)) } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index c685ac745d..3bddc7de4c 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -200,7 +200,7 @@ fn test_ls() { fn create_tree() ? { os.mkdir_all('myfolder/f1/f2/f3')? - os.mkdir_all('myfolder/a1/a2/a3')? + os.mkdir_all('myfolder/a1/a2/a3', mode: 0o700)? f3 := os.real_path('myfolder/f1/f2/f3') assert os.is_dir(f3) create_file('myfolder/f1/f2/f3/a.txt')? diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index e0c1bf52a4..e2d596b33f 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -203,7 +203,7 @@ pub fn is_dir(path string) bool { } */ // mkdir creates a new directory with the specified path. -pub fn mkdir(path string) ?bool { +pub fn mkdir(path string, params MkdirParams) ?bool { if path == '.' { return true } diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 5a631f5fc7..2deee678f7 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -493,7 +493,7 @@ pub fn get_vtmp_folder() string { uid := os.getuid() vtmp = os.join_path_single(os.temp_dir(), 'v_$uid') if !os.exists(vtmp) || !os.is_dir(vtmp) { - os.mkdir_all(vtmp) or { panic(err) } + os.mkdir_all(vtmp, mode: 0o700) or { panic(err) } // keep directory private } os.setenv('VTMP', vtmp, true) return vtmp diff --git a/vlib/v/vcache/vcache.v b/vlib/v/vcache/vcache.v index c5927c38fb..0eca4e59bd 100644 --- a/vlib/v/vcache/vcache.v +++ b/vlib/v/vcache/vcache.v @@ -42,7 +42,7 @@ pub fn new_cache_manager(opts []string) CacheManager { nlog(@FN, 'vcache_basepath: $vcache_basepath\n opts: $opts\n os.args: ${os.args.join(' ')}') dlog(@FN, 'vcache_basepath: $vcache_basepath | opts:\n $opts') if !os.is_dir(vcache_basepath) { - os.mkdir_all(vcache_basepath) or { panic(err) } + os.mkdir_all(vcache_basepath, mode: 0o700) or { panic(err) } // keep directory private dlog(@FN, 'created folder:\n $vcache_basepath') } readme_file := os.join_path(vcache_basepath, 'README.md') @@ -90,7 +90,6 @@ pub fn (mut cm CacheManager) key2cpath(key string) string { cpath = os.join_path(cprefix_folder, khash) if !os.is_dir(cprefix_folder) { os.mkdir_all(cprefix_folder) or { panic(err) } - os.chmod(cprefix_folder, 0o777) or { panic(err) } } dlog(@FN, 'new hk') dlog(@FN, ' key: $key')