os: clean up windows functions

pull/1639/head
Alexander Medvednikov 2019-08-17 16:17:43 +03:00
parent 7ed0438b04
commit 100bb7c54c
4 changed files with 68 additions and 66 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 96 KiB

View File

@ -404,7 +404,7 @@ pub fn unsetenv(name string) int {
} }
} }
// `file_exists` returns true if `path` exists. // file_exists returns true if `path` exists.
pub fn file_exists(_path string) bool { pub fn file_exists(_path string) bool {
$if windows { $if windows {
path := _path.replace('/', '\\') path := _path.replace('/', '\\')
@ -414,44 +414,6 @@ pub fn file_exists(_path string) bool {
} }
} }
pub fn dir_exists(path string) bool {
$if windows {
_path := path.replace('/', '\\')
attr := int(C.GetFileAttributes(_path.to_wide()))
if attr == C.INVALID_FILE_ATTRIBUTES {
return false
}
if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
return true
}
return false
}
$else {
dir := C.opendir(path.str)
res := !isnil(dir)
if res {
C.closedir(dir)
}
return res
}
}
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) {
$if windows {
_path := path.replace('/', '\\')
// Windows doesnt recursively create the folders
// so we need to help it out here
if _path.last_index('\\') != -1 {
mkdir(_path.all_before_last('\\'))
}
C.CreateDirectory(_path.to_wide(), 0)
}
$else {
C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO
}
}
// rm removes file in `path`. // rm removes file in `path`.
pub fn rm(path string) { pub fn rm(path string) {
$if windows { $if windows {
@ -766,33 +728,6 @@ pub fn getwd() string {
} }
} }
// win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
struct filetime {
dwLowDateTime u32
dwHighDateTime u32
}
// win: WIN32_FIND_DATA
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw
struct win32finddata {
mut:
dwFileAttributes u32
ftCreationTime filetime
ftLastAccessTime filetime
ftLastWriteTime filetime
nFileSizeHigh u32
nFileSizeLow u32
dwReserved0 u32
dwReserved1 u32
cFileName [260]u16 // MAX_PATH = 260
cAlternateFileName [14]u16 // 14
dwFileType u32
dwCreatorType u32
wFinderFlags u16
}
// walk_ext returns a recursive list of all file paths ending with `ext`. // walk_ext returns a recursive list of all file paths ending with `ext`.
pub fn walk_ext(path, ext string) []string { pub fn walk_ext(path, ext string) []string {
if !os.is_dir(path) { if !os.is_dir(path) {

View File

@ -39,3 +39,19 @@ pub fn ls(path string) []string {
C.closedir(dir) C.closedir(dir)
return res return res
} }
pub fn dir_exists(path string) bool {
dir := C.opendir(path.str)
res := !isnil(dir)
if res {
C.closedir(dir)
}
return res
}
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) {
C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO
}

View File

@ -11,6 +11,34 @@ const (
// A handle to an object. // A handle to an object.
type HANDLE voidptr type HANDLE voidptr
// win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
struct filetime {
dwLowDateTime u32
dwHighDateTime u32
}
// win: WIN32_FIND_DATA
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw
struct win32finddata {
mut:
dwFileAttributes u32
ftCreationTime filetime
ftLastAccessTime filetime
ftLastWriteTime filetime
nFileSizeHigh u32
nFileSizeLow u32
dwReserved0 u32
dwReserved1 u32
cFileName [260]u16 // MAX_PATH = 260
cAlternateFileName [14]u16 // 14
dwFileType u32
dwCreatorType u32
wFinderFlags u16
}
pub fn ls(path string) []string { pub fn ls(path string) []string {
mut find_file_data := win32finddata{} mut find_file_data := win32finddata{}
mut dir_files := []string mut dir_files := []string
@ -44,6 +72,29 @@ pub fn ls(path string) []string {
return dir_files return dir_files
} }
pub fn dir_exists(path string) bool {
_path := path.replace('/', '\\')
attr := int(C.GetFileAttributes(_path.to_wide()))
if attr == C.INVALID_FILE_ATTRIBUTES {
return false
}
if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
return true
}
return false
}
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) {
_path := path.replace('/', '\\')
// Windows doesnt recursively create the folders
// so we need to help it out here
if _path.last_index('\\') != -1 {
mkdir(_path.all_before_last('\\'))
}
C.CreateDirectory(_path.to_wide(), 0)
}
// 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 {