2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module os
|
|
|
|
|
2021-11-21 19:53:42 +01:00
|
|
|
import strings
|
|
|
|
|
|
|
|
pub const max_path_len = 4096
|
|
|
|
|
|
|
|
pub const wd_at_startup = getwd()
|
|
|
|
|
|
|
|
const f_ok = 0
|
|
|
|
|
|
|
|
const x_ok = 1
|
|
|
|
|
|
|
|
const w_ok = 2
|
|
|
|
|
|
|
|
const r_ok = 4
|
2019-10-31 22:57:16 +01:00
|
|
|
|
2021-04-04 16:05:06 +02:00
|
|
|
pub struct Result {
|
|
|
|
pub:
|
|
|
|
exit_code int
|
|
|
|
output string
|
|
|
|
// stderr string // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
pub fn (mut result Result) free() {
|
|
|
|
unsafe { result.output.free() }
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// cp_all will recursively copy `src` to `dst`,
|
|
|
|
// optionally overwriting files or dirs in `dst`.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn cp_all(src string, dst string, overwrite bool) ? {
|
2020-12-15 08:33:31 +01:00
|
|
|
source_path := real_path(src)
|
|
|
|
dest_path := real_path(dst)
|
|
|
|
if !exists(source_path) {
|
2021-01-13 06:05:27 +01:00
|
|
|
return error("Source path doesn't exist")
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
// single file copy
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_dir(source_path) {
|
2021-11-22 14:11:33 +01:00
|
|
|
fname := file_name(source_path)
|
2020-12-27 14:20:30 +01:00
|
|
|
adjusted_path := if is_dir(dest_path) {
|
2021-11-22 13:40:55 +01:00
|
|
|
join_path_single(dest_path, fname)
|
2020-12-27 14:20:30 +01:00
|
|
|
} else {
|
|
|
|
dest_path
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if exists(adjusted_path) {
|
2019-11-17 04:45:20 +01:00
|
|
|
if overwrite {
|
2021-01-26 15:43:10 +01:00
|
|
|
rm(adjusted_path) ?
|
2020-12-15 08:33:31 +01:00
|
|
|
} else {
|
2019-11-19 00:25:55 +01:00
|
|
|
return error('Destination file path already exist')
|
2019-11-17 04:45:20 +01:00
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
cp(source_path, adjusted_path) ?
|
2020-06-29 15:06:26 +02:00
|
|
|
return
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2021-03-12 15:26:55 +01:00
|
|
|
if !exists(dest_path) {
|
|
|
|
mkdir(dest_path) ?
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_dir(dest_path) {
|
2019-11-06 21:05:35 +01:00
|
|
|
return error('Destination path is not a valid directory')
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
files := ls(source_path) ?
|
2019-11-06 21:05:35 +01:00
|
|
|
for file in files {
|
2021-11-22 13:40:55 +01:00
|
|
|
sp := join_path_single(source_path, file)
|
|
|
|
dp := join_path_single(dest_path, file)
|
2020-12-15 08:33:31 +01:00
|
|
|
if is_dir(sp) {
|
2021-02-12 00:55:36 +01:00
|
|
|
if !exists(dp) {
|
|
|
|
mkdir(dp) ?
|
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2020-03-02 19:30:04 +01:00
|
|
|
cp_all(sp, dp, overwrite) or {
|
2021-02-28 21:20:21 +01:00
|
|
|
rmdir(dp) or { return err }
|
|
|
|
return err
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:55:03 +01:00
|
|
|
// mv_by_cp first copies the source file, and if it is copied successfully, deletes the source file.
|
2020-08-27 16:57:01 +02:00
|
|
|
// may be used when you are not sure that the source and target are on the same mount/partition.
|
2020-06-25 12:06:47 +02:00
|
|
|
pub fn mv_by_cp(source string, target string) ? {
|
2020-12-15 08:33:31 +01:00
|
|
|
cp(source, target) ?
|
|
|
|
rm(source) ?
|
2019-11-19 10:55:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-23 02:28:29 +02:00
|
|
|
// read_lines reads the file in `path` into an array of lines.
|
2021-09-18 09:45:04 +02:00
|
|
|
[manualfree]
|
2019-11-19 00:25:55 +01:00
|
|
|
pub fn read_lines(path string) ?[]string {
|
2020-12-15 08:33:31 +01:00
|
|
|
buf := read_file(path) ?
|
2021-03-22 10:20:55 +01:00
|
|
|
res := buf.split_into_lines()
|
|
|
|
unsafe { buf.free() }
|
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// sigint_to_signal_name will translate `si` signal integer code to it's string code representation.
|
2019-10-08 01:34:55 +02:00
|
|
|
pub fn sigint_to_signal_name(si int) string {
|
|
|
|
// POSIX signals:
|
2019-10-24 18:19:03 +02:00
|
|
|
match si {
|
2020-12-15 08:33:31 +01:00
|
|
|
1 { return 'SIGHUP' }
|
|
|
|
2 { return 'SIGINT' }
|
|
|
|
3 { return 'SIGQUIT' }
|
|
|
|
4 { return 'SIGILL' }
|
|
|
|
6 { return 'SIGABRT' }
|
|
|
|
8 { return 'SIGFPE' }
|
|
|
|
9 { return 'SIGKILL' }
|
|
|
|
11 { return 'SIGSEGV' }
|
|
|
|
13 { return 'SIGPIPE' }
|
|
|
|
14 { return 'SIGALRM' }
|
|
|
|
15 { return 'SIGTERM' }
|
2019-12-27 17:59:04 +01:00
|
|
|
else {}
|
|
|
|
}
|
2019-10-08 01:34:55 +02:00
|
|
|
$if linux {
|
|
|
|
// From `man 7 signal` on linux:
|
2019-10-24 18:19:03 +02:00
|
|
|
match si {
|
2020-04-24 16:04:39 +02:00
|
|
|
// TODO dependent on platform
|
|
|
|
// works only on x86/ARM/most others
|
2020-12-15 08:33:31 +01:00
|
|
|
10 /* , 30, 16 */ { return 'SIGUSR1' }
|
|
|
|
12 /* , 31, 17 */ { return 'SIGUSR2' }
|
|
|
|
17 /* , 20, 18 */ { return 'SIGCHLD' }
|
|
|
|
18 /* , 19, 25 */ { return 'SIGCONT' }
|
|
|
|
19 /* , 17, 23 */ { return 'SIGSTOP' }
|
|
|
|
20 /* , 18, 24 */ { return 'SIGTSTP' }
|
|
|
|
21 /* , 26 */ { return 'SIGTTIN' }
|
|
|
|
22 /* , 27 */ { return 'SIGTTOU' }
|
2019-12-19 22:29:37 +01:00
|
|
|
// /////////////////////////////
|
2020-12-15 08:33:31 +01:00
|
|
|
5 { return 'SIGTRAP' }
|
|
|
|
7 { return 'SIGBUS' }
|
2019-12-27 17:59:04 +01:00
|
|
|
else {}
|
2020-01-21 16:58:47 +01:00
|
|
|
}
|
2019-10-08 01:34:55 +02:00
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// rmdir_all recursively removes the specified directory.
|
2020-06-26 15:01:10 +02:00
|
|
|
pub fn rmdir_all(path string) ? {
|
|
|
|
mut ret_err := ''
|
2020-12-15 08:33:31 +01:00
|
|
|
items := ls(path) ?
|
2019-12-30 05:22:28 +01:00
|
|
|
for item in items {
|
2021-11-22 13:40:55 +01:00
|
|
|
fullpath := join_path_single(path, item)
|
2021-11-20 09:24:18 +01:00
|
|
|
if is_dir(fullpath) && !is_link(fullpath) {
|
2021-03-10 17:45:12 +01:00
|
|
|
rmdir_all(fullpath) or { ret_err = err.msg }
|
2021-03-13 07:43:38 +01:00
|
|
|
} else {
|
|
|
|
rm(fullpath) or { ret_err = err.msg }
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
2020-06-26 15:01:10 +02:00
|
|
|
}
|
2021-03-10 17:45:12 +01:00
|
|
|
rmdir(path) or { ret_err = err.msg }
|
2020-06-26 15:01:10 +02:00
|
|
|
if ret_err.len > 0 {
|
|
|
|
return error(ret_err)
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// is_dir_empty will return a `bool` whether or not `path` is empty.
|
2021-10-29 14:49:30 +02:00
|
|
|
[manualfree]
|
2019-12-30 05:22:28 +01:00
|
|
|
pub fn is_dir_empty(path string) bool {
|
2020-12-15 08:33:31 +01:00
|
|
|
items := ls(path) or { return true }
|
2021-10-29 14:49:30 +02:00
|
|
|
res := items.len == 0
|
|
|
|
unsafe { items.free() }
|
|
|
|
return res
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// file_ext will return the part after the last occurence of `.` in `path`.
|
|
|
|
// The `.` is included.
|
2020-03-26 14:18:08 +01:00
|
|
|
pub fn file_ext(path string) string {
|
2020-12-15 08:33:31 +01:00
|
|
|
pos := path.last_index('.') or { return '' }
|
2020-03-07 22:26:26 +01:00
|
|
|
return path[pos..]
|
2019-06-26 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 01:25:52 +02:00
|
|
|
// dir returns all but the last element of path, typically the path's directory.
|
|
|
|
// After dropping the final element, trailing slashes are removed.
|
|
|
|
// If the path is empty, dir returns ".". If the path consists entirely of separators,
|
|
|
|
// dir returns a single separator.
|
|
|
|
// The returned path does not end in a separator unless it is the root directory.
|
2021-07-31 12:52:54 +02:00
|
|
|
pub fn dir(opath string) string {
|
|
|
|
if opath == '' {
|
2020-10-01 01:25:52 +02:00
|
|
|
return '.'
|
|
|
|
}
|
2021-07-31 12:52:54 +02:00
|
|
|
path := opath.replace_each(['/', path_separator, r'\', path_separator])
|
2020-12-15 08:33:31 +01:00
|
|
|
pos := path.last_index(path_separator) or { return '.' }
|
2021-07-30 02:14:09 +02:00
|
|
|
if pos == 0 && path_separator == '/' {
|
|
|
|
return '/'
|
|
|
|
}
|
2020-03-07 22:26:26 +01:00
|
|
|
return path[..pos]
|
2019-06-26 12:56:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 01:25:52 +02:00
|
|
|
// base returns the last element of path.
|
|
|
|
// Trailing path separators are removed before extracting the last element.
|
|
|
|
// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a
|
|
|
|
// single separator.
|
2021-07-31 12:52:54 +02:00
|
|
|
pub fn base(opath string) string {
|
|
|
|
if opath == '' {
|
2020-10-01 01:25:52 +02:00
|
|
|
return '.'
|
|
|
|
}
|
2021-07-31 12:52:54 +02:00
|
|
|
path := opath.replace_each(['/', path_separator, r'\', path_separator])
|
2020-10-01 01:25:52 +02:00
|
|
|
if path == path_separator {
|
|
|
|
return path_separator
|
|
|
|
}
|
|
|
|
if path.ends_with(path_separator) {
|
2020-12-15 08:33:31 +01:00
|
|
|
path2 := path[..path.len - 1]
|
|
|
|
pos := path2.last_index(path_separator) or { return path2.clone() }
|
|
|
|
return path2[pos + 1..]
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
pos := path.last_index(path_separator) or { return path.clone() }
|
|
|
|
return path[pos + 1..]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// file_name will return all characters found after the last occurence of `path_separator`.
|
|
|
|
// file extension is included.
|
2021-07-31 12:52:54 +02:00
|
|
|
pub fn file_name(opath string) string {
|
|
|
|
path := opath.replace_each(['/', path_separator, r'\', path_separator])
|
2020-05-20 11:04:28 +02:00
|
|
|
return path.all_after_last(path_separator)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 11:50:15 +01:00
|
|
|
// input_opt returns a one-line string from stdin, after printing a prompt.
|
|
|
|
// In the event of error (end of input), it returns `none`.
|
|
|
|
pub fn input_opt(prompt string) ?string {
|
2020-04-25 21:03:51 +02:00
|
|
|
print(prompt)
|
|
|
|
flush()
|
2021-02-27 11:50:15 +01:00
|
|
|
res := get_raw_line()
|
|
|
|
if res.len > 0 {
|
|
|
|
return res.trim_right('\r\n')
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// input returns a one-line string from stdin, after printing a prompt.
|
|
|
|
// In the event of error (end of input), it returns '<EOF>'.
|
|
|
|
pub fn input(prompt string) string {
|
|
|
|
res := input_opt(prompt) or { return '<EOF>' }
|
|
|
|
return res
|
2020-04-25 21:03:51 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
// get_line returns a one-line string from stdin
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn get_line() string {
|
2019-12-19 22:29:37 +01:00
|
|
|
str := get_raw_line()
|
2019-09-15 18:07:40 +02:00
|
|
|
$if windows {
|
|
|
|
return str.trim_right('\r\n')
|
|
|
|
}
|
2021-02-27 11:50:15 +01:00
|
|
|
return str.trim_right('\n')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// get_lines returns an array of strings read from from stdin.
|
|
|
|
// reading is stopped when an empty line is read.
|
2019-07-24 18:14:13 +02:00
|
|
|
pub fn get_lines() []string {
|
2019-12-19 22:29:37 +01:00
|
|
|
mut line := ''
|
2020-04-26 09:17:13 +02:00
|
|
|
mut inputstr := []string{}
|
2019-12-19 22:29:37 +01:00
|
|
|
for {
|
|
|
|
line = get_line()
|
2020-03-27 14:57:19 +01:00
|
|
|
if line.len <= 0 {
|
2019-12-19 22:29:37 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr << line
|
|
|
|
}
|
|
|
|
return inputstr
|
2019-07-24 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// get_lines_joined returns a string of the values read from from stdin.
|
|
|
|
// reading is stopped when an empty line is read.
|
2019-07-24 18:14:13 +02:00
|
|
|
pub fn get_lines_joined() string {
|
2019-10-26 19:21:07 +02:00
|
|
|
mut line := ''
|
|
|
|
mut inputstr := ''
|
|
|
|
for {
|
|
|
|
line = get_line()
|
|
|
|
if line.len <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr += line
|
|
|
|
}
|
|
|
|
return inputstr
|
2019-07-24 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 11:38:54 +01:00
|
|
|
// get_raw_lines_joined reads *all* input lines from stdin.
|
|
|
|
// It returns them as one large string. NB: unlike os.get_lines_joined,
|
|
|
|
// empty lines (that contain only `\r\n` or `\n`), will be present in
|
|
|
|
// the output.
|
|
|
|
// Reading is stopped, only on EOF of stdin.
|
|
|
|
pub fn get_raw_lines_joined() string {
|
|
|
|
mut line := ''
|
|
|
|
mut lines := []string{}
|
|
|
|
for {
|
|
|
|
line = get_raw_line()
|
|
|
|
if line.len <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lines << line
|
|
|
|
}
|
|
|
|
res := lines.join('')
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-26 19:21:07 +02:00
|
|
|
// user_os returns current user operating system name.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn user_os() string {
|
|
|
|
$if linux {
|
|
|
|
return 'linux'
|
|
|
|
}
|
2019-12-03 14:29:24 +01:00
|
|
|
$if macos {
|
2020-09-29 21:10:33 +02:00
|
|
|
return 'macos'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return 'windows'
|
|
|
|
}
|
2019-07-15 17:24:40 +02:00
|
|
|
$if freebsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'freebsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if openbsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'openbsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if netbsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'netbsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if dragonfly {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'dragonfly'
|
2019-07-23 23:23:13 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
$if android {
|
2019-09-16 17:23:11 +02:00
|
|
|
return 'android'
|
|
|
|
}
|
2019-09-26 23:30:41 +02:00
|
|
|
$if solaris {
|
|
|
|
return 'solaris'
|
|
|
|
}
|
2019-12-03 09:26:47 +01:00
|
|
|
$if haiku {
|
|
|
|
return 'haiku'
|
|
|
|
}
|
2021-07-13 10:37:33 +02:00
|
|
|
$if serenity {
|
|
|
|
return 'serenity'
|
|
|
|
}
|
|
|
|
$if vinix {
|
|
|
|
return 'vinix'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2020-12-15 08:58:33 +01:00
|
|
|
// home_dir returns path to the user's home directory.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn home_dir() string {
|
|
|
|
$if windows {
|
2020-12-15 08:33:31 +01:00
|
|
|
return getenv('USERPROFILE')
|
2020-02-18 02:12:10 +01:00
|
|
|
} $else {
|
2020-12-15 08:33:31 +01:00
|
|
|
// println('home_dir() call')
|
|
|
|
// res:= os.getenv('HOME')
|
|
|
|
// println('res="$res"')
|
|
|
|
return getenv('HOME')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:32:28 +02:00
|
|
|
// expand_tilde_to_home expands the character `~` in `path` to the user's home directory.
|
|
|
|
// See also `home_dir()`.
|
|
|
|
pub fn expand_tilde_to_home(path string) string {
|
|
|
|
if path == '~' {
|
|
|
|
return home_dir().trim_right(path_separator)
|
|
|
|
}
|
|
|
|
if path.starts_with('~' + path_separator) {
|
|
|
|
return path.replace_once('~' + path_separator, home_dir().trim_right(path_separator) +
|
|
|
|
path_separator)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
// write_file writes `text` data to a file in `path`.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn write_file(path string, text string) ? {
|
2020-12-15 08:33:31 +01:00
|
|
|
mut f := create(path) ?
|
2021-09-08 12:09:32 +02:00
|
|
|
unsafe { f.write_full_buffer(text.str, usize(text.len)) ? }
|
2019-06-22 20:20:28 +02:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// executable_fallback is used when there is not a more platform specific and accurate implementation.
|
|
|
|
// It relies on path manipulation of os.args[0] and os.wd_at_startup, so it may not work properly in
|
2020-03-13 22:19:02 +01:00
|
|
|
// all cases, but it should be better, than just using os.args[0] directly.
|
|
|
|
fn executable_fallback() string {
|
2021-08-25 13:40:53 +02:00
|
|
|
if args.len == 0 {
|
2020-04-23 20:52:13 +02:00
|
|
|
// we are early in the bootstrap, os.args has not been initialized yet :-|
|
|
|
|
return ''
|
|
|
|
}
|
2021-08-25 13:40:53 +02:00
|
|
|
mut exepath := args[0]
|
2020-07-16 18:33:26 +02:00
|
|
|
$if windows {
|
|
|
|
if !exepath.contains('.exe') {
|
|
|
|
exepath += '.exe'
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_abs_path(exepath) {
|
2021-07-31 12:52:54 +02:00
|
|
|
rexepath := exepath.replace_each(['/', path_separator, r'\', path_separator])
|
|
|
|
if rexepath.contains(path_separator) {
|
2021-11-22 13:40:55 +01:00
|
|
|
exepath = join_path_single(os.wd_at_startup, exepath)
|
2020-12-15 08:33:31 +01:00
|
|
|
} else {
|
2020-03-13 22:19:02 +01:00
|
|
|
// no choice but to try to walk the PATH folders :-| ...
|
2020-12-15 08:33:31 +01:00
|
|
|
foundpath := find_abs_path_of_executable(exepath) or { '' }
|
2020-03-13 22:19:02 +01:00
|
|
|
if foundpath.len > 0 {
|
|
|
|
exepath = foundpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
exepath = real_path(exepath)
|
2020-03-13 22:19:02 +01:00
|
|
|
return exepath
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:53:42 +01:00
|
|
|
pub struct ErrExecutableNotFound {
|
|
|
|
msg string = 'os: failed to find executable'
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_failed_to_find_executable() IError {
|
|
|
|
return IError(&ErrExecutableNotFound{})
|
|
|
|
}
|
|
|
|
|
2020-03-13 22:19:02 +01:00
|
|
|
// find_exe_path walks the environment PATH, just like most shell do, it returns
|
|
|
|
// the absolute path of the executable if found
|
|
|
|
pub fn find_abs_path_of_executable(exepath string) ?string {
|
2021-03-12 16:05:26 +01:00
|
|
|
if exepath == '' {
|
|
|
|
return error('expected non empty `exepath`')
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if is_abs_path(exepath) {
|
|
|
|
return real_path(exepath)
|
2020-03-13 22:19:02 +01:00
|
|
|
}
|
|
|
|
mut res := ''
|
2021-11-21 19:53:42 +01:00
|
|
|
path := getenv('PATH')
|
|
|
|
paths := path.split(path_delimiter)
|
2020-03-13 22:19:02 +01:00
|
|
|
for p in paths {
|
2021-11-22 13:40:55 +01:00
|
|
|
found_abs_path := join_path_single(p, exepath)
|
2020-12-15 08:33:31 +01:00
|
|
|
if exists(found_abs_path) && is_executable(found_abs_path) {
|
2020-03-13 22:19:02 +01:00
|
|
|
res = found_abs_path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if res.len > 0 {
|
|
|
|
return real_path(res)
|
2020-03-13 22:19:02 +01:00
|
|
|
}
|
2021-11-21 19:53:42 +01:00
|
|
|
return error_failed_to_find_executable()
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// exists_in_system_path returns `true` if `prog` exists in the system's PATH
|
2020-05-16 22:46:05 +02:00
|
|
|
pub fn exists_in_system_path(prog string) bool {
|
2020-12-15 08:33:31 +01:00
|
|
|
find_abs_path_of_executable(prog) or { return false }
|
2020-05-16 17:10:34 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:27:52 +02:00
|
|
|
// is_file returns a `bool` indicating whether the given `path` is a file.
|
|
|
|
pub fn is_file(path string) bool {
|
|
|
|
return exists(path) && !is_dir(path)
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// is_abs_path returns `true` if `path` is absolute.
|
2020-03-10 16:09:37 +01:00
|
|
|
pub fn is_abs_path(path string) bool {
|
2021-05-09 09:45:34 +02:00
|
|
|
if path.len == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-03-07 22:26:26 +01:00
|
|
|
$if windows {
|
2021-01-23 10:33:53 +01:00
|
|
|
return path[0] == `/` || // incase we're in MingGW bash
|
2021-05-10 09:01:32 +02:00
|
|
|
(path[0].is_letter() && path.len > 1 && path[1] == `:`)
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
|
|
|
return path[0] == `/`
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// join_path returns a path as string from input string parameter(s).
|
2021-01-14 03:55:30 +01:00
|
|
|
[manualfree]
|
2020-03-09 02:23:34 +01:00
|
|
|
pub fn join_path(base string, dirs ...string) string {
|
2021-11-22 13:40:55 +01:00
|
|
|
// TODO: fix freeing of `dirs` when the passed arguments are variadic,
|
|
|
|
// but do not free the arr, when `os.join_path(base, ...arr)` is called.
|
2021-11-21 19:53:42 +01:00
|
|
|
mut sb := strings.new_builder(base.len + dirs.len * 50)
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
|
|
|
sbase := base.trim_right('\\/')
|
|
|
|
defer {
|
|
|
|
unsafe { sbase.free() }
|
|
|
|
}
|
|
|
|
sb.write_string(sbase)
|
2020-03-07 22:26:26 +01:00
|
|
|
for d in dirs {
|
2021-11-21 19:53:42 +01:00
|
|
|
sb.write_string(path_separator)
|
|
|
|
sb.write_string(d)
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
2021-11-21 19:53:42 +01:00
|
|
|
return sb.str()
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 13:40:55 +01:00
|
|
|
// join_path_single appends the `elem` after `base`, using a platform specific
|
|
|
|
// path_separator.
|
|
|
|
[manualfree]
|
|
|
|
pub fn join_path_single(base string, elem string) string {
|
|
|
|
// TODO: deprecate this and make it `return os.join_path(base, elem)`,
|
|
|
|
// when freeing variadic args vs ...arr is solved in the compiler
|
|
|
|
mut sb := strings.new_builder(base.len + elem.len + 1)
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
|
|
|
sbase := base.trim_right('\\/')
|
|
|
|
defer {
|
|
|
|
unsafe { sbase.free() }
|
|
|
|
}
|
|
|
|
sb.write_string(sbase)
|
|
|
|
sb.write_string(path_separator)
|
|
|
|
sb.write_string(elem)
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// walk_ext returns a recursive list of all files in `path` ending with `ext`.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn walk_ext(path string, ext string) []string {
|
2021-10-24 17:38:44 +02:00
|
|
|
mut res := []string{}
|
|
|
|
impl_walk_ext(path, ext, mut res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-10-24 17:48:54 +02:00
|
|
|
fn impl_walk_ext(path string, ext string, mut out []string) {
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_dir(path) {
|
2021-10-24 17:38:44 +02:00
|
|
|
return
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2021-10-24 17:38:44 +02:00
|
|
|
mut files := ls(path) or { return }
|
2020-12-15 08:33:31 +01:00
|
|
|
separator := if path.ends_with(path_separator) { '' } else { path_separator }
|
2020-04-27 15:16:31 +02:00
|
|
|
for file in files {
|
2019-08-16 14:05:11 +02:00
|
|
|
if file.starts_with('.') {
|
2019-08-22 22:19:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 17:35:26 +01:00
|
|
|
p := path + separator + file
|
2020-12-15 08:33:31 +01:00
|
|
|
if is_dir(p) && !is_link(p) {
|
2021-10-24 17:38:44 +02:00
|
|
|
impl_walk_ext(p, ext, mut out)
|
2020-12-15 08:33:31 +01:00
|
|
|
} else if file.ends_with(ext) {
|
2021-10-24 17:38:44 +02:00
|
|
|
out << p
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
|
2021-10-24 18:12:14 +02:00
|
|
|
// walk traverses the given directory `path`.
|
|
|
|
// When a file is encountred it will call the
|
|
|
|
// callback function `f` with current file as argument.
|
2020-12-15 08:33:31 +01:00
|
|
|
pub fn walk(path string, f fn (string)) {
|
2021-10-24 18:12:14 +02:00
|
|
|
if path.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_dir(path) {
|
2020-02-21 20:14:01 +01:00
|
|
|
return
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
mut files := ls(path) or { return }
|
2021-02-10 17:48:01 +01:00
|
|
|
mut local_path_separator := path_separator
|
|
|
|
if path.ends_with(path_separator) {
|
|
|
|
local_path_separator = ''
|
|
|
|
}
|
2019-10-24 14:17:09 +02:00
|
|
|
for file in files {
|
2021-02-10 17:48:01 +01:00
|
|
|
p := path + local_path_separator + file
|
2020-12-15 08:33:31 +01:00
|
|
|
if is_dir(p) && !is_link(p) {
|
2020-02-11 10:26:46 +01:00
|
|
|
walk(p, f)
|
2020-12-15 08:33:31 +01:00
|
|
|
} else if exists(p) {
|
2020-02-11 10:26:46 +01:00
|
|
|
f(p)
|
2019-10-24 14:17:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:12:14 +02:00
|
|
|
// FnWalkContextCB is used to define the callback functions, passed to os.walk_context
|
|
|
|
pub type FnWalkContextCB = fn (voidptr, string)
|
|
|
|
|
|
|
|
// walk_with_context traverses the given directory `path`.
|
|
|
|
// For each encountred file, it will call your `fcb` callback,
|
|
|
|
// passing it the arbitrary `context` in its first parameter,
|
|
|
|
// and the path to the file in its second parameter.
|
|
|
|
pub fn walk_with_context(path string, context voidptr, fcb FnWalkContextCB) {
|
|
|
|
if path.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !is_dir(path) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut files := ls(path) or { return }
|
|
|
|
mut local_path_separator := path_separator
|
|
|
|
if path.ends_with(path_separator) {
|
|
|
|
local_path_separator = ''
|
|
|
|
}
|
|
|
|
for file in files {
|
|
|
|
p := path + local_path_separator + file
|
|
|
|
if is_dir(p) && !is_link(p) {
|
|
|
|
walk_with_context(p, context, fcb)
|
|
|
|
} else {
|
|
|
|
fcb(context, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// log will print "os.log: "+`s` ...
|
2019-09-21 20:38:12 +02:00
|
|
|
pub fn log(s string) {
|
2021-01-23 10:32:00 +01:00
|
|
|
println('os.log: ' + s)
|
2019-06-30 14:55:48 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// mkdir_all will create a valid full path of all directories given in `path`.
|
2021-10-24 18:16:46 +02:00
|
|
|
pub fn mkdir_all(opath string) ? {
|
|
|
|
path := opath.replace('/', path_separator)
|
2020-12-15 08:33:31 +01:00
|
|
|
mut p := if path.starts_with(path_separator) { path_separator } else { '' }
|
|
|
|
path_parts := path.trim_left(path_separator).split(path_separator)
|
2020-07-06 14:08:38 +02:00
|
|
|
for subdir in path_parts {
|
2020-12-15 08:33:31 +01:00
|
|
|
p += subdir + path_separator
|
|
|
|
if exists(p) && is_dir(p) {
|
2020-07-06 14:08:38 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
mkdir(p) or { return error('folder: $p, error: $err') }
|
2019-10-12 04:04:56 +02:00
|
|
|
}
|
2019-10-12 07:41:41 +02:00
|
|
|
}
|
2019-10-15 17:08:46 +02:00
|
|
|
|
2020-03-08 15:57:47 +01:00
|
|
|
// cache_dir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
|
|
|
|
pub fn cache_dir() string {
|
2020-01-03 19:46:23 +01:00
|
|
|
// See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
// There is a single base directory relative to which user-specific non-essential
|
|
|
|
// (cached) data should be written. This directory is defined by the environment
|
|
|
|
// variable $XDG_CACHE_HOME.
|
|
|
|
// $XDG_CACHE_HOME defines the base directory relative to which user specific
|
|
|
|
// 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.
|
|
|
|
$if !windows {
|
2020-12-15 08:33:31 +01:00
|
|
|
xdg_cache_home := getenv('XDG_CACHE_HOME')
|
2020-01-03 19:46:23 +01:00
|
|
|
if xdg_cache_home != '' {
|
|
|
|
return xdg_cache_home
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 13:40:55 +01:00
|
|
|
cdir := join_path_single(home_dir(), '.cache')
|
2020-12-15 08:33:31 +01:00
|
|
|
if !is_dir(cdir) && !is_link(cdir) {
|
2021-03-01 00:18:14 +01:00
|
|
|
mkdir(cdir) or { panic(err) }
|
2020-01-03 19:46:23 +01:00
|
|
|
}
|
|
|
|
return cdir
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// temp_dir returns the path to a folder, that is suitable for storing temporary files.
|
2020-03-10 15:02:09 +01:00
|
|
|
pub fn temp_dir() string {
|
2020-12-15 08:33:31 +01:00
|
|
|
mut path := getenv('TMPDIR')
|
2019-11-17 04:45:20 +01:00
|
|
|
$if windows {
|
|
|
|
if path == '' {
|
|
|
|
// TODO see Qt's implementation?
|
|
|
|
// https://doc.qt.io/qt-5/qdir.html#tempPath
|
|
|
|
// https://github.com/qt/qtbase/blob/e164d61ca8263fc4b46fdd916e1ea77c7dd2b735/src/corelib/io/qfilesystemengine_win.cpp#L1275
|
2020-12-15 08:33:31 +01:00
|
|
|
path = getenv('TEMP')
|
2019-12-19 22:29:37 +01:00
|
|
|
if path == '' {
|
2020-12-15 08:33:31 +01:00
|
|
|
path = getenv('TMP')
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
|
|
|
if path == '' {
|
|
|
|
path = 'C:/tmp'
|
|
|
|
}
|
2019-11-17 04:45:20 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 02:14:27 +02:00
|
|
|
$if macos {
|
|
|
|
// avoid /var/folders/6j/cmsk8gd90pd.... on macs
|
|
|
|
return '/tmp'
|
|
|
|
}
|
2020-08-05 19:02:57 +02:00
|
|
|
$if android {
|
|
|
|
// TODO test+use '/data/local/tmp' on Android before using cache_dir()
|
|
|
|
if path == '' {
|
2020-12-15 08:33:31 +01:00
|
|
|
path = cache_dir()
|
2020-08-05 19:02:57 +02:00
|
|
|
}
|
2020-01-03 19:46:23 +01:00
|
|
|
}
|
2019-12-30 05:25:26 +01:00
|
|
|
if path == '' {
|
|
|
|
path = '/tmp'
|
|
|
|
}
|
2019-11-17 04:45:20 +01:00
|
|
|
return path
|
|
|
|
}
|
2019-11-24 04:27:02 +01:00
|
|
|
|
2020-11-30 18:31:37 +01:00
|
|
|
fn default_vmodules_path() string {
|
2021-11-21 19:53:42 +01:00
|
|
|
hdir := home_dir()
|
2021-11-22 13:40:55 +01:00
|
|
|
res := join_path_single(hdir, '.vmodules')
|
2021-11-21 19:53:42 +01:00
|
|
|
return res
|
2020-11-30 18:31:37 +01:00
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
|
2020-11-08 07:07:17 +01:00
|
|
|
// vmodules_dir returns the path to a folder, where v stores its global modules.
|
|
|
|
pub fn vmodules_dir() string {
|
2020-11-30 18:31:37 +01:00
|
|
|
paths := vmodules_paths()
|
|
|
|
if paths.len > 0 {
|
|
|
|
return paths[0]
|
|
|
|
}
|
2020-12-15 08:33:31 +01:00
|
|
|
return default_vmodules_path()
|
2020-11-30 18:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// vmodules_paths returns a list of paths, where v looks up for modules.
|
|
|
|
// You can customize it through setting the environment variable VMODULES
|
2021-11-21 21:34:38 +01:00
|
|
|
// [manualfree]
|
2020-11-30 18:31:37 +01:00
|
|
|
pub fn vmodules_paths() []string {
|
2020-12-15 08:33:31 +01:00
|
|
|
mut path := getenv('VMODULES')
|
2020-11-08 07:07:17 +01:00
|
|
|
if path == '' {
|
2021-11-21 21:34:38 +01:00
|
|
|
// unsafe { path.free() }
|
2020-12-15 08:33:31 +01:00
|
|
|
path = default_vmodules_path()
|
2020-11-08 07:07:17 +01:00
|
|
|
}
|
2021-11-21 19:53:42 +01:00
|
|
|
defer {
|
2021-11-21 21:34:38 +01:00
|
|
|
// unsafe { path.free() }
|
2021-11-21 19:53:42 +01:00
|
|
|
}
|
|
|
|
splitted := path.split(path_delimiter)
|
|
|
|
defer {
|
2021-11-21 21:34:38 +01:00
|
|
|
// unsafe { splitted.free() }
|
2021-11-21 19:53:42 +01:00
|
|
|
}
|
|
|
|
mut list := []string{cap: splitted.len}
|
|
|
|
for i in 0 .. splitted.len {
|
|
|
|
si := splitted[i]
|
|
|
|
trimmed := si.trim_right(path_separator)
|
|
|
|
list << trimmed
|
2021-11-21 21:34:38 +01:00
|
|
|
// unsafe { trimmed.free() }
|
|
|
|
// unsafe { si.free() }
|
2021-11-21 19:53:42 +01:00
|
|
|
}
|
2020-11-30 18:31:37 +01:00
|
|
|
return list
|
2020-11-08 07:07:17 +01:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:57:01 +02:00
|
|
|
// resource_abs_path returns an absolute path, for the given `path`.
|
2020-01-12 19:35:06 +01:00
|
|
|
// (the path is expected to be relative to the executable program)
|
|
|
|
// See https://discordapp.com/channels/592103645835821068/592294828432424960/630806741373943808
|
|
|
|
// It gives a convenient way to access program resources like images, fonts, sounds and so on,
|
|
|
|
// *no matter* how the program was started, and what is the current working directory.
|
2021-01-14 03:55:30 +01:00
|
|
|
[manualfree]
|
2020-01-12 19:35:06 +01:00
|
|
|
pub fn resource_abs_path(path string) string {
|
2021-01-14 03:55:30 +01:00
|
|
|
exe := executable()
|
|
|
|
dexe := dir(exe)
|
|
|
|
mut base_path := real_path(dexe)
|
2020-12-15 08:33:31 +01:00
|
|
|
vresource := getenv('V_RESOURCE_PATH')
|
2020-01-12 19:35:06 +01:00
|
|
|
if vresource.len != 0 {
|
2021-10-29 14:49:30 +02:00
|
|
|
unsafe { base_path.free() }
|
2020-01-12 19:35:06 +01:00
|
|
|
base_path = vresource
|
|
|
|
}
|
2021-11-22 13:40:55 +01:00
|
|
|
fp := join_path_single(base_path, path)
|
2021-01-14 03:55:30 +01:00
|
|
|
res := real_path(fp)
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
|
|
|
fp.free()
|
2021-10-29 14:49:30 +02:00
|
|
|
vresource.free()
|
2021-02-14 19:31:42 +01:00
|
|
|
base_path.free()
|
2021-10-29 14:49:30 +02:00
|
|
|
dexe.free()
|
|
|
|
exe.free()
|
2021-02-14 19:31:42 +01:00
|
|
|
}
|
2021-01-14 03:55:30 +01:00
|
|
|
return res
|
2020-01-12 19:35:06 +01:00
|
|
|
}
|
2020-05-19 12:38:06 +02:00
|
|
|
|
2020-06-14 15:46:30 +02:00
|
|
|
pub struct Uname {
|
|
|
|
pub mut:
|
|
|
|
sysname string
|
|
|
|
nodename string
|
|
|
|
release string
|
|
|
|
version string
|
|
|
|
machine string
|
|
|
|
}
|
2021-03-08 19:52:13 +01:00
|
|
|
|
|
|
|
pub fn execute_or_panic(cmd string) Result {
|
|
|
|
res := execute(cmd)
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
eprintln('failed cmd: $cmd')
|
|
|
|
eprintln('failed code: $res.exit_code')
|
|
|
|
panic(res.output)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2021-04-14 07:50:50 +02:00
|
|
|
|
2021-07-20 13:04:35 +02:00
|
|
|
pub fn execute_or_exit(cmd string) Result {
|
|
|
|
res := execute(cmd)
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
eprintln('failed cmd: $cmd')
|
|
|
|
eprintln('failed code: $res.exit_code')
|
|
|
|
eprintln(res.output)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|