tempfiles
nimony/lib/std/tempfiles.nim
This module creates temporary files and directories.
The random part of a name comes from the operating system's CSPRNG (std/crypto/sysrand); only if that cannot be read does it fall back to a std/random generator seeded from the monotonic clock. Either way a name is never trusted to be unique: the file or directory is created exclusively (O_CREAT or O_EXCL / CREATE_NEW, mkdir) and a fresh name is drawn when it already exists.
Unlike Nim's std/tempfiles, which takes and returns string, the directory and the returned paths are Path, matching std/files and std/dirs (so removeDir(createTempDir(...)) just works).
See also:
- appdirs module for
getTempDir - files module and dirs module for cleanup
proc genTempPath(prefix: string; suffix: string; dir: Path): PathGenerates a path name in
dir.The path begins with
prefixand ends withsuffix; between them arenimTempPathLength(default 8) random letters and digits... note::
dirmust exist (emptydirwill resolve to getTempDir).proc createTempFile(prefix: string; suffix: string; dir: Path): tuple[cfile: ref FileObj, path: Path]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, possibly after retrying to ensure it doesn't already exist. On POSIX the file is created with mode0o600.Raises the
ErrorCodeof the failure,NameExistswhen every name tried was taken... note:: It is the caller's responsibility to close
result.cfileand removeresult.pathwhen no longer needed. .. note::dirmust exist (emptydirwill resolve to getTempDir).proc createTempDir(prefix: string; suffix: string; dir: Path): PathCreates a new temporary directory in the directory
dir.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. On POSIX the directory is created with mode0o700.Raises the
ErrorCodeof the failure,NameExistswhen every name tried was taken... note:: It is the caller's responsibility to remove the directory when no longer needed. .. note::
dirmust exist (emptydirwill resolve to getTempDir).