os: fix os.File's tell/0 method for windows (fix #24217) (#24218)

This commit is contained in:
kbkpbot 2025-05-20 20:41:15 +08:00 committed by GitHub
parent 9e10b2392d
commit c72d77d8a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 5 deletions

View File

@ -148,7 +148,7 @@ fn C.rename(old_filename &char, new_filename &char) int
fn C.fgets(str &char, n int, stream &C.FILE) int fn C.fgets(str &char, n int, stream &C.FILE) int
fn C._telli64(handle int) isize fn C.fgetpos(&C.FILE, voidptr) int
@[trusted] @[trusted]
fn C.sigemptyset() int fn C.sigemptyset() int

View File

@ -861,13 +861,15 @@ pub fn (f &File) tell() !i64 {
if !f.is_opened { if !f.is_opened {
return error_file_not_opened() return error_file_not_opened()
} }
mut pos := isize(0)
mut pos := i64(0)
mut ret := 0
$if windows { $if windows {
pos = C._telli64(f.fd) ret = C.fgetpos(f.cfile, &pos)
} $else { } $else {
pos = C.ftell(f.cfile) pos = i64(C.ftell(f.cfile))
} }
if pos == -1 { if ret == -1 || pos == -1 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }
return pos return pos

View File

@ -357,6 +357,12 @@ fn test_tell() {
mut f := os.open_file(tfile, 'r')! mut f := os.open_file(tfile, 'r')!
f.seek(-5, .end)! f.seek(-5, .end)!
pos := f.tell()! pos := f.tell()!
f.seek(0, .start)!
c1 := f.tell()!
_ := f.read_bytes(8)
c2 := f.tell()!
assert c1 == 0
assert c2 == 8
f.close() f.close()
// dump(pos) // dump(pos)
assert pos == size - 5 assert pos == size - 5