// temp — process-local temporary files and directories. // // Mirrors Hare's temp:: at ref/hare/temp/+linux.ha; drops underscores // per plan 9 style and the layers Hare wires on top of raw os: there // is no fs::fs filesystem handle, no io::file abstraction, no defer // hook for auto-cleanup. Callers MUST [[os.close]] the returned fd // and [[os.remove]] / [[os.rmdir]] the returned path themselves; the // absence of defer is a deliberate ergonomic divergence drew-devault // was explicit about — do not invent auto-cleanup wrappers. // // Subset divergence from Hare's +linux.ha: // // * No TMPDIR env probe. lib/os doesn't expose getenv yet, so // [[file]] and [[dir]] are pinned to "/tmp". Graduate to a // `gettmpdir` that mirrors Hare's os::tryenv lookup when // lib/os.getenv lands. // * No O_TMPFILE fast path in [[file]]. We always go through // [[named]] and discard the returned path, matching Hare's // +freebsd.ha branch (and the +linux.ha fallback). The on-disk // entry leaks until the caller of [[named]] removes it; [[file]] // callers cannot remove it because they no longer hold the path. // Lift to a real O_TMPFILE branch when lib/os gains a TMPFILE // flag. // * [[named]] writes its (fd, path) result through out-pointers // instead of returning Hare's `(io::file, str) | fs::error` // 2-tuple. ww's cgen drops 32B return-by-value (task #5) and // mis-sizes the (i32, str) tuple (task #11), so we mirror // memio's / getopt's out-pointer shape until both close. // * Hare's `fs: *fs::fs` parameter is dropped — lib/os hands out // raw fds, there is no virtual-filesystem indirection. // * `iomode: mode` is our own subset of Hare's io::mode — only // WRITE and RDWR, the two values Hare's temp asserts on. // * `perm: i32` is the raw Linux mode_t bitset rather than // Hare's fs::mode enum; matches what [[os.open]] takes. // * No PRNG seeding from crypto::random. Hare reads /dev/urandom // (via crypto::random::buffer); ww doesn't ship crypto::random // yet, so we seed an inline SplitMix64 from [[os.getpid]] and // rely on O_EXCL retry for collision avoidance. Adopt a real // entropy source when crypto::random is ported. // * The path stored in pathbuf is NUL-terminated for direct // handoff to syscalls; the returned str view excludes the NUL. // Hare's path::buffer carries no terminator. // // GRADUATE-IN-ONE-GO WARNING (lib/CLAUDE.md policy). Once tasks #5 // and #11 close and lib/os exposes a real io::file abstraction plus // getenv, [[named]] graduates to Hare's value-returning // `(io::file, str) | fs::error` shape with a `fs: *fs::fs` first // arg, [[file]] grows an O_TMPFILE fast path, and [[gettmpdir]] // consults $TMPDIR. The old out-pointer surface disappears in the // same commit — callers MUST NOT bake it into themselves. // // Caller cleanup template (no defer yet): // // let fd: i32; let p: str; // match (temp.named(&fd, &p, "/tmp", temp.mode.RDWR, 384i32)) { // case void => { /* use fd */ os.close(fd); os.remove(p.ptr); }; // case let e: os.oserror => { /* error */ }; // }; // // let d: str = temp.dir(); // /* populate d ... */ // os.rmdir(d.ptr); package temp; import os; // mode — temp's io flavour. Hare exposes io::mode {READ, WRITE, // RDWR}; temp asserts iomode must be WRITE or RDWR, so we ship just // those two. Numeric values match the corresponding [[os.flag]] // bits so the OR with CREATE/EXCL composes correctly. export type mode = enum i32 { WRITE = 1, // os.flag.WRONLY RDWR = 2, // os.flag.RDWR }; // pathbuf — shared scratch path for [[named]] and [[dir]]. Sized // for any caller-supplied directory <= 233 bytes plus our 22-byte // "/temp.<16-hex-digits>\0" suffix; [[named]] returns ENAMETOOLONG // past that. Hare uses a path::buffer of PATH_MAX bytes; this // collapses to a single static slot because lib/path doesn't yet // ship a path::buffer type. NUL byte at `pathbuf[pathlen]` lets the // bytes be handed straight to [[os.open]] / [[os.mkdir]] / // [[os.remove]] / [[os.rmdir]]. let pathbuf: [256]u8; let pathlen: i32 = 0; // rng* — SplitMix64 state. Lazily seeded on first call from // [[os.getpid]]; collisions are caught by the O_EXCL retry loops in // [[named]] and [[dir]]. Inline rather than `use math.random;` to // keep temp's import surface minimal and avoid pulling random's // module-level scope into temp's namespace. let rngstate: u64 = 0u64; let rnginit: i32 = 0; fn seedrng() void = { if (rnginit == 0) { rngstate = (os.getpid(): u64) ^ 0x9E3779B97F4A7C15u64; rnginit = 1; }; }; fn nextrand() u64 = { seedrng(); rngstate = rngstate + 0x9E3779B97F4A7C15u64; let a: u64 = rngstate; a = (a ^ (a >> 30u64)) * 0xBF58476D1CE4E5B9u64; a = (a ^ (a >> 27u64)) * 0x94D049BB133111EBu64; return a ^ (a >> 31u64); }; // gettmpdir — Hare's get_tmpdir(). Hardcoded /tmp until lib/os // exposes getenv; documented in the file header. fn gettmpdir() str = { return "/tmp"; }; // Caps writes against pathbuf's capacity so a long caller-supplied // dir can't run off the end. fn puts(off: i32, s: str) i32 = { let i: i32 = 0; for (i < s.len) { if (off + i >= 255) { break; }; pathbuf[off + i] = s[i]; i += 1; }; return off + i; }; fn puthex(off: i32, v: u64) i32 = { let hex: str = "0123456789abcdef"; let i: i32 = 0; for (i < 16) { let shift: u64 = ((15 - i): u64) * 4u64; let nib: i32 = ((v >> shift) & 0xFu64): i32; if (off + i < 255) { pathbuf[off + i] = hex[nib]; }; i += 1; }; return off + 16; }; // makenamed — `