File.tempDir
func File.tempDir(pattern: string, dir: string | null = null): Result[string, Error]File.tempDir creates a new empty directory, mode 0o700, in dir or the host's temporary directory, and returns its path. Its name is pattern with the last * replaced by a random string, or with the string appended when there is no *. Nothing removes it for the program.
Details
- The mode is exactly
0o700; the umask does not apply, unlike File.mkdir. - Without
dir, it is made in the host's temporary directory:$TMPDIR, or/tmpwhen that is unset, on Unix, and%TEMP%on Windows. - Only the last
*ofpatternis replaced, so"cache-*.d"keeps its suffix. Apatternwith a path separator isErr; put the parent indir. - Nothing removes the directory, at exit or later. Remove it with
defer { File.removeAll(dir) }right after making it, and before calling Os.exit, which runs no deferred block.
Basic examples
Make a scratch directory, remove it when the script ends, and work inside it:
let dir = try File.tempDir("build-*")
defer { File.removeAll(dir) }
try File.write(Path.join([dir, "out.txt"]), "done")
println(try File.list(dir))["out.txt"]The name is the pattern with a random string in place of the *:
let dir = try File.tempDir("build-*")
defer { File.removeAll(dir) }
let name = Path.base(dir)
println(name.startsWith("build-"))
println(name.length() > "build-".length())true
trueScope
A * in the middle keeps what follows it, such as an extension:
let dir = try File.tempDir("snapshot-*.d")
defer { File.removeAll(dir) }
println(Path.base(dir).endsWith(".d"))trueWithout a *, the random string goes at the end. Only the last * is replaced, so the others stay in the name:
let plain = try File.tempDir("cache")
defer { File.removeAll(plain) }
let stars = try File.tempDir("a*b*")
defer { File.removeAll(stars) }
println(Path.base(plain).startsWith("cache"))
println(Path.base(stars).startsWith("a*b"))true
trueAn empty pattern gives a name that is the random string alone:
let dir = try File.tempDir("")
defer { File.removeAll(dir) }
println(Path.base(dir).isEmpty())falseNamed arguments
dir
dir: makes the directory there instead of in the host's temporary directory:
try File.mkdir("work")
let job = try File.tempDir("job-*", dir: "work")
println(Path.dir(job))
let entries = try File.list("work")
println(entries.length())work
1The directory must already exist; File.tempDir does not create it:
println(File.tempDir("job-*", dir: "missing"))
try File.mkdir("missing")
println(File.tempDir("job-*", dir: "missing").isOk())Err{ error: Error{ message: "stat missing: no such file or directory" } }
trueApplications
Run work in a directory of its own, removed however the work ends. The helper hands the directory to a function and returns its result:
func withScratch[T](f: func(string): Result[T, Error]): Result[T, Error] {
let dir = try File.tempDir("scratch-*")
defer { File.removeAll(dir) }
f(dir)
}
func countWords(dir: string): Result[int, Error] {
let path = Path.join([dir, "words.txt"])
try File.write(path, "one two three")
let text = try File.read(path)
Ok{ value: text.split(" ").length() }
}
println(try withScratch(countWords))3Build a whole directory out of sight, then move it into place with File.rename. Making it next to the target, with dir: ".", keeps the move inside one directory:
let tmp = try File.tempDir(".site-*", dir: ".")
try File.write(Path.join([tmp, "index.html"]), "<h1>Hi</h1>")
try File.write(Path.join([tmp, "about.html"]), "<h1>About</h1>")
try File.rename(tmp, "site")
println(try File.list("site"))["about.html", "index.html"]Properties and relations
Each call makes a different directory, new, empty, and private to its owner:
let a = try File.tempDir("t-*")
defer { File.removeAll(a) }
let b = try File.tempDir("t-*")
defer { File.removeAll(b) }
assert(a != b)
let info = try File.stat(a)
assert(info.isDir)
assert(info.mode == 0o700)
let entries = try File.list(a)
assert(entries.isEmpty())Possible issues
A path separator in the pattern is Err. Name the parent with dir: instead:
try File.mkdir("out")
println(File.tempDir("out/run-*"))
println(File.tempDir("run-*", dir: "out").isOk())Err{ error: Error{ message: "mkdirtemp out/run-*: pattern contains path separator" } }
trueOs.exit runs no deferred block, so the directory stays behind. Remove it before exiting:
let dir = try File.tempDir("job-*")
defer { File.removeAll(dir) }
if !File.exists(Path.join([dir, "result.txt"])) {
_ = File.removeAll(dir)
Os.exit(1)
}In a test, use Test.tempDir: it makes the directory and removes it when the test ends, with no defer to forget.
See also
File.tempFile · File.removeAll · File.mkdir · File.rename · Test.tempDir · Path.join · Os.exit
