fix #17898(randomPathName called twice in a row can return the same string on windows) (#18729)
* close #17898 * no need to consider js
This commit is contained in:
parent
061a9183f7
commit
cc08d5c2c6
1 changed files with 17 additions and 3 deletions
|
|
@ -17,7 +17,7 @@ See also:
|
||||||
* `mkstemp` (posix), refs https://man7.org/linux/man-pages/man3/mkstemp.3.html
|
* `mkstemp` (posix), refs https://man7.org/linux/man-pages/man3/mkstemp.3.html
|
||||||
]#
|
]#
|
||||||
|
|
||||||
import os, random
|
import os, random, std/monotimes
|
||||||
|
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
@ -96,11 +96,25 @@ proc safeOpen(filename: string): File =
|
||||||
discard posix.close(fileHandle) # TODO handles failure when closing file
|
discard posix.close(fileHandle) # TODO handles failure when closing file
|
||||||
raiseOSError(osLastError(), filename)
|
raiseOSError(osLastError(), filename)
|
||||||
|
|
||||||
|
|
||||||
|
type
|
||||||
|
NimTempPathState = object
|
||||||
|
state: Rand
|
||||||
|
isInit: bool
|
||||||
|
|
||||||
|
var nimTempPathState {.threadvar.}: NimTempPathState
|
||||||
|
|
||||||
template randomPathName(length: Natural): string =
|
template randomPathName(length: Natural): string =
|
||||||
var res = newString(length)
|
var res = newString(length)
|
||||||
var state = initRand()
|
if not nimTempPathState.isInit:
|
||||||
|
var time = getMonoTime().ticks
|
||||||
|
when compileOption("threads"):
|
||||||
|
time = time xor int64(getThreadId())
|
||||||
|
nimTempPathState.isInit = true
|
||||||
|
nimTempPathState.state = initRand(time)
|
||||||
|
|
||||||
for i in 0 ..< length:
|
for i in 0 ..< length:
|
||||||
res[i] = state.sample(letters)
|
res[i] = nimTempPathState.state.sample(letters)
|
||||||
res
|
res
|
||||||
|
|
||||||
proc getTempDirImpl(dir: string): string {.inline.} =
|
proc getTempDirImpl(dir: string): string {.inline.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue