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

os: fix truncate() on windows (#18262)

This commit is contained in:
yuyi 2023-05-25 19:37:53 +08:00 committed by GitHub
parent bc88183318
commit caee3935a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View File

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

View File

@ -447,5 +447,9 @@ fn test_open_file_on_chinese_windows() {
f1.close()
assert os.read_file('.txt')! == 'test'
assert os.file_size('.txt') == 4
os.truncate('.txt', 2)!
assert os.file_size('.txt') == 2
}
}

View File

@ -147,13 +147,17 @@ pub fn read_file(path string) !string {
// truncate changes the size of the file located in `path` to `len`.
// Note that changing symbolic links on Windows only works as admin.
pub fn truncate(path string, len u64) ! {
fp := C.open(&char(path.str), o_wronly | o_trunc, 0)
defer {
C.close(fp)
fp := $if windows {
C._wopen(path.to_wide(), o_wronly | o_trunc, 0)
} $else {
C.open(&char(path.str), o_wronly | o_trunc, 0)
}
if fp < 0 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
defer {
C.close(fp)
}
$if windows {
if C._chsize_s(fp, len) != 0 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)