ref: a367763275faf9bbda3196f58998e9e74484a9a0
parent: d4fac4da419b09f014c27b0f4334f1c8f4314faf
author: S. Gilles <sgilles@math.umd.edu>
date: Sun Aug 27 13:37:15 EDT 2017
Add mkdtemp()
--- a/lib/std/mktemp.myr
+++ b/lib/std/mktemp.myr
@@ -15,6 +15,8 @@
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[:])
;;
@@ -45,6 +47,36 @@
;;
| `Ok fd:
-> `Ok (fd, path)
+ ;;
+ std.slfree(path)
+ ;;
+ -> `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)
;;