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
3 changed files with 14 additions and 7 deletions

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)