FileWriteAndClose
Description
Open path for write (truncating), write all of out, close. Two arities via OVERLOAD:
FileWriteAndClose(path, container)–containerisBuf *
or Str *; writes its full length.
FileWriteAndClose(path, buf, n)– explicitvoid *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)
- In
File.c:317:
// 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();- In
File.c:335:
u64 n = ZstrLen(payload);
i64 wrote = FileWriteAndClose(&path, payload, n);
bool ok = (wrote == (i64)n);- In
File.c:854:
// content and length.
bool test_fm_498_write_close_read_close_roundtrip(void) {
WriteFmt("Testing FileWriteAndClose + FileReadAndClose exact round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:870:
u64 n = ZstrLen(payload);
i64 wrote = FileWriteAndClose(&path, payload, n);
bool ok = (wrote == (i64)n);- In
File.c:887:
// 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();- In
File.c:900:
// n == 0 path: returns 0, file is opened "wb" (truncated) and closed.
i64 wrote = FileWriteAndClose(&path, "ignored", (u64)0);
ok = ok && (wrote == 0);- In
File.c:917:
// 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();- In
File.c:934:
MemCopy(BufData(&in), "12345", 5);
i64 wrote = FileWriteAndClose(&path, &in);
ok = ok && (wrote == 5);- In
File.c:952:
// 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();- In
File.c:968:
StrAppendFmt(&in, "string-content");
i64 wrote = FileWriteAndClose(&path, &in);
bool ok = (wrote == (i64)StrLen(&in));- In
File.c:1095:
// 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();- In
File.c:1107:
// NULL buffer with n > 0 -> contract violation -> LOG_FATAL.
FileWriteAndClose(&path, (const void *)0, (u64)5);
// Unreachable on real code.
- In
File.c:1149:
// 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();- In
File.c:1164:
// 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);- In
Pe.c:1658:
}
FileClose(&f);
if (FileWriteAndClose((Zstr)StrBegin(&path), blob, sizeof(blob)) < 0) {
FileRemove(&path);
StrDeinit(&path);- In
Pe.c:1700:
}
FileClose(&f);
if (FileWriteAndClose((Zstr)StrBegin(&path), junk, sizeof(junk)) < 0) {
FileRemove(&path);
StrDeinit(&path);- In
Pdb.c:2463:
build_crossblock_blob();
if (FileWriteAndClose(X_TMP_VALID, xblob, (u64)sizeof(xblob)) < 0) {
DefaultAllocatorDeinit(&alloc);
return false;- In
Pdb.c:2491:
u8 junk[256];
MemSet(junk, 0xCC, sizeof(junk)); // bad magic
if (FileWriteAndClose(X_TMP_JUNK, junk, (u64)sizeof(junk)) < 0) {
DefaultAllocatorDeinit(&alloc);
return false;- In
MachO.c:1424:
wr_u32(&seg[64], 0); // nsects == 0
i64 wrote = FileWriteAndClose(&path, buf, sizeof(buf));
bool ok = (wrote == (i64)sizeof(buf));- In
MachO.c:1461:
wr_u32(&garbage[0], 0xDEADBEEFu);
i64 wrote = FileWriteAndClose(&path, garbage, sizeof(garbage));
bool ok = (wrote == (i64)sizeof(garbage));
Last updated on