2020-09-08 15:17:01 +03:00
|
|
|
module util
|
|
|
|
|
|
|
|
import os
|
|
|
|
import rand
|
2021-01-26 16:55:09 +03:00
|
|
|
import rand.seed as rseed
|
2020-09-08 15:17:01 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
retries = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
pub struct TempFileOptions {
|
|
|
|
path string = os.temp_dir()
|
2020-09-09 15:14:44 +03:00
|
|
|
pattern string
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// temp_file returns an uniquely named, open, writable, `os.File` and it's path
|
|
|
|
pub fn temp_file(tfo TempFileOptions) ?(os.File, string) {
|
|
|
|
mut d := tfo.path
|
|
|
|
if d == '' {
|
|
|
|
d = os.temp_dir()
|
|
|
|
}
|
|
|
|
os.is_writable_folder(d) or {
|
|
|
|
return error(@FN +
|
|
|
|
' could not create temporary file in "$d". Please ensure write permissions.')
|
|
|
|
}
|
|
|
|
d = d.trim_right(os.path_separator)
|
2021-03-03 14:41:00 +03:00
|
|
|
mut rng := rand.new_default({})
|
2021-02-28 22:24:29 +03:00
|
|
|
prefix, suffix := prefix_and_suffix(tfo.pattern) or { return error(@FN + ' ' + err.msg) }
|
2021-03-03 14:41:00 +03:00
|
|
|
for retry := 0; retry < util.retries; retry++ {
|
2020-09-08 15:17:01 +03:00
|
|
|
path := os.join_path(d, prefix + random_number(mut rng) + suffix)
|
|
|
|
mut mode := 'rw+'
|
|
|
|
$if windows {
|
|
|
|
mode = 'w+'
|
|
|
|
}
|
|
|
|
mut file := os.open_file(path, mode, 0o600) or {
|
2021-01-26 16:55:09 +03:00
|
|
|
rng.seed(rseed.time_seed_array(2))
|
2020-09-08 15:17:01 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if os.exists(path) && os.is_file(path) {
|
|
|
|
return file, path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error(@FN +
|
2021-03-03 14:41:00 +03:00
|
|
|
' could not create temporary file in "$d". Retry limit ($util.retries) exhausted. Please ensure write permissions.')
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TempDirOptions {
|
|
|
|
path string = os.temp_dir()
|
2020-09-09 15:14:44 +03:00
|
|
|
pattern string
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// temp_dir returns an uniquely named, writable, directory path
|
|
|
|
pub fn temp_dir(tdo TempFileOptions) ?string {
|
|
|
|
mut d := tdo.path
|
|
|
|
if d == '' {
|
|
|
|
d = os.temp_dir()
|
|
|
|
}
|
|
|
|
os.is_writable_folder(d) or {
|
|
|
|
return error(@FN +
|
|
|
|
' could not create temporary directory "$d". Please ensure write permissions.')
|
|
|
|
}
|
|
|
|
d = d.trim_right(os.path_separator)
|
2021-03-03 14:41:00 +03:00
|
|
|
mut rng := rand.new_default({})
|
2021-02-28 22:24:29 +03:00
|
|
|
prefix, suffix := prefix_and_suffix(tdo.pattern) or { return error(@FN + ' ' + err.msg) }
|
2021-03-03 14:41:00 +03:00
|
|
|
for retry := 0; retry < util.retries; retry++ {
|
2020-09-08 15:17:01 +03:00
|
|
|
path := os.join_path(d, prefix + random_number(mut rng) + suffix)
|
|
|
|
os.mkdir_all(path) or {
|
2021-01-26 16:55:09 +03:00
|
|
|
rng.seed(rseed.time_seed_array(2))
|
2020-09-08 15:17:01 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if os.is_dir(path) && os.exists(path) {
|
|
|
|
os.is_writable_folder(path) or {
|
|
|
|
return error(@FN +
|
|
|
|
' could not create temporary directory "$d". Please ensure write permissions.')
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error(@FN +
|
2021-03-03 14:41:00 +03:00
|
|
|
' could not create temporary directory "$d". Retry limit ($util.retries) exhausted. Please ensure write permissions.')
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// * Utility functions
|
2021-03-03 14:41:00 +03:00
|
|
|
fn random_number(mut rng rand.PRNG) string {
|
2020-09-08 15:17:01 +03:00
|
|
|
s := (u32(1e9) + (u32(os.getpid()) + rng.u32() % u32(1e9))).str()
|
|
|
|
return s.substr(1, s.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prefix_and_suffix(pattern string) ?(string, string) {
|
|
|
|
mut pat := pattern
|
|
|
|
if pat.contains(os.path_separator) {
|
|
|
|
return error('pattern cannot contain path separators ($os.path_separator).')
|
|
|
|
}
|
2021-01-26 16:55:09 +03:00
|
|
|
pos := pat.last_index('*') or { -1 }
|
2020-09-08 15:17:01 +03:00
|
|
|
mut prefix := ''
|
|
|
|
mut suffix := ''
|
|
|
|
if pos != -1 {
|
|
|
|
prefix = pat.substr(0, pos)
|
|
|
|
suffix = pat.substr(pos + 1, pat.len)
|
|
|
|
} else {
|
|
|
|
prefix = pat
|
|
|
|
}
|
|
|
|
return prefix, suffix
|
|
|
|
}
|