os: vfmt most of `os`, add it to `v test-cleancode`
parent
525b521b4a
commit
88a8507dd8
|
@ -18,7 +18,7 @@ const (
|
|||
'nonexistant',
|
||||
]
|
||||
vfmt_verify_list = [
|
||||
'cmd/tools/vdoc.v'
|
||||
'cmd/tools/vdoc.v',
|
||||
'cmd/v/v.v',
|
||||
'vlib/builtin/array.v',
|
||||
'vlib/builtin/map.v',
|
||||
|
@ -58,6 +58,19 @@ const (
|
|||
'vlib/v/vet/',
|
||||
'vlib/v/vmod/',
|
||||
'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',
|
||||
]
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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
|
||||
const (
|
||||
s_ifmt = 0xF000 // type of file
|
||||
|
|
|
@ -144,11 +144,9 @@ const (
|
|||
)
|
||||
|
||||
// Windows Registry Constants
|
||||
|
||||
pub const (
|
||||
hkey_local_machine = voidptr(0x80000002)
|
||||
hkey_current_user = voidptr(0x80000001)
|
||||
|
||||
key_query_value = 0x0001
|
||||
key_set_value = 0x0002
|
||||
key_enumerate_sub_keys = 0x0008
|
||||
|
@ -156,10 +154,8 @@ pub const (
|
|||
)
|
||||
|
||||
// Windows Messages
|
||||
|
||||
pub const (
|
||||
hwnd_broadcast = voidptr(0xFFFF)
|
||||
|
||||
wm_settingchange = 0x001A
|
||||
smto_abortifhung = 0x0002
|
||||
)
|
|
@ -4,11 +4,12 @@
|
|||
module os
|
||||
|
||||
fn C.getenv(charptr) &char
|
||||
|
||||
// C.GetEnvironmentStringsW & C.FreeEnvironmentStringsW are defined only on windows
|
||||
fn C.GetEnvironmentStringsW() &u16
|
||||
|
||||
|
||||
fn C.FreeEnvironmentStringsW(&u16) int
|
||||
|
||||
// `getenv` returns the value of the environment variable named by the key.
|
||||
pub fn getenv(key string) string {
|
||||
$if windows {
|
||||
|
@ -53,7 +54,7 @@ pub fn setenv(name string, value string, overwrite bool) int {
|
|||
// os.unsetenv clears an environment variable with `name`.
|
||||
pub fn unsetenv(name string) int {
|
||||
$if windows {
|
||||
format := '${name}='
|
||||
format := '$name='
|
||||
return C._putenv(charptr(format.str))
|
||||
} $else {
|
||||
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
|
||||
// os.environ returns a map of all the current environment variables
|
||||
pub fn environ() map[string]string {
|
||||
mut res := map[string]string
|
||||
mut res := map[string]string{}
|
||||
$if windows {
|
||||
mut estrings := C.GetEnvironmentStringsW()
|
||||
mut eline := ''
|
||||
|
@ -74,8 +75,7 @@ pub fn environ() map[string]string {
|
|||
if eq_index > 0 {
|
||||
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
unsafe {
|
||||
c = c + eline.len + 1
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
pub fn inode(path string) FileMode {
|
||||
mut attr := C.stat{}
|
||||
unsafe {
|
||||
C.stat(charptr(path.str), &attr)
|
||||
}
|
||||
|
||||
unsafe {C.stat(charptr(path.str), &attr)}
|
||||
mut typ := FileType.regular
|
||||
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFDIR) {
|
||||
typ = .directory
|
||||
|
@ -53,7 +50,6 @@ pub fn inode(path string) FileMode {
|
|||
typ = .socket
|
||||
}
|
||||
}
|
||||
|
||||
$if windows {
|
||||
return FileMode{
|
||||
typ: typ
|
||||
|
|
|
@ -24,9 +24,7 @@ fn testsuite_end() {
|
|||
|
||||
fn test_inode_file_type() {
|
||||
filename := './test1.txt'
|
||||
mut file := os.open_file(filename, 'w', 0o600) or {
|
||||
return
|
||||
}
|
||||
mut file := os.open_file(filename, 'w', 0o600) or { return }
|
||||
file.close()
|
||||
mode := os.inode(filename)
|
||||
os.rm(filename)
|
||||
|
@ -35,9 +33,7 @@ fn test_inode_file_type() {
|
|||
|
||||
fn test_inode_file_owner_permission() {
|
||||
filename := './test2.txt'
|
||||
mut file := os.open_file(filename, 'w', 0o600) or {
|
||||
return
|
||||
}
|
||||
mut file := os.open_file(filename, 'w', 0o600) or { return }
|
||||
file.close()
|
||||
mode := os.inode(filename)
|
||||
os.rm(filename)
|
||||
|
|
|
@ -202,7 +202,6 @@ pub fn fileno(cfile voidptr) int {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// open_append opens `path` file for appending.
|
||||
pub fn open_append(path string) ?File {
|
||||
mut file := File{}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module os
|
||||
|
||||
pub const (
|
||||
|
@ -13,6 +12,3 @@ pub const (
|
|||
sys_open_nocancel = 398
|
||||
sys_stat64 = 338
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module os
|
||||
|
||||
const (
|
||||
prot_read = 1
|
||||
prot_write = 2
|
||||
|
||||
map_private = 0x02
|
||||
map_anonymous = 0x20
|
||||
)
|
||||
|
|
|
@ -52,9 +52,7 @@ fn init_os_args(argc int, argv &&byte) []string {
|
|||
// mut args := []string{len:argc}
|
||||
for i in 0 .. argc {
|
||||
// args [i] = argv[i].vstring()
|
||||
unsafe {
|
||||
args << byteptr(argv[i]).vstring()
|
||||
}
|
||||
unsafe {args << byteptr(argv[i]).vstring()}
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
@ -124,10 +122,7 @@ pub fn mkdir(path string) ?bool {
|
|||
}
|
||||
}
|
||||
*/
|
||||
r := unsafe {
|
||||
C.mkdir(charptr(apath.str), 511)
|
||||
}
|
||||
|
||||
r := unsafe {C.mkdir(charptr(apath.str), 511)}
|
||||
if r == -1 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
|
@ -174,7 +169,6 @@ pub:
|
|||
|
||||
// pub fn command(cmd Command) Command {
|
||||
//}
|
||||
|
||||
pub fn (mut c Command) start() ? {
|
||||
pcmd := '$c.path 2>&1'
|
||||
c.f = vpopen(pcmd)
|
||||
|
@ -245,15 +239,16 @@ pub fn debugger_present() bool {
|
|||
}
|
||||
|
||||
fn C.mkstemp(stemplate byteptr) int
|
||||
|
||||
// `is_writable_folder` - `folder` exists and is writable to the process
|
||||
pub fn is_writable_folder(folder string) ?bool {
|
||||
if !os.exists(folder) {
|
||||
if !exists(folder) {
|
||||
return error('`$folder` does not exist')
|
||||
}
|
||||
if !os.is_dir(folder) {
|
||||
if !is_dir(folder) {
|
||||
return error('`folder` is not a folder')
|
||||
}
|
||||
tmp_perm_check := os.join_path(folder, 'XXXXXX')
|
||||
tmp_perm_check := join_path(folder, 'XXXXXX')
|
||||
unsafe {
|
||||
x := C.mkstemp(charptr(tmp_perm_check.str))
|
||||
if -1 == x {
|
||||
|
@ -261,7 +256,7 @@ pub fn is_writable_folder(folder string) ?bool {
|
|||
}
|
||||
C.close(x)
|
||||
}
|
||||
os.rm(tmp_perm_check)
|
||||
rm(tmp_perm_check)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -30,15 +30,11 @@ fn test_open_file() {
|
|||
assert err == 'No such file or directory'
|
||||
os.File{}
|
||||
}
|
||||
mut file := os.open_file(filename, 'w+', 0o666) or {
|
||||
panic(err)
|
||||
}
|
||||
mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) }
|
||||
file.write_str(hello)
|
||||
file.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_file(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
||||
assert hello == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
|
@ -50,16 +46,12 @@ fn test_open_file_binary() {
|
|||
assert err == 'No such file or directory'
|
||||
os.File{}
|
||||
}
|
||||
mut file := os.open_file(filename, 'wb+', 0o666) or {
|
||||
panic(err)
|
||||
}
|
||||
mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
|
||||
bytes := hello.bytes()
|
||||
file.write_bytes(bytes.data, bytes.len)
|
||||
file.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_bytes(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_bytes(filename) or { panic('error reading file $filename') }
|
||||
assert bytes == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
|
@ -84,13 +76,10 @@ fn test_open_file_binary() {
|
|||
// assert line1 == 'line 1\n'
|
||||
// assert line2 == 'line 2'
|
||||
// }
|
||||
|
||||
fn test_create_file() {
|
||||
filename := './test1.txt'
|
||||
hello := 'hello world!'
|
||||
mut f := os.create(filename) or {
|
||||
panic(err)
|
||||
}
|
||||
mut f := os.create(filename) or { panic(err) }
|
||||
f.write_str(hello)
|
||||
f.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
|
@ -132,9 +121,7 @@ fn test_write_and_read_string_to_file() {
|
|||
hello := 'hello world!'
|
||||
os.write_file(filename, hello)
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_file(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
||||
assert hello == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
|
@ -177,13 +164,9 @@ fn test_write_and_read_bytes() {
|
|||
|
||||
fn test_create_and_delete_folder() {
|
||||
folder := './test1'
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
assert os.is_dir(folder)
|
||||
folder_contents := os.ls(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
folder_contents := os.ls(folder) or { panic(err) }
|
||||
assert folder_contents.len == 0
|
||||
os.rmdir(folder)
|
||||
folder_exists := os.is_dir(folder)
|
||||
|
@ -199,9 +182,7 @@ fn walk_callback(file string) {
|
|||
|
||||
fn test_walk() {
|
||||
folder := 'test_walk'
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
file1 := folder + os.path_separator + 'test1'
|
||||
os.write_file(file1, 'test-1')
|
||||
os.walk(folder, walk_callback)
|
||||
|
@ -213,15 +194,9 @@ fn test_cp() {
|
|||
old_file_name := 'cp_example.txt'
|
||||
new_file_name := 'cp_new_example.txt'
|
||||
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~⭐')
|
||||
os.cp(old_file_name, new_file_name) or {
|
||||
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)
|
||||
}
|
||||
os.cp(old_file_name, new_file_name) or { 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) }
|
||||
assert old_file == new_file
|
||||
os.rm(old_file_name)
|
||||
os.rm(new_file_name)
|
||||
|
@ -272,37 +247,19 @@ fn test_cp_r() {
|
|||
// fileX -> dir/fileX
|
||||
// NB: clean up of the files happens inside the cleanup_leftovers function
|
||||
os.write_file('ex1.txt', 'wow!')
|
||||
os.mkdir('ex') or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex1.txt', 'ex', false) or {
|
||||
panic(err)
|
||||
}
|
||||
old := os.read_file('ex1.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
new := os.read_file('ex/ex1.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('ex') or { panic(err) }
|
||||
os.cp_all('ex1.txt', 'ex', false) or { panic(err) }
|
||||
old := os.read_file('ex1.txt') or { panic(err) }
|
||||
new := os.read_file('ex/ex1.txt') or { panic(err) }
|
||||
assert old == new
|
||||
os.mkdir('ex/ex2') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('ex/ex2') or { panic(err) }
|
||||
os.write_file('ex2.txt', 'great!')
|
||||
os.cp_all('ex2.txt', 'ex/ex2', false) or {
|
||||
panic(err)
|
||||
}
|
||||
old2 := os.read_file('ex2.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
new2 := os.read_file('ex/ex2/ex2.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex2.txt', 'ex/ex2', false) 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
|
||||
// recurring on dir -> local dir
|
||||
os.cp_all('ex', './', true) or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex', './', true) or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_tmpdir() {
|
||||
|
@ -313,9 +270,7 @@ fn test_tmpdir() {
|
|||
os.rm(tfile) // just in case
|
||||
tfile_content := 'this is a temporary file'
|
||||
os.write_file(tfile, tfile_content)
|
||||
tfile_content_read := os.read_file(tfile) or {
|
||||
panic(err)
|
||||
}
|
||||
tfile_content_read := os.read_file(tfile) or { panic(err) }
|
||||
assert tfile_content_read == tfile_content
|
||||
os.rm(tfile)
|
||||
}
|
||||
|
@ -339,12 +294,8 @@ fn test_make_symlink_check_is_link_and_remove_symlink() {
|
|||
symlink := 'tsymlink'
|
||||
os.rm(symlink)
|
||||
os.rm(folder)
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
folder_contents := os.ls(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
folder_contents := os.ls(folder) or { panic(err) }
|
||||
assert folder_contents.len == 0
|
||||
os.system('ln -s $folder $symlink')
|
||||
assert os.is_link(symlink) == true
|
||||
|
@ -381,12 +332,8 @@ fn test_symlink() {
|
|||
$if windows {
|
||||
return
|
||||
}
|
||||
os.mkdir('symlink') or {
|
||||
panic(err)
|
||||
}
|
||||
os.symlink('symlink', 'symlink2') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('symlink') or { panic(err) }
|
||||
os.symlink('symlink', 'symlink2') or { panic(err) }
|
||||
assert os.exists('symlink2')
|
||||
// cleanup
|
||||
os.rm('symlink')
|
||||
|
@ -485,9 +432,7 @@ fn test_write_file_array_bytes() {
|
|||
arr[i] = 65 + byte(i)
|
||||
}
|
||||
os.write_file_array(fpath, arr)
|
||||
rarr := os.read_bytes(fpath) or {
|
||||
panic(err)
|
||||
}
|
||||
rarr := os.read_bytes(fpath) or { panic(err) }
|
||||
assert arr == rarr
|
||||
// eprintln(arr.str())
|
||||
// eprintln(rarr.str())
|
||||
|
|
|
@ -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
|
||||
// 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 {
|
||||
cfile := vfopen(path, 'rb') or {
|
||||
return HANDLE(invalid_handle_value)
|
||||
}
|
||||
cfile := vfopen(path, 'rb') or { return HANDLE(invalid_handle_value) }
|
||||
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_-
|
||||
return handle
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue