From 19164929ed0887d61a4bbd3e87bd86e6a2ad8076 Mon Sep 17 00:00:00 2001 From: andri lim Date: Sat, 17 Mar 2018 22:21:22 +0700 Subject: [PATCH] fixes #7347, asyncfile.getFileSize (#7354) * fixes #7347 * fixes #7347 --- lib/pure/asyncfile.nim | 4 ++++ tests/async/hello.txt | 1 + tests/async/tasyncfile.nim | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 tests/async/hello.txt diff --git a/lib/pure/asyncfile.nim b/lib/pure/asyncfile.nim index 97bec28150..1df7c3fc0e 100644 --- a/lib/pure/asyncfile.nim +++ b/lib/pure/asyncfile.nim @@ -78,7 +78,10 @@ proc getFileSize*(f: AsyncFile): int64 = raiseOSError(osLastError()) result = (high shl 32) or low else: + let curPos = lseek(f.fd.cint, 0, SEEK_CUR) result = lseek(f.fd.cint, 0, SEEK_END) + f.offset = lseek(f.fd.cint, curPos, SEEK_SET) + assert(f.offset == curPos) proc newAsyncFile*(fd: AsyncFd): AsyncFile = ## Creates `AsyncFile` with a previously opened file descriptor `fd`. @@ -281,6 +284,7 @@ proc read*(f: AsyncFile, size: int): Future[string] = result = false # We still want this callback to be called. elif res == 0: # EOF + f.offset = lseek(fd.cint, 0, SEEK_CUR) retFuture.complete("") else: readBuffer.setLen(res) diff --git a/tests/async/hello.txt b/tests/async/hello.txt new file mode 100644 index 0000000000..854d6c20ad --- /dev/null +++ b/tests/async/hello.txt @@ -0,0 +1 @@ +hello humans! \ No newline at end of file diff --git a/tests/async/tasyncfile.nim b/tests/async/tasyncfile.nim index aa7f03ab12..c7b71a2f7e 100644 --- a/tests/async/tasyncfile.nim +++ b/tests/async/tasyncfile.nim @@ -1,4 +1,8 @@ discard """ + output: '''13 +hello humans! +13 +''' file: "tasyncfile.nim" exitcode: 0 """ @@ -48,5 +52,12 @@ proc main() {.async.} = doAssert data == "t3" file.close() + # Issue #7347 + block: + let appDir = getAppDir() + var file = openAsync(appDir & DirSep & "hello.txt") + echo file.getFileSize() + echo await file.readAll() + echo file.getFilePos() waitFor main()