2019-07-01 18:45:55 +03:00
|
|
|
module os
|
|
|
|
|
2020-02-03 06:01:39 +03:00
|
|
|
import strings
|
|
|
|
|
2021-04-25 18:16:14 +03:00
|
|
|
#flag windows -l advapi32
|
2020-08-05 09:09:18 +03:00
|
|
|
#include <process.h>
|
2021-06-30 10:21:23 +03:00
|
|
|
#include <sys/utime.h>
|
2021-02-01 16:50:41 +03:00
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
|
|
|
|
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
|
|
|
|
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createhardlinkw
|
2021-06-21 14:14:58 +03:00
|
|
|
fn C.CreateHardLinkW(&u16, &u16, C.SECURITY_ATTRIBUTES) int
|
2021-05-29 23:26:13 +03:00
|
|
|
|
|
|
|
fn C._getpid() int
|
|
|
|
|
2022-06-23 20:12:29 +03:00
|
|
|
const executable_suffixes = ['.exe', '.bat', '.cmd', '']
|
2022-06-23 03:36:15 +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-09-14 23:48:30 +03:00
|
|
|
)
|
2019-07-16 02:57:03 +03:00
|
|
|
|
2019-07-01 18:45:55 +03:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
|
|
|
|
// A handle to an object.
|
2020-09-25 13:02:32 +03:00
|
|
|
pub type HANDLE = voidptr
|
2021-04-04 17:05:06 +03:00
|
|
|
pub type HMODULE = voidptr
|
2019-07-01 18:45:55 +03:00
|
|
|
|
2019-08-17 16:17:43 +03:00
|
|
|
// win: FILETIME
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
|
2019-09-29 00:21:10 +03:00
|
|
|
struct Filetime {
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_low_date_time u32
|
|
|
|
dw_high_date_time u32
|
2019-08-17 16:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// win: WIN32_FIND_DATA
|
2020-07-12 16:18:52 +03:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw
|
2019-09-29 00:21:10 +03:00
|
|
|
struct Win32finddata {
|
2019-08-17 16:17:43 +03:00
|
|
|
mut:
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_file_attributes u32
|
|
|
|
ft_creation_time Filetime
|
|
|
|
ft_last_access_time Filetime
|
|
|
|
ft_last_write_time Filetime
|
|
|
|
n_file_size_high u32
|
|
|
|
n_file_size_low u32
|
|
|
|
dw_reserved0 u32
|
|
|
|
dw_reserved1 u32
|
|
|
|
c_file_name [260]u16 // max_path_len = 260
|
2021-01-03 23:10:25 +03:00
|
|
|
c_alternate_file_name [14]u16 // 14
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_file_type u32
|
|
|
|
dw_creator_type u32
|
|
|
|
w_finder_flags u16
|
2019-08-17 16:17:43 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:01:17 +03:00
|
|
|
struct ProcessInformation {
|
|
|
|
mut:
|
2020-10-15 15:54:44 +03:00
|
|
|
h_process voidptr
|
|
|
|
h_thread voidptr
|
2020-05-16 17:12:23 +03:00
|
|
|
dw_process_id u32
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_thread_id u32
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct StartupInfo {
|
2019-12-04 23:03:12 +03:00
|
|
|
mut:
|
2020-10-15 15:54:44 +03:00
|
|
|
cb u32
|
2022-09-15 07:59:31 +03:00
|
|
|
lp_reserved &u16 = unsafe { nil }
|
|
|
|
lp_desktop &u16 = unsafe { nil }
|
|
|
|
lp_title &u16 = unsafe { nil }
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_x u32
|
|
|
|
dw_y u32
|
|
|
|
dw_x_size u32
|
|
|
|
dw_y_size u32
|
|
|
|
dw_x_count_chars u32
|
|
|
|
dw_y_count_chars u32
|
2020-05-16 17:12:23 +03:00
|
|
|
dw_fill_attributes u32
|
2020-10-15 15:54:44 +03:00
|
|
|
dw_flags u32
|
|
|
|
w_show_window u16
|
|
|
|
cb_reserved2 u16
|
2022-09-15 07:59:31 +03:00
|
|
|
lp_reserved2 &u8 = unsafe { nil }
|
2020-10-15 15:54:44 +03:00
|
|
|
h_std_input voidptr
|
|
|
|
h_std_output voidptr
|
|
|
|
h_std_error voidptr
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SecurityAttributes {
|
|
|
|
mut:
|
2020-10-15 15:54:44 +03:00
|
|
|
n_length u32
|
2020-05-16 17:12:23 +03:00
|
|
|
lp_security_descriptor voidptr
|
2020-10-15 15:54:44 +03:00
|
|
|
b_inherit_handle bool
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2019-08-17 16:17:43 +03:00
|
|
|
|
2021-06-30 10:21:23 +03:00
|
|
|
struct C._utimbuf {
|
|
|
|
actime int
|
|
|
|
modtime int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn C._utime(&char, voidptr) int
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
fn init_os_args_wide(argc int, argv &&u8) []string {
|
2021-11-22 00:04:30 +03:00
|
|
|
mut args_ := []string{len: argc}
|
2020-10-15 15:54:44 +03:00
|
|
|
for i in 0 .. argc {
|
2021-11-22 00:04:30 +03:00
|
|
|
args_[i] = unsafe { string_from_wide(&u16(argv[i])) }
|
2019-09-14 23:48:30 +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
|
|
|
fn native_glob_pattern(pattern string, mut matches []string) ! {
|
2021-06-30 08:30:18 +03:00
|
|
|
$if debug {
|
|
|
|
// FindFirstFile() and FindNextFile() both have a globbing function.
|
|
|
|
// Unfortunately this is not as pronounced as under Unix, but should provide some functionality
|
|
|
|
eprintln('os.glob() does not have all the features on Windows as it has on Unix operating systems')
|
|
|
|
}
|
|
|
|
mut find_file_data := Win32finddata{}
|
|
|
|
wpattern := pattern.replace('/', '\\').to_wide()
|
|
|
|
h_find_files := C.FindFirstFile(wpattern, voidptr(&find_file_data))
|
|
|
|
|
|
|
|
defer {
|
|
|
|
C.FindClose(h_find_files)
|
|
|
|
}
|
|
|
|
|
|
|
|
if h_find_files == C.INVALID_HANDLE_VALUE {
|
|
|
|
return error('os.glob(): Could not get a file handle: ' +
|
|
|
|
get_error_msg(int(C.GetLastError())))
|
|
|
|
}
|
|
|
|
|
|
|
|
// save first finding
|
|
|
|
fname := unsafe { string_from_wide(&find_file_data.c_file_name[0]) }
|
|
|
|
if fname !in ['.', '..'] {
|
|
|
|
mut fp := fname.replace('\\', '/')
|
|
|
|
if find_file_data.dw_file_attributes & u32(C.FILE_ATTRIBUTE_DIRECTORY) > 0 {
|
|
|
|
fp += '/'
|
|
|
|
}
|
|
|
|
matches << fp
|
|
|
|
}
|
|
|
|
|
|
|
|
// check and save next findings
|
|
|
|
for i := 0; C.FindNextFile(h_find_files, voidptr(&find_file_data)) > 0; i++ {
|
|
|
|
filename := unsafe { string_from_wide(&find_file_data.c_file_name[0]) }
|
|
|
|
if filename in ['.', '..'] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mut fpath := filename.replace('\\', '/')
|
|
|
|
if find_file_data.dw_file_attributes & u32(C.FILE_ATTRIBUTE_DIRECTORY) > 0 {
|
|
|
|
fpath += '/'
|
|
|
|
}
|
|
|
|
matches << fpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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')
|
|
|
|
}
|
2019-09-29 00:21:10 +03:00
|
|
|
mut find_file_data := Win32finddata{}
|
2020-04-26 14:49:31 +03:00
|
|
|
mut dir_files := []string{}
|
2019-12-04 23:03:12 +03:00
|
|
|
// We can also check if the handle is valid. but using is_dir instead
|
2019-08-16 15:05:11 +03:00
|
|
|
// h_find_dir := C.FindFirstFile(path.str, &find_file_data)
|
2020-05-22 18:36:09 +03:00
|
|
|
// if (invalid_handle_value == h_find_dir) {
|
2020-10-15 15:54:44 +03:00
|
|
|
// return dir_files
|
2019-08-16 15:05:11 +03:00
|
|
|
// }
|
|
|
|
// C.FindClose(h_find_dir)
|
2019-12-04 23:03:12 +03:00
|
|
|
if !is_dir(path) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('ls() couldnt open dir "${path}": directory does not exist')
|
2019-08-16 15:05:11 +03:00
|
|
|
}
|
|
|
|
// we need to add files to path eg. c:\windows\*.dll or :\windows\*
|
2022-11-15 16:53:13 +03:00
|
|
|
path_files := '${path}\\*'
|
2019-08-16 15:05:11 +03:00
|
|
|
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
|
|
|
|
// we should use FindFirstFileW and FindNextFileW
|
2019-11-16 02:30:50 +03:00
|
|
|
h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&find_file_data))
|
2021-02-17 22:45:11 +03:00
|
|
|
first_filename := unsafe { string_from_wide(&find_file_data.c_file_name[0]) }
|
2019-08-16 15:05:11 +03:00
|
|
|
if first_filename != '.' && first_filename != '..' {
|
|
|
|
dir_files << first_filename
|
|
|
|
}
|
2020-03-28 12:19:38 +03:00
|
|
|
for C.FindNextFile(h_find_files, voidptr(&find_file_data)) > 0 {
|
2021-02-17 22:45:11 +03:00
|
|
|
filename := unsafe { string_from_wide(&find_file_data.c_file_name[0]) }
|
2019-08-16 15:05:11 +03:00
|
|
|
if filename != '.' && filename != '..' {
|
|
|
|
dir_files << filename.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.FindClose(h_find_files)
|
|
|
|
return dir_files
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-08-16 15:05:11 +03:00
|
|
|
|
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) ! {
|
2020-10-15 15:54:44 +03:00
|
|
|
if path == '.' {
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2020-10-15 15:54:44 +03:00
|
|
|
}
|
|
|
|
apath := real_path(path)
|
2019-11-23 21:40:32 +03:00
|
|
|
if !C.CreateDirectory(apath.to_wide(), 0) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('mkdir failed for "${apath}", because CreateDirectory returned: ' +
|
2021-01-23 11:33:22 +03:00
|
|
|
get_error_msg(int(C.GetLastError())))
|
2019-08-17 16:17:43 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-08-17 16:17:43 +03:00
|
|
|
|
2019-07-14 23:59:25 +03:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
|
|
|
|
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
|
2019-07-01 18:45:55 +03:00
|
|
|
pub fn get_file_handle(path string) HANDLE {
|
2020-12-16 12:02:36 +03:00
|
|
|
cfile := vfopen(path, 'rb') or { return HANDLE(invalid_handle_value) }
|
2020-10-15 15:54:44 +03:00
|
|
|
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_-
|
|
|
|
return handle
|
2019-07-01 18:45:55 +03:00
|
|
|
}
|
2019-07-14 23:59:25 +03:00
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea
|
2019-09-14 23:48:30 +03:00
|
|
|
// get_module_filename retrieves the fully qualified path for the file that contains the specified module.
|
2019-07-14 23:59:25 +03:00
|
|
|
// The module must have been loaded by the current process.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn get_module_filename(handle HANDLE) !string {
|
2020-02-22 14:41:24 +03:00
|
|
|
unsafe {
|
|
|
|
mut sz := 4096 // Optimized length
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := &u16(malloc_noscan(4096))
|
2020-02-22 14:41:24 +03:00
|
|
|
for {
|
|
|
|
status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz))
|
|
|
|
match status {
|
2020-04-02 15:00:28 +03:00
|
|
|
success {
|
2020-05-16 17:12:23 +03:00
|
|
|
return string_from_wide2(buf, sz)
|
2020-02-22 14:41:24 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Must handled with GetLastError and converted by FormatMessage
|
|
|
|
return error('Cannot get file name from handle')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 15:54:44 +03:00
|
|
|
panic('this should be unreachable') // TODO remove unreachable after loop
|
2019-07-14 23:59:25 +03:00
|
|
|
}
|
2019-07-15 20:30:40 +03:00
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea#parameters
|
|
|
|
const (
|
2020-10-15 15:54:44 +03:00
|
|
|
format_message_allocate_buffer = 0x00000100
|
|
|
|
format_message_argument_array = 0x00002000
|
|
|
|
format_message_from_hmodule = 0x00000800
|
|
|
|
format_message_from_string = 0x00000400
|
|
|
|
format_message_from_system = 0x00001000
|
|
|
|
format_message_ignore_inserts = 0x00000200
|
2019-07-15 20:30:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ref - winnt.h
|
|
|
|
const (
|
2020-10-15 15:54:44 +03:00
|
|
|
sublang_neutral = 0x00
|
|
|
|
sublang_default = 0x01
|
2021-04-12 16:01:03 +03:00
|
|
|
lang_neutral = sublang_neutral
|
2019-09-14 23:48:30 +03:00
|
|
|
)
|
2019-07-15 20:30:40 +03:00
|
|
|
|
2019-07-16 21:38:19 +03:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-
|
|
|
|
const (
|
2020-10-15 15:54:44 +03:00
|
|
|
max_error_code = 15841 // ERROR_API_UNAVAILABLE
|
2019-07-16 21:38:19 +03:00
|
|
|
)
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
// ptr_win_get_error_msg return string (voidptr)
|
|
|
|
// representation of error, only for windows.
|
2019-07-18 21:21:48 +03:00
|
|
|
fn ptr_win_get_error_msg(code u32) voidptr {
|
2022-07-21 21:01:30 +03:00
|
|
|
mut buf := unsafe { nil }
|
2020-10-15 15:54:44 +03:00
|
|
|
// Check for code overflow
|
2021-01-25 12:26:20 +03:00
|
|
|
if code > u32(os.max_error_code) {
|
2020-10-15 15:54:44 +03:00
|
|
|
return buf
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
C.FormatMessage(os.format_message_allocate_buffer | os.format_message_from_system | os.format_message_ignore_inserts,
|
|
|
|
0, code, C.MAKELANGID(os.lang_neutral, os.sublang_default), voidptr(&buf), 0,
|
|
|
|
0)
|
2020-10-15 15:54:44 +03:00
|
|
|
return buf
|
2019-07-15 20:30:40 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-10-15 15:54:44 +03:00
|
|
|
if code < 0 { // skip negative
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
ptr_text := ptr_win_get_error_msg(u32(code))
|
|
|
|
if ptr_text == 0 { // compare with null
|
|
|
|
return ''
|
|
|
|
}
|
2021-02-15 18:15:52 +03:00
|
|
|
return unsafe { string_from_wide(ptr_text) }
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
|
2021-03-08 21:52:13 +03:00
|
|
|
// execute starts the specified command, waits for it to complete, and returns its output.
|
2022-05-11 16:56:03 +03:00
|
|
|
// In opposition to `raw_execute` this function will safeguard against content that is known to cause
|
|
|
|
// a lot of problems when executing shell commands on Windows.
|
2021-03-08 21:52:13 +03:00
|
|
|
pub fn execute(cmd string) Result {
|
2019-11-07 16:01:17 +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-11-07 16:01:17 +03:00
|
|
|
}
|
2022-05-11 16:56:03 +03:00
|
|
|
return unsafe { raw_execute(cmd) }
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-11-07 16:01:17 +03:00
|
|
|
mut child_stdin := &u32(0)
|
|
|
|
mut child_stdout_read := &u32(0)
|
|
|
|
mut child_stdout_write := &u32(0)
|
2020-10-15 15:54:44 +03:00
|
|
|
mut sa := SecurityAttributes{}
|
2020-05-16 17:12:23 +03:00
|
|
|
sa.n_length = sizeof(C.SECURITY_ATTRIBUTES)
|
|
|
|
sa.b_inherit_handle = true
|
2020-10-15 15:54:44 +03:00
|
|
|
create_pipe_ok := C.CreatePipe(voidptr(&child_stdout_read), voidptr(&child_stdout_write),
|
|
|
|
voidptr(&sa), 0)
|
2019-11-23 21:40:32 +03:00
|
|
|
if !create_pipe_ok {
|
2020-08-05 06:26:56 +03:00
|
|
|
error_num := int(C.GetLastError())
|
|
|
|
error_msg := get_error_msg(error_num)
|
2021-03-08 21:52:13 +03:00
|
|
|
return Result{
|
|
|
|
exit_code: error_num
|
2022-11-15 16:53:13 +03:00
|
|
|
output: 'exec failed (CreatePipe): ${error_msg}'
|
2021-03-08 21:52:13 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2020-10-15 15:54:44 +03:00
|
|
|
set_handle_info_ok := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT,
|
|
|
|
0)
|
2019-11-23 21:40:32 +03:00
|
|
|
if !set_handle_info_ok {
|
2020-08-05 06:26:56 +03:00
|
|
|
error_num := int(C.GetLastError())
|
|
|
|
error_msg := get_error_msg(error_num)
|
2021-03-08 21:52:13 +03:00
|
|
|
return Result{
|
|
|
|
exit_code: error_num
|
2022-11-15 16:53:13 +03:00
|
|
|
output: 'exec failed (SetHandleInformation): ${error_msg}'
|
2021-03-08 21:52:13 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2019-12-04 23:03:12 +03:00
|
|
|
proc_info := ProcessInformation{}
|
2020-04-04 12:59:25 +03:00
|
|
|
start_info := StartupInfo{
|
2021-04-05 20:47:32 +03:00
|
|
|
lp_reserved2: 0
|
2020-05-16 17:12:23 +03:00
|
|
|
lp_reserved: 0
|
|
|
|
lp_desktop: 0
|
|
|
|
lp_title: 0
|
2020-04-04 12:59:25 +03:00
|
|
|
cb: sizeof(C.PROCESS_INFORMATION)
|
2020-05-16 17:12:23 +03:00
|
|
|
h_std_input: child_stdin
|
|
|
|
h_std_output: child_stdout_write
|
|
|
|
h_std_error: child_stdout_write
|
|
|
|
dw_flags: u32(C.STARTF_USESTDHANDLES)
|
2020-04-04 12:59:25 +03:00
|
|
|
}
|
2020-08-16 05:54:05 +03:00
|
|
|
command_line := [32768]u16{}
|
2019-11-16 02:30:50 +03:00
|
|
|
C.ExpandEnvironmentStringsW(cmd.to_wide(), voidptr(&command_line), 32768)
|
2021-03-15 16:55:07 +03:00
|
|
|
create_process_ok := C.CreateProcessW(0, &command_line[0], 0, 0, C.TRUE, 0, 0, 0,
|
|
|
|
voidptr(&start_info), voidptr(&proc_info))
|
2019-11-23 21:40:32 +03:00
|
|
|
if !create_process_ok {
|
2020-07-17 14:03:07 +03:00
|
|
|
error_num := int(C.GetLastError())
|
|
|
|
error_msg := get_error_msg(error_num)
|
2021-03-08 21:52:13 +03:00
|
|
|
return Result{
|
|
|
|
exit_code: error_num
|
2022-11-15 16:53:13 +03:00
|
|
|
output: 'exec failed (CreateProcess) with code ${error_num}: ${error_msg} cmd: ${cmd}'
|
2021-03-08 21:52:13 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
|
|
|
C.CloseHandle(child_stdin)
|
2019-12-04 23:03:12 +03:00
|
|
|
C.CloseHandle(child_stdout_write)
|
2022-04-15 15:35:35 +03:00
|
|
|
buf := [4096]u8{}
|
2019-11-16 02:30:50 +03:00
|
|
|
mut bytes_read := u32(0)
|
2020-02-03 06:01:39 +03:00
|
|
|
mut read_data := strings.new_builder(1024)
|
2019-11-07 16:01:17 +03:00
|
|
|
for {
|
2021-02-15 18:15:52 +03:00
|
|
|
mut result := false
|
|
|
|
unsafe {
|
2021-03-15 16:55:07 +03:00
|
|
|
result = C.ReadFile(child_stdout_read, &buf[0], 1000, voidptr(&bytes_read),
|
|
|
|
0)
|
2021-03-20 10:02:28 +03:00
|
|
|
read_data.write_ptr(&buf[0], int(bytes_read))
|
2021-02-15 18:15:52 +03:00
|
|
|
}
|
|
|
|
if result == false || int(bytes_read) == 0 {
|
2019-11-23 21:40:32 +03:00
|
|
|
break
|
2019-12-04 23:03:12 +03:00
|
|
|
}
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2022-01-25 15:09:56 +03:00
|
|
|
soutput := read_data.str()
|
2021-02-15 18:15:52 +03:00
|
|
|
unsafe { read_data.free() }
|
2019-11-16 02:30:50 +03:00
|
|
|
exit_code := u32(0)
|
2020-05-16 17:12:23 +03:00
|
|
|
C.WaitForSingleObject(proc_info.h_process, C.INFINITE)
|
|
|
|
C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code))
|
|
|
|
C.CloseHandle(proc_info.h_process)
|
|
|
|
C.CloseHandle(proc_info.h_thread)
|
2020-10-15 15:54:44 +03:00
|
|
|
return Result{
|
2020-02-03 06:01:39 +03:00
|
|
|
output: soutput
|
2019-11-16 02:30:50 +03:00
|
|
|
exit_code: int(exit_code)
|
2019-11-07 16:01:17 +03:00
|
|
|
}
|
2019-11-10 03:08:53 +03:00
|
|
|
}
|
2019-12-27 21:10:06 +03:00
|
|
|
|
2022-10-20 13:56:06 +03:00
|
|
|
pub fn symlink(origin string, target string) ! {
|
2021-05-29 23:26:13 +03:00
|
|
|
// this is a temporary fix for TCC32 due to runtime error
|
2021-06-21 14:14:58 +03:00
|
|
|
// TODO: find the cause why TCC32 for Windows does not work without the compiletime option
|
2021-05-29 23:26:13 +03:00
|
|
|
$if x64 || x32 {
|
|
|
|
mut flags := 0
|
|
|
|
if is_dir(origin) {
|
|
|
|
flags ^= 1
|
|
|
|
}
|
2019-12-27 21:10:06 +03:00
|
|
|
|
2021-05-29 23:26:13 +03:00
|
|
|
flags ^= 2 // SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
|
|
|
|
res := C.CreateSymbolicLinkW(target.to_wide(), origin.to_wide(), flags)
|
|
|
|
|
|
|
|
// 1 = success, != 1 failure => https://stackoverflow.com/questions/33010440/createsymboliclink-on-windows-10
|
|
|
|
if res != 1 {
|
|
|
|
return error(get_error_msg(int(C.GetLastError())))
|
|
|
|
}
|
|
|
|
if !exists(target) {
|
|
|
|
return error('C.CreateSymbolicLinkW reported success, but symlink still does not exist')
|
|
|
|
}
|
2022-10-20 13:56:06 +03:00
|
|
|
return
|
2020-10-22 17:28:58 +03:00
|
|
|
}
|
2022-10-20 13:56:06 +03:00
|
|
|
return error('could not symlink')
|
2021-05-29 23:26:13 +03:00
|
|
|
}
|
|
|
|
|
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.CreateHardLinkW(target.to_wide(), origin.to_wide(), C.NULL)
|
|
|
|
// 1 = success, != 1 failure => https://stackoverflow.com/questions/33010440/createsymboliclink-on-windows-10
|
|
|
|
if res != 1 {
|
2020-10-22 17:28:58 +03:00
|
|
|
return error(get_error_msg(int(C.GetLastError())))
|
2019-12-27 21:10:06 +03:00
|
|
|
}
|
2021-05-29 23:26:13 +03:00
|
|
|
if !exists(target) {
|
|
|
|
return error('C.CreateHardLinkW reported success, but link still does not exist')
|
|
|
|
}
|
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-29 04:06:27 +03:00
|
|
|
|
|
|
|
pub struct ExceptionRecord {
|
|
|
|
pub:
|
|
|
|
// status_ constants
|
2020-10-15 15:54:44 +03:00
|
|
|
code u32
|
|
|
|
flags u32
|
2022-09-15 07:59:31 +03:00
|
|
|
record &ExceptionRecord = unsafe { nil }
|
2020-10-15 15:54:44 +03:00
|
|
|
address voidptr
|
2020-05-29 04:06:27 +03:00
|
|
|
param_count u32
|
|
|
|
// params []voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ContextRecord {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ExceptionPointers {
|
|
|
|
pub:
|
2022-09-15 07:59:31 +03:00
|
|
|
exception_record &ExceptionRecord = unsafe { nil }
|
|
|
|
context_record &ContextRecord = unsafe { nil }
|
2020-05-29 04:06:27 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:02:36 +03:00
|
|
|
pub type VectoredExceptionHandler = fn (&ExceptionPointers) u32
|
2020-05-29 04:06:27 +03:00
|
|
|
|
|
|
|
// This is defined in builtin because we use vectored exception handling
|
|
|
|
// for our unhandled exception handler on windows
|
|
|
|
// As a result this definition is commented out to prevent
|
|
|
|
// duplicate definitions from displeasing the compiler
|
|
|
|
// fn C.AddVectoredExceptionHandler(u32, VectoredExceptionHandler)
|
|
|
|
pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandler) {
|
2020-09-16 17:40:02 +03:00
|
|
|
C.AddVectoredExceptionHandler(u32(first), C.PVECTORED_EXCEPTION_HANDLER(handler))
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-22 14:18:28 +03:00
|
|
|
// uname returns information about the platform on which the program is running.
|
|
|
|
// Currently `uname` on windows is not standardized, so it just mimics current practices from other popular software/language implementations:
|
|
|
|
// busybox-v1.35.0 * `busybox uname -a` => "Windows_NT HOSTNAME 10.0 19044 x86_64 MS/Windows"
|
|
|
|
// rust/coreutils-v0.0.17 * `coreutils uname -a` => `Windows_NT HOSTNAME 10.0 19044 x86_64 MS/Windows (Windows 10)`
|
|
|
|
// Python3 => `uname_result(system='Windows', node='HOSTNAME', release='10', version='10.0.19044', machine='AMD64')`
|
|
|
|
// See: [NT Version Info](https://en.wikipedia.org/wiki/Windows_NT) @@ <https://archive.is/GnnvF>
|
|
|
|
// and: [NT Version Info (detailed)](https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions#NT_Kernel-based_2)
|
2020-06-14 16:46:30 +03:00
|
|
|
pub fn uname() Uname {
|
2021-04-25 18:16:14 +03:00
|
|
|
nodename := hostname()
|
2023-01-22 12:59:42 +03:00
|
|
|
// ToDO: environment variables have low reliability; check for another quick way
|
|
|
|
machine := getenv('PROCESSOR_ARCHITECTURE') // * note: 'AMD64' == 'x86_64' (not standardized, but 'x86_64' use is more common; but, python == 'AMD64')
|
|
|
|
version_info := execute('cmd /d/c ver').output
|
|
|
|
version_n := (version_info.split(' '))[3].replace(']', '').trim_space()
|
2020-06-14 16:46:30 +03:00
|
|
|
return Uname{
|
2023-01-22 12:59:42 +03:00
|
|
|
sysname: 'Windows_NT' // as of 2022-12, WinOS has only two possible kernels ~ 'Windows_NT' or 'Windows_9x'
|
2021-04-15 02:49:05 +03:00
|
|
|
nodename: nodename
|
2023-01-22 12:59:42 +03:00
|
|
|
machine: machine.trim_space()
|
|
|
|
release: (version_n.split('.'))[0..2].join('.').trim_space() // Major.minor-only == "primary"/release version
|
|
|
|
version: (version_n.split('.'))[2].trim_space()
|
2020-06-14 16:46:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 18:19:36 +03:00
|
|
|
|
2021-04-15 02:49:05 +03:00
|
|
|
pub fn hostname() string {
|
2021-04-24 13:22:04 +03:00
|
|
|
hostname := [255]u16{}
|
|
|
|
size := u32(255)
|
|
|
|
res := C.GetComputerNameW(&hostname[0], &size)
|
|
|
|
if !res {
|
2021-07-05 05:05:08 +03:00
|
|
|
return get_error_msg(int(C.GetLastError()))
|
2021-04-24 13:22:04 +03:00
|
|
|
}
|
|
|
|
return unsafe { string_from_wide(&hostname[0]) }
|
2021-04-15 02:49:05 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 14:57:25 +03:00
|
|
|
pub fn loginname() string {
|
2021-04-25 18:16:14 +03:00
|
|
|
loginname := [255]u16{}
|
|
|
|
size := u32(255)
|
|
|
|
res := C.GetUserNameW(&loginname[0], &size)
|
|
|
|
if !res {
|
2021-07-05 05:05:08 +03:00
|
|
|
return get_error_msg(int(C.GetLastError()))
|
2021-04-25 18:16:14 +03:00
|
|
|
}
|
|
|
|
return unsafe { string_from_wide(&loginname[0]) }
|
2021-04-19 14:57:25 +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.
|
|
|
|
pub fn ensure_folder_is_writable(folder string) ! {
|
2020-10-15 15:54:44 +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-10-15 15:54:44 +03:00
|
|
|
if !is_dir(folder) {
|
2022-10-20 13:56:06 +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_folder_name := 'tmp_perm_check_pid_' + getpid().str()
|
|
|
|
tmp_perm_check := join_path_single(folder, tmp_folder_name)
|
2022-10-20 13:56:06 +03:00
|
|
|
write_file(tmp_perm_check, 'test') or {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('cannot write to folder "${folder}": ${err}', 3)
|
2022-10-20 13:56:06 +03:00
|
|
|
}
|
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 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getuid() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn geteuid() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getgid() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn getegid() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:57:17 +03:00
|
|
|
pub fn posix_set_permission_bit(path_s string, mode u32, enable bool) {
|
|
|
|
// windows has no concept of a permission mask, so do nothing
|
|
|
|
}
|
2022-01-30 01:44:52 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut c Command) start() ! {
|
2022-01-30 01:44:52 +03:00
|
|
|
panic('not implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c Command) read_line() string {
|
|
|
|
panic('not implemented')
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut c Command) close() ! {
|
2022-01-30 01:44:52 +03:00
|
|
|
panic('not implemented')
|
|
|
|
}
|