mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 14:25:23 +00:00
Fix closeHandle bug, add setFileSize, make resize work on Windows (#21375)
* Add general purpose `setFileSize` (unexported for now). Use to simplify
`memfiles.open` as well as make robust (via hard allocation, not merely
`ftruncate` address space allocation) on systems with `posix_fallocate`.
As part of this, fix a bad `closeHandle` return check bug on Windows and
add `MemFile.resize` for Windows now that setFileSize makes that easier.
* Adapt existing test to exercise newly portable `MemFile.resize`.
* Since Apple has never provided `posix_fallocate`, provide a fallback.
This is presently written in terms of `ftruncate`, but it can be
improved to use `F_PREALLOCATE` instead, as mentioned in a comment.
(cherry picked from commit c91ef1a09f)
This commit is contained in:
@@ -12,8 +12,9 @@ var
|
||||
|
||||
if fileExists(fn): removeFile(fn)
|
||||
|
||||
# Create a new file, data all zeros
|
||||
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
|
||||
# Create a new file, data all zeros, starting at size 10
|
||||
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 10, allowRemap=true)
|
||||
mm.resize 20 # resize up to 20
|
||||
mm.close()
|
||||
|
||||
# read, change
|
||||
|
||||
Reference in New Issue
Block a user