mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: replace dir_exists with is_dir; file_exists() => exists()
This commit is contained in:
42
vlib/os/os.v
42
vlib/os/os.v
@ -164,7 +164,7 @@ pub fn cp(old, new string) ?bool {
|
||||
pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
|
||||
source_path := os.realpath( osource_path )
|
||||
dest_path := os.realpath( odest_path )
|
||||
if !os.file_exists(source_path) {
|
||||
if !os.exists(source_path) {
|
||||
return error('Source path doesn\'t exist')
|
||||
}
|
||||
//single file copy
|
||||
@ -174,7 +174,7 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
|
||||
} else {
|
||||
dest_path
|
||||
}
|
||||
if os.file_exists(adjasted_path) {
|
||||
if os.exists(adjasted_path) {
|
||||
if overwrite {
|
||||
os.rm(adjasted_path)
|
||||
}
|
||||
@ -523,16 +523,21 @@ pub fn unsetenv(name string) int {
|
||||
}
|
||||
}
|
||||
|
||||
// file_exists returns true if `path` exists.
|
||||
pub fn file_exists(_path string) bool {
|
||||
// exists returns true if `path` exists.
|
||||
pub fn exists(path string) bool {
|
||||
$if windows {
|
||||
path := _path.replace('/', '\\')
|
||||
return C._waccess(path.to_wide(), 0) != -1
|
||||
p := path.replace('/', '\\')
|
||||
return C._waccess(p.to_wide(), 0) != -1
|
||||
} $else {
|
||||
return C.access(_path.str, 0 ) != -1
|
||||
return C.access(path.str, 0 ) != -1
|
||||
}
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn file_exists(_path string) bool {
|
||||
panic('use os.exists(path) instead of os.file_exists(path)')
|
||||
}
|
||||
|
||||
// rm removes file in `path`.
|
||||
pub fn rm(path string) {
|
||||
$if windows {
|
||||
@ -822,13 +827,24 @@ pub fn executable() string {
|
||||
return os.args[0]
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn dir_exists(path string) bool {
|
||||
panic('use os.is_dir()')
|
||||
//return false
|
||||
}
|
||||
|
||||
// is_dir returns a boolean indicating whether the given path is a directory.
|
||||
pub fn is_dir(path string) bool {
|
||||
$if windows {
|
||||
return dir_exists(path)
|
||||
//val := int(C.GetFileAttributes(path.to_wide()))
|
||||
// Note: this return is broke (wrong). we have dir_exists already how will this differ?
|
||||
//return (val &FILE_ATTRIBUTE_DIRECTORY) > 0
|
||||
_path := path.replace('/', '\\')
|
||||
attr := C.GetFileAttributesW(_path.to_wide())
|
||||
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) {
|
||||
return false
|
||||
}
|
||||
if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
$else {
|
||||
statbuf := C.stat{}
|
||||
@ -927,7 +943,7 @@ pub fn walk(path string, fnc fn(path string)) {
|
||||
if os.is_dir(p) {
|
||||
walk(p, fnc)
|
||||
}
|
||||
else if os.file_exists(p) {
|
||||
else if os.exists(p) {
|
||||
fnc(p)
|
||||
}
|
||||
}
|
||||
@ -996,7 +1012,7 @@ pub fn mkdir_all(path string) {
|
||||
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
|
||||
for subdir in path.split(os.path_separator) {
|
||||
p += subdir + os.path_separator
|
||||
if !os.dir_exists(p) {
|
||||
if !os.is_dir(p) {
|
||||
os.mkdir(p) or { panic(err) }
|
||||
}
|
||||
}
|
||||
|
@ -47,12 +47,11 @@ pub fn ls(path string) ?[]string {
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn dir_exists(path string) bool {
|
||||
/*
|
||||
$if linux {
|
||||
C.syscall(4, path.str) // sys_newstat
|
||||
}
|
||||
*/
|
||||
/*
|
||||
pub fn is_dir(path string) bool {
|
||||
//$if linux {
|
||||
//C.syscall(4, path.str) // sys_newstat
|
||||
//}
|
||||
dir := C.opendir(path.str)
|
||||
res := !isnil(dir)
|
||||
if res {
|
||||
@ -60,6 +59,7 @@ pub fn dir_exists(path string) bool {
|
||||
}
|
||||
return res
|
||||
}
|
||||
*/
|
||||
|
||||
// mkdir creates a new directory with the specified path.
|
||||
pub fn mkdir(path string) ?bool {
|
||||
|
@ -76,14 +76,14 @@ fn test_write_and_read_bytes() {
|
||||
fn test_create_and_delete_folder() {
|
||||
folder := './test1'
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
assert os.dir_exists(folder)
|
||||
assert os.is_dir(folder)
|
||||
|
||||
folder_contents := os.ls(folder) or { panic(err) }
|
||||
assert folder_contents.len == 0
|
||||
|
||||
os.rmdir(folder)
|
||||
|
||||
folder_exists := os.dir_exists(folder)
|
||||
folder_exists := os.is_dir(folder)
|
||||
|
||||
assert folder_exists == false
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ mut:
|
||||
}
|
||||
|
||||
struct StartupInfo {
|
||||
mut:
|
||||
mut:
|
||||
cb u32
|
||||
lpReserved &u16
|
||||
lpDesktop &u16
|
||||
@ -89,13 +89,13 @@ fn init_os_args(argc int, argv &byteptr) []string {
|
||||
pub fn ls(path string) ?[]string {
|
||||
mut find_file_data := Win32finddata{}
|
||||
mut dir_files := []string
|
||||
// We can also check if the handle is valid. but using dir_exists instead
|
||||
// We can also check if the handle is valid. but using is_dir instead
|
||||
// h_find_dir := C.FindFirstFile(path.str, &find_file_data)
|
||||
// if (INVALID_HANDLE_VALUE == h_find_dir) {
|
||||
// return dir_files
|
||||
// }
|
||||
// C.FindClose(h_find_dir)
|
||||
if !dir_exists(path) {
|
||||
if !is_dir(path) {
|
||||
return error('ls() couldnt open dir "$path": directory does not exist')
|
||||
}
|
||||
// NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR)
|
||||
@ -118,7 +118,8 @@ pub fn ls(path string) ?[]string {
|
||||
return dir_files
|
||||
}
|
||||
|
||||
pub fn dir_exists(path string) bool {
|
||||
/*
|
||||
pub fn is_dir(path string) bool {
|
||||
_path := path.replace('/', '\\')
|
||||
attr := C.GetFileAttributesW(_path.to_wide())
|
||||
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) {
|
||||
@ -129,6 +130,7 @@ pub fn dir_exists(path string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@ -250,7 +252,7 @@ pub fn exec(cmd string) ?Result {
|
||||
panic('exec failed (SetHandleInformation): $error_msg')
|
||||
}
|
||||
|
||||
proc_info := ProcessInformation{}
|
||||
proc_info := ProcessInformation{}
|
||||
mut start_info := StartupInfo{}
|
||||
start_info.cb = sizeof(C.PROCESS_INFORMATION)
|
||||
start_info.hStdInput = child_stdin
|
||||
@ -265,7 +267,7 @@ pub fn exec(cmd string) ?Result {
|
||||
return error('exec failed (CreateProcess): $error_msg')
|
||||
}
|
||||
C.CloseHandle(child_stdin)
|
||||
C.CloseHandle(child_stdout_write)
|
||||
C.CloseHandle(child_stdout_write)
|
||||
buf := [1000]byte
|
||||
mut bytes_read := u32(0)
|
||||
mut read_data := ''
|
||||
@ -274,7 +276,7 @@ pub fn exec(cmd string) ?Result {
|
||||
read_data += tos(buf, int(bytes_read))
|
||||
if readfile_result == false || int(bytes_read) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
read_data = read_data.trim_space()
|
||||
exit_code := u32(0)
|
||||
|
Reference in New Issue
Block a user