2020-09-08 15:17:01 +03:00
|
|
|
module util
|
|
|
|
|
|
|
|
import os
|
|
|
|
import rand
|
|
|
|
|
|
|
|
const (
|
|
|
|
retries = 10000
|
|
|
|
)
|
|
|
|
|
2021-09-14 00:05:30 +03:00
|
|
|
[params]
|
2020-09-08 15:17:01 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-25 23:03:20 +03:00
|
|
|
// temp_file returns a uniquely named, open, writable, `os.File` and it's path.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn temp_file(tfo TempFileOptions) !(os.File, string) {
|
2020-09-08 15:17:01 +03:00
|
|
|
mut d := tfo.path
|
|
|
|
if d == '' {
|
|
|
|
d = os.temp_dir()
|
|
|
|
}
|
2022-10-20 13:56:06 +03:00
|
|
|
os.ensure_folder_is_writable(d) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
return error(@FN +
|
2022-11-15 16:53:13 +03:00
|
|
|
' could not create temporary file in "${d}". Please ensure write permissions.')
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
d = d.trim_right(os.path_separator)
|
2022-11-15 16:53:13 +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++ {
|
2021-09-23 14:20:39 +03:00
|
|
|
path := os.join_path(d, prefix + random_number() + suffix)
|
2020-09-08 15:17:01 +03:00
|
|
|
mut mode := 'rw+'
|
|
|
|
$if windows {
|
|
|
|
mode = 'w+'
|
|
|
|
}
|
2021-09-23 14:20:39 +03:00
|
|
|
mut file := os.open_file(path, mode, 0o600) or { continue }
|
2020-09-08 15:17:01 +03:00
|
|
|
if os.exists(path) && os.is_file(path) {
|
|
|
|
return file, path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error(@FN +
|
2022-11-15 16:53:13 +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
|
|
|
}
|
|
|
|
|
2021-09-14 00:05:30 +03:00
|
|
|
[params]
|
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
|
|
|
}
|
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
fn error_for_temporary_folder(fn_name string, d string) !string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('${fn_name} could not create temporary directory "${d}". Please ensure you have write permissions for it.')
|
2022-10-20 13:56:06 +03:00
|
|
|
}
|
|
|
|
|
2023-01-25 23:03:20 +03:00
|
|
|
// temp_dir returns a uniquely named, writable, directory path.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn temp_dir(tdo TempFileOptions) !string {
|
2020-09-08 15:17:01 +03:00
|
|
|
mut d := tdo.path
|
|
|
|
if d == '' {
|
|
|
|
d = os.temp_dir()
|
|
|
|
}
|
2022-10-20 13:56:06 +03:00
|
|
|
os.ensure_folder_is_writable(d) or { return error_for_temporary_folder(@FN, d) }
|
2020-09-08 15:17:01 +03:00
|
|
|
d = d.trim_right(os.path_separator)
|
2022-11-15 16:53:13 +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++ {
|
2021-09-23 14:20:39 +03:00
|
|
|
path := os.join_path(d, prefix + random_number() + suffix)
|
|
|
|
os.mkdir_all(path) or { continue }
|
2020-09-08 15:17:01 +03:00
|
|
|
if os.is_dir(path) && os.exists(path) {
|
2022-10-20 13:56:06 +03:00
|
|
|
os.ensure_folder_is_writable(path) or { return error_for_temporary_folder(@FN, d) }
|
2020-09-08 15:17:01 +03:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('${@FN} could not create temporary directory "${d}". Retry limit (${util.retries}) exhausted.')
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// * Utility functions
|
2021-09-23 14:20:39 +03:00
|
|
|
fn random_number() string {
|
2022-02-23 13:36:14 +03:00
|
|
|
s := (1_000_000_000 + (u32(os.getpid()) + rand.u32n(1_000_000_000) or { 0 })).str()
|
2020-09-08 15:17:01 +03:00
|
|
|
return s.substr(1, s.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prefix_and_suffix(pattern string) ?(string, string) {
|
|
|
|
mut pat := pattern
|
|
|
|
if pat.contains(os.path_separator) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pattern cannot contain path separators (${os.path_separator}).')
|
2020-09-08 15:17:01 +03:00
|
|
|
}
|
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
|
|
|
|
}
|