FileClose
Description
Close a file we opened via FileOpen. Idempotent and safe on a borrowed (owns == false) handle; in that case it just clears the local state without touching the underlying fd / HANDLE.
Success
Returns true.
Failure
Returns false if the close syscall failed (logged).
Usage example (Cross-references)
Usage examples (Cross-references)
- In
File.h:38:
#endif
bool at_eof; // last read returned 0 bytes
bool owns; // true when FileClose should release the handle
} File;- In
File.c:205:
}
bool FileClose(File *f) {
if (!f) {
return false;- In
Dns.c:121:
}
}
FileClose(&f);
return ok;
}- In
_Helpers.h:27:
return false;
}
FileClose(&f);
return true;
}- In
ProcMaps.c:242:
}
bool ok = proc_maps_read_all(&f, &out->raw);
FileClose(&f);
if (!ok) {
LOG_ERROR("ProcMapsLoad: FileRead failed");- In
File.c:22:
i64 written = FileWrite(&f, text, (u64)n);
bool ok = (written == (i64)n);
FileClose(&f);
if (!ok) {
FileRemove(out_path);- In
File.c:52:
Str body = StrInit(alloc_base);
i64 got = FileRead(&f, &body);
FileClose(&f);
bool result = (got == (i64)ZstrLen("hello from file")) && (StrLen(&body) == (size)ZstrLen("hello from file")) &&- In
File.c:86:
Str body = StrInit(alloc_base);
i64 got = FileRead(&f, &body);
FileClose(&f);
Zstr expected = "this is longer than the initial buffer";- In
File.c:117:
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
FileClose(&f);
// After close the handle must read as not-open.
- In
File.c:151:
File f = FileOpen(&path, "q");
ok = ok && !FileIsOpen(&f);
FileClose(&f);
// An empty mode is also rejected.
- In
File.c:156:
File g = FileOpen(&path, "");
ok = ok && !FileIsOpen(&g);
FileClose(&g);
FileRemove(&path);- In
File.c:202:
ok = ok && (FileSeek(&f, 0, FILE_SEEK_END) == 8);
FileClose(&f);
FileRemove(&path);
StrDeinit(&path);- In
File.c:237:
ok = ok && (FileRead(&f, scratch, 2) == 0) && FileIsEof(&f);
FileClose(&f);
FileRemove(&path);
StrDeinit(&path);- In
File.c:262:
ok = ok && FileIsOpen(&w);
ok = ok && (FileWrite(&w, "new", 3) == 3);
FileClose(&w);
File r = FileOpen(&path, "rb");- In
File.c:268:
Str body = StrInit(alloc_base);
i64 got = FileRead(&r, &body);
FileClose(&r);
ok = ok && (got == 3) && (StrLen(&body) == 3) && ZstrCompare(StrBegin(&body), "new") == 0;- In
File.c:297:
ok = ok && FileIsOpen(&a);
ok = ok && (FileWrite(&a, "tail", 4) == 4);
FileClose(&a);
File r = FileOpen(&path, "rb");- In
File.c:302:
Str body = StrInit(alloc_base);
i64 got = FileRead(&r, &body);
FileClose(&r);
ok = ok && (got == 8) && ZstrCompare(StrBegin(&body), "headtail") == 0;- In
File.c:330:
return false;
}
FileClose(&seed);
Zstr payload = "round-trip payload";- In
File.c:364:
return false;
}
FileClose(&seed);
FileRemove(&path);- In
File.c:400:
// A read-only handle must reject writes.
ok = ok && (FileWrite(&f, "X", 1) == -1);
FileClose(&f);
FileRemove(&path);- In
File.c:427:
// "r+" must permit writes (does not truncate). Overwrite first 3 bytes.
ok = ok && (FileWrite(&f, "AAA", 3) == 3);
FileClose(&f);
// Confirm the write landed.
- In
File.c:433:
File r = FileOpen(&path, "rb");
i64 got = FileRead(&r, &body);
FileClose(&r);
ok = ok && (got == 10) && ZstrCompare(StrBegin(&body), "AAA3456789") == 0;- In
File.c:468:
ok = ok && (got == 7) && (MemCompare(buf, "read-me", 7) == 0);
ok = ok && (FileWrite(&f, "X", 1) == -1);
FileClose(&f);
FileRemove(&path);- In
File.c:522:
// really close the fd and the subsequent owner write would fail.
File borrowed = FileFromFd(FileFd(&owner));
FileClose(&borrowed);
// Owner's fd must still be live.
- In
File.c:526:
// Owner's fd must still be live.
ok = ok && (FileWrite(&owner, "world", 5) == 5);
FileClose(&owner);
Str body = StrInit(alloc_base);- In
File.c:547:
// eq_to_ne on `r == 0` would report failure on a clean close.
bool test_fm_222_close_returns_true(void) {
WriteFmt("Testing FileClose returns true on a clean close (r == 0)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:559:
}
// A clean close of an owned fd returns true.
bool ok = (FileClose(&f) == true);
// And the handle is now not-open.
ok = ok && !FileIsOpen(&f);- In
File.c:573:
// would attempt to close fd -1 and could report failure.
bool test_fm_228_double_close_clears_owns(void) {
WriteFmt("Testing FileClose clears owns: second close is a no-op true\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:584:
return false;
}
bool ok = FileClose(&f);
// Second close: fd was reset to -1 and owns to false, so it short-circuits
// and returns true without touching any fd.
- In
File.c:587:
// Second close: fd was reset to -1 and owns to false, so it short-circuits
// and returns true without touching any fd.
ok = ok && (FileClose(&f) == true);
ok = ok && !FileIsOpen(&f);- In
File.c:622:
i64 got = FileRead(&f, buf, 5);
ok = ok && (got == 5) && (MemCompare(buf, "ABCDE", 5) == 0);
FileClose(&f);
FileRemove(&path);- In
File.c:650:
// The next read hits EOF: 0 bytes and at_eof becomes true.
ok = ok && (FileRead(&f, buf, 2) == 0) && FileIsEof(&f);
FileClose(&f);
FileRemove(&path);- In
File.c:690:
char buf2[3] = {0};
ok = ok && (FileRead(&f, buf2, 3) == 3) && (MemCompare(buf2, "abc", 3) == 0);
FileClose(&f);
FileRemove(&path);- In
File.c:720:
char buf[2] = {0};
ok = ok && (FileRead(&f, buf, 1) == 1) && (buf[0] == '7');
FileClose(&f);
FileRemove(&path);- In
File.c:756:
Str body = StrInit(alloc_base);
i64 got = FileRead(&f, &body);
FileClose(&f);
ok = ok && (got == 6) && (StrLen(&body) == 6) && ZstrCompare(StrBegin(&body), "ABCDEF") == 0;- In
File.c:787:
Str body = StrInit(alloc_base);
i64 got = FileRead(&f, &body);
FileClose(&f);
ok = ok && (got == (i64)ZstrLen(payload)) && (StrLen(&body) == (size)ZstrLen(payload)) &&- In
File.c:828:
}
ok = ok && (FileWrite(&f, BufData(&src), (u64)N) == (i64)N);
FileClose(&f);
File r = FileOpen(&path, "rb");- In
File.c:834:
Buf dst = BufInit(alloc_base);
i64 got = FileRead(&r, &dst);
FileClose(&r);
ok = ok && (got == (i64)N) && (BufLength(&dst) == (size)N) && (MemCompare(BufData(&dst), BufData(&src), N) == 0);- In
File.c:865:
return false;
}
FileClose(&seed);
Zstr payload = "payload-through-the-convenience-API";- In
File.c:928:
return false;
}
FileClose(&seed);
Buf in = BufInit(alloc_base);- In
File.c:963:
return false;
}
FileClose(&seed);
Str in = StrInit(alloc_base);- In
File.c:1006:
char buf[10] = {0};
ok = ok && (FileRead(&f, buf, 9) == 9) && (MemCompare(buf, "temp-data", 9) == 0);
FileClose(&f);
FileRemove(&path);- In
File.c:1037:
ok = ok && (FileWrite(&f1, "one", 3) == 3);
ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
FileClose(&f1);
FileClose(&f2);- In
File.c:1038:
ok = ok && (FileWrite(&f2, "twotwo", 6) == 6);
FileClose(&f1);
FileClose(&f2);
Str b1 = StrInit(alloc_base);- In
File.c:1076:
// Open handle flushes true.
bool ok = (FileFlush(&f) == true);
FileClose(&f);
// Closed handle must flush false (the open gate fires).
ok = ok && (FileFlush(&f) == false);- In
File.c:1103:
File seed = FileOpenTemp(&path, alloc_base);
if (FileIsOpen(&seed)) {
FileClose(&seed);
}- In
File.c:1188:
// ---------------------------------------------------------------------------
bool test_close_releases_fd_slot(void) {
WriteFmt("Testing FileClose actually releases the fd (slot is reused)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:1201:
if (!FileIsOpen(&a) || !FileIsOpen(&b)) {
if (FileIsOpen(&a)) {
FileClose(&a);
FileRemove(&pa);
StrDeinit(&pa);- In
File.c:1206:
}
if (FileIsOpen(&b)) {
FileClose(&b);
FileRemove(&pb);
StrDeinit(&pb);- In
File.c:1218:
// Really close A. On clean source this frees fd_a; the ge_to_lt mutant
// skips close() so the kernel still holds fd_a open.
bool ok = FileClose(&a);
// Reopen a fresh temp. POSIX returns the lowest free fd: on clean source
- In
File.c:1229:
bool reuse = (fd_c == fd_a);
FileClose(&b);
FileClose(&c);
FileRemove(&pa);- In
File.c:1230:
FileClose(&b);
FileClose(&c);
FileRemove(&pa);
FileRemove(&pb);- In
ArgParse.c:632:
FileSeek(&tmp, 0, FILE_SEEK_SET);
FileRead(&tmp, out);
FileClose(&tmp);
}- In
ArgParse.c:901:
FileSeek(&tmp, 0, FILE_SEEK_SET);
FileRead(&tmp, out);
FileClose(&tmp);
return ok;
}- In
ArgParse.c:1786:
FileSeek(&tmp, 0, FILE_SEEK_SET);
FileRead(&tmp, out);
FileClose(&tmp);
return rc;
}- In
Write.c:981:
static void m4_cleanup(File *f, Str *path) {
FileClose(f);
FileRemove(path);
StrDeinit(path);- In
Write.c:1017:
ok = (ZstrCompare(StrBegin(&back), expect) == 0);
StrDeinit(&back);
FileClose(&f);
}
DefaultAllocatorDeinit(&alloc);- In
Write.c:3039:
Zstr s = "World";
bool ret = FWriteFmt(&f, "Hello {}!", s);
FileClose(&f);
// Real: write succeeds -> ret true, file holds the exact rendered line.
- In
Write.c:3056:
i32 n = 12345;
bool ret = FWriteFmt(&f, "n={}", n);
FileClose(&f);
return ret && roundtrip_eq(path, "n=12345");- In
Write.c:3072:
bool ret = FWriteFmtLn(&f, "line");
FileClose(&f);
return ret && roundtrip_eq(path, "line\n");- In
Write.c:4995:
if (ok) {
ok = ok && FWriteFmtLn(&f, "n={}", LVAL((i32)42));
FileClose(&f);
File r = FileOpen(StrBegin(&path), "r");
if (FileIsOpen(&r)) {- In
Write.c:5002:
ok = ok && (ZstrCompare(StrBegin(&back), "n=42\n") == 0);
StrDeinit(&back);
FileClose(&r);
} else {
ok = false;- In
Write.c:5026:
ok = ok && FWriteFmt(&f, "");
ok = ok && FWriteFmt(&f, "X");
FileClose(&f);
File r = FileOpen(StrBegin(&path), "r");
if (FileIsOpen(&r)) {- In
Write.c:5033:
ok = ok && (ZstrCompare(StrBegin(&back), "X") == 0);
StrDeinit(&back);
FileClose(&r);
} else {
ok = false;- In
Read.c:49:
static void m4_cleanup(File *f, Str *path) {
FileClose(f);
FileRemove(path);
StrDeinit(path);- In
Read.c:2691:
FReadFmt(&f, "{}", v);
ok = (v == 42);
FileClose(&f);
FileRemove(&path);
}- In
Read.c:2716:
i64 got = FileRead(&f, &c, 1);
ok = (v == 123) && (got == 1) && (c == 'x');
FileClose(&f);
FileRemove(&path);
}- In
PdbCache.c:93:
return false;
bool ok = (u64)FileWrite(&f, data, size) == size;
FileClose(&f);
return ok;
}- In
SysDns.c:106:
FileWrite(&f, body, n);
}
FileClose(&f);
}
return path;- In
MachoCache.c:48:
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;
}- In
MachoCache.c:310:
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;
}- In
MachoCache.c:927:
return false;
bool ok = FileWrite(&f, data, size) == (i64)size;
FileClose(&f);
return ok;
}- In
Pe.c:1657:
return false;
}
FileClose(&f);
if (FileWriteAndClose((Zstr)StrBegin(&path), blob, sizeof(blob)) < 0) {
FileRemove(&path);- In
Pe.c:1699:
return false;
}
FileClose(&f);
if (FileWriteAndClose((Zstr)StrBegin(&path), junk, sizeof(junk)) < 0) {
FileRemove(&path);- In
ProcMaps.c:590:
wrote_ok = (w == (i64)ZstrLen(late));
}
FileClose(&f);
if (!wrote_ok) {
FileRemove(&path);- In
ProcMaps.c:608:
ProcMaps m;
bool loaded = ProcMapsLoadFrom(&m, &rf, &alloc);
FileClose(&rf);
bool ok = loaded;- In
ProcMaps.c:649:
ProcMaps m;
bool loaded = ProcMapsLoadFrom(&m, &dir, base);
FileClose(&dir);
// The unreadable directory makes the chunked read fail, so the loader
- In
Http.c:219:
Zstr payload = "hello-body";
ok = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
FileClose(&f);
HttpResponse response = HttpResponseInit(alloc_base);- In
Http.c:246:
File f = FileOpenTemp(&path, alloc_base);
bool ok = FileIsOpen(&f);
FileClose(&f); // leave it empty
HttpResponse response = HttpResponseInit(alloc_base);- In
Http.c:392:
Zstr payload = "file-payload-long-enough-to-heap-allocate-body";
ok = ok && (FileWrite(&f, payload, ZstrLen(payload)) == (i64)ZstrLen(payload));
FileClose(&f);
// Serving the file must free the old (HTML) body before installing the new.
- In
MachO.c:1412:
return false;
}
FileClose(&seed);
u8 buf[HDR_SIZE + SEG64_HDR];- In
MachO.c:1454:
return false;
}
FileClose(&seed);
// Bad magic (not a Mach-O), but enough bytes to read successfully.
Last updated on