2019-08-23 20:55:45 +00:00
|
|
|
module os
|
2019-07-01 15:04:09 +00:00
|
|
|
|
2020-02-03 03:01:39 +00:00
|
|
|
import strings
|
|
|
|
|
2019-07-01 15:04:09 +00:00
|
|
|
#include <dirent.h>
|
2019-07-23 21:23:13 +00:00
|
|
|
#include <unistd.h>
|
2020-01-21 15:58:47 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2019-10-24 09:36:57 +00:00
|
|
|
pub const (
|
2019-10-12 19:18:19 +00:00
|
|
|
path_separator = '/'
|
2019-07-18 18:21:48 +00:00
|
|
|
)
|
|
|
|
|
2019-12-31 07:53:53 +00:00
|
|
|
const (
|
|
|
|
stdin_value = 0
|
|
|
|
stdout_value = 1
|
2020-01-05 19:13:35 +00:00
|
|
|
stderr_value = 2
|
2019-12-31 07:53:53 +00:00
|
|
|
)
|
|
|
|
|
2019-12-27 18:10:06 +00:00
|
|
|
fn C.symlink(charptr, charptr) int
|
|
|
|
|
2020-02-21 13:47:42 +00:00
|
|
|
fn init_os_args(argc int, argv &byteptr) []string {
|
2019-09-14 20:48:30 +00:00
|
|
|
mut args := []string
|
2020-03-21 18:52:19 +00:00
|
|
|
//mut args := []string(make(0, argc, sizeof(string)))
|
|
|
|
//mut args := []string{len:argc}
|
2019-10-12 22:50:15 +00:00
|
|
|
for i in 0 .. argc {
|
2020-03-21 18:52:19 +00:00
|
|
|
|
|
|
|
//args [i] = string(argv[i])
|
2019-09-14 20:48:30 +00:00
|
|
|
args << string(argv[i])
|
2019-11-29 16:14:26 +00:00
|
|
|
}
|
2019-09-14 20:48:30 +00:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-10-17 11:30:05 +00:00
|
|
|
pub fn ls(path string) ?[]string {
|
2019-08-16 12:05:11 +00:00
|
|
|
mut res := []string
|
2019-08-23 20:55:45 +00:00
|
|
|
dir := C.opendir(path.str)
|
2019-08-16 12:05:11 +00:00
|
|
|
if isnil(dir) {
|
2019-10-17 11:30:05 +00:00
|
|
|
return error('ls() couldnt open dir "$path"')
|
2019-08-16 12:05:11 +00:00
|
|
|
}
|
2019-12-04 09:19:32 +00:00
|
|
|
mut ent := &C.dirent(0)
|
2019-12-19 21:29:37 +00:00
|
|
|
// mut ent := &C.dirent{!}
|
2019-08-16 12:05:11 +00:00
|
|
|
for {
|
|
|
|
ent = C.readdir(dir)
|
|
|
|
if isnil(ent) {
|
|
|
|
break
|
|
|
|
}
|
2019-09-15 16:07:40 +00:00
|
|
|
name := tos_clone(byteptr(ent.d_name))
|
2019-08-16 12:05:11 +00:00
|
|
|
if name != '.' && name != '..' && name != '' {
|
|
|
|
res << name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|
2019-08-17 13:17:43 +00:00
|
|
|
|
2019-12-04 20:03:12 +00:00
|
|
|
/*
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
//$if linux {
|
|
|
|
//C.syscall(4, path.str) // sys_newstat
|
|
|
|
//}
|
2019-08-17 13:17:43 +00:00
|
|
|
dir := C.opendir(path.str)
|
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-12-04 20:03:12 +00:00
|
|
|
*/
|
2019-08-17 13:17:43 +00:00
|
|
|
|
2020-01-21 15:58:47 +00:00
|
|
|
// open opens a file at the specified and returns back a read-only `File` object
|
2019-12-31 07:53:53 +00:00
|
|
|
pub fn open(path string) ?File {
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
fd := C.syscall(sys_open, path.str, 511)
|
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
return File{
|
|
|
|
fd: fd
|
|
|
|
opened: true
|
|
|
|
}
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2020-01-21 15:58:47 +00:00
|
|
|
file := File{
|
|
|
|
cfile: C.fopen(charptr(path.str), 'rb')
|
2020-01-05 19:13:35 +00:00
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to open file "$path"')
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
2020-01-05 19:13:35 +00:00
|
|
|
return file
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 15:58:47 +00:00
|
|
|
// create creates or opens a file at a specified location and returns a write-only `File` object
|
2019-12-31 07:53:53 +00:00
|
|
|
pub fn create(path string) ?File {
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 19:13:35 +00:00
|
|
|
// NB: android/termux/bionic is also a kind of linux,
|
|
|
|
// but linux syscalls there sometimes fail,
|
|
|
|
// while the libc version should work.
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
2020-01-05 21:49:09 +00:00
|
|
|
//$if macos {
|
|
|
|
// fd = C.syscall(398, path.str, 0x601, 0x1b6)
|
|
|
|
//}
|
|
|
|
//$if linux {
|
|
|
|
fd = C.syscall(sys_creat, path.str, 511)
|
|
|
|
//}
|
2020-01-05 19:13:35 +00:00
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
|
|
|
file = File{
|
|
|
|
fd: fd
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
return file
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2020-01-21 15:58:47 +00:00
|
|
|
file := File{
|
2019-12-31 07:53:53 +00:00
|
|
|
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 10:28:10 +00:00
|
|
|
/*
|
|
|
|
pub fn (f mut File) fseek(pos, mode int) {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-01-05 19:13:35 +00:00
|
|
|
|
2019-12-31 07:53:53 +00:00
|
|
|
pub fn (f mut File) write(s string) {
|
|
|
|
if !f.opened {
|
|
|
|
return
|
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
C.syscall(sys_write, f.fd, s.str, s.len)
|
|
|
|
return
|
|
|
|
}
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2019-12-31 07:53:53 +00:00
|
|
|
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
|
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
snl := s + '\n'
|
|
|
|
C.syscall(sys_write, f.fd, snl.str, snl.len)
|
|
|
|
return
|
|
|
|
}
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2019-12-31 07:53:53 +00:00
|
|
|
// 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 13:17:43 +00:00
|
|
|
// mkdir creates a new directory with the specified path.
|
2019-11-23 16:55:18 +00:00
|
|
|
pub fn mkdir(path string) ?bool {
|
2019-12-19 21:29:37 +00:00
|
|
|
if path == '.' {
|
|
|
|
return true
|
|
|
|
}
|
2020-03-20 15:41:18 +00:00
|
|
|
apath := os.real_path(path)
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
ret := C.syscall(sys_mkdir, apath.str, 511)
|
|
|
|
if ret == -1 {
|
2020-01-21 15:58:47 +00:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2020-01-05 19:13:35 +00:00
|
|
|
}
|
|
|
|
return true
|
2019-12-31 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2019-12-07 12:51:00 +00:00
|
|
|
r := C.mkdir(apath.str, 511)
|
2019-11-23 16:55:18 +00:00
|
|
|
if r == -1 {
|
2020-01-21 15:58:47 +00:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2019-11-23 16:55:18 +00:00
|
|
|
}
|
|
|
|
return true
|
2019-08-17 13:17:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:01:17 +00:00
|
|
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
|
|
|
pub fn exec(cmd string) ?Result {
|
2019-12-19 21:29:37 +00:00
|
|
|
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
|
|
|
// return error(';, &&, || and \\n are not allowed in shell commands')
|
|
|
|
// }
|
2019-11-07 13:01:17 +00:00
|
|
|
pcmd := '$cmd 2>&1'
|
|
|
|
f := vpopen(pcmd)
|
|
|
|
if isnil(f) {
|
|
|
|
return error('exec("$cmd") failed')
|
|
|
|
}
|
2020-02-03 03:01:39 +00:00
|
|
|
buf := [4096]byte
|
|
|
|
mut res := strings.new_builder(1024)
|
|
|
|
for C.fgets(charptr(buf), 4096, f) != 0 {
|
|
|
|
res.write_bytes( buf, vstrlen(buf) )
|
2019-11-07 13:01:17 +00:00
|
|
|
}
|
2020-02-03 03:01:39 +00:00
|
|
|
soutput := res.str().trim_space()
|
2020-03-22 15:55:42 +00:00
|
|
|
//res.free()
|
2019-11-07 13:01:17 +00:00
|
|
|
exit_code := vpclose(f)
|
2019-12-19 21:29:37 +00:00
|
|
|
// if exit_code != 0 {
|
|
|
|
// return error(res)
|
|
|
|
// }
|
|
|
|
return Result{
|
2020-02-03 03:01:39 +00:00
|
|
|
output: soutput
|
2019-11-07 13:01:17 +00:00
|
|
|
exit_code: exit_code
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 21:29:37 +00:00
|
|
|
|
2019-12-27 18:10:06 +00:00
|
|
|
pub fn symlink(origin, target string) ?bool {
|
|
|
|
res := C.symlink(origin.str, target.str)
|
2020-01-05 19:13:35 +00:00
|
|
|
if res == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-21 15:58:47 +00: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 18:10:06 +00:00
|
|
|
}
|
2020-01-01 11:50:25 +00:00
|
|
|
|
|
|
|
// convert any value to []byte (LittleEndian) and write it
|
|
|
|
// for example if we have write(7, 4), "07 00 00 00" gets written
|
|
|
|
// write(0x1234, 2) => "34 12"
|
|
|
|
pub fn (f mut File) write_bytes(data voidptr, size int) {
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
C.syscall(sys_write, f.fd, data, 1)
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 11:50:25 +00:00
|
|
|
}
|
2020-02-17 19:31:23 +00:00
|
|
|
*/
|
2020-01-05 19:13:35 +00:00
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
2020-01-01 11:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f mut File) close() {
|
|
|
|
if !f.opened {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.opened = false
|
2020-01-12 18:35:06 +00:00
|
|
|
/*
|
2020-01-05 21:49:09 +00:00
|
|
|
$if linux {
|
2020-01-05 19:13:35 +00:00
|
|
|
$if !android {
|
|
|
|
C.syscall(sys_close, f.fd)
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 11:50:25 +00:00
|
|
|
}
|
2020-01-12 18:35:06 +00:00
|
|
|
*/
|
2020-01-01 11:50:25 +00:00
|
|
|
C.fflush(f.cfile)
|
|
|
|
C.fclose(f.cfile)
|
|
|
|
}
|