1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: add an optional "mode" parameter to os.mkdir and os.mkdir_all (#14887)

This commit is contained in:
Markus F.X.J. Oberhumer 2022-06-30 12:49:47 +02:00 committed by GitHub
parent 7c3571b274
commit 74bb5ae17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 12 deletions

View File

@ -58,7 +58,7 @@ fn main() {
if term_colors { if term_colors {
os.setenv('VCOLORS', 'always', true) os.setenv('VCOLORS', 'always', true)
} }
os.mkdir_all(vcheckfolder) or {} os.mkdir_all(vcheckfolder, mode: 0o700) or {} // keep directory private
defer { defer {
os.rmdir_all(vcheckfolder) or {} os.rmdir_all(vcheckfolder) or {}
} }

View File

@ -622,8 +622,13 @@ pub fn log(s string) {
println('os.log: ' + s) 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`. // 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) path := opath.replace('/', path_separator)
mut p := if path.starts_with(path_separator) { path_separator } else { '' } mut p := if path.starts_with(path_separator) { path_separator } else { '' }
path_parts := path.trim_left(path_separator).split(path_separator) 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) { if exists(p) && is_dir(p) {
continue continue
} }
mkdir(p) or { return error('folder: $p, error: $err') } mkdir(p, params) or { return error('folder: $p, error: $err') }
} }
} }

View File

@ -1,6 +1,6 @@
module os module os
pub fn mkdir(path string) ?bool { pub fn mkdir(path string, params MkdirParams) ?bool {
$if js_node { $if js_node {
if path == '.' { if path == '.' {
return true return true

View File

@ -296,7 +296,7 @@ pub fn is_dir(path string) bool {
*/ */
// mkdir creates a new directory with the specified path. // 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 == '.' { if path == '.' {
return true return true
} }
@ -313,7 +313,7 @@ pub fn mkdir(path string) ?bool {
/* /*
$if linux { $if linux {
$if !android { $if !android {
ret := C.syscall(sys_mkdir, apath.str, 511) ret := C.syscall(sys_mkdir, apath.str, params.mode)
if ret == -1 { if ret == -1 {
return error(posix_get_error_msg(C.errno)) 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 { if r == -1 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }

View File

@ -200,7 +200,7 @@ fn test_ls() {
fn create_tree() ? { fn create_tree() ? {
os.mkdir_all('myfolder/f1/f2/f3')? 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') f3 := os.real_path('myfolder/f1/f2/f3')
assert os.is_dir(f3) assert os.is_dir(f3)
create_file('myfolder/f1/f2/f3/a.txt')? create_file('myfolder/f1/f2/f3/a.txt')?

View File

@ -203,7 +203,7 @@ pub fn is_dir(path string) bool {
} }
*/ */
// mkdir creates a new directory with the specified path. // 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 == '.' { if path == '.' {
return true return true
} }

View File

@ -493,7 +493,7 @@ pub fn get_vtmp_folder() string {
uid := os.getuid() uid := os.getuid()
vtmp = os.join_path_single(os.temp_dir(), 'v_$uid') vtmp = os.join_path_single(os.temp_dir(), 'v_$uid')
if !os.exists(vtmp) || !os.is_dir(vtmp) { 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) os.setenv('VTMP', vtmp, true)
return vtmp return vtmp

View File

@ -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(' ')}') nlog(@FN, 'vcache_basepath: $vcache_basepath\n opts: $opts\n os.args: ${os.args.join(' ')}')
dlog(@FN, 'vcache_basepath: $vcache_basepath | opts:\n $opts') dlog(@FN, 'vcache_basepath: $vcache_basepath | opts:\n $opts')
if !os.is_dir(vcache_basepath) { 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') dlog(@FN, 'created folder:\n $vcache_basepath')
} }
readme_file := os.join_path(vcache_basepath, 'README.md') 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) cpath = os.join_path(cprefix_folder, khash)
if !os.is_dir(cprefix_folder) { if !os.is_dir(cprefix_folder) {
os.mkdir_all(cprefix_folder) or { panic(err) } os.mkdir_all(cprefix_folder) or { panic(err) }
os.chmod(cprefix_folder, 0o777) or { panic(err) }
} }
dlog(@FN, 'new hk') dlog(@FN, 'new hk')
dlog(@FN, ' key: $key') dlog(@FN, ' key: $key')