2019-08-23 23:55:45 +03:00
|
|
|
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>
|
2019-07-24 00:23:13 +03:00
|
|
|
#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>
|
2021-06-29 15:00:48 +03:00
|
|
|
#include <sys/types.h>
|
2021-06-30 10:21:23 +03:00
|
|
|
#include <utime.h>
|
2021-02-01 16:50:41 +03:00
|
|
|
|
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-07-18 21:21:48 +03:00
|
|
|
)
|
|
|
|
|
2022-06-23 03:36:15 +03:00
|
|
|
const executable_suffixes = ['']
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-01-04 20:57:17 +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
|
|
|
|
)
|
|
|
|
|
2021-06-30 10:21:23 +03:00
|
|
|
fn C.utime(&char, voidptr) int
|
|
|
|
|
2020-06-14 16:46:30 +03:00
|
|
|
fn C.uname(name voidptr) int
|
2020-07-21 16:28:30 +03:00
|
|
|
|
2021-04-05 08:08:51 +03:00
|
|
|
fn C.symlink(&char, &char) int
|
2019-12-27 21:10:06 +03:00
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
fn C.link(&char, &char) int
|
|
|
|
|
2021-04-15 02:49:05 +03:00
|
|
|
fn C.gethostname(&char, int) int
|
|
|
|
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: not available on Android fn C.getlogin_r(&char, int) int
|
2021-04-24 13:32:26 +03:00
|
|
|
fn C.getlogin() &char
|
2021-04-19 14:57:25 +03:00
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
fn C.getppid() int
|
|
|
|
|
|
|
|
fn C.getgid() int
|
|
|
|
|
|
|
|
fn C.getegid() int
|
|
|
|
|
2021-07-09 03:27:16 +03:00
|
|
|
enum GlobMatch {
|
|
|
|
exact
|
|
|
|
ends_with
|
|
|
|
starts_with
|
|
|
|
start_and_ends_with
|
|
|
|
contains
|
|
|
|
any
|
|
|
|
}
|
2021-06-30 08:30:18 +03:00
|
|
|
|
2021-07-09 03:27:16 +03:00
|
|
|
fn glob_match(dir string, pattern string, next_pattern string, mut matches []string) []string {
|
|
|
|
mut subdirs := []string{}
|
|
|
|
if is_file(dir) {
|
|
|
|
return subdirs
|
|
|
|
}
|
|
|
|
mut files := ls(dir) or { return subdirs }
|
|
|
|
mut mode := GlobMatch.exact
|
|
|
|
mut pat := pattern
|
|
|
|
if pat == '*' {
|
|
|
|
mode = GlobMatch.any
|
|
|
|
if next_pattern != pattern && next_pattern != '' {
|
|
|
|
for file in files {
|
2022-11-15 16:53:13 +03:00
|
|
|
if is_dir('${dir}/${file}') {
|
|
|
|
subdirs << '${dir}/${file}'
|
2021-07-09 03:27:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return subdirs
|
|
|
|
}
|
2021-07-08 00:00:32 +03:00
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
if pat == '**' {
|
|
|
|
files = walk_ext(dir, '')
|
|
|
|
pat = next_pattern
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
if pat.starts_with('*') {
|
|
|
|
mode = .ends_with
|
|
|
|
pat = pat[1..]
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
if pat.ends_with('*') {
|
|
|
|
mode = if mode == .ends_with { GlobMatch.contains } else { GlobMatch.starts_with }
|
|
|
|
pat = pat[..pat.len - 1]
|
|
|
|
}
|
|
|
|
if pat.contains('*') {
|
|
|
|
mode = .start_and_ends_with
|
|
|
|
}
|
|
|
|
for file in files {
|
|
|
|
mut fpath := file
|
|
|
|
f := if file.contains(os.path_separator) {
|
|
|
|
pathwalk := file.split(os.path_separator)
|
|
|
|
pathwalk[pathwalk.len - 1]
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
fpath = if dir == '.' { file } else { '${dir}/${file}' }
|
2021-07-09 03:27:16 +03:00
|
|
|
file
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
if f in ['.', '..'] || f == '' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hit := match mode {
|
|
|
|
.any {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
.exact {
|
|
|
|
f == pat
|
|
|
|
}
|
|
|
|
.starts_with {
|
|
|
|
f.starts_with(pat)
|
|
|
|
}
|
|
|
|
.ends_with {
|
|
|
|
f.ends_with(pat)
|
|
|
|
}
|
|
|
|
.start_and_ends_with {
|
|
|
|
p := pat.split('*')
|
|
|
|
f.starts_with(p[0]) && f.ends_with(p[1])
|
|
|
|
}
|
|
|
|
.contains {
|
|
|
|
f.contains(pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
if is_dir(fpath) {
|
|
|
|
subdirs << fpath
|
|
|
|
if next_pattern == pattern && next_pattern != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
matches << '${fpath}${os.path_separator}'
|
2021-07-08 00:00:32 +03:00
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
} else {
|
|
|
|
matches << fpath
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 03:27:16 +03:00
|
|
|
return subdirs
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn native_glob_pattern(pattern string, mut matches []string) ! {
|
2021-07-09 03:27:16 +03:00
|
|
|
steps := pattern.split(os.path_separator)
|
|
|
|
mut cwd := if pattern.starts_with(os.path_separator) { os.path_separator } else { '.' }
|
|
|
|
mut subdirs := [cwd]
|
|
|
|
for i := 0; i < steps.len; i++ {
|
|
|
|
step := steps[i]
|
|
|
|
step2 := if i + 1 == steps.len { step } else { steps[i + 1] }
|
|
|
|
if step == '' {
|
|
|
|
continue
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
if is_dir('${cwd}${os.path_separator}${step}') {
|
2021-07-09 03:27:16 +03:00
|
|
|
dd := if cwd == '/' {
|
|
|
|
step
|
|
|
|
} else {
|
|
|
|
if cwd == '.' || cwd == '' {
|
|
|
|
step
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
if step == '.' || step == '/' { cwd } else { '${cwd}/${step}' }
|
2021-07-09 03:27:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if i + 1 != steps.len {
|
|
|
|
if dd !in subdirs {
|
|
|
|
subdirs << dd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut subs := []string{}
|
|
|
|
for sd in subdirs {
|
|
|
|
d := if cwd == '/' {
|
|
|
|
sd
|
|
|
|
} else {
|
|
|
|
if cwd == '.' || cwd == '' {
|
|
|
|
sd
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
if sd == '.' || sd == '/' { cwd } else { '${cwd}/${sd}' }
|
2021-07-09 03:27:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
subs << glob_match(d.replace('//', '/'), step, step2, mut matches)
|
|
|
|
}
|
|
|
|
subdirs = subs.clone()
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 15:00:48 +03:00
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn utime(path string, actime int, modtime int) ! {
|
2021-06-30 10:21:23 +03:00
|
|
|
mut u := C.utimbuf{actime, modtime}
|
|
|
|
if C.utime(&char(path.str), voidptr(&u)) != 0 {
|
|
|
|
return error_with_code(posix_get_error_msg(C.errno), C.errno)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 14:18:28 +03:00
|
|
|
// uname returns information about the platform on which the program is running
|
|
|
|
// For example:
|
|
|
|
// os.Uname{
|
|
|
|
// sysname: 'Linux'
|
|
|
|
// nodename: 'nemesis'
|
|
|
|
// release: '5.15.0-57-generic'
|
|
|
|
// version: '#63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022'
|
|
|
|
// machine: 'x86_64'
|
|
|
|
// }
|
|
|
|
// where the fields have the following meaning:
|
|
|
|
// sysname is the name of this implementation of the operating system
|
|
|
|
// nodename is the name of this node within an implementation-dependent communications network
|
|
|
|
// release is the current release level of this implementation
|
|
|
|
// version is the current version level of this release
|
|
|
|
// machine is the name of the hardware type, on which the system is running
|
|
|
|
// See also https://pubs.opengroup.org/onlinepubs/7908799/xsh/sysutsname.h.html
|
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)
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
2021-06-15 14:47:11 +03:00
|
|
|
x := malloc_noscan(int(utsize))
|
2021-02-14 21:31:42 +03:00
|
|
|
d := &C.utsname(x)
|
|
|
|
if C.uname(d) == 0 {
|
2021-04-05 08:08:51 +03: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 21:31:42 +03:00
|
|
|
}
|
|
|
|
free(d)
|
2020-06-14 16:46:30 +03:00
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2023-02-27 05:21:23 +03:00
|
|
|
pub fn hostname() !string {
|
2021-04-15 02:49:05 +03:00
|
|
|
mut hstnme := ''
|
|
|
|
size := 256
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := unsafe { &char(malloc_noscan(size)) }
|
2021-04-15 02:49:05 +03:00
|
|
|
if C.gethostname(buf, size) == 0 {
|
|
|
|
hstnme = unsafe { cstring_to_vstring(buf) }
|
|
|
|
unsafe { free(buf) }
|
|
|
|
return hstnme
|
|
|
|
}
|
2023-02-27 05:21:23 +03:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2021-04-15 02:49:05 +03:00
|
|
|
}
|
|
|
|
|
2023-02-27 05:21:23 +03:00
|
|
|
pub fn loginname() !string {
|
2021-04-24 13:32:26 +03:00
|
|
|
x := C.getlogin()
|
|
|
|
if !isnil(x) {
|
|
|
|
return unsafe { cstring_to_vstring(x) }
|
2021-04-19 14:57:25 +03:00
|
|
|
}
|
2023-02-27 05:21:23 +03:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
2021-04-19 14:57:25 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:45:52 +03:00
|
|
|
fn init_os_args(argc int, argv &&u8) []string {
|
2021-11-21 21:53:42 +03:00
|
|
|
mut args_ := []string{len: argc}
|
2019-10-13 01:50:15 +03:00
|
|
|
for i in 0 .. argc {
|
2021-11-21 21:53:42 +03:00
|
|
|
args_[i] = unsafe { tos_clone(argv[i]) }
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2021-01-10 23:41:29 +03:00
|
|
|
return args_
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn ls(path string) ![]string {
|
2021-10-24 18:38:44 +03:00
|
|
|
if path.len == 0 {
|
|
|
|
return error('ls() expects a folder, not an empty string')
|
|
|
|
}
|
2021-10-29 15:49:30 +03:00
|
|
|
mut res := []string{cap: 50}
|
2021-04-04 17:43:32 +03:00
|
|
|
dir := unsafe { C.opendir(&char(path.str)) }
|
2019-08-16 15:05:11 +03:00
|
|
|
if isnil(dir) {
|
2022-11-15 16:53:13 +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
|
|
|
|
}
|
2021-01-14 05:55:30 +03:00
|
|
|
unsafe {
|
2022-04-15 14:45:52 +03:00
|
|
|
bptr := &u8(&ent.d_name[0])
|
2021-01-23 11:33:22 +03:00
|
|
|
if bptr[0] == 0 || (bptr[0] == `.` && bptr[1] == 0)
|
|
|
|
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
|
2021-01-14 05:55:30 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-15 18:15:52 +03:00
|
|
|
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
|
|
|
|
|
|
|
// mkdir creates a new directory with the specified path.
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn mkdir(path string, params MkdirParams) ! {
|
2019-12-20 00:29:37 +03:00
|
|
|
if path == '.' {
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2020-07-01 15:34:14 +03:00
|
|
|
}
|
2020-07-21 16:28:30 +03:00
|
|
|
apath := real_path(path)
|
2022-06-30 13:49:47 +03:00
|
|
|
r := unsafe { C.mkdir(&char(apath.str), params.mode) }
|
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
|
|
|
}
|
2019-08-17 16:17:43 +03:00
|
|
|
}
|
|
|
|
|
2021-03-08 21:52:13 +03:00
|
|
|
// execute starts the specified command, waits for it to complete, and returns its output.
|
2021-03-23 21:36:19 +03:00
|
|
|
[manualfree]
|
2021-03-08 21:52:13 +03:00
|
|
|
pub fn execute(cmd string) Result {
|
2019-12-20 00:29:37 +03:00
|
|
|
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
2021-03-08 21:52:13 +03:00
|
|
|
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
|
2019-12-20 00:29:37 +03:00
|
|
|
// }
|
2022-11-15 16:53:13 +03:00
|
|
|
pcmd := if cmd.contains('2>') { cmd.clone() } else { '${cmd} 2>&1' }
|
2022-04-09 13:03:52 +03:00
|
|
|
defer {
|
|
|
|
unsafe { pcmd.free() }
|
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
f := vpopen(pcmd)
|
|
|
|
if isnil(f) {
|
2021-03-08 21:52:13 +03:00
|
|
|
return Result{
|
|
|
|
exit_code: -1
|
2022-11-15 16:53:13 +03:00
|
|
|
output: 'exec("${cmd}") failed'
|
2021-03-08 21:52:13 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2021-11-12 10:39:15 +03:00
|
|
|
fd := fileno(f)
|
2020-02-03 06:01:39 +03:00
|
|
|
mut res := strings.new_builder(1024)
|
2021-03-23 21:36:19 +03:00
|
|
|
defer {
|
|
|
|
unsafe { res.free() }
|
|
|
|
}
|
2022-04-15 14:45:52 +03:00
|
|
|
buf := [4096]u8{}
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
2021-11-12 12:44:26 +03:00
|
|
|
pbuf := &buf[0]
|
2021-11-12 10:39:15 +03:00
|
|
|
for {
|
2021-11-12 12:44:26 +03:00
|
|
|
len := C.read(fd, pbuf, 4096)
|
2021-11-12 10:39:15 +03:00
|
|
|
if len == 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-11-12 12:44:26 +03:00
|
|
|
res.write_ptr(pbuf, len)
|
2021-02-14 21:31:42 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2020-06-14 02:41:47 +03:00
|
|
|
soutput := res.str()
|
2019-11-07 16:01:17 +03:00
|
|
|
exit_code := vpclose(f)
|
2019-12-20 00:29:37 +03:00
|
|
|
return Result{
|
2020-05-18 18:05:48 +03:00
|
|
|
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
|
|
|
|
2022-05-11 16:56:03 +03:00
|
|
|
// raw_execute does the same as `execute` on Unix platforms.
|
|
|
|
// On Windows raw_execute starts the specified command, waits for it to complete, and returns its output.
|
|
|
|
// It's marked as `unsafe` to help emphasize the problems that may arise by allowing, for example,
|
|
|
|
// user provided escape sequences.
|
|
|
|
[unsafe]
|
|
|
|
pub fn raw_execute(cmd string) Result {
|
|
|
|
return execute(cmd)
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:36:19 +03:00
|
|
|
[manualfree]
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut c Command) start() ! {
|
2021-03-23 21:36:19 +03:00
|
|
|
pcmd := c.path + ' 2>&1'
|
|
|
|
defer {
|
|
|
|
unsafe { pcmd.free() }
|
|
|
|
}
|
2020-11-05 10:44:34 +03:00
|
|
|
c.f = vpopen(pcmd)
|
|
|
|
if isnil(c.f) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('exec("${c.path}") failed')
|
2020-11-05 10:44:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:36:19 +03:00
|
|
|
[manualfree]
|
2020-11-05 10:44:34 +03:00
|
|
|
pub fn (mut c Command) read_line() string {
|
2022-04-15 14:45:52 +03:00
|
|
|
buf := [4096]u8{}
|
2020-11-05 10:44:34 +03:00
|
|
|
mut res := strings.new_builder(1024)
|
2021-03-23 21:36:19 +03:00
|
|
|
defer {
|
|
|
|
unsafe { res.free() }
|
|
|
|
}
|
2020-11-05 10:44:34 +03:00
|
|
|
unsafe {
|
2021-02-17 22:45:11 +03:00
|
|
|
bufbp := &buf[0]
|
2021-04-04 17:43:32 +03:00
|
|
|
for C.fgets(&char(bufbp), 4096, c.f) != 0 {
|
2020-11-05 10:44:34 +03:00
|
|
|
len := vstrlen(bufbp)
|
2020-12-16 12:02:36 +03:00
|
|
|
for i in 0 .. len {
|
2021-02-17 22:45:11 +03:00
|
|
|
if bufbp[i] == `\n` {
|
2021-03-20 10:02:28 +03:00
|
|
|
res.write_ptr(bufbp, i)
|
2021-03-23 21:36:19 +03:00
|
|
|
final := res.str()
|
|
|
|
return final
|
2020-11-05 10:44:34 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 10:02:28 +03:00
|
|
|
res.write_ptr(bufbp, len)
|
2020-11-05 10:44:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.eof = true
|
2021-03-23 21:36:19 +03:00
|
|
|
final := res.str()
|
|
|
|
return final
|
2020-11-05 10:44:34 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut c Command) close() ! {
|
2022-01-30 01:44:52 +03:00
|
|
|
c.exit_code = vpclose(c.f)
|
|
|
|
if c.exit_code == 127 {
|
2020-11-05 10:44:34 +03:00
|
|
|
return error_with_code('error', 127)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn symlink(origin string, target string) ! {
|
2021-04-04 17:43:32 +03:00
|
|
|
res := C.symlink(&char(origin.str), &char(target.str))
|
2020-01-05 22:13:35 +03:00
|
|
|
if res == 0 {
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2020-01-05 22:13:35 +03:00
|
|
|
}
|
2020-01-21 18:58:47 +03:00
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn link(origin string, target string) ! {
|
2021-05-29 23:26:13 +03:00
|
|
|
res := C.link(&char(origin.str), &char(target.str))
|
|
|
|
if res == 0 {
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2021-05-29 23:26:13 +03:00
|
|
|
}
|
|
|
|
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-01-01 14:50:25 +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 {
|
2020-01-01 14:50:25 +03:00
|
|
|
return
|
|
|
|
}
|
2020-08-02 00:07:37 +03:00
|
|
|
f.is_opened = false
|
2020-01-01 14:50:25 +03:00
|
|
|
C.fflush(f.cfile)
|
|
|
|
C.fclose(f.cfile)
|
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
|
2022-04-15 14:45:52 +03:00
|
|
|
fn C.mkstemp(stemplate &u8) int
|
2020-12-16 12:02:36 +03:00
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
// ensure_folder_is_writable checks that `folder` exists, and is writable to the process
|
|
|
|
// by creating an empty file in it, then deleting it.
|
2021-10-29 15:49:30 +03:00
|
|
|
[manualfree]
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn ensure_folder_is_writable(folder string) ! {
|
2020-12-16 12:02:36 +03:00
|
|
|
if !exists(folder) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('`${folder}` does not exist', 1)
|
2020-08-03 18:19:36 +03:00
|
|
|
}
|
2020-12-16 12:02:36 +03:00
|
|
|
if !is_dir(folder) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('`${folder}` is not a folder', 2)
|
2020-08-03 18:19:36 +03:00
|
|
|
}
|
2021-11-22 15:40:55 +03:00
|
|
|
tmp_perm_check := join_path_single(folder, 'XXXXXX')
|
2021-10-29 15:49:30 +03:00
|
|
|
defer {
|
|
|
|
unsafe { tmp_perm_check.free() }
|
|
|
|
}
|
2020-08-03 18:19:36 +03:00
|
|
|
unsafe {
|
2021-04-04 17:43:32 +03:00
|
|
|
x := C.mkstemp(&char(tmp_perm_check.str))
|
2020-08-03 18:19:36 +03:00
|
|
|
if -1 == x {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('folder `${folder}` is not writable', 3)
|
2020-08-03 18:19:36 +03:00
|
|
|
}
|
|
|
|
C.close(x)
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
rm(tmp_perm_check)!
|
2020-08-03 18:19:36 +03:00
|
|
|
}
|
2020-08-05 09:04:40 +03:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getpid() int {
|
|
|
|
return C.getpid()
|
|
|
|
}
|
2021-01-04 20:57:17 +03:00
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
[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()
|
|
|
|
}
|
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
[inline]
|
|
|
|
pub fn getgid() int {
|
|
|
|
return C.getgid()
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getegid() int {
|
|
|
|
return C.getegid()
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:57:17 +03: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 17:43:32 +03:00
|
|
|
path := &char(path_s.str)
|
2021-01-04 20:57:17 +03: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))
|
|
|
|
}
|
2023-03-14 01:51:52 +03:00
|
|
|
|
|
|
|
// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
|
|
|
|
// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
|
|
|
|
fn get_long_path(path string) !string {
|
|
|
|
return path
|
|
|
|
}
|