2021-08-23 13:25:02 +02:00
|
|
|
module os
|
|
|
|
|
2021-12-20 14:18:21 +01:00
|
|
|
$if js_node {
|
2022-01-15 08:55:03 +01:00
|
|
|
#var $fs = require('fs');
|
|
|
|
#var $path = require('path');
|
|
|
|
#var tty = require('tty')
|
2021-12-20 14:18:21 +01:00
|
|
|
}
|
2021-08-23 13:25:02 +02:00
|
|
|
|
|
|
|
pub const (
|
2022-01-15 08:55:03 +01:00
|
|
|
path_delimiter = get_path_delimiter()
|
|
|
|
path_separator = get_path_separator()
|
2021-08-25 13:40:53 +02:00
|
|
|
args = []string{}
|
2021-08-23 13:25:02 +02:00
|
|
|
)
|
|
|
|
|
2022-01-15 08:55:03 +01:00
|
|
|
fn get_path_delimiter() string {
|
|
|
|
delimiter := ':'
|
|
|
|
$if js_node {
|
|
|
|
#delimiter.str = $path.delimiter
|
|
|
|
}
|
|
|
|
return delimiter
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path_separator() string {
|
|
|
|
separator := '/'
|
|
|
|
$if js_node {
|
|
|
|
#separator.str = $path.sep
|
|
|
|
}
|
|
|
|
return separator
|
|
|
|
}
|
|
|
|
|
2021-10-22 21:03:19 +02:00
|
|
|
fn init() {
|
|
|
|
$if js_node {
|
|
|
|
#$process.argv.forEach(function(val,index) { os__args.arr[index] = new string(val); })
|
|
|
|
}
|
2021-08-23 13:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// real_path returns the full absolute path for fpath, with all relative ../../, symlinks and so on resolved.
|
|
|
|
// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
|
|
|
|
// Also https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
|
|
|
|
// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
|
2022-03-06 18:01:22 +01:00
|
|
|
// Note: this particular rabbit hole is *deep* ...
|
2021-08-23 13:25:02 +02:00
|
|
|
pub fn real_path(fpath string) string {
|
|
|
|
$if js_node {
|
|
|
|
mut res := ''
|
|
|
|
#res = new string( $fs.realpathSync(fpath))
|
|
|
|
|
|
|
|
return res
|
|
|
|
} $else {
|
|
|
|
return fpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush will flush the stdout buffer.
|
|
|
|
pub fn flush() {
|
|
|
|
$if js_node {
|
|
|
|
#$process.stdout.write('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 08:40:14 +02:00
|
|
|
pub fn getpid() int {
|
|
|
|
res := 0
|
|
|
|
#res.val = $process.pid
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-08-23 13:25:02 +02:00
|
|
|
// chmod change file access attributes of `path` to `mode`.
|
|
|
|
// Octals like `0o600` can be used.
|
2021-10-13 08:40:14 +02:00
|
|
|
pub fn chmod(path string, mode int) ? {
|
2021-08-23 13:25:02 +02:00
|
|
|
$if js_node {
|
2021-10-13 08:40:14 +02:00
|
|
|
#try {
|
2021-08-23 13:25:02 +02:00
|
|
|
#$fs.chmodSync(''+path,mode.valueOf())
|
2021-10-13 08:40:14 +02:00
|
|
|
#} catch (error) {
|
|
|
|
#return error_with_code(new string("chmod failed: " + error.message),new int(error.code))
|
|
|
|
#}
|
|
|
|
} $else {
|
|
|
|
return error('os.chmod() is available only for NodeJS')
|
2021-08-23 13:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chown changes the owner and group attributes of `path` to `owner` and `group`.
|
|
|
|
// Octals like `0o600` can be used.
|
2021-10-13 08:40:14 +02:00
|
|
|
pub fn chown(path string, owner int, group int) ? {
|
2021-08-23 13:25:02 +02:00
|
|
|
$if js_node {
|
2021-10-13 08:40:14 +02:00
|
|
|
#try {
|
2021-08-23 13:25:02 +02:00
|
|
|
#$fs.chownSync(''+path,owner.valueOf(),group.valueOf())
|
2021-10-13 08:40:14 +02:00
|
|
|
#} catch (error) { return error_with_code(new string("chown failed: " + error.message),new int(error.code)) }
|
|
|
|
} $else {
|
|
|
|
return error('os.chown() is available only for NodeJS')
|
2021-08-23 13:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn temp_dir() string {
|
|
|
|
mut res := ''
|
|
|
|
$if js_node {
|
2021-09-08 19:30:46 +02:00
|
|
|
#res = new string($os.tmpdir())
|
2021-08-23 13:25:02 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn home_dir() string {
|
|
|
|
mut res := ''
|
|
|
|
$if js_node {
|
2021-09-08 19:30:46 +02:00
|
|
|
#res = new string($os.homedir())
|
2021-08-23 13:25:02 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// join_path returns a path as string from input string parameter(s).
|
|
|
|
pub fn join_path(base string, dirs ...string) string {
|
|
|
|
mut result := []string{}
|
|
|
|
result << base.trim_right('\\/')
|
|
|
|
for d in dirs {
|
|
|
|
result << d
|
|
|
|
}
|
|
|
|
mut path_sep := ''
|
|
|
|
#path_sep = $path.sep;
|
|
|
|
|
|
|
|
res := result.join(path_sep)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-11-22 14:03:05 +01:00
|
|
|
pub fn join_path_single(base string, elem string) string {
|
|
|
|
// TODO: deprecate this
|
|
|
|
return join_path(base, elem)
|
|
|
|
}
|
|
|
|
|
2021-08-23 13:25:02 +02:00
|
|
|
pub fn execute(cmd string) Result {
|
|
|
|
mut exit_code := 0
|
|
|
|
mut stdout := ''
|
|
|
|
#let commands = cmd.str.split(' ');
|
|
|
|
#let output = $child_process.spawnSync(commands[0],commands.slice(1,commands.length));
|
2021-09-08 19:30:46 +02:00
|
|
|
#exit_code = new int(output.status)
|
2021-10-13 08:40:14 +02:00
|
|
|
#stdout = new string(output.stdout + '')
|
2021-08-23 13:25:02 +02:00
|
|
|
|
|
|
|
return Result{
|
|
|
|
exit_code: exit_code
|
|
|
|
output: stdout
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 13:00:15 +02:00
|
|
|
|
2021-10-13 08:40:14 +02:00
|
|
|
pub fn system(cmd string) int {
|
|
|
|
exit_code := 0
|
|
|
|
#let commands = cmd.str.split(' ');
|
|
|
|
#exit_code.val = $child_process.execSync(commands[0],commands.slice(1,commands.length));
|
|
|
|
|
|
|
|
return exit_code
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:00:15 +02:00
|
|
|
pub fn is_atty(fd int) int {
|
|
|
|
res := 0
|
|
|
|
#res.val = +tty.isatty(fd.val)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-19 11:11:54 +02:00
|
|
|
|
|
|
|
pub fn glob(patterns ...string) ?[]string {
|
|
|
|
panic('not yet implemented')
|
|
|
|
return none
|
|
|
|
}
|
2021-10-20 15:02:21 +02:00
|
|
|
|
|
|
|
pub fn write_file_array(path string, buffer array) ? {
|
|
|
|
mut f := create(path) ?
|
|
|
|
f.write_array(buffer) ?
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chdir(s string) ? {
|
|
|
|
#try { $process.chdir(s.str); } catch (e) { return error(new string('' + s)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_last_mod_unix(path string) int {
|
|
|
|
mtime := 0
|
|
|
|
#mtime.val = Math.floor($fs.lstatSync(path.str).mtime.getTime() / 1000)
|
|
|
|
|
|
|
|
return mtime
|
|
|
|
}
|