Add mkdtemp()
@@ -15,6 +15,8 @@ use "types"
pkg std =
const mktemp : (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
const mktempat : (dir : byte[:], base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+ const mkdtemp : (base : byte[:], mode : int64 -> std.result(byte[:], errno))
+ const mkdtempat : (dir : byte[:], base : byte[:], mode : int64 -> std.result(byte[:], errno))
const mktemppath : (base : byte[:] -> byte[:])
;;
@@ -51,6 +53,36 @@ const mktempat = {tmpdir, base, opt, mode
-> `Err Eexist
}
+const mkdtemp = {base, mode
+ var tmpdir
+
+ match std.getenv("TMPDIR")
+ | `std.Some d: tmpdir = d
+ | `std.None: tmpdir = "/tmp"
+ ;;
+
+ -> mkdtempat(tmpdir, base, mode)
+}
+
+const mkdtempat = {tmpdir, base, mode
+ var path
+
+ for var i = 0; i < Retries; i++
+ path = randpath(tmpdir, base)
+ match std.mkdir(path, mode)
+ | Enone:
+ -> `Ok path
+ | e:
+ if e != Eexist
+ std.slfree(path)
+ -> `Err e
+ ;;
+ ;;
+ std.slfree(path)
+ ;;
+ -> `Err Eexist
+}
+
const mktemppath = {base
var tmpdir, path
