Improve as previously discussed
Better name for exposed primitive function, checks for pre-existing files
This commit is contained in:
parent
785e402247
commit
1cd4799b01
2 changed files with 31 additions and 11 deletions
|
|
@ -1009,13 +1009,10 @@ proc removeDir*(dir: string) {.rtl, extern: "nos$1", tags: [
|
||||||
of pcDir: removeDir(path)
|
of pcDir: removeDir(path)
|
||||||
rawRemoveDir(dir)
|
rawRemoveDir(dir)
|
||||||
|
|
||||||
proc tryCreateDir*(dir: string): bool =
|
proc rawCreateDir(dir: string): bool =
|
||||||
## Try to create a `directory`:idx: `dir`.
|
# Try to create one directory (not the whole path).
|
||||||
##
|
# returns `true` for success, `false` if the path has previously existed
|
||||||
## Does not create parent directories (fails if parent does not exist).
|
#
|
||||||
## Returns `true` if a new directory was created, `false` if *path*
|
|
||||||
## (not necessarily a directory) `dir` already exists.
|
|
||||||
|
|
||||||
# This is a thin wrapper over mkDir (or alternatives on other systems),
|
# This is a thin wrapper over mkDir (or alternatives on other systems),
|
||||||
# so in case of a pre-existing path we don't check that it is a directory.
|
# so in case of a pre-existing path we don't check that it is a directory.
|
||||||
when defined(solaris):
|
when defined(solaris):
|
||||||
|
|
@ -1048,12 +1045,25 @@ proc tryCreateDir*(dir: string): bool =
|
||||||
else:
|
else:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
|
|
||||||
proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
|
proc existsOrCreateDir*(dir: string): bool =
|
||||||
|
## Check if a `directory`:idx: `dir` exists, and create it otherwise.
|
||||||
|
##
|
||||||
|
## Does not create parent directories (fails if parent does not exist).
|
||||||
|
## Returns `true` if the directory already exists, and `false`
|
||||||
|
## otherwise.
|
||||||
|
result = not rawCreateDir(dir)
|
||||||
|
if result:
|
||||||
|
# path already exists - need to check that it is indeed a directory
|
||||||
|
if not existsDir(dir):
|
||||||
|
raise newException(IOError, "Failed to create the directory")
|
||||||
|
|
||||||
|
proc createDir*(dir: string) {.rtl, extern: "nos$1",
|
||||||
|
tags: [WriteDirEffect, ReadDirEffect].} =
|
||||||
## Creates the `directory`:idx: `dir`.
|
## Creates the `directory`:idx: `dir`.
|
||||||
##
|
##
|
||||||
## The directory may contain several subdirectories that do not exist yet.
|
## 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**
|
## 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
|
## fail if the directory already exists because for most usages this does not
|
||||||
## indicate an error.
|
## indicate an error.
|
||||||
var omitNext = false
|
var omitNext = false
|
||||||
when doslike:
|
when doslike:
|
||||||
|
|
@ -1063,12 +1073,12 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
|
||||||
if omitNext:
|
if omitNext:
|
||||||
omitNext = false
|
omitNext = false
|
||||||
else:
|
else:
|
||||||
discard tryCreateDir(substr(dir, 0, i-1))
|
discard existsOrCreateDir(substr(dir, 0, i-1))
|
||||||
|
|
||||||
# The loop does not create the dir itself if it doesn't end in separator
|
# The loop does not create the dir itself if it doesn't end in separator
|
||||||
if dir.len > 0 and not omitNext and
|
if dir.len > 0 and not omitNext and
|
||||||
dir[^1] notin {DirSep, AltSep}:
|
dir[^1] notin {DirSep, AltSep}:
|
||||||
discard tryCreateDir(dir)
|
discard existsOrCreateDir(dir)
|
||||||
|
|
||||||
proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
|
proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
|
||||||
tags: [WriteIOEffect, ReadIOEffect], benign.} =
|
tags: [WriteIOEffect, ReadIOEffect], benign.} =
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ false
|
||||||
false
|
false
|
||||||
true
|
true
|
||||||
true
|
true
|
||||||
|
Raises
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
# test os path creation, iteration, and deletion
|
# test os path creation, iteration, and deletion
|
||||||
|
|
@ -98,3 +99,12 @@ removeDir(dirs[0])
|
||||||
createDir(dname / "")
|
createDir(dname / "")
|
||||||
echo dirExists(dname) # true
|
echo dirExists(dname) # true
|
||||||
removeDir(dname)
|
removeDir(dname)
|
||||||
|
|
||||||
|
# createDir should raise IOError if the path exists
|
||||||
|
# and is not a directory
|
||||||
|
open(dname, fmWrite).close
|
||||||
|
try:
|
||||||
|
createDir(dname)
|
||||||
|
except IOError:
|
||||||
|
echo "Raises"
|
||||||
|
removeFile(dname)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue