os: add fast path to mkdir_all, when the given folder already exists (#19869)

This commit is contained in:
Turiiya 2023-11-14 22:41:39 +01:00 committed by GitHub
parent b5673c6172
commit b7400fc058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -695,6 +695,12 @@ pub struct MkdirParams {
// 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, params MkdirParams) ! { 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 { '/' } other_separator := if path_separator == '/' { '\\' } else { '/' }
path := opath.replace(other_separator, path_separator) path := opath.replace(other_separator, path_separator)
mut p := if path.starts_with(path_separator) { path_separator } else { '' } mut p := if path.starts_with(path_separator) { path_separator } else { '' }

View File

@ -1016,3 +1016,17 @@ fn test_mv_across_partitions() {
fn test_page_size() { fn test_page_size() {
assert os.page_size() >= 4096 // this is normal and assumed. 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
}