FileOpen
Description
Open a file. mode accepts: "r"/"rb", "w"/"wb", "a"/"ab", "r+", "w+", "a+". Binary is always implied; the "b" suffix is accepted but has no effect on the implementation.
Parameters
| Name | Direction | Description |
|---|---|---|
path |
in | Path to open. Prefer Str * (carries length, can’t silently drop the NUL terminator). Zstr is accepted as a literal / borrowed-buffer convenience. |
Success
Returns a File where FileIsOpen(&out) is true.
Failure
Returns a File where FileIsOpen(&out) is false.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
File.c:40:
(void)mode;
(void)out_flags;
return false; // Windows uses a different mode encoding (see FileOpen).
#else
if (!mode || !*mode) {- In
File.c:108:
HANDLE h = CreateFileA(path, access, FILE_SHARE_READ, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
LOG_ERROR("FileOpen: CreateFileA(\"{}\") failed (err {})", path, (u32)GetLastError());
return f;
}- In
File.c:121:
int flags;
if (!parse_open_mode(mode, &flags)) {
LOG_ERROR("FileOpen: invalid mode \"{}\"", mode);
return f;
}- In
File.c:157:
// 256-byte slot. Until that pipeline learns a default-allocator
// fall-through, surface the errno number directly here.
LOG_ERROR("FileOpen: open(\"{}\") failed (errno {})", path, ErrnoOf(fd));
return f;
}- In
Dns.c:101:
// with an empty Str.
static bool slurp_file(Zstr path, Str *out) {
File f = FileOpen(path, "rb");
if (!FileIsOpen(&f)) {
// Missing config file is fine -- resolver just won't know about it.
- In
_Helpers.h:23:
/// callers (cache lookups, sidecar discovery).
static inline bool sys_path_exists(Zstr path) {
File f = FileOpen(path, "rb");
if (!FileIsOpen(&f)) {
return false;- In
ProcMaps.c:235:
// `/proc/self/maps` reports stat-size 0 because it's generated by the
// kernel on read, so we loop-read into a growing buffer ourselves.
File f = FileOpen("/proc/self/maps", "rb");
if (!FileIsOpen(&f)) {
LOG_ERROR("ProcMapsLoad: FileOpen(/proc/self/maps) failed");- In
ProcMaps.c:237:
File f = FileOpen("/proc/self/maps", "rb");
if (!FileIsOpen(&f)) {
LOG_ERROR("ProcMapsLoad: FileOpen(/proc/self/maps) failed");
ProcMapsDeinit(out);
return false;- In
File.c:42:
}
File f = FileOpen(&path, "rb");
if (!FileIsOpen(&f)) {
FileRemove(&path);- In
File.c:76:
}
File f = FileOpen(&path, "rb");
if (!FileIsOpen(&f)) {
FileRemove(&path);- In
File.c:115:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
FileClose(&f);- In
File.c:136:
// An invalid mode string yields a File that reports not-open.
bool test_open_invalid_mode(void) {
WriteFmt("Testing FileOpen with an invalid mode returns a not-open file\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:149:
// "q" is not a recognised mode.
File f = FileOpen(&path, "q");
ok = ok && !FileIsOpen(&f);
FileClose(&f);- In
File.c:154:
// An empty mode is also rejected.
File g = FileOpen(&path, "");
ok = ok && !FileIsOpen(&g);
FileClose(&g);- In
File.c:224:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);- In
File.c:259:
}
File w = FileOpen(&path, "w");
ok = ok && FileIsOpen(&w);
ok = ok && (FileWrite(&w, "new", 3) == 3);- In
File.c:264:
FileClose(&w);
File r = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&r);
Str body = StrInit(alloc_base);- In
File.c:294:
}
File a = FileOpen(&path, "a");
ok = ok && FileIsOpen(&a);
ok = ok && (FileWrite(&a, "tail", 4) == 4);- In
File.c:299:
FileClose(&a);
File r = FileOpen(&path, "rb");
Str body = StrInit(alloc_base);
i64 got = FileRead(&r, &body);- In
File.c:396:
}
File f = FileOpen(&path, "r");
ok = ok && FileIsOpen(&f);
// A read-only handle must reject writes.
- In
File.c:423:
}
File f = FileOpen(&path, "r+");
ok = ok && FileIsOpen(&f);
// "r+" must permit writes (does not truncate). Overwrite first 3 bytes.
- In
File.c:431:
// Confirm the write landed.
Str body = StrInit(alloc_base);
File r = FileOpen(&path, "rb");
i64 got = FileRead(&r, &body);
FileClose(&r);- In
File.c:459:
}
File f = FileOpen(&path, "r");
ok = ok && FileIsOpen(&f);
char buf[8] = {0};- In
File.c:644:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
char buf[4] = {0};- In
File.c:677:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
char buf[8] = {0};- In
File.c:749:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
// Move the cursor to offset 10; remaining size must be 16 - 10 = 6.
- In
File.c:783:
}
File f = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&f);
Str body = StrInit(alloc_base);- In
File.c:830:
FileClose(&f);
File r = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&r);
Buf dst = BufInit(alloc_base);- In
Write.c:1010:
static bool roundtrip_eq(Zstr path, Zstr expect) {
DefaultAllocator alloc = DefaultAllocatorInit();
File f = FileOpen(path, "r");
bool ok = false;
if (FileIsOpen(&f)) {- In
Write.c:3032:
static bool test_m28_fwrite_roundtrip_string(void) {
Zstr path = "io_mutants28_str.txt"; // CWD-relative: portable (no /tmp on Windows)
File f = FileOpen(path, "w");
if (!FileIsOpen(&f)) {
return false;- In
Write.c:3049:
static bool test_m28_fwrite_roundtrip_int(void) {
Zstr path = "io_mutants28_int.txt"; // CWD-relative: portable (no /tmp on Windows)
File f = FileOpen(path, "w");
if (!FileIsOpen(&f)) {
return false;- In
Write.c:3066:
static bool test_m28_fwriteln_roundtrip(void) {
Zstr path = "io_mutants28_ln.txt"; // CWD-relative: portable (no /tmp on Windows)
File f = FileOpen(path, "w");
if (!FileIsOpen(&f)) {
return false;- In
Write.c:4996:
ok = ok && FWriteFmtLn(&f, "n={}", LVAL((i32)42));
FileClose(&f);
File r = FileOpen(StrBegin(&path), "r");
if (FileIsOpen(&r)) {
Str back = StrInit(&alloc);- In
Write.c:5027:
ok = ok && FWriteFmt(&f, "X");
FileClose(&f);
File r = FileOpen(StrBegin(&path), "r");
if (FileIsOpen(&r)) {
Str back = StrInit(&alloc);- In
PdbCache.c:89:
// Use Misra's File API so this test runs under -nostdlib too
// (no libc fopen/fwrite/fclose).
File f = FileOpen(path, "w");
if (!FileIsOpen(&f))
return false;- In
MachoCache.c:44:
static bool write_file(Zstr path, const u8 *data, u64 size) {
File f = FileOpen(path, "wb");
if (!FileIsOpen(&f))
return false;- In
MachoCache.c:306:
static bool mc_write_file(Zstr path, const u8 *data, u64 size) {
File f = FileOpen(path, "wb");
if (!FileIsOpen(&f))
return false;- In
MachoCache.c:923:
static bool bl_write_file(Zstr path, const u8 *data, u64 size) {
File f = FileOpen(path, "wb");
if (!FileIsOpen(&f))
return false;- In
ProcMaps.c:598:
}
File rf = FileOpen(&path, "rb");
if (!FileIsOpen(&rf)) {
FileRemove(&path);- In
ProcMaps.c:639:
// A directory: open(2) succeeds (FileIsOpen true) but read(2) returns -1.
File dir = FileOpen("/proc/self", "rb");
if (!FileIsOpen(&dir)) {
DebugAllocatorDeinit(&dbg);
Last updated on