os: vfmt most of `os`, add it to `v test-cleancode`

pull/7362/head
Delyan Angelov 2020-12-16 11:02:36 +02:00
parent 525b521b4a
commit 88a8507dd8
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
14 changed files with 208 additions and 276 deletions

View File

@ -18,7 +18,7 @@ const (
'nonexistant', 'nonexistant',
] ]
vfmt_verify_list = [ vfmt_verify_list = [
'cmd/tools/vdoc.v' 'cmd/tools/vdoc.v',
'cmd/v/v.v', 'cmd/v/v.v',
'vlib/builtin/array.v', 'vlib/builtin/array.v',
'vlib/builtin/map.v', 'vlib/builtin/map.v',
@ -58,6 +58,19 @@ const (
'vlib/v/vet/', 'vlib/v/vet/',
'vlib/v/vmod/', 'vlib/v/vmod/',
'vlib/gg/gg.v', 'vlib/gg/gg.v',
'vlib/os/const.v',
'vlib/os/const_windows.c.v',
'vlib/os/environment.c.v',
'vlib/os/environment_test.v',
'vlib/os/inode.c.v',
'vlib/os/inode_test.v',
'vlib/os/os.v',
'vlib/os/os_c.v',
'vlib/os/os_darwin.c.v',
'vlib/os/os_linux.c.v',
'vlib/os/os_nix.c.v',
'vlib/os/os_test.v',
'vlib/os/os_windows.c.v',
] ]
) )

View File

@ -1,18 +1,18 @@
module os module os
// (Must be realized in Syscall) (Must be specified)
// (Must be realized in Syscall) (Must be specified)
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html // ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
const ( const (
s_ifmt = 0xF000 // type of file s_ifmt = 0xF000 // type of file
s_ifdir = 0x4000 // directory s_ifdir = 0x4000 // directory
s_iflnk = 0xa000 // link s_iflnk = 0xa000 // link
s_ixusr = 0o100 // is executable by the owner s_ixusr = 0o100 // is executable by the owner
s_ixgrp = 0o010 // is executable by group s_ixgrp = 0o010 // is executable by group
s_ixoth = 0o001 // is executable by others s_ixoth = 0o001 // is executable by others
) )
const ( const (
std_input_handle = -10 std_input_handle = -10
std_output_handle = -11 std_output_handle = -11
std_error_handle = -12 std_error_handle = -12
) )

View File

@ -2,164 +2,160 @@ module os
// Ref - winnt.h // Ref - winnt.h
const ( const (
success = 0x0000 // ERROR_SUCCESS success = 0x0000 // ERROR_SUCCESS
error_insufficient_buffer = 0x0082 error_insufficient_buffer = 0x0082
) )
const ( const (
handle_generic_read = 0x80000000 handle_generic_read = 0x80000000
handle_open_existing = 0x00000003 handle_open_existing = 0x00000003
) )
const ( const (
file_share_read = 0x01 file_share_read = 0x01
file_share_write = 0x02 file_share_write = 0x02
file_share_delete = 0x04 file_share_delete = 0x04
) )
const ( const (
file_notify_change_file_name = 0x01 file_notify_change_file_name = 0x01
file_notify_change_dir_name = 0x02 file_notify_change_dir_name = 0x02
file_notify_change_attributes = 0x04 file_notify_change_attributes = 0x04
file_notify_change_size = 0x08 file_notify_change_size = 0x08
file_notify_change_last_write = 0x10 file_notify_change_last_write = 0x10
file_notify_change_last_access = 0x20 file_notify_change_last_access = 0x20
file_notify_change_creation = 0x40 file_notify_change_creation = 0x40
file_notify_change_security = 0x80 file_notify_change_security = 0x80
) )
const ( const (
file_action_added = 0x01 file_action_added = 0x01
file_action_removed = 0x02 file_action_removed = 0x02
file_action_modified = 0x03 file_action_modified = 0x03
file_action_renamed_old_name = 0x04 file_action_renamed_old_name = 0x04
file_action_renamed_new_name = 0x05 file_action_renamed_new_name = 0x05
) )
const ( const (
file_attr_readonly = 0x00000001 file_attr_readonly = 0x00000001
file_attr_hidden = 0x00000002 file_attr_hidden = 0x00000002
file_attr_system = 0x00000004 file_attr_system = 0x00000004
file_attr_directory = 0x00000010 file_attr_directory = 0x00000010
file_attr_archive = 0x00000020 file_attr_archive = 0x00000020
file_attr_device = 0x00000040 file_attr_device = 0x00000040
file_attr_normal = 0x00000080 file_attr_normal = 0x00000080
file_attr_temporary = 0x00000100 file_attr_temporary = 0x00000100
file_attr_sparse_file = 0x00000200 file_attr_sparse_file = 0x00000200
file_attr_reparse_point = 0x00000400 file_attr_reparse_point = 0x00000400
file_attr_compressed = 0x00000800 file_attr_compressed = 0x00000800
file_attr_offline = 0x00001000 file_attr_offline = 0x00001000
file_attr_not_content_indexed = 0x00002000 file_attr_not_content_indexed = 0x00002000
file_attr_encrypted = 0x00004000 file_attr_encrypted = 0x00004000
file_attr_integrity_stream = 0x00008000 file_attr_integrity_stream = 0x00008000
file_attr_virtual = 0x00010000 file_attr_virtual = 0x00010000
file_attr_no_scrub_data = 0x00020000 file_attr_no_scrub_data = 0x00020000
// file_attr_recall_on_open = u32(0x...) // file_attr_recall_on_open = u32(0x...)
// file_attr_recall_on_data_access = u32(0x...) // file_attr_recall_on_data_access = u32(0x...)
) )
const ( const (
file_type_unknown = 0x00 file_type_unknown = 0x00
file_type_disk = 0x01 file_type_disk = 0x01
file_type_char = 0x02 file_type_char = 0x02
file_type_pipe = 0x03 file_type_pipe = 0x03
) )
const ( const (
file_invalid_file_id = (-1) file_invalid_file_id = (-1)
) )
const( const (
invalid_handle_value = voidptr(-1) invalid_handle_value = voidptr(-1)
) )
// https://docs.microsoft.com/en-us/windows/console/setconsolemode // https://docs.microsoft.com/en-us/windows/console/setconsolemode
const ( const (
// Input Buffer // Input Buffer
enable_echo_input = 0x0004 enable_echo_input = 0x0004
enable_extended_flags = 0x0080 enable_extended_flags = 0x0080
enable_insert_mode = 0x0020 enable_insert_mode = 0x0020
enable_line_input = 0x0002 enable_line_input = 0x0002
enable_mouse_input = 0x0010 enable_mouse_input = 0x0010
enable_processed_input = 0x0001 enable_processed_input = 0x0001
enable_quick_edit_mode = 0x0040 enable_quick_edit_mode = 0x0040
enable_window_input = 0x0008 enable_window_input = 0x0008
enable_virtual_terminal_input = 0x0200 enable_virtual_terminal_input = 0x0200
// Output Screen Buffer // Output Screen Buffer
enable_processed_output = 0x01 enable_processed_output = 0x01
enable_wrap_at_eol_output = 0x02 enable_wrap_at_eol_output = 0x02
enable_virtual_terminal_processing = 0x04 enable_virtual_terminal_processing = 0x04
disable_newline_auto_return = 0x08 disable_newline_auto_return = 0x08
enable_lvb_grid_worldwide = 0x10 enable_lvb_grid_worldwide = 0x10
) )
// File modes // File modes
const ( const (
o_rdonly = 0x0000 // open the file read-only. o_rdonly = 0x0000 // open the file read-only.
o_wronly = 0x0001 // open the file write-only. o_wronly = 0x0001 // open the file write-only.
o_rdwr = 0x0002 // open the file read-write. o_rdwr = 0x0002 // open the file read-write.
o_append = 0x0008 // append data to the file when writing. o_append = 0x0008 // append data to the file when writing.
o_create = 0x0100 // create a new file if none exists. o_create = 0x0100 // create a new file if none exists.
o_binary = 0x8000 // input and output is not translated. o_binary = 0x8000 // input and output is not translated.
o_trunc = 0x0200 // truncate regular writable file when opened. o_trunc = 0x0200 // truncate regular writable file when opened.
o_excl = 0x0400 // used with o_create, file must not exist. o_excl = 0x0400 // used with o_create, file must not exist.
o_sync = 0x0000 // open for synchronous I/O (ignored on Windows) o_sync = 0x0000 // open for synchronous I/O (ignored on Windows)
o_noctty = 0x0000 // make file non-controlling tty (ignored on Windows) o_noctty = 0x0000 // make file non-controlling tty (ignored on Windows)
o_nonblock = 0x0000 // don't block on opening file (ignored on Windows) o_nonblock = 0x0000 // don't block on opening file (ignored on Windows)
) )
const ( const (
status_access_violation = 0xC0000005 status_access_violation = 0xC0000005
status_in_page_error = 0xC0000006 status_in_page_error = 0xC0000006
status_invalid_handle = 0xC0000008 status_invalid_handle = 0xC0000008
status_invalid_parameter = 0xC000000D status_invalid_parameter = 0xC000000D
status_no_memory = 0xC0000017 status_no_memory = 0xC0000017
status_illegal_instruction = 0xC000001D status_illegal_instruction = 0xC000001D
status_noncontinuable_exception = 0xC0000025 status_noncontinuable_exception = 0xC0000025
status_invalid_disposition = 0xC0000026 status_invalid_disposition = 0xC0000026
status_array_bounds_exceeded = 0xC000008C status_array_bounds_exceeded = 0xC000008C
status_float_denormal_operand = 0xC000008D status_float_denormal_operand = 0xC000008D
status_float_divide_by_zero = 0xC000008E status_float_divide_by_zero = 0xC000008E
status_float_inexact_result = 0xC000008F status_float_inexact_result = 0xC000008F
status_float_invalid_operation = 0xC0000090 status_float_invalid_operation = 0xC0000090
status_float_overflow = 0xC0000091 status_float_overflow = 0xC0000091
status_float_stack_check = 0xC0000092 status_float_stack_check = 0xC0000092
status_float_underflow = 0xC0000093 status_float_underflow = 0xC0000093
status_integer_divide_by_zero = 0xC0000094 status_integer_divide_by_zero = 0xC0000094
status_integer_overflow = 0xC0000095 status_integer_overflow = 0xC0000095
status_privileged_instruction = 0xC0000096 status_privileged_instruction = 0xC0000096
status_stack_overflow = 0xC00000FD status_stack_overflow = 0xC00000FD
status_dll_not_found = 0xC0000135 status_dll_not_found = 0xC0000135
status_ordinal_not_found = 0xC0000138 status_ordinal_not_found = 0xC0000138
status_entrypoint_not_found = 0xC0000139 status_entrypoint_not_found = 0xC0000139
status_control_c_exit = 0xC000013A status_control_c_exit = 0xC000013A
status_dll_init_failed = 0xC0000142 status_dll_init_failed = 0xC0000142
status_float_multiple_faults = 0xC00002B4 status_float_multiple_faults = 0xC00002B4
status_float_multiple_traps = 0xC00002B5 status_float_multiple_traps = 0xC00002B5
status_reg_nat_consumption = 0xC00002C9 status_reg_nat_consumption = 0xC00002C9
status_heap_corruption = 0xC0000374 status_heap_corruption = 0xC0000374
status_stack_buffer_overrun = 0xC0000409 status_stack_buffer_overrun = 0xC0000409
status_invalid_cruntime_parameter = 0xC0000417 status_invalid_cruntime_parameter = 0xC0000417
status_assertion_failure = 0xC0000420 status_assertion_failure = 0xC0000420
) )
// Windows Registry Constants // Windows Registry Constants
pub const ( pub const (
hkey_local_machine = voidptr(0x80000002) hkey_local_machine = voidptr(0x80000002)
hkey_current_user = voidptr(0x80000001) hkey_current_user = voidptr(0x80000001)
key_query_value = 0x0001
key_query_value = 0x0001 key_set_value = 0x0002
key_set_value = 0x0002 key_enumerate_sub_keys = 0x0008
key_enumerate_sub_keys = 0x0008 key_wow64_32key = 0x0200
key_wow64_32key = 0x0200
) )
// Windows Messages // Windows Messages
pub const ( pub const (
hwnd_broadcast = voidptr(0xFFFF) hwnd_broadcast = voidptr(0xFFFF)
wm_settingchange = 0x001A
wm_settingchange = 0x001A smto_abortifhung = 0x0002
smto_abortifhung = 0x0002
) )

View File

@ -4,11 +4,12 @@
module os module os
fn C.getenv(charptr) &char fn C.getenv(charptr) &char
// C.GetEnvironmentStringsW & C.FreeEnvironmentStringsW are defined only on windows // C.GetEnvironmentStringsW & C.FreeEnvironmentStringsW are defined only on windows
fn C.GetEnvironmentStringsW() &u16 fn C.GetEnvironmentStringsW() &u16
fn C.FreeEnvironmentStringsW(&u16) int fn C.FreeEnvironmentStringsW(&u16) int
// `getenv` returns the value of the environment variable named by the key. // `getenv` returns the value of the environment variable named by the key.
pub fn getenv(key string) string { pub fn getenv(key string) string {
$if windows { $if windows {
@ -53,7 +54,7 @@ pub fn setenv(name string, value string, overwrite bool) int {
// os.unsetenv clears an environment variable with `name`. // os.unsetenv clears an environment variable with `name`.
pub fn unsetenv(name string) int { pub fn unsetenv(name string) int {
$if windows { $if windows {
format := '${name}=' format := '$name='
return C._putenv(charptr(format.str)) return C._putenv(charptr(format.str))
} $else { } $else {
return C.unsetenv(charptr(name.str)) return C.unsetenv(charptr(name.str))
@ -64,7 +65,7 @@ pub fn unsetenv(name string) int {
// See: https://docs.microsoft.com/bg-bg/windows/win32/api/processenv/nf-processenv-getenvironmentstrings // See: https://docs.microsoft.com/bg-bg/windows/win32/api/processenv/nf-processenv-getenvironmentstrings
// os.environ returns a map of all the current environment variables // os.environ returns a map of all the current environment variables
pub fn environ() map[string]string { pub fn environ() map[string]string {
mut res := map[string]string mut res := map[string]string{}
$if windows { $if windows {
mut estrings := C.GetEnvironmentStringsW() mut estrings := C.GetEnvironmentStringsW()
mut eline := '' mut eline := ''
@ -74,8 +75,7 @@ pub fn environ() map[string]string {
if eq_index > 0 { if eq_index > 0 {
res[eline[0..eq_index]] = eline[eq_index + 1..] res[eline[0..eq_index]] = eline[eq_index + 1..]
} }
unsafe unsafe {
{
c = c + eline.len + 1 c = c + eline.len + 1
} }
} }

View File

@ -36,13 +36,13 @@ fn test_environ() {
assert all['myvar_not_defined'] == '' assert all['myvar_not_defined'] == ''
} }
fn test_setenv_var_not_exists(){ fn test_setenv_var_not_exists() {
key := time.new_time(time.now()).unix key := time.new_time(time.now()).unix
os.setenv('foo$key', 'bar', false) os.setenv('foo$key', 'bar', false)
assert os.getenv('foo$key') == 'bar' assert os.getenv('foo$key') == 'bar'
} }
fn test_getenv_empty_var(){ fn test_getenv_empty_var() {
key := time.new_time(time.now()).unix key := time.new_time(time.now()).unix
os.setenv('empty$key', '""', false) os.setenv('empty$key', '""', false)
assert os.getenv('empty$key') == '""' assert os.getenv('empty$key') == '""'

View File

@ -15,16 +15,16 @@ enum FileType {
struct FilePermission { struct FilePermission {
pub: pub:
read bool read bool
write bool write bool
execute bool execute bool
} }
struct FileMode { struct FileMode {
pub: pub:
typ FileType typ FileType
owner FilePermission owner FilePermission
group FilePermission group FilePermission
others FilePermission others FilePermission
} }
@ -32,10 +32,7 @@ pub:
// it supports windows for regular files but it doesn't matter if you use owner, group or others when checking permissions on windows // it supports windows for regular files but it doesn't matter if you use owner, group or others when checking permissions on windows
pub fn inode(path string) FileMode { pub fn inode(path string) FileMode {
mut attr := C.stat{} mut attr := C.stat{}
unsafe { unsafe {C.stat(charptr(path.str), &attr)}
C.stat(charptr(path.str), &attr)
}
mut typ := FileType.regular mut typ := FileType.regular
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFDIR) { if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFDIR) {
typ = .directory typ = .directory
@ -53,7 +50,6 @@ pub fn inode(path string) FileMode {
typ = .socket typ = .socket
} }
} }
$if windows { $if windows {
return FileMode{ return FileMode{
typ: typ typ: typ

View File

@ -24,9 +24,7 @@ fn testsuite_end() {
fn test_inode_file_type() { fn test_inode_file_type() {
filename := './test1.txt' filename := './test1.txt'
mut file := os.open_file(filename, 'w', 0o600) or { mut file := os.open_file(filename, 'w', 0o600) or { return }
return
}
file.close() file.close()
mode := os.inode(filename) mode := os.inode(filename)
os.rm(filename) os.rm(filename)
@ -35,9 +33,7 @@ fn test_inode_file_type() {
fn test_inode_file_owner_permission() { fn test_inode_file_owner_permission() {
filename := './test2.txt' filename := './test2.txt'
mut file := os.open_file(filename, 'w', 0o600) or { mut file := os.open_file(filename, 'w', 0o600) or { return }
return
}
file.close() file.close()
mode := os.inode(filename) mode := os.inode(filename)
os.rm(filename) os.rm(filename)

View File

@ -11,8 +11,8 @@ pub:
} }
pub const ( pub const (
args = []string{} args = []string{}
max_path_len = 4096 max_path_len = 4096
wd_at_startup = getwd() wd_at_startup = getwd()
) )

View File

@ -202,7 +202,6 @@ pub fn fileno(cfile voidptr) int {
} }
} }
// open_append opens `path` file for appending. // open_append opens `path` file for appending.
pub fn open_append(path string) ?File { pub fn open_append(path string) ?File {
mut file := File{} mut file := File{}

View File

@ -1,18 +1,14 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module os module os
pub const ( pub const (
sys_write = 4 sys_write = 4
sys_open = 5 sys_open = 5
sys_close = 6 sys_close = 6
sys_mkdir = 136 sys_mkdir = 136
sys_creat = 8 sys_creat = 8
sys_open_nocancel = 398 sys_open_nocancel = 398
sys_stat64 = 338 sys_stat64 = 338
) )

View File

@ -1,20 +1,18 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module os module os
const ( const (
prot_read = 1 prot_read = 1
prot_write = 2 prot_write = 2
map_private = 0x02
map_private = 0x02
map_anonymous = 0x20 map_anonymous = 0x20
) )
pub const ( pub const (
sys_write = 1 sys_write = 1
sys_open = 2 sys_open = 2
sys_close = 3 sys_close = 3
sys_mkdir = 83 sys_mkdir = 83
sys_creat = 85 sys_creat = 85

View File

@ -52,9 +52,7 @@ fn init_os_args(argc int, argv &&byte) []string {
// mut args := []string{len:argc} // mut args := []string{len:argc}
for i in 0 .. argc { for i in 0 .. argc {
// args [i] = argv[i].vstring() // args [i] = argv[i].vstring()
unsafe { unsafe {args << byteptr(argv[i]).vstring()}
args << byteptr(argv[i]).vstring()
}
} }
return args return args
} }
@ -110,8 +108,8 @@ pub fn mkdir(path string) ?bool {
} }
*/ */
apath := real_path(path) apath := real_path(path)
//defer { // defer {
//apath.free() // apath.free()
//} //}
/* /*
$if linux { $if linux {
@ -124,10 +122,7 @@ pub fn mkdir(path string) ?bool {
} }
} }
*/ */
r := unsafe { r := unsafe {C.mkdir(charptr(apath.str), 511)}
C.mkdir(charptr(apath.str), 511)
}
if r == -1 { if r == -1 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }
@ -162,20 +157,19 @@ pub fn exec(cmd string) ?Result {
} }
} }
pub struct Command { pub struct Command {
mut: mut:
f voidptr f voidptr
pub mut: pub mut:
eof bool eof bool
pub: pub:
path string path string
redirect_stdout bool redirect_stdout bool
} }
//pub fn command(cmd Command) Command { // pub fn command(cmd Command) Command {
//} //}
pub fn (mut c Command) start() ? {
pub fn (mut c Command) start()? {
pcmd := '$c.path 2>&1' pcmd := '$c.path 2>&1'
c.f = vpopen(pcmd) c.f = vpopen(pcmd)
if isnil(c.f) { if isnil(c.f) {
@ -190,7 +184,7 @@ pub fn (mut c Command) read_line() string {
for C.fgets(charptr(buf), 4096, c.f) != 0 { for C.fgets(charptr(buf), 4096, c.f) != 0 {
bufbp := byteptr(buf) bufbp := byteptr(buf)
len := vstrlen(bufbp) len := vstrlen(bufbp)
for i in 0..len { for i in 0 .. len {
if int(bufbp[i]) == `\n` { if int(bufbp[i]) == `\n` {
res.write_bytes(bufbp, i) res.write_bytes(bufbp, i)
return res.str() return res.str()
@ -203,7 +197,7 @@ pub fn (mut c Command) read_line() string {
return res.str() return res.str()
} }
pub fn (c &Command) close()? { pub fn (c &Command) close() ? {
exit_code := vpclose(c.f) exit_code := vpclose(c.f)
if exit_code == 127 { if exit_code == 127 {
return error_with_code('error', 127) return error_with_code('error', 127)
@ -245,15 +239,16 @@ pub fn debugger_present() bool {
} }
fn C.mkstemp(stemplate byteptr) int fn C.mkstemp(stemplate byteptr) int
// `is_writable_folder` - `folder` exists and is writable to the process // `is_writable_folder` - `folder` exists and is writable to the process
pub fn is_writable_folder(folder string) ?bool { pub fn is_writable_folder(folder string) ?bool {
if !os.exists(folder) { if !exists(folder) {
return error('`$folder` does not exist') return error('`$folder` does not exist')
} }
if !os.is_dir(folder) { if !is_dir(folder) {
return error('`folder` is not a folder') return error('`folder` is not a folder')
} }
tmp_perm_check := os.join_path(folder, 'XXXXXX') tmp_perm_check := join_path(folder, 'XXXXXX')
unsafe { unsafe {
x := C.mkstemp(charptr(tmp_perm_check.str)) x := C.mkstemp(charptr(tmp_perm_check.str))
if -1 == x { if -1 == x {
@ -261,7 +256,7 @@ pub fn is_writable_folder(folder string) ?bool {
} }
C.close(x) C.close(x)
} }
os.rm(tmp_perm_check) rm(tmp_perm_check)
return true return true
} }

View File

@ -30,15 +30,11 @@ fn test_open_file() {
assert err == 'No such file or directory' assert err == 'No such file or directory'
os.File{} os.File{}
} }
mut file := os.open_file(filename, 'w+', 0o666) or { mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) }
panic(err)
}
file.write_str(hello) file.write_str(hello)
file.close() file.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or { read_hello := os.read_file(filename) or { panic('error reading file $filename') }
panic('error reading file $filename')
}
assert hello == read_hello assert hello == read_hello
os.rm(filename) os.rm(filename)
} }
@ -50,16 +46,12 @@ fn test_open_file_binary() {
assert err == 'No such file or directory' assert err == 'No such file or directory'
os.File{} os.File{}
} }
mut file := os.open_file(filename, 'wb+', 0o666) or { mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
panic(err)
}
bytes := hello.bytes() bytes := hello.bytes()
file.write_bytes(bytes.data, bytes.len) file.write_bytes(bytes.data, bytes.len)
file.close() file.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_bytes(filename) or { read_hello := os.read_bytes(filename) or { panic('error reading file $filename') }
panic('error reading file $filename')
}
assert bytes == read_hello assert bytes == read_hello
os.rm(filename) os.rm(filename)
} }
@ -84,13 +76,10 @@ fn test_open_file_binary() {
// assert line1 == 'line 1\n' // assert line1 == 'line 1\n'
// assert line2 == 'line 2' // assert line2 == 'line 2'
// } // }
fn test_create_file() { fn test_create_file() {
filename := './test1.txt' filename := './test1.txt'
hello := 'hello world!' hello := 'hello world!'
mut f := os.create(filename) or { mut f := os.create(filename) or { panic(err) }
panic(err)
}
f.write_str(hello) f.write_str(hello)
f.close() f.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
@ -132,9 +121,7 @@ fn test_write_and_read_string_to_file() {
hello := 'hello world!' hello := 'hello world!'
os.write_file(filename, hello) os.write_file(filename, hello)
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or { read_hello := os.read_file(filename) or { panic('error reading file $filename') }
panic('error reading file $filename')
}
assert hello == read_hello assert hello == read_hello
os.rm(filename) os.rm(filename)
} }
@ -177,13 +164,9 @@ fn test_write_and_read_bytes() {
fn test_create_and_delete_folder() { fn test_create_and_delete_folder() {
folder := './test1' folder := './test1'
os.mkdir(folder) or { os.mkdir(folder) or { panic(err) }
panic(err)
}
assert os.is_dir(folder) assert os.is_dir(folder)
folder_contents := os.ls(folder) or { folder_contents := os.ls(folder) or { panic(err) }
panic(err)
}
assert folder_contents.len == 0 assert folder_contents.len == 0
os.rmdir(folder) os.rmdir(folder)
folder_exists := os.is_dir(folder) folder_exists := os.is_dir(folder)
@ -199,9 +182,7 @@ fn walk_callback(file string) {
fn test_walk() { fn test_walk() {
folder := 'test_walk' folder := 'test_walk'
os.mkdir(folder) or { os.mkdir(folder) or { panic(err) }
panic(err)
}
file1 := folder + os.path_separator + 'test1' file1 := folder + os.path_separator + 'test1'
os.write_file(file1, 'test-1') os.write_file(file1, 'test-1')
os.walk(folder, walk_callback) os.walk(folder, walk_callback)
@ -213,15 +194,9 @@ fn test_cp() {
old_file_name := 'cp_example.txt' old_file_name := 'cp_example.txt'
new_file_name := 'cp_new_example.txt' new_file_name := 'cp_new_example.txt'
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~') os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~')
os.cp(old_file_name, new_file_name) or { os.cp(old_file_name, new_file_name) or { panic('$err: errcode: $errcode') }
panic('$err: errcode: $errcode') old_file := os.read_file(old_file_name) or { panic(err) }
} new_file := os.read_file(new_file_name) or { panic(err) }
old_file := os.read_file(old_file_name) or {
panic(err)
}
new_file := os.read_file(new_file_name) or {
panic(err)
}
assert old_file == new_file assert old_file == new_file
os.rm(old_file_name) os.rm(old_file_name)
os.rm(new_file_name) os.rm(new_file_name)
@ -272,37 +247,19 @@ fn test_cp_r() {
// fileX -> dir/fileX // fileX -> dir/fileX
// NB: clean up of the files happens inside the cleanup_leftovers function // NB: clean up of the files happens inside the cleanup_leftovers function
os.write_file('ex1.txt', 'wow!') os.write_file('ex1.txt', 'wow!')
os.mkdir('ex') or { os.mkdir('ex') or { panic(err) }
panic(err) os.cp_all('ex1.txt', 'ex', false) or { panic(err) }
} old := os.read_file('ex1.txt') or { panic(err) }
os.cp_all('ex1.txt', 'ex', false) or { new := os.read_file('ex/ex1.txt') or { panic(err) }
panic(err)
}
old := os.read_file('ex1.txt') or {
panic(err)
}
new := os.read_file('ex/ex1.txt') or {
panic(err)
}
assert old == new assert old == new
os.mkdir('ex/ex2') or { os.mkdir('ex/ex2') or { panic(err) }
panic(err)
}
os.write_file('ex2.txt', 'great!') os.write_file('ex2.txt', 'great!')
os.cp_all('ex2.txt', 'ex/ex2', false) or { os.cp_all('ex2.txt', 'ex/ex2', false) or { panic(err) }
panic(err) old2 := os.read_file('ex2.txt') or { panic(err) }
} new2 := os.read_file('ex/ex2/ex2.txt') or { panic(err) }
old2 := os.read_file('ex2.txt') or {
panic(err)
}
new2 := os.read_file('ex/ex2/ex2.txt') or {
panic(err)
}
assert old2 == new2 assert old2 == new2
// recurring on dir -> local dir // recurring on dir -> local dir
os.cp_all('ex', './', true) or { os.cp_all('ex', './', true) or { panic(err) }
panic(err)
}
} }
fn test_tmpdir() { fn test_tmpdir() {
@ -313,9 +270,7 @@ fn test_tmpdir() {
os.rm(tfile) // just in case os.rm(tfile) // just in case
tfile_content := 'this is a temporary file' tfile_content := 'this is a temporary file'
os.write_file(tfile, tfile_content) os.write_file(tfile, tfile_content)
tfile_content_read := os.read_file(tfile) or { tfile_content_read := os.read_file(tfile) or { panic(err) }
panic(err)
}
assert tfile_content_read == tfile_content assert tfile_content_read == tfile_content
os.rm(tfile) os.rm(tfile)
} }
@ -339,12 +294,8 @@ fn test_make_symlink_check_is_link_and_remove_symlink() {
symlink := 'tsymlink' symlink := 'tsymlink'
os.rm(symlink) os.rm(symlink)
os.rm(folder) os.rm(folder)
os.mkdir(folder) or { os.mkdir(folder) or { panic(err) }
panic(err) folder_contents := os.ls(folder) or { panic(err) }
}
folder_contents := os.ls(folder) or {
panic(err)
}
assert folder_contents.len == 0 assert folder_contents.len == 0
os.system('ln -s $folder $symlink') os.system('ln -s $folder $symlink')
assert os.is_link(symlink) == true assert os.is_link(symlink) == true
@ -381,12 +332,8 @@ fn test_symlink() {
$if windows { $if windows {
return return
} }
os.mkdir('symlink') or { os.mkdir('symlink') or { panic(err) }
panic(err) os.symlink('symlink', 'symlink2') or { panic(err) }
}
os.symlink('symlink', 'symlink2') or {
panic(err)
}
assert os.exists('symlink2') assert os.exists('symlink2')
// cleanup // cleanup
os.rm('symlink') os.rm('symlink')
@ -485,9 +432,7 @@ fn test_write_file_array_bytes() {
arr[i] = 65 + byte(i) arr[i] = 65 + byte(i)
} }
os.write_file_array(fpath, arr) os.write_file_array(fpath, arr)
rarr := os.read_bytes(fpath) or { rarr := os.read_bytes(fpath) or { panic(err) }
panic(err)
}
assert arr == rarr assert arr == rarr
// eprintln(arr.str()) // eprintln(arr.str())
// eprintln(rarr.str()) // eprintln(rarr.str())
@ -508,7 +453,7 @@ fn test_write_file_array_structs() {
fn test_stdout_capture() { fn test_stdout_capture() {
/* /*
mut cmd := os.Command{ mut cmd := os.Command{
path:'cat' path:'cat'
redirect_stdout: true redirect_stdout: true
} }
@ -518,5 +463,5 @@ for !cmd.eof {
println('line="$line"') println('line="$line"')
} }
cmd.close() cmd.close()
*/ */
} }

View File

@ -143,9 +143,7 @@ pub fn mkdir(path string) ?bool {
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019 // 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. // get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
pub fn get_file_handle(path string) HANDLE { pub fn get_file_handle(path string) HANDLE {
cfile := vfopen(path, 'rb') or { cfile := vfopen(path, 'rb') or { return HANDLE(invalid_handle_value) }
return HANDLE(invalid_handle_value)
}
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_- handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_-
return handle return handle
} }
@ -341,7 +339,7 @@ pub:
context_record &ContextRecord context_record &ContextRecord
} }
pub type VectoredExceptionHandler = fn ( &ExceptionPointers) u32 pub type VectoredExceptionHandler = fn (&ExceptionPointers) u32
// This is defined in builtin because we use vectored exception handling // This is defined in builtin because we use vectored exception handling
// for our unhandled exception handler on windows // for our unhandled exception handler on windows