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:
parent
bc88183318
commit
caee3935a5
@ -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))
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user