Skip to content
FileWriteAndClose

FileWriteAndClose

Description

Open path for write (truncating), write all of out, close. Two arities via OVERLOAD:

  • FileWriteAndClose(path, container)container is Buf *

or Str *; writes its full length.

  • FileWriteAndClose(path, buf, n) – explicit void *buf

and u64 n byte count.

Success

Returns total bytes written; file is closed.

Failure

Returns -1; file is closed (or never opened).

Usage example (Cross-references)

Usage examples (Cross-references)
    // the open+op+close convenience round-trip.
    bool test_write_and_read_and_close(void) {
        WriteFmt("Testing FileWriteAndClose + FileReadAndClose round-trip\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
        u64  n       = ZstrLen(payload);
    
        i64  wrote = FileWriteAndClose(&path, payload, n);
        bool ok    = (wrote == (i64)n);
    // content and length.
    bool test_fm_498_write_close_read_close_roundtrip(void) {
        WriteFmt("Testing FileWriteAndClose + FileReadAndClose exact round-trip\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
        u64  n       = ZstrLen(payload);
    
        i64  wrote = FileWriteAndClose(&path, payload, n);
        bool ok    = (wrote == (i64)n);
    // the file to empty (so a subsequent read-and-close returns 0).
    bool test_fm_498_zero_length_write(void) {
        WriteFmt("Testing FileWriteAndClose with n==0 writes nothing, truncates file\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        // n == 0 path: returns 0, file is opened "wb" (truncated) and closed.
        i64 wrote = FileWriteAndClose(&path, "ignored", (u64)0);
        ok        = ok && (wrote == 0);
    // length and content.
    bool test_fm_514_write_close_from_buf(void) {
        WriteFmt("Testing FileWriteAndClose(Buf) writes the buffer length exactly\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
        MemCopy(BufData(&in), "12345", 5);
    
        i64 wrote = FileWriteAndClose(&path, &in);
        ok        = ok && (wrote == 5);
    // length and content.
    bool test_fm_521_write_close_from_str(void) {
        WriteFmt("Testing FileWriteAndClose(Str) writes the string length exactly\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
        StrAppendFmt(&in, "string-content");
    
        i64  wrote = FileWriteAndClose(&path, &in);
        bool ok    = (wrote == (i64)StrLen(&in));
    // deadend pins that the abort fires.
    bool test_fm_498_null_buf_positive_n_aborts(void) {
        WriteFmt("Testing FileWriteAndClose(NULL, 5) aborts (NULL-buf contract)\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
    
        // NULL buffer with n > 0 -> contract violation -> LOG_FATAL.
        FileWriteAndClose(&path, (const void *)0, (u64)5);
    
        // Unreachable on real code.
    // non-NULL buffer, so it never exercises the `!buf` arm at n == 0.
    bool test_mut_498_null_buf_zero_n_ok(void) {
        WriteFmt("Testing FileWriteAndClose(path, NULL, 0) returns 0 (n > 0 guard)\n");
    
        DefaultAllocator alloc      = DefaultAllocatorInit();
        // NULL buffer with n == 0 is a legal no-op write: it must NOT abort
        // and must return 0 (the file is opened "wb", truncated, closed).
        i64 wrote = FileWriteAndClose(&path, (const void *)0, (u64)0);
        ok        = ok && (wrote == 0);
        }
        FileClose(&f);
        if (FileWriteAndClose((Zstr)StrBegin(&path), blob, sizeof(blob)) < 0) {
            FileRemove(&path);
            StrDeinit(&path);
        }
        FileClose(&f);
        if (FileWriteAndClose((Zstr)StrBegin(&path), junk, sizeof(junk)) < 0) {
            FileRemove(&path);
            StrDeinit(&path);
    
        build_crossblock_blob();
        if (FileWriteAndClose(X_TMP_VALID, xblob, (u64)sizeof(xblob)) < 0) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        u8 junk[256];
        MemSet(junk, 0xCC, sizeof(junk)); // bad magic
        if (FileWriteAndClose(X_TMP_JUNK, junk, (u64)sizeof(junk)) < 0) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        wr_u32(&seg[64], 0); // nsects == 0
    
        i64  wrote = FileWriteAndClose(&path, buf, sizeof(buf));
        bool ok    = (wrote == (i64)sizeof(buf));
        wr_u32(&garbage[0], 0xDEADBEEFu);
    
        i64  wrote = FileWriteAndClose(&path, garbage, sizeof(garbage));
        bool ok    = (wrote == (i64)sizeof(garbage));
Last updated on