From a5c3253716fc42aeb8b3905b3bd2a1c5fd2812af Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 29 Apr 2021 02:33:38 -0700 Subject: [PATCH] refactor with createTempDir --- lib/std/tempfiles.nim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/std/tempfiles.nim b/lib/std/tempfiles.nim index c9fff0e7f8..9f954679a4 100644 --- a/lib/std/tempfiles.nim +++ b/lib/std/tempfiles.nim @@ -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 `_) 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