Add support for mktemps (#14347)

This commit is contained in:
Max Grender-Jones 2020-05-25 11:18:35 +01:00 • committed by GitHub
commit cbfe9325c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 8 deletions

View file

@ -78,16 +78,23 @@ proc sendSignal*(pid: Pid, signal: int) =
if kill(pid, signal.cint) != 0:
raise newException(OSError, $strerror(errno))
proc mkstemp*(prefix: string): (string, File) =
## Creates a unique temporary file from a prefix string. Adds a six chars suffix.
proc mkstemp*(prefix: string, suffix=""): (string, File) =
## Creates a unique temporary file from a prefix string. A six-character string
## will be added. If suffix is provided it will be added to the string
## The file is created with perms 0600.
## Returns the filename and a file opened in r/w mode.
var tmpl = cstring(prefix & "XXXXXX")
var tmpl = cstring(prefix & "XXXXXX" & suffix)
let fd =
when declared(mkostemp):
mkostemp(tmpl, O_CLOEXEC)
if len(suffix)==0:
when declared(mkostemp):
mkostemp(tmpl, O_CLOEXEC)
else:
mkstemp(tmpl)
else:
mkstemp(tmpl)
when declared(mkostemps):
mkostemps(tmpl, cint(len(suffix)), O_CLOEXEC)
else:
mkstemps(tmpl, cint(len(suffix)))
var f: File
if open(f, fd, fmReadWrite):
return ($tmpl, f)