refactor with createTempDir

This commit is contained in:
Timothee Cour
2021-04-29 02:33:38 -07:00
parent a192bbfd7e
commit a5c3253716

View File

@@ -104,7 +104,8 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
## Creates a new temporary file in the directory `dir`.
##
## This generates a path name using `genTempPath(prefix, suffix, dir)` and
## returns a file handle to an open file and the path of that file.
## returns a file handle to an open file and the path of that file, possibly after
## retrying to ensure it doesn't already exist.
##
## If failing to create a temporary file, `IOError` will be raised.
##
@@ -126,12 +127,11 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
raise newException(IOError, "Failed to create a temporary file under directory " & dir)
proc createTempDir*(prefix, suffix: string, dir = ""): string =
## `createTempDir` creates a new temporary directory in the directory `dir`.
## Creates a new temporary directory in the directory `dir`.
##
## If `dir` is the empty string, the default directory for temporary files
## (`getTempDir <os.html#getTempDir>`_) will be used.
## The temporary directory name begins with `prefix` and ends with `suffix`.
## `createTempDir` returns the path of that temporary firectory.
## This generates a dir name using `genTempPath(prefix, suffix, dir)`, creates
## the directory and returns it, possibly after retrying to ensure it doesn't
## already exist.
##
## If failing to create a temporary directory, `IOError` will be raised.
##
@@ -144,7 +144,7 @@ proc createTempDir*(prefix, suffix: string, dir = ""): string =
createDir(dir)
for i in 0 ..< maxRetry:
result = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
result.path = genTempPath(prefix, suffix, dir)
try:
if not existsOrCreateDir(result):
return