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,6 +1,6 @@
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

View File

@ -144,11 +144,9 @@ const (
) )
// 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
@ -156,10 +154,8 @@ pub const (
) )
// 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

@ -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

@ -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,7 +1,6 @@
// 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 (
@ -13,6 +12,3 @@ pub const (
sys_open_nocancel = 398 sys_open_nocancel = 398
sys_stat64 = 338 sys_stat64 = 338
) )

View File

@ -1,13 +1,11 @@
// 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
) )

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
} }
@ -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))
} }
@ -174,7 +169,6 @@ pub:
// 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)
@ -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())

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
} }