2021-08-23 14:25:02 +03:00
|
|
|
module os
|
|
|
|
|
|
|
|
#const $fs = require('fs');
|
|
|
|
#const $path = require('path');
|
2021-09-16 14:00:15 +03:00
|
|
|
#const tty = require('tty')
|
2021-08-23 14:25:02 +03:00
|
|
|
|
|
|
|
pub const (
|
|
|
|
path_delimiter = '/'
|
|
|
|
path_separator = '/'
|
2021-08-25 14:40:53 +03:00
|
|
|
args = []string{}
|
2021-08-23 14:25:02 +03:00
|
|
|
)
|
|
|
|
|
2021-10-22 22:03:19 +03:00
|
|
|
fn init() {
|
|
|
|
$if js_node {
|
|
|
|
#$process.argv.forEach(function(val,index) { os__args.arr[index] = new string(val); })
|
|
|
|
}
|
2021-08-23 14:25:02 +03: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
|
|
|
|
// NB: this particular rabbit hole is *deep* ...
|
|
|
|
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 09:40:14 +03:00
|
|
|
pub fn getpid() int {
|
|
|
|
res := 0
|
|
|
|
#res.val = $process.pid
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:25:02 +03:00
|
|
|
// chmod change file access attributes of `path` to `mode`.
|
|
|
|
// Octals like `0o600` can be used.
|
2021-10-13 09:40:14 +03:00
|
|
|
pub fn chmod(path string, mode int) ? {
|
2021-08-23 14:25:02 +03:00
|
|
|
$if js_node {
|
2021-10-13 09:40:14 +03:00
|
|
|
#try {
|
2021-08-23 14:25:02 +03:00
|
|
|
#$fs.chmodSync(''+path,mode.valueOf())
|
2021-10-13 09:40:14 +03: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 14:25:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chown changes the owner and group attributes of `path` to `owner` and `group`.
|
|
|
|
// Octals like `0o600` can be used.
|
2021-10-13 09:40:14 +03:00
|
|
|
pub fn chown(path string, owner int, group int) ? {
|
2021-08-23 14:25:02 +03:00
|
|
|
$if js_node {
|
2021-10-13 09:40:14 +03:00
|
|
|
#try {
|
2021-08-23 14:25:02 +03:00
|
|
|
#$fs.chownSync(''+path,owner.valueOf(),group.valueOf())
|
2021-10-13 09:40:14 +03: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 14:25:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn temp_dir() string {
|
|
|
|
mut res := ''
|
|
|
|
$if js_node {
|
2021-09-08 20:30:46 +03:00
|
|
|
#res = new string($os.tmpdir())
|
2021-08-23 14:25:02 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn home_dir() string {
|
|
|
|
mut res := ''
|
|
|
|
$if js_node {
|
2021-09-08 20:30:46 +03:00
|
|
|
#res = new string($os.homedir())
|
2021-08-23 14:25:02 +03: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 16:03:05 +03:00
|
|
|
pub fn join_path_single(base string, elem string) string {
|
|
|
|
// TODO: deprecate this
|
|
|
|
return join_path(base, elem)
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:25:02 +03: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 20:30:46 +03:00
|
|
|
#exit_code = new int(output.status)
|
2021-10-13 09:40:14 +03:00
|
|
|
#stdout = new string(output.stdout + '')
|
2021-08-23 14:25:02 +03:00
|
|
|
|
|
|
|
return Result{
|
|
|
|
exit_code: exit_code
|
|
|
|
output: stdout
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 14:00:15 +03:00
|
|
|
|
2021-10-13 09:40:14 +03: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 14:00:15 +03:00
|
|
|
pub fn is_atty(fd int) int {
|
|
|
|
res := 0
|
|
|
|
#res.val = +tty.isatty(fd.val)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-19 12:11:54 +03:00
|
|
|
|
|
|
|
pub fn glob(patterns ...string) ?[]string {
|
|
|
|
panic('not yet implemented')
|
|
|
|
return none
|
|
|
|
}
|
2021-10-20 16:02:21 +03: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
|
|
|
|
}
|