mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-20 16:01:29 +00:00
* Replace ftell and fseek with (windows) _ftelli64, _fseeki64 and (posix) ftello, fseeko * disable large file test
This commit is contained in:
@@ -47,10 +47,16 @@ when not declared(c_fwrite):
|
||||
# C routine that is used here:
|
||||
proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
|
||||
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
|
||||
proc c_fseek(f: File, offset: clong, whence: cint): cint {.
|
||||
importc: "fseek", header: "<stdio.h>", tags: [].}
|
||||
proc c_ftell(f: File): clong {.
|
||||
importc: "ftell", header: "<stdio.h>", tags: [].}
|
||||
when defined(windows):
|
||||
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
|
||||
importc: "_fseeki64", header: "<stdio.h>", tags: [].}
|
||||
proc c_ftell(f: File): int64 {.
|
||||
importc: "_ftelli64", header: "<stdio.h>", tags: [].}
|
||||
else:
|
||||
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
|
||||
importc: "fseeko", header: "<stdio.h>", tags: [].}
|
||||
proc c_ftell(f: File): int64 {.
|
||||
importc: "ftello", header: "<stdio.h>", tags: [].}
|
||||
proc c_ferror(f: File): cint {.
|
||||
importc: "ferror", header: "<stdio.h>", tags: [].}
|
||||
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
|
||||
@@ -210,12 +216,12 @@ proc readAllBuffer(file: File): string =
|
||||
result.add(buffer)
|
||||
break
|
||||
|
||||
proc rawFileSize(file: File): int =
|
||||
proc rawFileSize(file: File): int64 =
|
||||
# this does not raise an error opposed to `getFileSize`
|
||||
var oldPos = c_ftell(file)
|
||||
discard c_fseek(file, 0, 2) # seek the end of the file
|
||||
result = c_ftell(file)
|
||||
discard c_fseek(file, clong(oldPos), 0)
|
||||
discard c_fseek(file, oldPos, 0)
|
||||
|
||||
proc endOfFile(f: File): bool =
|
||||
var c = c_fgetc(f)
|
||||
@@ -223,7 +229,7 @@ proc endOfFile(f: File): bool =
|
||||
return c < 0'i32
|
||||
#result = c_feof(f) != 0
|
||||
|
||||
proc readAllFile(file: File, len: int): string =
|
||||
proc readAllFile(file: File, len: int64): string =
|
||||
# We acquire the filesize beforehand and hope it doesn't change.
|
||||
# Speeds things up.
|
||||
result = newString(len)
|
||||
@@ -363,7 +369,7 @@ proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
|
||||
result = f != nil
|
||||
|
||||
proc setFilePos(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) =
|
||||
if c_fseek(f, clong(pos), cint(relativeTo)) != 0:
|
||||
if c_fseek(f, pos, cint(relativeTo)) != 0:
|
||||
raiseEIO("cannot set file position")
|
||||
|
||||
proc getFilePos(f: File): int64 =
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import
|
||||
unittest, osproc, streams, os
|
||||
unittest, osproc, streams, os, strformat
|
||||
const STRING_DATA = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
|
||||
const TEST_FILE = "tests/testdata/string.txt"
|
||||
|
||||
@@ -23,3 +23,26 @@ suite "io":
|
||||
test "file":
|
||||
check:
|
||||
readFile(TEST_FILE) == STRING_DATA
|
||||
|
||||
|
||||
proc verifyFileSize(sz: int64) =
|
||||
# issue 7121, large file size (2-4GB and >4Gb)
|
||||
const fn = "tmpfile112358"
|
||||
let size_in_mb = sz div 1_000_000
|
||||
|
||||
when defined(windows):
|
||||
discard execProcess(&"fsutil file createnew {fn} {sz}" )
|
||||
else:
|
||||
discard execProcess(&"dd if=/dev/zero of={fn} bs=1000000 count={size_in_mb}")
|
||||
|
||||
doAssert os.getFileSize(fn) == sz # Verify OS filesize by string
|
||||
|
||||
var f = open(fn)
|
||||
doAssert f.getFileSize() == sz # Verify file handle filesize
|
||||
f.close()
|
||||
|
||||
os.removeFile(fn)
|
||||
|
||||
#disable tests for automatic testers
|
||||
#for s in [50_000_000'i64, 3_000_000_000, 5_000_000_000]:
|
||||
# verifyFileSize(s)
|
||||
|
||||
Reference in New Issue
Block a user