Skip to content

FileRemove

Description

Remove a regular file. Direct syscall (unlink on Linux x86_64 / Darwin, unlinkat on Linux aarch64, DeleteFileA on Windows). Symlinks are removed, not followed.

Return type is i8 (0/1) rather than bool to dodge the bool-typedef redefinition trap Misra’s Types.h documents: system headers transitively #define bool _Bool and the SAME identifier ends up meaning different types across TUs. Callers can still use the result as a boolean (if (FileRemove(p)) ...).

Parameters

Name Direction Description
path in Path of the file to remove. Prefer Str *; Zstr accepted for literals / borrowed buffers.

Success

Returns 1; the directory entry is gone.

Failure

Returns 0; logs the failing syscall.

Usage example (Cross-references)

Usage examples (Cross-references)
    i8 file_remove(Zstr path) {
        if (!path) {
            LOG_FATAL("FileRemove: NULL path");
        }
    #if PLATFORM_WINDOWS
    #if PLATFORM_WINDOWS
        if (!DeleteFileA(path)) {
            LOG_ERROR("FileRemove(\"{}\"): DeleteFileA failed (GetLastError={})", path, (i32)GetLastError());
            return 0;
        }
    #    endif
        if (ret < 0) {
            LOG_SYS_ERROR(ErrnoOf(ret), "FileRemove(\"{}\")", path);
            return 0;
        }
        return 1;
    #else
    #    error "FileRemove: no direct-syscall path. Add the right MISRA_SYS_unlink* number in _Syscall.h for this arch."
    #endif
    }
                    inner_ok = DirRemoveAll(&child);
                } else {
                    inner_ok = FileRemove(&child);
                }
            }
        FileClose(&f);
        if (!ok) {
            FileRemove(out_path);
            StrDeinit(out_path);
        }
        File f = FileOpen(&path, "rb");
        if (!FileIsOpen(&f)) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        File f = FileOpen(&path, "rb");
        if (!FileIsOpen(&f)) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        ok              = ok && (FileTell(&f) == -1);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&g);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        FileClose(&f);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        FileClose(&f);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        }
        FileClose(&seed);
        FileRemove(&path);
    
        Str  body = StrInit(alloc_base);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        ok = ok && !FileIsOpen(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        ok = ok && !FileIsOpen(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        BufDeinit(&src);
        BufDeinit(&dst);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        StrDeinit(&body);
        BufDeinit(&in);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        StrDeinit(&body);
        StrDeinit(&in);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        StrDeinit(&b1);
        StrDeinit(&b2);
        FileRemove(&p1);
        FileRemove(&p2);
        StrDeinit(&p1);
        StrDeinit(&b2);
        FileRemove(&p1);
        FileRemove(&p2);
        StrDeinit(&p1);
        StrDeinit(&p2);
        ok = ok && (FileFlush(&f) == false);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        // Unreachable on real code.
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        StrDeinit(&body);
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
            if (FileIsOpen(&a)) {
                FileClose(&a);
                FileRemove(&pa);
                StrDeinit(&pa);
            }
            if (FileIsOpen(&b)) {
                FileClose(&b);
                FileRemove(&pb);
                StrDeinit(&pb);
            }
        FileClose(&b);
        FileClose(&c);
        FileRemove(&pa);
        FileRemove(&pb);
        FileRemove(&pc);
        FileClose(&c);
        FileRemove(&pa);
        FileRemove(&pb);
        FileRemove(&pc);
        StrDeinit(&pa);
        FileRemove(&pa);
        FileRemove(&pb);
        FileRemove(&pc);
        StrDeinit(&pa);
        StrDeinit(&pb);
    static void m4_cleanup(File *f, Str *path) {
        FileClose(f);
        FileRemove(path);
        StrDeinit(path);
    }
                ok = false;
            }
            FileRemove(&path);
        }
        StrDeinit(&path);
                ok = false;
            }
            FileRemove(&path);
        }
        StrDeinit(&path);
    static void m4_cleanup(File *f, Str *path) {
        FileClose(f);
        FileRemove(path);
        StrDeinit(path);
    }
            ok = (v == 42);
            FileClose(&f);
            FileRemove(&path);
        }
        StrDeinit(&path);
            ok       = (v == 123) && (got == 1) && (c == 'x');
            FileClose(&f);
            FileRemove(&path);
        }
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        return ok;
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        return ok;
    }
        }
    
        FileRemove(pe_name);
        FileRemove(pdb_name);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove(pe_name);
        FileRemove(pdb_name);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        DebugAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DebugAllocatorDeinit(&alloc);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DebugAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
    
        FileRemove((Zstr)pe_path);
        FileRemove((Zstr)pdb_path);
        DebugAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DefaultAllocatorDeinit(&alloc);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        }
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
    
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        FileRemove((Zstr)a_pe);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DebugAllocatorDeinit(&alloc);
        FileRemove((Zstr)a_pdb);
        FileRemove((Zstr)b_pe);
        FileRemove((Zstr)b_pdb);
        DebugAllocatorDeinit(&alloc);
        return ok;
    
    static void drop_temp(Str *path) {
        FileRemove(path);
        StrDeinit(path);
    }
                  host_v4_is(find_host(&r.hosts, "beta"), 2, 2, 2, 2);
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
            StrDeinit(p);
                  ns_fmt_is(&r, 1, a, "10.0.0.2:53");
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
            StrDeinit(p);
                  VecLen(&r.nameservers) == 1 && ns_fmt_is(&r, 0, a, "9.9.9.9:53");
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
    
        FileRemove(&p0);
        FileRemove(&p1);
        VecForeachPtr(&paths, p) {
            StrDeinit(p);
        MachoCacheDeinit(&cache);
        DefaultAllocatorDeinit(&alloc);
        FileRemove(bin_path);
        return ok;
    }
    
        // Cleanup
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_macho_stripped.dSYM");
        // Cleanup
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_macho_stripped.dSYM");
        return ok;
        DefaultAllocatorDeinit(&alloc);
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_macho_uuidmiss.dSYM");
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_macho_uuidmiss.dSYM");
        return ok;
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_path);
        DebugAllocatorDeinit(&dbg);
        return ok;
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_a);
        FileRemove(bin_b);
        DebugAllocatorDeinit(&dbg);
    
        FileRemove(bin_a);
        FileRemove(bin_b);
        DebugAllocatorDeinit(&dbg);
        return ok;
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_dsym.dSYM");
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_dsym.dSYM");
        DebugAllocatorDeinit(&dbg);
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_uuidmiss.dSYM");
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_uuidmiss.dSYM");
        DebugAllocatorDeinit(&dbg);
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_dwarf.dSYM");
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_mc_dwarf.dSYM");
        DebugAllocatorDeinit(&dbg);
    
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
        StrDeinit(&mod);
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        DefaultAllocatorDeinit(&alloc);
        return ok;
    
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf.dSYM");
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf.dSYM");
        DefaultAllocatorDeinit(&alloc);
    
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf2.dSYM");
        MachoCacheDeinit(&cache);
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf2.dSYM");
        DefaultAllocatorDeinit(&alloc);
        ok = ok && DebugAllocatorLiveCount(&dbg) == baseline;
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf3.dSYM");
    
        FileRemove(bin_path);
        FileRemove(dsym_path);
        DirRemoveAll("/tmp/misra_bl_dwarf3.dSYM");
        DebugAllocatorDeinit(&dbg);
        FileClose(&f);
        if (FileWriteAndClose((Zstr)StrBegin(&path), blob, sizeof(blob)) < 0) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
        }
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
        if (FileWriteAndClose((Zstr)StrBegin(&path), junk, sizeof(junk)) < 0) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
            PeDeinit(&pe);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        FileClose(&f);
        if (!wrote_ok) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
        File rf = FileOpen(&path, "rb");
        if (!FileIsOpen(&rf)) {
            FileRemove(&path);
            StrDeinit(&path);
            DefaultAllocatorDeinit(&alloc);
        }
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
             (ZstrCompare(StrBegin(&response.body), payload) == 0);
    
        FileRemove(StrBegin(&path));
        StrDeinit(&path);
        HttpResponseDeinit(&response);
        ok = ok && (res != NULL) && (response.status_code == HTTP_RESPONSE_CODE_OK) && (StrLen(&response.body) == 0);
    
        FileRemove(StrBegin(&path));
        StrDeinit(&path);
        HttpResponseDeinit(&response);
        ok = ok && (res != NULL);
    
        FileRemove(StrBegin(&path));
        StrDeinit(&path);
        HttpResponseDeinit(&response);
            MachoDeinit(&m);
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
        ok = ok && !opened; // garbage must be rejected
    
        FileRemove(&path);
        StrDeinit(&path);
        DefaultAllocatorDeinit(&alloc);
Last updated on