2019-08-23 22:55:45 +02:00
|
|
|
module os
|
2019-07-01 17:04:09 +02:00
|
|
|
|
2020-02-03 04:01:39 +01:00
|
|
|
import strings
|
|
|
|
|
2019-07-01 17:04:09 +02:00
|
|
|
#include <dirent.h>
|
2019-07-23 23:23:13 +02:00
|
|
|
#include <unistd.h>
|
2020-01-21 16:58:47 +01:00
|
|
|
#include <fcntl.h>
|
2020-06-14 15:46:30 +02:00
|
|
|
#include <sys/utsname.h>
|
2021-02-01 14:50:41 +01:00
|
|
|
|
2019-10-24 11:36:57 +02:00
|
|
|
pub const (
|
2019-10-12 21:18:19 +02:00
|
|
|
path_separator = '/'
|
2020-05-26 03:17:52 +02:00
|
|
|
path_delimiter = ':'
|
2019-07-18 20:21:48 +02:00
|
|
|
)
|
|
|
|
|
2019-12-31 08:53:53 +01:00
|
|
|
const (
|
2020-07-21 15:28:30 +02:00
|
|
|
stdin_value = 0
|
2019-12-31 08:53:53 +01:00
|
|
|
stdout_value = 1
|
2020-01-05 20:13:35 +01:00
|
|
|
stderr_value = 2
|
2019-12-31 08:53:53 +01:00
|
|
|
)
|
|
|
|
|
2021-01-04 18:57:17 +01:00
|
|
|
// (Must be realized in Syscall) (Must be specified)
|
|
|
|
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
|
|
|
|
pub const (
|
|
|
|
s_ifmt = 0xF000 // type of file
|
|
|
|
s_ifdir = 0x4000 // directory
|
|
|
|
s_iflnk = 0xa000 // link
|
|
|
|
s_isuid = 0o4000 // SUID
|
|
|
|
s_isgid = 0o2000 // SGID
|
|
|
|
s_isvtx = 0o1000 // Sticky
|
|
|
|
s_irusr = 0o0400 // Read by owner
|
|
|
|
s_iwusr = 0o0200 // Write by owner
|
|
|
|
s_ixusr = 0o0100 // Execute by owner
|
|
|
|
s_irgrp = 0o0040 // Read by group
|
|
|
|
s_iwgrp = 0o0020 // Write by group
|
|
|
|
s_ixgrp = 0o0010 // Execute by group
|
|
|
|
s_iroth = 0o0004 // Read by others
|
|
|
|
s_iwoth = 0o0002 // Write by others
|
|
|
|
s_ixoth = 0o0001 // Execute by others
|
|
|
|
)
|
|
|
|
|
2020-06-14 15:46:30 +02:00
|
|
|
struct C.utsname {
|
|
|
|
mut:
|
2021-04-05 07:08:51 +02:00
|
|
|
sysname &char
|
|
|
|
nodename &char
|
|
|
|
release &char
|
|
|
|
version &char
|
|
|
|
machine &char
|
2020-06-14 15:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.uname(name voidptr) int
|
2020-07-21 15:28:30 +02:00
|
|
|
|
2021-04-05 07:08:51 +02:00
|
|
|
fn C.symlink(&char, &char) int
|
2019-12-27 19:10:06 +01:00
|
|
|
|
2021-04-15 01:49:05 +02:00
|
|
|
fn C.gethostname(&char, int) int
|
|
|
|
|
2021-04-24 12:32:26 +02:00
|
|
|
// NB: not available on Android fn C.getlogin_r(&char, int) int
|
|
|
|
fn C.getlogin() &char
|
2021-04-19 13:57:25 +02:00
|
|
|
|
2020-06-14 15:46:30 +02:00
|
|
|
pub fn uname() Uname {
|
|
|
|
mut u := Uname{}
|
2020-07-21 15:28:30 +02:00
|
|
|
utsize := sizeof(C.utsname)
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
|
|
|
x := malloc(int(utsize))
|
|
|
|
d := &C.utsname(x)
|
|
|
|
if C.uname(d) == 0 {
|
2021-04-05 07:08:51 +02:00
|
|
|
u.sysname = cstring_to_vstring(d.sysname)
|
|
|
|
u.nodename = cstring_to_vstring(d.nodename)
|
|
|
|
u.release = cstring_to_vstring(d.release)
|
|
|
|
u.version = cstring_to_vstring(d.version)
|
|
|
|
u.machine = cstring_to_vstring(d.machine)
|
2021-02-14 19:31:42 +01:00
|
|
|
}
|
|
|
|
free(d)
|
2020-06-14 15:46:30 +02:00
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:49:05 +02:00
|
|
|
pub fn hostname() string {
|
|
|
|
mut hstnme := ''
|
|
|
|
size := 256
|
|
|
|
mut buf := unsafe { &char(malloc(size)) }
|
|
|
|
if C.gethostname(buf, size) == 0 {
|
|
|
|
hstnme = unsafe { cstring_to_vstring(buf) }
|
|
|
|
unsafe { free(buf) }
|
|
|
|
return hstnme
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:57:25 +02:00
|
|
|
pub fn loginname() string {
|
2021-04-24 12:32:26 +02:00
|
|
|
x := C.getlogin()
|
|
|
|
if !isnil(x) {
|
|
|
|
return unsafe { cstring_to_vstring(x) }
|
2021-04-19 13:57:25 +02:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:26:42 +02:00
|
|
|
fn init_os_args(argc int, argv &&byte) []string {
|
2021-01-10 21:41:29 +01:00
|
|
|
mut args_ := []string{}
|
2020-07-21 15:28:30 +02:00
|
|
|
// mut args := []string(make(0, argc, sizeof(string)))
|
|
|
|
// mut args := []string{len:argc}
|
2019-10-13 00:50:15 +02:00
|
|
|
for i in 0 .. argc {
|
2020-08-10 18:05:26 +02:00
|
|
|
// args [i] = argv[i].vstring()
|
2021-04-04 16:43:32 +02:00
|
|
|
unsafe { args_ << (&byte(argv[i])).vstring_literal() }
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2021-01-10 21:41:29 +01:00
|
|
|
return args_
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 13:30:05 +02:00
|
|
|
pub fn ls(path string) ?[]string {
|
2020-04-26 09:17:13 +02:00
|
|
|
mut res := []string{}
|
2021-04-04 16:43:32 +02:00
|
|
|
dir := unsafe { C.opendir(&char(path.str)) }
|
2019-08-16 14:05:11 +02:00
|
|
|
if isnil(dir) {
|
2019-10-17 13:30:05 +02:00
|
|
|
return error('ls() couldnt open dir "$path"')
|
2019-08-16 14:05:11 +02:00
|
|
|
}
|
2019-12-04 10:19:32 +01:00
|
|
|
mut ent := &C.dirent(0)
|
2019-12-19 22:29:37 +01:00
|
|
|
// mut ent := &C.dirent{!}
|
2019-08-16 14:05:11 +02:00
|
|
|
for {
|
|
|
|
ent = C.readdir(dir)
|
|
|
|
if isnil(ent) {
|
|
|
|
break
|
|
|
|
}
|
2021-01-14 03:55:30 +01:00
|
|
|
unsafe {
|
2021-04-05 07:08:51 +02:00
|
|
|
bptr := &byte(&ent.d_name[0])
|
2021-01-23 09:33:22 +01:00
|
|
|
if bptr[0] == 0 || (bptr[0] == `.` && bptr[1] == 0)
|
|
|
|
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
|
2021-01-14 03:55:30 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-02-15 16:15:52 +01:00
|
|
|
res << tos_clone(bptr)
|
2019-08-16 14:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|
2019-08-17 15:17:43 +02:00
|
|
|
|
2019-12-04 21:03:12 +01:00
|
|
|
/*
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
//$if linux {
|
|
|
|
//C.syscall(4, path.str) // sys_newstat
|
|
|
|
//}
|
2019-08-17 15:17:43 +02:00
|
|
|
dir := C.opendir(path.str)
|
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-12-04 21:03:12 +01:00
|
|
|
*/
|
2019-12-31 11:28:10 +01:00
|
|
|
/*
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut f File) fseek(pos, mode int) {
|
2019-12-31 11:28:10 +01:00
|
|
|
}
|
|
|
|
*/
|
2019-08-17 15:17:43 +02:00
|
|
|
// mkdir creates a new directory with the specified path.
|
2019-11-23 17:55:18 +01:00
|
|
|
pub fn mkdir(path string) ?bool {
|
2019-12-19 22:29:37 +01:00
|
|
|
if path == '.' {
|
|
|
|
return true
|
|
|
|
}
|
2020-07-01 14:34:14 +02:00
|
|
|
/*
|
|
|
|
mut k := 0
|
|
|
|
defer {
|
|
|
|
k = 1
|
|
|
|
}
|
|
|
|
*/
|
2020-07-21 15:28:30 +02:00
|
|
|
apath := real_path(path)
|
2020-12-16 10:02:36 +01:00
|
|
|
// defer {
|
|
|
|
// apath.free()
|
2020-11-09 14:24:46 +01:00
|
|
|
//}
|
2020-07-21 15:28:30 +02:00
|
|
|
/*
|
2020-01-05 22:49:09 +01:00
|
|
|
$if linux {
|
2020-01-05 20:13:35 +01:00
|
|
|
$if !android {
|
|
|
|
ret := C.syscall(sys_mkdir, apath.str, 511)
|
|
|
|
if ret == -1 {
|
2020-01-21 16:58:47 +01:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2020-01-05 20:13:35 +01:00
|
|
|
}
|
|
|
|
return true
|
2019-12-31 08:53:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 15:28:30 +02:00
|
|
|
*/
|
2021-04-04 16:43:32 +02:00
|
|
|
r := unsafe { C.mkdir(&char(apath.str), 511) }
|
2019-11-23 17:55:18 +01:00
|
|
|
if r == -1 {
|
2020-01-21 16:58:47 +01:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2019-11-23 17:55:18 +01:00
|
|
|
}
|
|
|
|
return true
|
2019-08-17 15:17:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 19:52:13 +01:00
|
|
|
// execute starts the specified command, waits for it to complete, and returns its output.
|
2021-03-23 19:36:19 +01:00
|
|
|
[manualfree]
|
2021-03-08 19:52:13 +01:00
|
|
|
pub fn execute(cmd string) Result {
|
2019-12-19 22:29:37 +01:00
|
|
|
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
2021-03-08 19:52:13 +01:00
|
|
|
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
|
2019-12-19 22:29:37 +01:00
|
|
|
// }
|
2019-11-07 14:01:17 +01:00
|
|
|
pcmd := '$cmd 2>&1'
|
|
|
|
f := vpopen(pcmd)
|
|
|
|
if isnil(f) {
|
2021-03-08 19:52:13 +01:00
|
|
|
return Result{
|
|
|
|
exit_code: -1
|
|
|
|
output: 'exec("$cmd") failed'
|
|
|
|
}
|
2019-11-07 14:01:17 +01:00
|
|
|
}
|
2021-04-05 22:42:26 +02:00
|
|
|
buf := unsafe { malloc(4096) }
|
2020-02-03 04:01:39 +01:00
|
|
|
mut res := strings.new_builder(1024)
|
2021-03-23 19:36:19 +01:00
|
|
|
defer {
|
|
|
|
unsafe { res.free() }
|
|
|
|
}
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
2021-04-05 22:35:00 +02:00
|
|
|
bufbp := buf
|
2021-04-05 22:42:26 +02:00
|
|
|
for C.fgets(&char(bufbp), 4096, f) != 0 {
|
|
|
|
buflen := vstrlen(bufbp)
|
|
|
|
res.write_ptr(bufbp, buflen)
|
2021-02-14 19:31:42 +01:00
|
|
|
}
|
2019-11-07 14:01:17 +01:00
|
|
|
}
|
2020-06-14 01:41:47 +02:00
|
|
|
soutput := res.str()
|
2019-11-07 14:01:17 +01:00
|
|
|
exit_code := vpclose(f)
|
2021-04-05 22:35:00 +02:00
|
|
|
unsafe { free(buf) }
|
2019-12-19 22:29:37 +01:00
|
|
|
return Result{
|
2020-05-18 17:05:48 +02:00
|
|
|
exit_code: exit_code
|
2020-05-18 21:38:06 +02:00
|
|
|
output: soutput
|
2019-11-07 14:01:17 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2020-12-16 10:02:36 +01:00
|
|
|
pub struct Command {
|
2020-11-05 08:44:34 +01:00
|
|
|
mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
f voidptr
|
2020-11-05 08:44:34 +01:00
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
eof bool
|
2020-11-05 08:44:34 +01:00
|
|
|
pub:
|
2020-12-16 10:02:36 +01:00
|
|
|
path string
|
2020-11-05 08:44:34 +01:00
|
|
|
redirect_stdout bool
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:36:19 +01:00
|
|
|
[manualfree]
|
2020-12-16 10:02:36 +01:00
|
|
|
pub fn (mut c Command) start() ? {
|
2021-03-23 19:36:19 +01:00
|
|
|
pcmd := c.path + ' 2>&1'
|
|
|
|
defer {
|
|
|
|
unsafe { pcmd.free() }
|
|
|
|
}
|
2020-11-05 08:44:34 +01:00
|
|
|
c.f = vpopen(pcmd)
|
|
|
|
if isnil(c.f) {
|
|
|
|
return error('exec("$c.path") failed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:36:19 +01:00
|
|
|
[manualfree]
|
2020-11-05 08:44:34 +01:00
|
|
|
pub fn (mut c Command) read_line() string {
|
|
|
|
buf := [4096]byte{}
|
|
|
|
mut res := strings.new_builder(1024)
|
2021-03-23 19:36:19 +01:00
|
|
|
defer {
|
|
|
|
unsafe { res.free() }
|
|
|
|
}
|
2020-11-05 08:44:34 +01:00
|
|
|
unsafe {
|
2021-02-17 20:45:11 +01:00
|
|
|
bufbp := &buf[0]
|
2021-04-04 16:43:32 +02:00
|
|
|
for C.fgets(&char(bufbp), 4096, c.f) != 0 {
|
2020-11-05 08:44:34 +01:00
|
|
|
len := vstrlen(bufbp)
|
2020-12-16 10:02:36 +01:00
|
|
|
for i in 0 .. len {
|
2021-02-17 20:45:11 +01:00
|
|
|
if bufbp[i] == `\n` {
|
2021-03-20 08:02:28 +01:00
|
|
|
res.write_ptr(bufbp, i)
|
2021-03-23 19:36:19 +01:00
|
|
|
final := res.str()
|
|
|
|
return final
|
2020-11-05 08:44:34 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 08:02:28 +01:00
|
|
|
res.write_ptr(bufbp, len)
|
2020-11-05 08:44:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.eof = true
|
2021-03-23 19:36:19 +01:00
|
|
|
final := res.str()
|
|
|
|
return final
|
2020-11-05 08:44:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 10:02:36 +01:00
|
|
|
pub fn (c &Command) close() ? {
|
2020-11-05 08:44:34 +01:00
|
|
|
exit_code := vpclose(c.f)
|
|
|
|
if exit_code == 127 {
|
|
|
|
return error_with_code('error', 127)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn symlink(origin string, target string) ?bool {
|
2021-04-04 16:43:32 +02:00
|
|
|
res := C.symlink(&char(origin.str), &char(target.str))
|
2020-01-05 20:13:35 +01:00
|
|
|
if res == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-21 16:58:47 +01:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_error_msg return error code representation in string.
|
|
|
|
pub fn get_error_msg(code int) string {
|
|
|
|
return posix_get_error_msg(code)
|
2019-12-27 19:10:06 +01:00
|
|
|
}
|
2020-01-01 12:50:25 +01:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut f File) close() {
|
2020-08-01 23:07:37 +02:00
|
|
|
if !f.is_opened {
|
2020-01-01 12:50:25 +01:00
|
|
|
return
|
|
|
|
}
|
2020-08-01 23:07:37 +02:00
|
|
|
f.is_opened = false
|
2020-07-21 15:28:30 +02:00
|
|
|
/*
|
2020-01-05 22:49:09 +01:00
|
|
|
$if linux {
|
2020-01-05 20:13:35 +01:00
|
|
|
$if !android {
|
|
|
|
C.syscall(sys_close, f.fd)
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 12:50:25 +01:00
|
|
|
}
|
2020-07-21 15:28:30 +02:00
|
|
|
*/
|
2020-01-01 12:50:25 +01:00
|
|
|
C.fflush(f.cfile)
|
|
|
|
C.fclose(f.cfile)
|
|
|
|
}
|
2020-05-31 12:57:26 +02:00
|
|
|
|
|
|
|
pub fn debugger_present() bool {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-03 17:19:36 +02:00
|
|
|
|
2021-04-05 07:08:51 +02:00
|
|
|
fn C.mkstemp(stemplate &byte) int
|
2020-12-16 10:02:36 +01:00
|
|
|
|
2020-08-03 17:19:36 +02:00
|
|
|
// `is_writable_folder` - `folder` exists and is writable to the process
|
|
|
|
pub fn is_writable_folder(folder string) ?bool {
|
2020-12-16 10:02:36 +01:00
|
|
|
if !exists(folder) {
|
2020-08-03 17:19:36 +02:00
|
|
|
return error('`$folder` does not exist')
|
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
if !is_dir(folder) {
|
2020-08-03 17:19:36 +02:00
|
|
|
return error('`folder` is not a folder')
|
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
tmp_perm_check := join_path(folder, 'XXXXXX')
|
2020-08-03 17:19:36 +02:00
|
|
|
unsafe {
|
2021-04-04 16:43:32 +02:00
|
|
|
x := C.mkstemp(&char(tmp_perm_check.str))
|
2020-08-03 17:19:36 +02:00
|
|
|
if -1 == x {
|
|
|
|
return error('folder `$folder` is not writable')
|
|
|
|
}
|
|
|
|
C.close(x)
|
|
|
|
}
|
2021-01-26 15:43:10 +01:00
|
|
|
rm(tmp_perm_check) ?
|
2020-08-03 17:19:36 +02:00
|
|
|
return true
|
|
|
|
}
|
2020-08-05 08:04:40 +02:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getpid() int {
|
|
|
|
return C.getpid()
|
|
|
|
}
|
2021-01-04 18:57:17 +01:00
|
|
|
|
2021-05-05 13:20:11 +02:00
|
|
|
[inline]
|
|
|
|
pub fn getuid() int {
|
|
|
|
return C.getuid()
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn geteuid() int {
|
|
|
|
return C.geteuid()
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:57:17 +01:00
|
|
|
// Turns the given bit on or off, depending on the `enable` parameter
|
|
|
|
pub fn posix_set_permission_bit(path_s string, mode u32, enable bool) {
|
|
|
|
mut s := C.stat{}
|
|
|
|
mut new_mode := u32(0)
|
2021-04-04 16:43:32 +02:00
|
|
|
path := &char(path_s.str)
|
2021-01-04 18:57:17 +01:00
|
|
|
unsafe {
|
|
|
|
C.stat(path, &s)
|
|
|
|
new_mode = s.st_mode
|
|
|
|
}
|
|
|
|
match enable {
|
|
|
|
true { new_mode |= mode }
|
|
|
|
false { new_mode &= (0o7777 - mode) }
|
|
|
|
}
|
|
|
|
C.chmod(path, int(new_mode))
|
|
|
|
}
|