os: fix os.real_path on Windows (#8822)

pull/8833/head
Stanislav Ershov 2021-02-19 14:20:06 +05:00 committed by GitHub
parent 745b40c0a3
commit 6a752512b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View File

@ -275,6 +275,8 @@ fn C._wgetcwd() int
fn C._fullpath() int fn C._fullpath() int
fn C.GetFullPathName(voidptr, u32, voidptr, voidptr) u32
fn C.GetCommandLine() voidptr fn C.GetCommandLine() voidptr
fn C.LocalFree() fn C.LocalFree()

View File

@ -744,23 +744,32 @@ pub fn getwd() string {
// NB: this particular rabbit hole is *deep* ... // NB: this particular rabbit hole is *deep* ...
[manualfree] [manualfree]
pub fn real_path(fpath string) string { pub fn real_path(fpath string) string {
mut fullpath := vcalloc(max_path_len) mut fullpath := byteptr(0)
defer { defer {
unsafe { free(fullpath) } unsafe { free(fullpath) }
} }
mut ret := charptr(0)
$if windows { $if windows {
ret = charptr(C._fullpath(charptr(fullpath), charptr(fpath.str), max_path_len)) 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 { if ret == 0 {
return fpath return fpath
} }
} $else { } $else {
ret = charptr(C.realpath(charptr(fpath.str), charptr(fullpath))) fullpath = vcalloc(max_path_len)
ret := charptr(C.realpath(charptr(fpath.str), charptr(fullpath)))
if ret == 0 { if ret == 0 {
return fpath return fpath
} }
} }
res := unsafe { fullpath.vstring() }
mut res := ''
$if windows {
res = unsafe { string_from_wide(fullpath) }
} $else {
res = unsafe { fullpath.vstring() }
}
nres := normalize_drive_letter(res) nres := normalize_drive_letter(res)
cres := nres.clone() cres := nres.clone()
return cres return cres

View File

@ -271,6 +271,10 @@ fn test_cp_all() {
os.cp_all('ex', './', true) or { panic(err) } os.cp_all('ex', './', true) or { panic(err) }
} }
fn test_realpath() {
assert os.real_path('') == ''
}
fn test_tmpdir() { fn test_tmpdir() {
t := os.temp_dir() t := os.temp_dir()
assert t.len > 0 assert t.len > 0