summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDmitry Polienko <nigredo.tori@gmail.com>2016-10-22 12:29:03 +0700
committerDmitry Polienko <nigredo.tori@gmail.com>2016-10-22 12:29:03 +0700
commit48ef6761d8f5183139920b05b2b084edb312bc40 (patch)
treeec9e6aa426a6bf39e76d5e7018827aa7505cc601
parent7c22c03876d3eb268d0d0fcc6c5c466f1a09fad8 (diff)
downloadNim-48ef6761d8f5183139920b05b2b084edb312bc40.tar.gz
Make createDir return discardable bool
-rw-r--r--lib/pure/os.nim46
-rw-r--r--tests/stdlib/tos.nim19
2 files changed, 54 insertions, 11 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index cdbe170cc..34819b396 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -1009,29 +1009,51 @@ proc removeDir*(dir: string) {.rtl, extern: "nos$1", tags: [
     of pcDir: removeDir(path)
   rawRemoveDir(dir)
 
-proc rawCreateDir(dir: string) =
+proc rawCreateDir(dir: string): bool =
+  # Create directory.
+  # Does not create parent directories (fails if parent does not exist).
+  # Returns `true` if the directory was created, `false` if it already exists.
   when defined(solaris):
-    if mkdir(dir, 0o777) != 0'i32 and errno != EEXIST and errno != ENOSYS:
+    let res = mkdir(dir, 0o777)
+    case res
+    of 0'i32:
+      result = true
+    of EEXIST, ENOSYS:
+      result = false
+    else:
       raiseOSError(osLastError())
   elif defined(unix):
-    if mkdir(dir, 0o777) != 0'i32 and errno != EEXIST:
+    let res = mkdir(dir, 0o777)
+    case res
+    of 0'i32:
+      result = true
+    of EEXIST:
+      result = false
+    else:
       raiseOSError(osLastError())
   else:
     when useWinUnicode:
       wrapUnary(res, createDirectoryW, dir)
     else:
-      var res = createDirectoryA(dir)
-    if res == 0'i32 and getLastError() != 183'i32:
+      let res = createDirectoryA(dir)
+
+    if res != 0'i32:
+      result = true
+    elif getLastError() == 183'i32:
+      result = false
+    else:
       raiseOSError(osLastError())
 
-proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
+proc createDir*(dir: string): bool {.discardable, rtl,
+  extern: "nos$1", tags: [WriteDirEffect].} =
   ## Creates the `directory`:idx: `dir`.
   ##
   ## The directory may contain several subdirectories that do not exist yet.
-  ## The full path is created. If this fails, `OSError` is raised. It does **not**
-  ## fail if the path already exists because for most usages this does not
-  ## indicate an error.
+  ## The full path is created. If this fails, `OSError` is raised.
+  ##
+  ## Returns `true` if the directory did not previously exist
   var omitNext = false
+  result = false
   when doslike:
     omitNext = isAbsolute(dir)
   for i in 1.. dir.len-1:
@@ -1039,8 +1061,10 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
       if omitNext:
         omitNext = false
       else:
-        rawCreateDir(substr(dir, 0, i-1))
-  rawCreateDir(dir)
+        result = rawCreateDir(substr(dir, 0, i-1))
+  # The loop does not create the dir itself if it doesn't end in separator
+  if dir[^1] notin {DirSep, AltSep}:
+    result = rawCreateDir(dir)
 
 proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
   tags: [WriteIOEffect, ReadIOEffect], benign.} =
diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim
index 1ddaacfcb..0d726de56 100644
--- a/tests/stdlib/tos.nim
+++ b/tests/stdlib/tos.nim
@@ -36,6 +36,10 @@ false
 false
 false
 false
+true
+true
+true
+false
 '''
 """
 # test os path creation, iteration, and deletion
@@ -86,3 +90,18 @@ for file in files:
 
 removeDir(dname)
 echo dirExists(dname)
+
+# createDir should create recursive directories
+createDir(dirs[0] / dirs[1])
+echo dirExists(dirs[0] / dirs[1]) # true
+removeDir(dirs[0])
+
+# createDir should properly handle trailing separator
+createDir(dname / "")
+echo dirExists(dname) # true
+removeDir(dname)
+
+# Check createDir return value
+echo createDir(dname) # true
+echo createDir(dname) # false
+removeDir(dname)