summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorClay Sweetser <clay.sweetser@gmail.com>2013-12-18 15:32:26 -0500
committerClay Sweetser <clay.sweetser@gmail.com>2013-12-18 15:32:26 -0500
commitc26e787e5dab153b35470ea88f71dbf373de51f7 (patch)
treec09f01c9f446b40c6cab3449a54cf974093b356e /lib/pure
parentab2eb884a0d440796b0c712346a17bbe1ff54e94 (diff)
downloadNim-c26e787e5dab153b35470ea88f71dbf373de51f7.tar.gz
os.nim - Modify removeFile to use native Windows API calls
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/os.nim27
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index ecab692cf..a366a7965 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -972,8 +972,16 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1",
 
 when not defined(ENOENT):
   var ENOENT {.importc, header: "<errno.h>".}: cint
-when not defined(EACCES):
-  var EACCES {.importc, header: "<errno.h>".}: cint
+
+when defined(Windows):
+  when useWinUnicode:
+    template DeleteFile(file: expr): expr {.immediate.} = DeleteFileW(file)
+    template SetFileAttributes(file, attrs: expr): expr {.immediate.} = 
+      SetFileAttributesW(file, attrs)
+  else:
+    template DeleteFile(file: expr): expr {.immediate.} = DeleteFileA(file)
+    template SetFileAttributes(file, attrs: expr): expr {.immediate.} = 
+      SetFileAttributesA(file, attrs)
 
 proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
   ## Removes the `file`. If this fails, `EOS` is raised. This does not fail
@@ -981,11 +989,18 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
   ## On Windows, ignores the read-only attribute.
   if cremove(file) != 0'i32 and errno != ENOENT:
     when defined(Windows):
-      if errno == EACCES:  # Turn this into a case stmt?
-        setFilePermissions(file, {fpUserWrite}) # Use lower level code?
-        if cremove(file) != 0'i32 and errno != ENOENT:
-          raise newException(EOS, $strerror(errno))
+    when useWinUnicode:
+      let f = newWideCString(file)
     else:
+      let f = file
+    if DeleteFile(f) == 0:
+      if GetLastError() == ERROR_ACCESS_DENIED: 
+        if SetFileAttributes(f, FILE_ATTRIBUTE_NORMAL) == 0:
+          OSError(OSLastError())
+        if DeleteFile(f) == 0:
+          OSError(OSLastError())
+  else:
+    if cremove(file) != 0'i32 and errno != ENOENT:
       raise newException(EOS, $strerror(errno))
 
 proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",