Use TMPDIR env var if available to get the temp dir name (#11443) [bugfix]
Additionally, use normalizePathEnd to suffix the dir name with "/" or "\" as appropriate for the current OS. Fixes https://github.com/nim-lang/Nim/issues/11439.
This commit is contained in:
parent
94177f7357
commit
2334680b3d
2 changed files with 20 additions and 5 deletions
|
|
@ -798,13 +798,16 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
|
||||||
## * `expandTilde proc <#expandTilde,string>`_
|
## * `expandTilde proc <#expandTilde,string>`_
|
||||||
## * `getCurrentDir proc <#getCurrentDir>`_
|
## * `getCurrentDir proc <#getCurrentDir>`_
|
||||||
## * `setCurrentDir proc <#setCurrentDir,string>`_
|
## * `setCurrentDir proc <#setCurrentDir,string>`_
|
||||||
const tempDirDefault = "/tmp/"
|
const tempDirDefault = "/tmp"
|
||||||
|
result = tempDirDefault
|
||||||
when defined(tempDir):
|
when defined(tempDir):
|
||||||
const tempDir {.strdefine.}: string = tempDirDefault
|
const tempDir {.strdefine.}: string = tempDirDefault
|
||||||
return tempDir
|
result = tempDir
|
||||||
elif defined(windows): return string(getEnv("TEMP")) & "\\"
|
elif defined(windows): result = string(getEnv("TEMP"))
|
||||||
elif defined(android): return getHomeDir()
|
elif defined(android): result = getHomeDir()
|
||||||
else: return tempDirDefault
|
else:
|
||||||
|
if existsEnv("TMPDIR"): result = string(getEnv("TMPDIR"))
|
||||||
|
normalizePathEnd(result, trailingSep=true)
|
||||||
|
|
||||||
proc expandTilde*(path: string): string {.
|
proc expandTilde*(path: string): string {.
|
||||||
tags: [ReadEnvEffect, ReadIOEffect].} =
|
tags: [ReadEnvEffect, ReadIOEffect].} =
|
||||||
|
|
|
||||||
|
|
@ -333,3 +333,15 @@ block ospaths:
|
||||||
doAssert joinPath("", "lib") == "lib"
|
doAssert joinPath("", "lib") == "lib"
|
||||||
doAssert joinPath("", "/lib") == unixToNativePath"/lib"
|
doAssert joinPath("", "/lib") == unixToNativePath"/lib"
|
||||||
doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"
|
doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"
|
||||||
|
|
||||||
|
block getTempDir:
|
||||||
|
block TMPDIR:
|
||||||
|
# TMPDIR env var is not used if either of these are defined.
|
||||||
|
when not (defined(tempDir) or defined(windows) or defined(android)):
|
||||||
|
if existsEnv("TMPDIR"):
|
||||||
|
let origTmpDir = getEnv("TMPDIR")
|
||||||
|
putEnv("TMPDIR", "/mytmp")
|
||||||
|
doAssert getTempDir() == "/mytmp/"
|
||||||
|
putEnv("TMPDIR", origTmpDir)
|
||||||
|
else:
|
||||||
|
doAssert getTempDir() == "/tmp/"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue