Skip to content

Commit

Permalink
os: add fast path to mkdir_all, when the given folder already exists (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 14, 2023
1 parent b5673c6 commit b7400fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/os/os.v
Expand Up @@ -695,6 +695,12 @@ pub struct MkdirParams {

// mkdir_all will create a valid full path of all directories given in `path`.
pub fn mkdir_all(opath string, params MkdirParams) ! {
if exists(opath) {
if is_dir(opath) {
return
}
return error('path `${opath}` already exists, and is not a folder')
}
other_separator := if path_separator == '/' { '\\' } else { '/' }
path := opath.replace(other_separator, path_separator)
mut p := if path.starts_with(path_separator) { path_separator } else { '' }
Expand Down
14 changes: 14 additions & 0 deletions vlib/os/os_test.c.v
Expand Up @@ -1016,3 +1016,17 @@ fn test_mv_across_partitions() {
fn test_page_size() {
assert os.page_size() >= 4096 // this is normal and assumed.
}

fn test_mkdir_at_file_dst() {
f3 := os.join_path('myfolder', 'f1', 'f2', 'f3')
os.mkdir_all(f3)!
assert os.is_dir(f3)
path := os.join_path(f3, 'no_ext_doc')
os.write_file(path, '')!
assert os.exists(path) && os.is_file(path)
os.mkdir_all(path) or {
assert err.msg() == 'path `${path}` already exists, and is not a folder', err.msg()
return
}
assert false
}

0 comments on commit b7400fc

Please sign in to comment.