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

os: fix open_file() on windows (fix #18245) (#18253)

This commit is contained in:
yuyi 2023-05-25 08:50:52 +08:00 committed by GitHub
parent 351b2e0e42
commit 190f5c69ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -101,7 +101,12 @@ pub fn open_file(path string, mode string, options ...int) !File {
}
}
p := fix_windows_path(path)
fd := C.open(&char(p.str), flags, permission)
mut fd := 0
$if windows {
fd = C._wopen(p.to_wide(), flags, permission)
} $else {
fd = C.open(&char(p.str), flags, permission)
}
if fd == -1 {
return error(posix_get_error_msg(C.errno))
}

View File

@ -418,7 +418,7 @@ fn test_open_file_wb_ab() {
mut afile := os.open_file('text.txt', 'ab', 0o666)!
afile.write_string('hello')!
afile.close()
assert os.read_file('text.txt')? == 'hellohello'
assert os.read_file('text.txt')! == 'hellohello'
}
fn test_open_append() {
@ -438,3 +438,14 @@ fn test_open_append() {
f3.close()
assert os.read_lines(tfile)! == ['abc', 'abc', 'def']
}
fn test_open_file_on_chinese_windows() {
$if windows {
os.rm('.txt') or {}
mut f1 := os.open_file('.txt', 'w+', 0x666) or { panic(err) }
f1.write_string('test')!
f1.close()
assert os.read_file('.txt')! == 'test'
}
}

View File

@ -19,6 +19,8 @@ fn C.sigaction(int, voidptr, int) int
fn C.open(&char, int, ...int) int
fn C._wopen(&u16, int, ...int) int
fn C.fdopen(fd int, mode &char) &C.FILE
fn C.ferror(stream &C.FILE) int