FileWrite
Description
Write up to n bytes from buf. Same short-write semantics as FileRead.
Success
Returns the number of bytes written (>= 0).
Failure
Returns -1 on error.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Log.c:58:
File out = (type == LOG_MESSAGE_TYPE_INFO) ? FileFromFd(1) : FileFromFd(2);
(void)FileWrite(&out, StrBegin(&full), StrLen(&full));
#if FEATURE_SYS_BACKTRACE && (!defined(LOG_NO_BACKTRACE) || !LOG_NO_BACKTRACE)- In
Log.c:71:
// boundary; pass at the call site, no intermediate variable.
FormatStackTrace(&trace, frames, n, ALLOCATOR_OF(&h));
(void)FileWrite(&out, StrBegin(&trace), StrLen(&trace));
StrDeinit(&trace);
}- In
File.c:285:
}
i64 FileWrite(File *f, const void *buf, u64 n) {
if (!FileIsOpen(f) || !buf) {
return -1;- In
Io.c:543:
}
if (ok && StrLen(&out) > 0 && FileWrite(stream, StrBegin(&out), StrLen(&out)) != (i64)StrLen(&out)) {
LOG_ERROR("Failed to write formatted output");
ok = false;- In
File.c:20:
}
size n = ZstrLen(text);
i64 written = FileWrite(&f, text, (u64)n);
bool ok = (written == (i64)n);
FileClose(&f);- In
File.c:124:
char scratch[8] = {0};
ok = ok && (FileRead(&f, scratch, sizeof(scratch)) == -1);
ok = ok && (FileWrite(&f, "x", 1) == -1);
ok = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == -1);
ok = ok && (FileTell(&f) == -1);- In
File.c:169:
// observable values.
bool test_write_seek_read_roundtrip(void) {
WriteFmt("Testing FileWrite/FileSeek/FileTell/FileRead round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:182:
Zstr msg = "abcdefgh"; // 8 bytes
i64 wrote = FileWrite(&f, msg, 8);
bool ok = (wrote == 8);- In
File.c:261:
File w = FileOpen(&path, "w");
ok = ok && FileIsOpen(&w);
ok = ok && (FileWrite(&w, "new", 3) == 3);
FileClose(&w);- In
File.c:296:
File a = FileOpen(&path, "a");
ok = ok && FileIsOpen(&a);
ok = ok && (FileWrite(&a, "tail", 4) == 4);
FileClose(&a);- In
File.c:399:
ok = ok && FileIsOpen(&f);
// A read-only handle must reject writes.
ok = ok && (FileWrite(&f, "X", 1) == -1);
FileClose(&f);- In
File.c:426:
ok = ok && FileIsOpen(&f);
// "r+" must permit writes (does not truncate). Overwrite first 3 bytes.
ok = ok && (FileWrite(&f, "AAA", 3) == 3);
FileClose(&f);- In
File.c:467:
// breaking at least one of these.
ok = ok && (got == 7) && (MemCompare(buf, "read-me", 7) == 0);
ok = ok && (FileWrite(&f, "X", 1) == -1);
FileClose(&f);- In
File.c:516:
return false;
}
bool ok = (FileWrite(&owner, "hello", 5) == 5);
// Borrow the same fd and close the borrowed wrapper. owns==false means
- In
File.c:525:
// Owner's fd must still be live.
ok = ok && (FileWrite(&owner, "world", 5) == 5);
FileClose(&owner);- In
File.c:615:
}
// FileWrite must report exactly 12 bytes written.
bool ok = (FileWrite(&f, "ABCDEFGHIJKL", 12) == 12);
ok = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);- In
File.c:712:
return false;
}
bool ok = (FileWrite(&f, "0123456789", 10) == 10);
ok = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);
// Advance 3 from current (0 -> 3).
- In
File.c:827:
BufData(&src)[i] = (u8)('A' + (i % 26));
}
ok = ok && (FileWrite(&f, BufData(&src), (u64)N) == (i64)N);
FileClose(&f);- In
File.c:1002:
// Round-trip a payload through the freshly created temp file.
ok = ok && (FileWrite(&f, "temp-data", 9) == 9);
ok = ok && (FileSeek(&f, 0, FILE_SEEK_SET) == 0);
char buf[10] = {0};- In
File.c:1035:
// They are independent files: writing to one does not affect the other.
ok = ok && (FileWrite(&f1, "one", 3) == 3);
ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
FileClose(&f1);- In
File.c:1036:
// They are independent files: writing to one does not affect the other.
ok = ok && (FileWrite(&f1, "one", 3) == 3);
ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
FileClose(&f1);
FileClose(&f2);- In
Write.c:975:
if (!FileIsOpen(&f))
return f;
FileWrite(&f, content, len);
FileSeek(&f, 0, FILE_SEEK_SET);
return f;- In
Read.c:43:
if (!FileIsOpen(&f))
return f;
FileWrite(&f, content, len);
FileSeek(&f, 0, FILE_SEEK_SET);
return f;- In
Read.c:2686:
bool ok = FileIsOpen(&f);
if (ok) {
FileWrite(&f, "42", 2);
FileSeek(&f, 0, FILE_SEEK_SET);
i32 v = 0;- In
Read.c:2708:
bool ok = FileIsOpen(&f);
if (ok) {
FileWrite(&f, "xx", 2);
FileSeek(&f, 0, FILE_SEEK_SET);
i32 v = 123;- In
PdbCache.c:92:
if (!FileIsOpen(&f))
return false;
bool ok = (u64)FileWrite(&f, data, size) == size;
FileClose(&f);
return ok;- In
SysDns.c:104:
u64 n = ZstrLen(body);
if (n > 0) {
FileWrite(&f, body, n);
}
FileClose(&f);- In
MachoCache.c:47:
if (!FileIsOpen(&f))
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;- In
MachoCache.c:309:
if (!FileIsOpen(&f))
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;- In
MachoCache.c:926:
if (!FileIsOpen(&f))
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;- In
ProcMaps.c:582:
bool wrote_ok = true;
for (int i = 0; i < 200 && wrote_ok; ++i) {
i64 w = FileWrite(&f, filler, (u64)ZstrLen(filler));
wrote_ok = (w == (i64)ZstrLen(filler));
}- In
ProcMaps.c:587:
Zstr late = "deadbeef000-deadbeef100 r-xp 0 0:0 0 /late\n";
if (wrote_ok) {
i64 w = FileWrite(&f, late, (u64)ZstrLen(late));
wrote_ok = (w == (i64)ZstrLen(late));
}- In
Http.c:218:
Zstr payload = "hello-body";
ok = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
FileClose(&f);- In
Http.c:391:
bool ok = FileIsOpen(&f);
Zstr payload = "file-payload-long-enough-to-heap-allocate-body";
ok = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
FileClose(&f);
Last updated on