diff options
author | Araq <rumpf_a@web.de> | 2011-11-06 01:59:13 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-11-06 01:59:13 +0100 |
commit | 0ce9d4960144468c12de493487ada62e8eb04f5d (patch) | |
tree | dfdf93645314d86d550916061de0b4384eaa85f4 /lib/pure | |
parent | 089e287c6e95edab56e610f7a615933e42aaff52 (diff) | |
download | Nim-0ce9d4960144468c12de493487ada62e8eb04f5d.tar.gz |
better exception behavior for os.removeFile and os.removeDir
Diffstat (limited to 'lib/pure')
-rwxr-xr-x | lib/pure/os.nim | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 50c6ffeba..122b38c57 100755 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -613,9 +613,13 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1".} = ## Moves a file from `source` to `dest`. If this fails, `EOS` is raised. if crename(source, dest) != 0'i32: OSError() +when not defined(ENOENT): + var ENOENT* {.importc, header: "<errno.h>".}: cint + proc removeFile*(file: string) {.rtl, extern: "nos$1".} = - ## Removes the `file`. If this fails, `EOS` is raised. - if cremove(file) != 0'i32: OSError() + ## Removes the `file`. If this fails, `EOS` is raised. This does not fail + ## if the file never existed in the first place. + if cremove(file) != 0'i32 and errno != ENOENT: OSError() proc execShellCmd*(command: string): int {.rtl, extern: "nos$1".} = ## Executes a `shell command`:idx:. @@ -855,13 +859,14 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string = proc rawRemoveDir(dir: string) = when defined(windows): - if RemoveDirectoryA(dir) == 0'i32: OSError() + if RemoveDirectoryA(dir) == 0'i32 and GetLastError() != 3'i32: OSError() else: - if rmdir(dir) != 0'i32: OSError() + if rmdir(dir) != 0'i32 and errno != ENOENT: OSError() proc removeDir*(dir: string) {.rtl, extern: "nos$1".} = ## Removes the directory `dir` including all subdirectories and files - ## in `dir` (recursively). If this fails, `EOS` is raised. + ## in `dir` (recursively). If this fails, `EOS` is raised. This does not fail + ## if the directory never existed in the first place. for kind, path in walkDir(dir): case kind of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path) |