Skip to content

Commit

Permalink
os: add an optional "mode" parameter to os.mkdir and os.mkdir_all (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-oberhumer-forks committed Jun 30, 2022
1 parent 7c3571b commit 74bb5ae
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/tools/vcheck-md.v
Expand Up @@ -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 {}
}
Expand Down
9 changes: 7 additions & 2 deletions vlib/os/os.v
Expand Up @@ -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)
Expand All @@ -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') }
}
}

Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
6 changes: 3 additions & 3 deletions vlib/os/os_nix.c.v
Expand Up @@ -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
}
Expand All @@ -313,15 +313,15 @@ 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))
}
return true
}
}
*/
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))
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os_test.v
Expand Up @@ -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')?
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os_windows.c.v
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/util/util.v
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/vcache/vcache.v
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 74bb5ae

Please sign in to comment.