FileOpenTemp
Description
Open a unique temporary file for read+write. The name is the 16-hex-digit of Prng64() – no caller-supplied prefix, no kernel entropy per call. Open is O_RDWR | O_CREAT | O_EXCL | 0600, so two callers racing can never collide – one wins, the other retries with a new draw. The file lands in the process’s current working directory.
Two forms via argument-count overload (allocator backs out_path):
FileOpenTemp(out_path)– inside aScope; allocator is
MisraScope.
FileOpenTemp(out_path, allocator)– explicit allocator.
Parameters
| Name | Direction | Description |
|---|---|---|
out_path |
out | Fresh Str * – receives the resolved 16-char hex name. Caller StrDeinits when done; the on-disk file is NOT auto-removed (use FileRemove(out_path)). |
Success
Returns an open File with FileIsOpen true.
Failure
Returns a File where FileIsOpen is false.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
File.c:15:
// caller can read it back and remove it.
static bool write_test_file(Zstr text, Str *out_path, Allocator *alloc) {
File f = FileOpenTemp(out_path, alloc);
if (!FileIsOpen(&f)) {
return false;- In
File.c:175:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:325:
// then write to it through the convenience API.
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:359:
// Mint a unique name then remove it so the path is guaranteed absent.
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:511:
Str path;
File owner = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&owner)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:553:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:579:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:609:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:707:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:814:
// Build a 10000-byte payload (> FILE_READ_CHUNK=4096) on disk.
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:860:
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:923:
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:958:
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:990:
// file with a non-empty path that round-trips a payload.
bool test_fm_602_temp_open_roundtrips(void) {
WriteFmt("Testing FileOpenTemp creates an open, writable, named file\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:996:
Str path;
File f = FileOpenTemp(&path, alloc_base);
bool ok = FileIsOpen(&f);
// The resolved path must be non-empty (16 hex chars).
- In
File.c:1019:
// hex name, owns the new fd).
bool test_fm_547_temp_names_distinct(void) {
WriteFmt("Testing two FileOpenTemp calls yield distinct, independent files\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
File.c:1025:
Str p1;
File f1 = FileOpenTemp(&p1, alloc_base);
Str p2;
File f2 = FileOpenTemp(&p2, alloc_base);- In
File.c:1027:
File f1 = FileOpenTemp(&p1, alloc_base);
Str p2;
File f2 = FileOpenTemp(&p2, alloc_base);
bool ok = FileIsOpen(&f1) && FileIsOpen(&f2);- In
File.c:1069:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
File.c:1101:
Str path;
File seed = FileOpenTemp(&path, alloc_base);
if (FileIsOpen(&seed)) {
FileClose(&seed);- In
File.c:1196:
// middle of the table. Record A's fd.
Str pa;
File a = FileOpenTemp(&pa, alloc_base);
Str pb;
File b = FileOpenTemp(&pb, alloc_base);- In
File.c:1198:
File a = FileOpenTemp(&pa, alloc_base);
Str pb;
File b = FileOpenTemp(&pb, alloc_base);
if (!FileIsOpen(&a) || !FileIsOpen(&b)) {
if (FileIsOpen(&a)) {- In
File.c:1224:
// occupied so we get a strictly higher number.
Str pc;
File c = FileOpenTemp(&pc, alloc_base);
ok = ok && FileIsOpen(&c);
i32 fd_c = FileFd(&c);- In
ArgParse.c:617:
static void capture_help(ArgParse *p, Str *out) {
Str tmp_path = StrInit(p->alloc);
File tmp = FileOpenTemp(&tmp_path, p->alloc);
StrDeinit(&tmp_path);
if (!FileIsOpen(&tmp))- In
ArgParse.c:620:
StrDeinit(&tmp_path);
if (!FileIsOpen(&tmp))
LOG_FATAL("capture_help: FileOpenTemp failed");
p->out = &tmp;- In
ArgParse.c:888:
static bool capture_help_file(ArgParse *p, Str *out) {
Str tmp_path = StrInit(p->alloc);
File tmp = FileOpenTemp(&tmp_path, p->alloc);
StrDeinit(&tmp_path);
if (!FileIsOpen(&tmp))- In
ArgParse.c:1775:
static ArgRun capture_run(ArgParse *p, int argc, char **argv, Str *out) {
Str tmp_path = StrInit(p->alloc);
File tmp = FileOpenTemp(&tmp_path, p->alloc);
StrDeinit(&tmp_path);
if (!FileIsOpen(&tmp))- In
ArgParse.c:1778:
StrDeinit(&tmp_path);
if (!FileIsOpen(&tmp))
LOG_FATAL("capture_run: FileOpenTemp failed");
p->out = &tmp;- In
Write.c:972:
// caller can FileRemove + StrDeinit it afterwards.
static File m4_make_temp(DefaultAllocator *alloc, Str *out_path, const char *content, u64 len) {
File f = FileOpenTemp(out_path, alloc);
if (!FileIsOpen(&f))
return f;- In
Write.c:4991:
DefaultAllocator alloc = DefaultAllocatorInit();
Str path = StrInit(&alloc);
File f = FileOpenTemp(&path, &alloc);
bool ok = FileIsOpen(&f);
if (ok) {- In
Write.c:5020:
DefaultAllocator alloc = DefaultAllocatorInit();
Str path = StrInit(&alloc);
File f = FileOpenTemp(&path, &alloc);
bool ok = FileIsOpen(&f);
if (ok) {- In
Read.c:40:
// caller can FileRemove + StrDeinit it afterwards.
static File m4_make_temp(DefaultAllocator *alloc, Str *out_path, const char *content, u64 len) {
File f = FileOpenTemp(out_path, alloc);
if (!FileIsOpen(&f))
return f;- In
Read.c:2683:
DefaultAllocator alloc = DefaultAllocatorInit();
Str path = StrInit(&alloc);
File f = FileOpenTemp(&path, &alloc);
bool ok = FileIsOpen(&f);
if (ok) {- In
Read.c:2705:
DefaultAllocator alloc = DefaultAllocatorInit();
Str path = StrInit(&alloc);
File f = FileOpenTemp(&path, &alloc);
bool ok = FileIsOpen(&f);
if (ok) {- In
Pe.c:1651:
Str path = StrInit(base);
File f = FileOpenTemp(&path, base);
if (!FileIsOpen(&f)) {
StrDeinit(&path);- In
Pe.c:1693:
Str path = StrInit(base);
File f = FileOpenTemp(&path, base);
if (!FileIsOpen(&f)) {
StrDeinit(&path);- In
ProcMaps.c:571:
Str path;
File f = FileOpenTemp(&path, alloc_base);
if (!FileIsOpen(&f)) {
DefaultAllocatorDeinit(&alloc);- In
Http.c:214:
Str path = StrInit(alloc_base);
File f = FileOpenTemp(&path, alloc_base);
bool ok = FileIsOpen(&f);- In
Http.c:244:
Str path = StrInit(alloc_base);
File f = FileOpenTemp(&path, alloc_base);
bool ok = FileIsOpen(&f);
FileClose(&f); // leave it empty
- In
Http.c:388:
// Create a temp file with content.
Str path = StrInit(adbg);
File f = FileOpenTemp(&path, adbg);
bool ok = FileIsOpen(&f);
Zstr payload = "file-payload-long-enough-to-heap-allocate-body";- In
MachO.c:1407:
// Mint a unique temp path, then write a minimal valid Mach-O to it.
Str path;
File seed = FileOpenTemp(&path, base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);- In
MachO.c:1449:
Str path;
File seed = FileOpenTemp(&path, base);
if (!FileIsOpen(&seed)) {
DefaultAllocatorDeinit(&alloc);
Last updated on