os: create os.hostname() and os.chown() + os.uname() for windows (#9722)

pull/9739/head
Bastian Buck 2021-04-15 01:49:05 +02:00 committed by GitHub
parent 25a9d30a70
commit 546dc91967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 8 deletions

View File

@ -31,6 +31,8 @@ fn C.CopyFile(&u16, &u16, bool) int
fn C._wstat64(&char, voidptr) u64
fn C.chown(&char, int, int) int
// fn C.proc_pidpath(int, byteptr, int) int
struct C.stat {
st_size u64
@ -827,7 +829,24 @@ pub fn flush() {
// chmod change file access attributes of `path` to `mode`.
// Octals like `0o600` can be used.
pub fn chmod(path string, mode int) {
C.chmod(&char(path.str), mode)
if C.chmod(&char(path.str), mode) != 0 {
panic(posix_get_error_msg(C.errno))
}
}
// chown change owner and group attributes of path to `owner` and `group`.
pub fn chown(path string, owner int, group int) ? {
$if windows {
return error('os.chown() not implemented for Windows')
} $else {
if owner < 0 || group < 0 {
return error('os.chown() uid and gid cannot be negative: Not changing owner!')
} else {
if C.chown(&char(path.str), owner, group) != 0 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
}
}
}
// open_append opens `path` file for appending.

View File

@ -51,6 +51,8 @@ fn C.uname(name voidptr) int
fn C.symlink(&char, &char) int
fn C.gethostname(&char, int) int
pub fn uname() Uname {
mut u := Uname{}
utsize := sizeof(C.utsname)
@ -69,6 +71,18 @@ pub fn uname() Uname {
return u
}
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 ''
}
fn init_os_args(argc int, argv &&byte) []string {
mut args_ := []string{}
// mut args := []string(make(0, argc, sizeof(string)))

View File

@ -376,17 +376,24 @@ pub fn debugger_present() bool {
}
pub fn uname() Uname {
// TODO: implement `os.uname()` for windows
unknown := 'unknown'
// TODO: use Win32 Api functions instead
sys_and_ver := execute('cmd /c ver').output.split('[')
nodename := execute('cmd /c hostname').output
machine := execute('cmd /c echo %PROCESSOR_ARCHITECTURE%').output
return Uname{
sysname: unknown
nodename: unknown
release: unknown
version: unknown
machine: unknown
sysname: sys_and_ver[0].trim_space()
nodename: nodename
release: sys_and_ver[1].replace(']', '')
version: sys_and_ver[0] + '[' + sys_and_ver[1]
machine: machine
}
}
pub fn hostname() string {
// TODO: use C.GetComputerName(&u16, u32) int instead
return execute('cmd /c hostname').output
}
// `is_writable_folder` - `folder` exists and is writable to the process
pub fn is_writable_folder(folder string) ?bool {
if !exists(folder) {