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

os: fix os.is_link and os.symlink on windows, add new functions os.getppid, os.getgid, os.getegid (#10251)

This commit is contained in:
Bastian Buck
2021-05-29 22:26:13 +02:00
committed by GitHub
parent e4f6369cd1
commit d6e462a6ca
4 changed files with 181 additions and 37 deletions

View File

@ -728,7 +728,9 @@ pub fn is_dir(path string) bool {
// is_link returns a boolean indicating whether `path` is a link.
pub fn is_link(path string) bool {
$if windows {
return false // TODO
path_ := path.replace('/', '\\')
attr := C.GetFileAttributesW(path_.to_wide())
return int(attr) != int(C.INVALID_FILE_ATTRIBUTES) && (attr & 0x400) != 0
} $else {
statbuf := C.stat{}
if C.lstat(&char(path.str), &statbuf) != 0 {
@ -784,26 +786,45 @@ pub fn real_path(fpath string) string {
defer {
unsafe { free(fullpath) }
}
mut res := ''
$if windows {
/*
// GetFullPathName doesn't work with symbolic links
// TODO: TCC32 gets runtime error
max := 512
size := max * 2 // max_path_len * sizeof(wchar_t)
// gets handle with GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
// use get_file_handle instead of C.CreateFile(fpath.to_wide(), 0x80000000, 1, 0, 3, 0x80, 0)
// try to open the file to get symbolic link path
file := get_file_handle(fpath)
if file != voidptr(-1) {
fullpath = unsafe { &u16(vcalloc(size)) }
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
final_len := C.GetFinalPathNameByHandleW(file, fullpath, size, 0)
if final_len < size {
ret := unsafe { string_from_wide2(fullpath, final_len) }
res = ret[4..]
} else {
eprintln('os.real_path() saw that the file path was too long')
}
} else {
*/
// if it is not a file get full path
fullpath = unsafe { &u16(vcalloc(max_path_len * 2)) }
// TODO: check errors if path len is not enough
ret := C.GetFullPathName(fpath.to_wide(), max_path_len, fullpath, 0)
if ret == 0 {
return fpath
}
res = unsafe { string_from_wide(fullpath) }
//}
// C.CloseHandle(file)
} $else {
fullpath = vcalloc(max_path_len)
ret := &char(C.realpath(&char(fpath.str), &char(fullpath)))
if ret == 0 {
return fpath
}
}
mut res := ''
$if windows {
res = unsafe { string_from_wide(fullpath) }
} $else {
res = unsafe { fullpath.vstring() }
}
nres := normalize_drive_letter(res)