2019-08-23 23:55:45 +03:00
|
|
|
module os
|
2019-07-01 18:04:09 +03:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2019-07-24 00:23:13 +03:00
|
|
|
#include <unistd.h>
|
2019-10-24 12:36:57 +03:00
|
|
|
pub const (
|
2019-10-12 22:18:19 +03:00
|
|
|
path_separator = '/'
|
2019-07-18 21:21:48 +03:00
|
|
|
)
|
|
|
|
|
2019-12-31 10:53:53 +03:00
|
|
|
pub const (
|
|
|
|
sys_write = 1
|
|
|
|
sys_open = 2
|
|
|
|
sys_close = 3
|
|
|
|
sys_mkdir = 83
|
|
|
|
sys_creat = 85
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
stdin_value = 0
|
|
|
|
stdout_value = 1
|
|
|
|
stderr_value = 2
|
|
|
|
)
|
|
|
|
|
2019-12-27 21:10:06 +03:00
|
|
|
fn C.symlink(charptr, charptr) int
|
|
|
|
|
2019-10-31 13:08:01 +03:00
|
|
|
pub fn init_os_args(argc int, argv &byteptr) []string {
|
2019-09-14 23:48:30 +03:00
|
|
|
mut args := []string
|
2019-10-13 01:50:15 +03:00
|
|
|
for i in 0 .. argc {
|
2019-09-14 23:48:30 +03:00
|
|
|
args << string(argv[i])
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:21:48 +03:00
|
|
|
// get_error_msg return error code representation in string.
|
|
|
|
pub fn get_error_msg(code int) string {
|
2019-11-24 06:27:02 +03:00
|
|
|
ptr_text := C.strerror(code) // voidptr?
|
|
|
|
if ptr_text == 0 {
|
2019-07-18 21:21:48 +03:00
|
|
|
return ''
|
|
|
|
}
|
2019-11-24 06:27:02 +03:00
|
|
|
return tos3(ptr_text)
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
2019-08-16 15:05:11 +03:00
|
|
|
|
2019-10-17 14:30:05 +03:00
|
|
|
pub fn ls(path string) ?[]string {
|
2019-08-16 15:05:11 +03:00
|
|
|
mut res := []string
|
2019-08-23 23:55:45 +03:00
|
|
|
dir := C.opendir(path.str)
|
2019-08-16 15:05:11 +03:00
|
|
|
if isnil(dir) {
|
2019-10-17 14:30:05 +03:00
|
|
|
return error('ls() couldnt open dir "$path"')
|
2019-08-16 15:05:11 +03:00
|
|
|
}
|
2019-12-04 12:19:32 +03:00
|
|
|
mut ent := &C.dirent(0)
|
2019-12-20 00:29:37 +03:00
|
|
|
// mut ent := &C.dirent{!}
|
2019-08-16 15:05:11 +03:00
|
|
|
for {
|
|
|
|
ent = C.readdir(dir)
|
|
|
|
if isnil(ent) {
|
|
|
|
break
|
|
|
|
}
|
2019-09-15 19:07:40 +03:00
|
|
|
name := tos_clone(byteptr(ent.d_name))
|
2019-08-16 15:05:11 +03:00
|
|
|
if name != '.' && name != '..' && name != '' {
|
|
|
|
res << name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|
2019-08-17 16:17:43 +03:00
|
|
|
|
2019-12-04 23:03:12 +03:00
|
|
|
/*
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
//$if linux {
|
|
|
|
//C.syscall(4, path.str) // sys_newstat
|
|
|
|
//}
|
2019-08-17 16:17:43 +03:00
|
|
|
dir := C.opendir(path.str)
|
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-12-04 23:03:12 +03:00
|
|
|
*/
|
2019-08-17 16:17:43 +03:00
|
|
|
|
2019-12-31 10:53:53 +03:00
|
|
|
pub fn open(path string) ?File {
|
|
|
|
$if linux {
|
|
|
|
fd := C.syscall(sys_open, path.str, 511)
|
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
return File{
|
|
|
|
fd: fd
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
cpath := path.str
|
|
|
|
file := File{
|
|
|
|
cfile: C.fopen(charptr(cpath), 'rb')
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create creates a file at a specified location and returns a writable `File` object.
|
|
|
|
pub fn create(path string) ?File {
|
|
|
|
$if linux {
|
|
|
|
fd := C.syscall(sys_creat, path.str, 511)
|
|
|
|
//////println('Fd=$fd')
|
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
|
|
|
return File{
|
|
|
|
fd: fd
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
mut file := File{
|
|
|
|
cfile: C.fopen(charptr(path.str), 'wb')
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:28:10 +03:00
|
|
|
/*
|
|
|
|
pub fn (f mut File) fseek(pos, mode int) {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-12-31 10:53:53 +03:00
|
|
|
pub fn (f mut File) write(s string) {
|
|
|
|
if !f.opened {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
C.syscall(sys_write, f.fd, s.str, s.len)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
C.fputs(s.str, f.cfile)
|
|
|
|
// C.fwrite(s.str, 1, s.len, f.cfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f mut File) writeln(s string) {
|
|
|
|
if !f.opened {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
snl := s + '\n'
|
|
|
|
C.syscall(sys_write, f.fd, snl.str, snl.len)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// C.fwrite(s.str, 1, s.len, f.cfile)
|
|
|
|
// ss := s.clone()
|
|
|
|
// TODO perf
|
|
|
|
C.fputs(s.str, f.cfile)
|
|
|
|
// ss.free()
|
|
|
|
C.fputs('\n', f.cfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-17 16:17:43 +03:00
|
|
|
// mkdir creates a new directory with the specified path.
|
2019-11-23 19:55:18 +03:00
|
|
|
pub fn mkdir(path string) ?bool {
|
2019-12-20 00:29:37 +03:00
|
|
|
if path == '.' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
apath := os.realpath(path)
|
2019-12-31 10:53:53 +03:00
|
|
|
$if linux {
|
|
|
|
ret := C.syscall(sys_mkdir, apath.str, 511)
|
|
|
|
if ret == -1 {
|
|
|
|
return error(get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-12-07 15:51:00 +03:00
|
|
|
r := C.mkdir(apath.str, 511)
|
2019-11-23 19:55:18 +03:00
|
|
|
if r == -1 {
|
|
|
|
return error(get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
return true
|
2019-08-17 16:17:43 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:01:17 +03:00
|
|
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
|
|
|
pub fn exec(cmd string) ?Result {
|
2019-12-20 00:29:37 +03:00
|
|
|
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
|
|
|
// return error(';, &&, || and \\n are not allowed in shell commands')
|
|
|
|
// }
|
2019-11-07 16:01:17 +03:00
|
|
|
pcmd := '$cmd 2>&1'
|
|
|
|
f := vpopen(pcmd)
|
|
|
|
if isnil(f) {
|
|
|
|
return error('exec("$cmd") failed')
|
|
|
|
}
|
|
|
|
buf := [1000]byte
|
|
|
|
mut res := ''
|
2019-12-01 10:33:26 +03:00
|
|
|
for C.fgets(charptr(buf), 1000, f) != 0 {
|
2019-11-07 16:01:17 +03:00
|
|
|
res += tos(buf, vstrlen(buf))
|
|
|
|
}
|
|
|
|
res = res.trim_space()
|
|
|
|
exit_code := vpclose(f)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if exit_code != 0 {
|
|
|
|
// return error(res)
|
|
|
|
// }
|
|
|
|
return Result{
|
2019-11-07 16:01:17 +03:00
|
|
|
output: res
|
|
|
|
exit_code: exit_code
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2019-12-27 21:10:06 +03:00
|
|
|
pub fn symlink(origin, target string) ?bool {
|
|
|
|
res := C.symlink(origin.str, target.str)
|
|
|
|
if res == 0 { return true }
|
|
|
|
return error(get_error_msg(C.errno))
|
|
|
|
}
|