1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/os/os_nix.c.v

443 lines
8.4 KiB
V
Raw Normal View History

module os
2019-07-01 18:04:09 +03:00
2020-02-03 06:01:39 +03:00
import strings
2019-07-01 18:04:09 +03:00
#include <dirent.h>
#include <unistd.h>
2020-01-21 18:58:47 +03:00
#include <fcntl.h>
2020-06-14 16:46:30 +03:00
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/ptrace.h>
2021-06-30 08:30:18 +03:00
#include <glob.h>
2019-10-24 12:36:57 +03:00
pub const (
2019-10-12 22:18:19 +03:00
path_separator = '/'
2020-05-26 04:17:52 +03:00
path_delimiter = ':'
)
2019-12-31 10:53:53 +03:00
const (
2020-07-21 16:28:30 +03:00
stdin_value = 0
2019-12-31 10:53:53 +03:00
stdout_value = 1
2020-01-05 22:13:35 +03:00
stderr_value = 2
2019-12-31 10:53:53 +03: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 16:46:30 +03:00
struct C.utsname {
mut:
sysname &char
nodename &char
release &char
version &char
machine &char
2020-06-14 16:46:30 +03:00
}
2021-06-30 08:30:18 +03:00
[typedef]
struct C.glob_t {
mut:
gl_pathc size_t // number of matched paths
gl_pathv &&char // list of matched pathnames
gl_offs size_t // slots to reserve in gl_pathv
}
2020-06-14 16:46:30 +03:00
fn C.uname(name voidptr) int
2020-07-21 16:28:30 +03:00
fn C.symlink(&char, &char) int
2019-12-27 21:10:06 +03:00
fn C.link(&char, &char) int
fn C.gethostname(&char, int) int
// NB: not available on Android fn C.getlogin_r(&char, int) int
fn C.getlogin() &char
fn C.getppid() int
fn C.getgid() int
fn C.getegid() int
fn C.ptrace(u32, u32, voidptr, int) u64
2021-06-30 08:30:18 +03:00
fn C.glob(&char, int, voidptr, voidptr) int
fn C.globfree(voidptr)
pub fn glob(patterns ...string) ?[]string {
mut matches := []string{}
if patterns.len == 0 {
return matches
}
mut globdata := C.glob_t{
gl_pathv: 0
}
mut flags := int(C.GLOB_DOOFFS | C.GLOB_MARK)
for i, pattern in patterns {
if i > 0 {
flags |= C.GLOB_APPEND
}
unsafe {
if C.glob(&char(pattern.str), flags, C.NULL, &globdata) != 0 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
}
}
for i := 0; i < int(globdata.gl_pathc); i++ {
unsafe {
matches << cstring_to_vstring(globdata.gl_pathv[i])
}
}
C.globfree(&globdata)
return matches
}
2020-06-14 16:46:30 +03:00
pub fn uname() Uname {
mut u := Uname{}
2020-07-21 16:28:30 +03:00
utsize := sizeof(C.utsname)
unsafe {
x := malloc_noscan(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 {
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)
}
free(d)
2020-06-14 16:46:30 +03:00
}
return u
}
pub fn hostname() string {
mut hstnme := ''
size := 256
mut buf := unsafe { &char(malloc_noscan(size)) }
if C.gethostname(buf, size) == 0 {
hstnme = unsafe { cstring_to_vstring(buf) }
unsafe { free(buf) }
return hstnme
}
return ''
}
pub fn loginname() string {
x := C.getlogin()
if !isnil(x) {
return unsafe { cstring_to_vstring(x) }
}
return ''
}
2020-05-05 16:26:42 +03:00
fn init_os_args(argc int, argv &&byte) []string {
mut args_ := []string{}
2020-07-21 16:28:30 +03:00
// mut args := []string(make(0, argc, sizeof(string)))
// mut args := []string{len:argc}
2019-10-13 01:50:15 +03:00
for i in 0 .. argc {
// args [i] = argv[i].vstring()
unsafe { args_ << (&byte(argv[i])).vstring_literal() }
2019-11-29 19:14:26 +03:00
}
return args_
2019-09-14 23:48:30 +03:00
}
2019-10-17 14:30:05 +03:00
pub fn ls(path string) ?[]string {
mut res := []string{}
dir := unsafe { C.opendir(&char(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
}
unsafe {
bptr := &byte(&ent.d_name[0])
if bptr[0] == 0 || (bptr[0] == `.` && bptr[1] == 0)
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
continue
}
res << tos_clone(bptr)
2019-08-16 15:05:11 +03:00
}
}
C.closedir(dir)
return res
}
2019-08-17 16:17:43 +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
}
*/
/*
2020-05-17 14:51:18 +03:00
pub fn (mut f File) fseek(pos, mode int) {
}
*/
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
}
/*
mut k := 0
defer {
k = 1
}
*/
2020-07-21 16:28:30 +03:00
apath := real_path(path)
// defer {
// apath.free()
//}
2020-07-21 16:28:30 +03:00
/*
$if linux {
2020-01-05 22:13:35 +03:00
$if !android {
ret := C.syscall(sys_mkdir, apath.str, 511)
if ret == -1 {
2020-01-21 18:58:47 +03:00
return error(posix_get_error_msg(C.errno))
2020-01-05 22:13:35 +03:00
}
return true
2019-12-31 10:53:53 +03:00
}
}
2020-07-21 16:28:30 +03:00
*/
r := unsafe { C.mkdir(&char(apath.str), 511) }
2019-11-23 19:55:18 +03:00
if r == -1 {
2020-01-21 18:58:47 +03:00
return error(posix_get_error_msg(C.errno))
2019-11-23 19:55:18 +03:00
}
return true
2019-08-17 16:17:43 +03:00
}
// execute starts the specified command, waits for it to complete, and returns its output.
[manualfree]
pub fn execute(cmd string) Result {
2019-12-20 00:29:37 +03:00
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
2019-12-20 00:29:37 +03:00
// }
2019-11-07 16:01:17 +03:00
pcmd := '$cmd 2>&1'
f := vpopen(pcmd)
if isnil(f) {
return Result{
exit_code: -1
output: 'exec("$cmd") failed'
}
2019-11-07 16:01:17 +03:00
}
buf := unsafe { malloc_noscan(4096) }
2020-02-03 06:01:39 +03:00
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
2021-04-05 23:35:00 +03:00
bufbp := buf
2021-04-05 23:42:26 +03:00
for C.fgets(&char(bufbp), 4096, f) != 0 {
buflen := vstrlen(bufbp)
res.write_ptr(bufbp, buflen)
}
2019-11-07 16:01:17 +03:00
}
soutput := res.str()
2019-11-07 16:01:17 +03:00
exit_code := vpclose(f)
2021-04-05 23:35:00 +03:00
unsafe { free(buf) }
2019-12-20 00:29:37 +03:00
return Result{
exit_code: exit_code
2020-05-18 22:38:06 +03:00
output: soutput
2019-11-07 16:01:17 +03:00
}
}
2019-12-20 00:29:37 +03:00
pub struct Command {
mut:
f voidptr
pub mut:
eof bool
pub:
path string
redirect_stdout bool
}
[manualfree]
pub fn (mut c Command) start() ? {
pcmd := c.path + ' 2>&1'
defer {
unsafe { pcmd.free() }
}
c.f = vpopen(pcmd)
if isnil(c.f) {
return error('exec("$c.path") failed')
}
}
[manualfree]
pub fn (mut c Command) read_line() string {
buf := [4096]byte{}
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
bufbp := &buf[0]
for C.fgets(&char(bufbp), 4096, c.f) != 0 {
len := vstrlen(bufbp)
for i in 0 .. len {
if bufbp[i] == `\n` {
res.write_ptr(bufbp, i)
final := res.str()
return final
}
}
res.write_ptr(bufbp, len)
}
}
c.eof = true
final := res.str()
return final
}
pub fn (c &Command) close() ? {
exit_code := vpclose(c.f)
if exit_code == 127 {
return error_with_code('error', 127)
}
}
pub fn symlink(origin string, target string) ?bool {
res := C.symlink(&char(origin.str), &char(target.str))
2020-01-05 22:13:35 +03:00
if res == 0 {
return true
}
2020-01-21 18:58:47 +03:00
return error(posix_get_error_msg(C.errno))
}
pub fn link(origin string, target string) ?bool {
res := C.link(&char(origin.str), &char(target.str))
if res == 0 {
return true
}
return error(posix_get_error_msg(C.errno))
}
2020-01-21 18:58:47 +03:00
// 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 21:10:06 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut f File) close() {
2020-08-02 00:07:37 +03:00
if !f.is_opened {
return
}
2020-08-02 00:07:37 +03:00
f.is_opened = false
2020-07-21 16:28:30 +03:00
/*
$if linux {
2020-01-05 22:13:35 +03:00
$if !android {
C.syscall(sys_close, f.fd)
return
}
}
2020-07-21 16:28:30 +03:00
*/
C.fflush(f.cfile)
C.fclose(f.cfile)
}
2020-05-31 13:57:26 +03:00
[inline]
2020-05-31 13:57:26 +03:00
pub fn debugger_present() bool {
// check if the parent could trace its process,
// if not a debugger must be present
$if linux {
return C.ptrace(C.PTRACE_TRACEME, 0, 1, 0) == -1
} $else $if macos {
return C.ptrace(C.PT_TRACE_ME, 0, voidptr(1), 0) == -1
}
2020-05-31 13:57:26 +03:00
return false
}
fn C.mkstemp(stemplate &byte) int
// `is_writable_folder` - `folder` exists and is writable to the process
pub fn is_writable_folder(folder string) ?bool {
if !exists(folder) {
return error('`$folder` does not exist')
}
if !is_dir(folder) {
return error('`folder` is not a folder')
}
tmp_perm_check := join_path(folder, 'XXXXXX')
unsafe {
x := C.mkstemp(&char(tmp_perm_check.str))
if -1 == x {
return error('folder `$folder` is not writable')
}
C.close(x)
}
rm(tmp_perm_check) ?
return true
}
[inline]
pub fn getpid() int {
return C.getpid()
}
[inline]
pub fn getppid() int {
return C.getppid()
}
2021-05-05 14:20:11 +03:00
[inline]
pub fn getuid() int {
return C.getuid()
}
[inline]
pub fn geteuid() int {
return C.geteuid()
}
[inline]
pub fn getgid() int {
return C.getgid()
}
[inline]
pub fn getegid() int {
return C.getegid()
}
// 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)
path := &char(path_s.str)
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))
}