mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-04 19:04:46 +00:00
refactor with createTempDir
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user