* nimgrab tool bugfix: don't divide by zero * fixes #5532 (asyncfile write on Windows) * add a comment about what has been tried instead
This commit is contained in:
parent
ec50dab57d
commit
3afd852f54
3 changed files with 28 additions and 5 deletions
|
|
@ -339,13 +339,17 @@ proc writeBuffer*(f: AsyncFile, buf: pointer, size: int): Future[void] =
|
||||||
if not retFuture.finished:
|
if not retFuture.finished:
|
||||||
if errcode == OSErrorCode(-1):
|
if errcode == OSErrorCode(-1):
|
||||||
assert bytesCount == size.int32
|
assert bytesCount == size.int32
|
||||||
f.offset.inc(size)
|
|
||||||
retFuture.complete()
|
retFuture.complete()
|
||||||
else:
|
else:
|
||||||
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
|
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
|
||||||
)
|
)
|
||||||
|
# passing -1 here should work according to MSDN, but doesn't. For more
|
||||||
|
# information see
|
||||||
|
# http://stackoverflow.com/questions/33650899/does-asynchronous-file-
|
||||||
|
# appending-in-windows-preserve-order
|
||||||
ol.offset = DWord(f.offset and 0xffffffff)
|
ol.offset = DWord(f.offset and 0xffffffff)
|
||||||
ol.offsetHigh = DWord(f.offset shr 32)
|
ol.offsetHigh = DWord(f.offset shr 32)
|
||||||
|
f.offset.inc(size)
|
||||||
|
|
||||||
# According to MSDN we're supposed to pass nil to lpNumberOfBytesWritten.
|
# According to MSDN we're supposed to pass nil to lpNumberOfBytesWritten.
|
||||||
let ret = writeFile(f.fd.Handle, buf, size.int32, nil,
|
let ret = writeFile(f.fd.Handle, buf, size.int32, nil,
|
||||||
|
|
@ -364,7 +368,6 @@ proc writeBuffer*(f: AsyncFile, buf: pointer, size: int): Future[void] =
|
||||||
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
|
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
|
||||||
else:
|
else:
|
||||||
assert bytesWritten == size.int32
|
assert bytesWritten == size.int32
|
||||||
f.offset.inc(size)
|
|
||||||
retFuture.complete()
|
retFuture.complete()
|
||||||
else:
|
else:
|
||||||
var written = 0
|
var written = 0
|
||||||
|
|
@ -410,7 +413,6 @@ proc write*(f: AsyncFile, data: string): Future[void] =
|
||||||
if not retFuture.finished:
|
if not retFuture.finished:
|
||||||
if errcode == OSErrorCode(-1):
|
if errcode == OSErrorCode(-1):
|
||||||
assert bytesCount == data.len.int32
|
assert bytesCount == data.len.int32
|
||||||
f.offset.inc(data.len)
|
|
||||||
retFuture.complete()
|
retFuture.complete()
|
||||||
else:
|
else:
|
||||||
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
|
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
|
||||||
|
|
@ -420,6 +422,7 @@ proc write*(f: AsyncFile, data: string): Future[void] =
|
||||||
)
|
)
|
||||||
ol.offset = DWord(f.offset and 0xffffffff)
|
ol.offset = DWord(f.offset and 0xffffffff)
|
||||||
ol.offsetHigh = DWord(f.offset shr 32)
|
ol.offsetHigh = DWord(f.offset shr 32)
|
||||||
|
f.offset.inc(data.len)
|
||||||
|
|
||||||
# According to MSDN we're supposed to pass nil to lpNumberOfBytesWritten.
|
# According to MSDN we're supposed to pass nil to lpNumberOfBytesWritten.
|
||||||
let ret = writeFile(f.fd.Handle, buffer, data.len.int32, nil,
|
let ret = writeFile(f.fd.Handle, buffer, data.len.int32, nil,
|
||||||
|
|
@ -441,7 +444,6 @@ proc write*(f: AsyncFile, data: string): Future[void] =
|
||||||
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
|
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
|
||||||
else:
|
else:
|
||||||
assert bytesWritten == data.len.int32
|
assert bytesWritten == data.len.int32
|
||||||
f.offset.inc(data.len)
|
|
||||||
retFuture.complete()
|
retFuture.complete()
|
||||||
else:
|
else:
|
||||||
var written = 0
|
var written = 0
|
||||||
|
|
|
||||||
17
tests/async/tasyncfilewrite.nim
Normal file
17
tests/async/tasyncfilewrite.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
discard """
|
||||||
|
output: '''string 1
|
||||||
|
string 2
|
||||||
|
string 3'''
|
||||||
|
"""
|
||||||
|
# bug #5532
|
||||||
|
import os, asyncfile, asyncdispatch
|
||||||
|
|
||||||
|
removeFile("test.txt")
|
||||||
|
let f = openAsync("test.txt", fmWrite)
|
||||||
|
var futs = newSeq[Future[void]]()
|
||||||
|
for i in 1..3:
|
||||||
|
futs.add(f.write("string " & $i & "\n"))
|
||||||
|
waitFor(all(futs))
|
||||||
|
f.close()
|
||||||
|
echo readFile("test.txt")
|
||||||
|
|
||||||
|
|
@ -7,7 +7,11 @@ when defined(windows):
|
||||||
proc progress(status: DownloadStatus, progress: uint, total: uint,
|
proc progress(status: DownloadStatus, progress: uint, total: uint,
|
||||||
message: string) {.procvar, gcsafe.} =
|
message: string) {.procvar, gcsafe.} =
|
||||||
echo "Downloading " & url
|
echo "Downloading " & url
|
||||||
echo clamp(int(progress.BiggestInt*100 div total.BiggestInt), 0, 100), "%"
|
let t = total.BiggestInt
|
||||||
|
if t != 0:
|
||||||
|
echo clamp(int(progress.BiggestInt*100 div t), 0, 100), "%"
|
||||||
|
else:
|
||||||
|
echo "0%"
|
||||||
|
|
||||||
downloadToFile(url, file, {optUseCache}, progress)
|
downloadToFile(url, file, {optUseCache}, progress)
|
||||||
echo "100%"
|
echo "100%"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue