1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
Delyan Angelov
2020-12-16 11:02:36 +02:00
parent 525b521b4a
commit 88a8507dd8
14 changed files with 208 additions and 276 deletions

View File

@ -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
}
@ -110,8 +108,8 @@ pub fn mkdir(path string) ?bool {
}
*/
apath := real_path(path)
//defer {
//apath.free()
// defer {
// apath.free()
//}
/*
$if linux {
@ -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))
}
@ -162,20 +157,19 @@ pub fn exec(cmd string) ?Result {
}
}
pub struct Command {
pub struct Command {
mut:
f voidptr
f voidptr
pub mut:
eof bool
eof bool
pub:
path string
path string
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'
c.f = vpopen(pcmd)
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 {
bufbp := byteptr(buf)
len := vstrlen(bufbp)
for i in 0..len {
for i in 0 .. len {
if int(bufbp[i]) == `\n` {
res.write_bytes(bufbp, i)
return res.str()
@ -203,7 +197,7 @@ pub fn (mut c Command) read_line() string {
return res.str()
}
pub fn (c &Command) close()? {
pub fn (c &Command) close() ? {
exit_code := vpclose(c.f)
if exit_code == 127 {
return error_with_code('error', 127)
@ -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
}