Skip to content

FileOpenTemp

Description

Open a unique temporary file for read+write. The name is the 16-hex-digit of Prng64() – no caller-supplied prefix, no kernel entropy per call. Open is O_RDWR | O_CREAT | O_EXCL | 0600, so two callers racing can never collide – one wins, the other retries with a new draw. The file lands in the process’s current working directory.

Two forms via argument-count overload (allocator backs out_path):

  • FileOpenTemp(out_path) – inside a Scope; allocator is

MisraScope.

  • FileOpenTemp(out_path, allocator) – explicit allocator.

Parameters

Name Direction Description
out_path out Fresh Str * – receives the resolved 16-char hex name. Caller StrDeinits when done; the on-disk file is NOT auto-removed (use FileRemove(out_path)).

Success

Returns an open File with FileIsOpen true.

Failure

Returns a File where FileIsOpen is false.

Usage example (Cross-references)

Usage examples (Cross-references)
    // caller can read it back and remove it.
    static bool write_test_file(Zstr text, Str *out_path, Allocator *alloc) {
        File f = FileOpenTemp(out_path, alloc);
        if (!FileIsOpen(&f)) {
            return false;
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
        // then write to it through the convenience API.
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
        // Mint a unique name then remove it so the path is guaranteed absent.
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File owner = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&owner)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
        // Build a 10000-byte payload (> FILE_READ_CHUNK=4096) on disk.
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
    // file with a non-empty path that round-trips a payload.
    bool test_fm_602_temp_open_roundtrips(void) {
        WriteFmt("Testing FileOpenTemp creates an open, writable, named file\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        Str  path;
        File f  = FileOpenTemp(&path, alloc_base);
        bool ok = FileIsOpen(&f);
        // The resolved path must be non-empty (16 hex chars).
    // hex name, owns the new fd).
    bool test_fm_547_temp_names_distinct(void) {
        WriteFmt("Testing two FileOpenTemp calls yield distinct, independent files\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        Str  p1;
        File f1 = FileOpenTemp(&p1, alloc_base);
        Str  p2;
        File f2 = FileOpenTemp(&p2, alloc_base);
        File f1 = FileOpenTemp(&p1, alloc_base);
        Str  p2;
        File f2 = FileOpenTemp(&p2, alloc_base);
    
        bool ok = FileIsOpen(&f1) && FileIsOpen(&f2);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (FileIsOpen(&seed)) {
            FileClose(&seed);
        // middle of the table. Record A's fd.
        Str  pa;
        File a = FileOpenTemp(&pa, alloc_base);
        Str  pb;
        File b = FileOpenTemp(&pb, alloc_base);
        File a = FileOpenTemp(&pa, alloc_base);
        Str  pb;
        File b = FileOpenTemp(&pb, alloc_base);
        if (!FileIsOpen(&a) || !FileIsOpen(&b)) {
            if (FileIsOpen(&a)) {
        // occupied so we get a strictly higher number.
        Str  pc;
        File c     = FileOpenTemp(&pc, alloc_base);
        ok         = ok && FileIsOpen(&c);
        i32  fd_c  = FileFd(&c);
    static void capture_help(ArgParse *p, Str *out) {
        Str  tmp_path = StrInit(p->alloc);
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
            LOG_FATAL("capture_help: FileOpenTemp failed");
    
        p->out        = &tmp;
    static bool capture_help_file(ArgParse *p, Str *out) {
        Str  tmp_path = StrInit(p->alloc);
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
    static ArgRun capture_run(ArgParse *p, int argc, char **argv, Str *out) {
        Str  tmp_path = StrInit(p->alloc);
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
            LOG_FATAL("capture_run: FileOpenTemp failed");
    
        p->out    = &tmp;
    // caller can FileRemove + StrDeinit it afterwards.
    static File m4_make_temp(DefaultAllocator *alloc, Str *out_path, const char *content, u64 len) {
        File f = FileOpenTemp(out_path, alloc);
        if (!FileIsOpen(&f))
            return f;
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
    // caller can FileRemove + StrDeinit it afterwards.
    static File m4_make_temp(DefaultAllocator *alloc, Str *out_path, const char *content, u64 len) {
        File f = FileOpenTemp(out_path, alloc);
        if (!FileIsOpen(&f))
            return f;
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
    
        Str  path = StrInit(base);
        File f    = FileOpenTemp(&path, base);
        if (!FileIsOpen(&f)) {
            StrDeinit(&path);
    
        Str  path = StrInit(base);
        File f    = FileOpenTemp(&path, base);
        if (!FileIsOpen(&f)) {
            StrDeinit(&path);
    
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path = StrInit(alloc_base);
        File f    = FileOpenTemp(&path, alloc_base);
        bool ok   = FileIsOpen(&f);
    
        Str  path = StrInit(alloc_base);
        File f    = FileOpenTemp(&path, alloc_base);
        bool ok   = FileIsOpen(&f);
        FileClose(&f); // leave it empty
        // Create a temp file with content.
        Str  path    = StrInit(adbg);
        File f       = FileOpenTemp(&path, adbg);
        bool ok      = FileIsOpen(&f);
        Zstr payload = "file-payload-long-enough-to-heap-allocate-body";
        // Mint a unique temp path, then write a minimal valid Mach-O to it.
        Str  path;
        File seed = FileOpenTemp(&path, base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
    
        Str  path;
        File seed = FileOpenTemp(&path, base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
Last updated on