Skip to content

Commit b7400fc

Browse files
authored
os: add fast path to mkdir_all, when the given folder already exists (#19869)
1 parent b5673c6 commit b7400fc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

vlib/os/os.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,12 @@ pub struct MkdirParams {
695695

696696
// mkdir_all will create a valid full path of all directories given in `path`.
697697
pub fn mkdir_all(opath string, params MkdirParams) ! {
698+
if exists(opath) {
699+
if is_dir(opath) {
700+
return
701+
}
702+
return error('path `${opath}` already exists, and is not a folder')
703+
}
698704
other_separator := if path_separator == '/' { '\\' } else { '/' }
699705
path := opath.replace(other_separator, path_separator)
700706
mut p := if path.starts_with(path_separator) { path_separator } else { '' }

vlib/os/os_test.c.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,17 @@ fn test_mv_across_partitions() {
10161016
fn test_page_size() {
10171017
assert os.page_size() >= 4096 // this is normal and assumed.
10181018
}
1019+
1020+
fn test_mkdir_at_file_dst() {
1021+
f3 := os.join_path('myfolder', 'f1', 'f2', 'f3')
1022+
os.mkdir_all(f3)!
1023+
assert os.is_dir(f3)
1024+
path := os.join_path(f3, 'no_ext_doc')
1025+
os.write_file(path, '')!
1026+
assert os.exists(path) && os.is_file(path)
1027+
os.mkdir_all(path) or {
1028+
assert err.msg() == 'path `${path}` already exists, and is not a folder', err.msg()
1029+
return
1030+
}
1031+
assert false
1032+
}

0 commit comments

Comments
 (0)