summary refs log tree commit diff stats
path: root/lib/pure/os.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/os.nim')
-rwxr-xr-xlib/pure/os.nim21
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index afa145e9f..85ac9c83c 100755
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -635,22 +635,23 @@ proc copyFile*(dest, source: string) =
     if CopyFileA(source, dest, 0'i32) == 0'i32: OSError()
   else:
     # generic version of copyFile which works for any platform:
-    const
-      bufSize = 8192 # 8K buffer
-    var
-      d, s: TFile
+    const bufSize = 8000 # better for memory manager
+    var d, s: TFile
     if not open(s, source): OSError()
     if not open(d, dest, fmWrite):
       close(s)
       OSError()
-    var
-      buf: Pointer = alloc(bufsize)
-      bytesread, byteswritten: int
+    var buf = alloc(bufsize)
     while True:
-      bytesread = readBuffer(s, buf, bufsize)
-      byteswritten = writeBuffer(d, buf, bytesread)
+      var bytesread = readBuffer(s, buf, bufsize)
+      if bytesread > 0:
+        var byteswritten = writeBuffer(d, buf, bytesread)
+        if bytesread != bytesWritten:
+          dealloc(buf)
+          close(s)
+          close(d)
+          OSError()
       if bytesread != bufSize: break
-      if bytesread != bytesWritten: OSError()
     dealloc(buf)
     close(s)
     close(d)