From 190f5c69ea9b8c84f89642de5e0592909244572b Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 25 May 2023 08:50:52 +0800 Subject: [PATCH] os: fix open_file() on windows (fix #18245) (#18253) --- vlib/os/file.c.v | 7 ++++++- vlib/os/file_test.v | 13 ++++++++++++- vlib/os/os.c.v | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 2bcedf12a7..a3cfd623e3 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -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)) } diff --git a/vlib/os/file_test.v b/vlib/os/file_test.v index 449dc940b8..e7f0e03707 100644 --- a/vlib/os/file_test.v +++ b/vlib/os/file_test.v @@ -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' + } +} diff --git a/vlib/os/os.c.v b/vlib/os/os.c.v index 92927e9eda..755cf40e2a 100644 --- a/vlib/os/os.c.v +++ b/vlib/os/os.c.v @@ -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