Skip to content

FileIsOpen

Description

Check whether the file handle currently holds an open fd / HANDLE.

Success

Returns true when the handle is open.

Failure

Returns false otherwise (closed or never opened). Cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool FileIsOpen(const File *f) {
        if (!f) {
            return false;
    
    i64 file_read(File *f, void *buf, u64 n) {
        if (!FileIsOpen(f) || !buf) {
            return -1;
        }
    
    i64 FileWrite(File *f, const void *buf, u64 n) {
        if (!FileIsOpen(f) || !buf) {
            return -1;
        }
    
    i64 FileSeek(File *f, i64 offset, FileWhence whence) {
        if (!FileIsOpen(f)) {
            return -1;
        }
    
    bool FileFlush(File *f) {
        if (!FileIsOpen(f)) {
            return false;
        }
    static bool slurp_file(Zstr path, Str *out) {
        File f = FileOpen(path, "rb");
        if (!FileIsOpen(&f)) {
            // Missing config file is fine -- resolver just won't know about it.
            return true;
    static inline bool sys_path_exists(Zstr path) {
        File f = FileOpen(path, "rb");
        if (!FileIsOpen(&f)) {
            return false;
        }
        // kernel on read, so we loop-read into a growing buffer ourselves.
        File f = FileOpen("/proc/self/maps", "rb");
        if (!FileIsOpen(&f)) {
            LOG_ERROR("ProcMapsLoad: FileOpen(/proc/self/maps) failed");
            ProcMapsDeinit(out);
        out->raw     = StrInit(alloc);
        out->entries = VecInitT(out->entries, alloc);
        if (!FileIsOpen(f)) {
            LOG_ERROR("ProcMapsLoadFrom: file is not open");
            ProcMapsDeinit(out);
    static bool write_test_file(Zstr text, Str *out_path, Allocator *alloc) {
        File f = FileOpenTemp(out_path, alloc);
        if (!FileIsOpen(&f)) {
            return false;
        }
    
        File f = FileOpen(&path, "rb");
        if (!FileIsOpen(&f)) {
            FileRemove(&path);
            StrDeinit(&path);
    
        File f = FileOpen(&path, "rb");
        if (!FileIsOpen(&f)) {
            FileRemove(&path);
            StrDeinit(&path);
    
        File f = FileOpen(&path, "rb");
        ok     = ok && FileIsOpen(&f);
        FileClose(&f);
    
        // After close the handle must read as not-open.
        ok = ok && !FileIsOpen(&f);
    
        char scratch[8] = {0};
        // "q" is not a recognised mode.
        File f = FileOpen(&path, "q");
        ok     = ok && !FileIsOpen(&f);
        FileClose(&f);
        // An empty mode is also rejected.
        File g = FileOpen(&path, "");
        ok     = ok && !FileIsOpen(&g);
        FileClose(&g);
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File f = FileOpen(&path, "rb");
        ok     = ok && FileIsOpen(&f);
    
        // A zero-byte request returns 0 and must NOT flag eof.
    
        File w = FileOpen(&path, "w");
        ok     = ok && FileIsOpen(&w);
        ok     = ok && (FileWrite(&w, "new", 3) == 3);
        FileClose(&w);
    
        File r   = FileOpen(&path, "rb");
        ok       = ok && FileIsOpen(&r);
        Str body = StrInit(alloc_base);
        i64 got  = FileRead(&r, &body);
    
        File a = FileOpen(&path, "a");
        ok     = ok && FileIsOpen(&a);
        ok     = ok && (FileWrite(&a, "tail", 4) == 4);
        FileClose(&a);
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File f = FileOpen(&path, "r");
        ok     = ok && FileIsOpen(&f);
        // A read-only handle must reject writes.
        ok = ok && (FileWrite(&f, "X", 1) == -1);
    
        File f = FileOpen(&path, "r+");
        ok     = ok && FileIsOpen(&f);
        // "r+" must permit writes (does not truncate). Overwrite first 3 bytes.
        ok = ok && (FileWrite(&f, "AAA", 3) == 3);
    
        File f      = FileOpen(&path, "r");
        ok          = ok && FileIsOpen(&f);
        char buf[8] = {0};
        i64  got    = FileRead(&f, buf, 7);
        Str  path;
        File owner = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&owner)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        bool ok = (FileClose(&f) == true);
        // And the handle is now not-open.
        ok = ok && !FileIsOpen(&f);
    
        FileRemove(&path);
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        // and returns true without touching any fd.
        ok = ok && (FileClose(&f) == true);
        ok = ok && !FileIsOpen(&f);
    
        FileRemove(&path);
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File f      = FileOpen(&path, "rb");
        ok          = ok && FileIsOpen(&f);
        char buf[4] = {0};
        ok          = ok && (FileRead(&f, buf, 2) == 2) && !FileIsEof(&f);
    
        File f      = FileOpen(&path, "rb");
        ok          = ok && FileIsOpen(&f);
        char buf[8] = {0};
        // Drain to EOF to set at_eof.
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File f = FileOpen(&path, "rb");
        ok     = ok && FileIsOpen(&f);
        // Move the cursor to offset 10; remaining size must be 16 - 10 = 6.
        ok = ok && (FileSeek(&f, 10, FILE_SEEK_SET) == 10);
    
        File f   = FileOpen(&path, "rb");
        ok       = ok && FileIsOpen(&f);
        Str body = StrInit(alloc_base);
        i64 got  = FileRead(&f, &body);
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File r  = FileOpen(&path, "rb");
        ok      = ok && FileIsOpen(&r);
        Buf dst = BufInit(alloc_base);
        i64 got = FileRead(&r, &dst);
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File f  = FileOpenTemp(&path, alloc_base);
        bool ok = FileIsOpen(&f);
        // The resolved path must be non-empty (16 hex chars).
        ok = ok && (StrLen(&path) == 16);
        File f2 = FileOpenTemp(&p2, alloc_base);
    
        bool ok = FileIsOpen(&f1) && FileIsOpen(&f2);
        ok      = ok && (StrLen(&p1) == 16) && (StrLen(&p2) == 16);
        // Distinct names (overwhelmingly likely; the loop draws fresh entropy).
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File seed = FileOpenTemp(&path, alloc_base);
        if (FileIsOpen(&seed)) {
            FileClose(&seed);
        }
    // FileIsOpen on fd 0, so the `>= 0` vs `> 0` boundary is unpinned there.
    bool test_mut_240_isopen_fd_zero(void) {
        WriteFmt("Testing FileIsOpen reports a borrowed fd 0 as open (>= 0 boundary)\n");
    
    #if PLATFORM_WINDOWS
        // a `> 0` mutant would wrongly report it not-open.
        File f  = FileFromFd(0);
        bool ok = (FileIsOpen(&f) == true);
        // Sanity: a genuinely-negative fd is not open under either form, so
        // the discriminating case really is fd == 0.
        // the discriminating case really is fd == 0.
        File g = FileFromFd(-1);
        ok     = ok && (FileIsOpen(&g) == false);
        return ok;
    #endif
        Str  pb;
        File b = FileOpenTemp(&pb, alloc_base);
        if (!FileIsOpen(&a) || !FileIsOpen(&b)) {
            if (FileIsOpen(&a)) {
                FileClose(&a);
        File b = FileOpenTemp(&pb, alloc_base);
        if (!FileIsOpen(&a) || !FileIsOpen(&b)) {
            if (FileIsOpen(&a)) {
                FileClose(&a);
                FileRemove(&pa);
                StrDeinit(&pa);
            }
            if (FileIsOpen(&b)) {
                FileClose(&b);
                FileRemove(&pb);
        Str  pc;
        File c     = FileOpenTemp(&pc, alloc_base);
        ok         = ok && FileIsOpen(&c);
        i32  fd_c  = FileFd(&c);
        bool reuse = (fd_c == fd_a);
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
            LOG_FATAL("capture_help: FileOpenTemp failed");
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
            return false;
        File tmp      = FileOpenTemp(&tmp_path, p->alloc);
        StrDeinit(&tmp_path);
        if (!FileIsOpen(&tmp))
            LOG_FATAL("capture_run: FileOpenTemp failed");
    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;
        FileWrite(&f, content, len);
        File             f     = FileOpen(path, "r");
        bool             ok    = false;
        if (FileIsOpen(&f)) {
            Str back = StrInit(&alloc);
            FileRead(&f, &back);
        Zstr path = "io_mutants28_str.txt"; // CWD-relative: portable (no /tmp on Windows)
        File f    = FileOpen(path, "w");
        if (!FileIsOpen(&f)) {
            return false;
        }
        Zstr path = "io_mutants28_int.txt"; // CWD-relative: portable (no /tmp on Windows)
        File f    = FileOpen(path, "w");
        if (!FileIsOpen(&f)) {
            return false;
        }
        Zstr path = "io_mutants28_ln.txt"; // CWD-relative: portable (no /tmp on Windows)
        File f    = FileOpen(path, "w");
        if (!FileIsOpen(&f)) {
            return false;
        }
        Str              path  = StrInit(&alloc);
        File             f     = m4_make_temp(&alloc, &path, "42 zzzzzzz", 10);
        bool             ok    = FileIsOpen(&f);
    
        i32 v = 0;
        Str              path  = StrInit(&alloc);
        File             f     = m4_make_temp(&alloc, &path, "7", 1);
        bool             ok    = FileIsOpen(&f);
    
        i32 v = -1;
        Str              path  = StrInit(&alloc);
        File             f     = m4_make_temp(&alloc, &path, "53 and more text", 16);
        bool             ok    = FileIsOpen(&f);
    
        i32 v = 0;
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            ok = ok && FWriteFmtLn(&f, "n={}", LVAL((i32)42));
            FileClose(&f);
            File r = FileOpen(StrBegin(&path), "r");
            if (FileIsOpen(&r)) {
                Str back = StrInit(&alloc);
                FileRead(&r, &back);
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            // FWriteFmt (no newline) of empty string: nothing to write.
            FileClose(&f);
            File r = FileOpen(StrBegin(&path), "r");
            if (FileIsOpen(&r)) {
                Str back = StrInit(&alloc);
                FileRead(&r, &back);
    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;
        FileWrite(&f, content, len);
    
        File f  = m4_make_temp(&alloc, &path, content, 52);
        bool ok = FileIsOpen(&f);
    
        i32 v = -1;
        Str              path  = StrInit(&alloc);
        File             f     = m4_make_temp(&alloc, &path, "", 0);
        bool             ok    = FileIsOpen(&f);
    
        i32 v = 1234;
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            FileWrite(&f, "42", 2);
        Str              path  = StrInit(&alloc);
        File             f     = FileOpenTemp(&path, &alloc);
        bool             ok    = FileIsOpen(&f);
        if (ok) {
            FileWrite(&f, "xx", 2);
        // (no libc fopen/fwrite/fclose).
        File f = FileOpen(path, "w");
        if (!FileIsOpen(&f))
            return false;
        bool ok = (u64)FileWrite(&f, data, size) == size;
        Str  path = StrInit(a);
        File f    = file_open_temp(&path, a);
        if (FileIsOpen(&f)) {
            u64 n = ZstrLen(body);
            if (n > 0) {
    static bool write_file(Zstr path, const u8 *data, u64 size) {
        File f = FileOpen(path, "wb");
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
    static bool mc_write_file(Zstr path, const u8 *data, u64 size) {
        File f = FileOpen(path, "wb");
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
    static bool bl_write_file(Zstr path, const u8 *data, u64 size) {
        File f = FileOpen(path, "wb");
        if (!FileIsOpen(&f))
            return false;
        bool ok = FileWrite(&f, data, size) == (i64)size;
        Str  path = StrInit(base);
        File f    = FileOpenTemp(&path, base);
        if (!FileIsOpen(&f)) {
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
        Str  path = StrInit(base);
        File f    = FileOpenTemp(&path, base);
        if (!FileIsOpen(&f)) {
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
        Str  path;
        File f = FileOpenTemp(&path, alloc_base);
        if (!FileIsOpen(&f)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        File rf = FileOpen(&path, "rb");
        if (!FileIsOpen(&rf)) {
            FileRemove(&path);
            StrDeinit(&path);
        // A directory: open(2) succeeds (FileIsOpen true) but read(2) returns -1.
        File dir = FileOpen("/proc/self", "rb");
        if (!FileIsOpen(&dir)) {
            DebugAllocatorDeinit(&dbg);
            return false;
        Str  path = StrInit(alloc_base);
        File f    = FileOpenTemp(&path, alloc_base);
        bool ok   = FileIsOpen(&f);
    
        Zstr payload = "hello-body";
        Str  path = StrInit(alloc_base);
        File f    = FileOpenTemp(&path, alloc_base);
        bool ok   = FileIsOpen(&f);
        FileClose(&f); // leave it empty
        Str  path    = StrInit(adbg);
        File f       = FileOpenTemp(&path, adbg);
        bool ok      = FileIsOpen(&f);
        Zstr payload = "file-payload-long-enough-to-heap-allocate-body";
        ok           = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
        Str  path;
        File seed = FileOpenTemp(&path, base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        Str  path;
        File seed = FileOpenTemp(&path, base);
        if (!FileIsOpen(&seed)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
Last updated on