Skip to content

Commit

Permalink
os: make os.cache_dir() and os.vtmp_dir() more robust to parallel tes…
Browse files Browse the repository at this point in the history
…t executions on windows (#20495)
  • Loading branch information
spytheman committed Jan 12, 2024
1 parent d986f82 commit b120c10
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions vlib/os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,21 @@ pub fn mkdir_all(opath string, params MkdirParams) ! {
}
}

fn create_folder_when_it_does_not_exist(path string) {
if is_dir(path) || is_link(path) {
return
}
mkdir_all(path, mode: 0o700) or {
if is_dir(path) || is_link(path) {
// A race had been won, and the `path` folder had been created,
// by another concurrent V executable, since the folder now exists,
// but it did not right before ... we will just use it too.
return
}
panic(err)
}
}

// cache_dir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
pub fn cache_dir() string {
// See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Expand All @@ -748,16 +763,12 @@ pub fn cache_dir() string {
// non-essential data files should be stored. If $XDG_CACHE_HOME is either not set
// or empty, a default equal to $HOME/.cache should be used.
xdg_cache_home := getenv('XDG_CACHE_HOME')
if xdg_cache_home != '' {
if !is_dir(xdg_cache_home) && !is_link(xdg_cache_home) {
mkdir_all(xdg_cache_home, mode: 0o700) or { panic(err) }
}
return xdg_cache_home
}
cdir := join_path_single(home_dir(), '.cache')
if !is_dir(cdir) && !is_link(cdir) {
mkdir(cdir) or { panic(err) }
cdir := if xdg_cache_home.len > 0 {
xdg_cache_home
} else {
join_path_single(home_dir(), '.cache')
}
create_folder_when_it_does_not_exist(cdir)
return cdir
}

Expand Down Expand Up @@ -803,14 +814,12 @@ pub fn temp_dir() string {
pub fn vtmp_dir() string {
mut vtmp := getenv('VTMP')
if vtmp.len > 0 {
create_folder_when_it_does_not_exist(vtmp)
return vtmp
}
uid := getuid()
vtmp = join_path_single(temp_dir(), 'v_${uid}')
if !exists(vtmp) || !is_dir(vtmp) {
// create a new directory, that is private to the user:
mkdir_all(vtmp, mode: 0o700) or { panic(err) }
}
create_folder_when_it_does_not_exist(vtmp)
setenv('VTMP', vtmp, true)
return vtmp
}
Expand Down

0 comments on commit b120c10

Please sign in to comment.