Files
sx/library/modules/std/fs.sx
agra 1d17b0abcf lang: introduce cstring — the C-boundary string (Odin model)
cstring is ONE pointer to a null-terminated u8 buffer, C's char*: thin
(8 bytes, no length; cstring_len walks to the terminator), crossing
#foreign boundaries verbatim in both directions, with ?cstring as the
nullable case lowering to the same bare pointer (null = absent).

Conversion discipline mirrors Odin: a string LITERAL coerces implicitly
(its bytes are terminated constants); any other string is rejected with
a diagnostic naming to_cstring (it may be an unterminated view); and
cstring never coerces to string implicitly — from_cstring(c) is the
explicit zero-copy view, pricing the strlen.

Plumbing: TypeId/TypeInfo builtin slot 18 (first_user 19), name
classifiers, size/align/name tables, LLVM ptr lowering, the ?T pointer
niche, the xx pointer ladder, the literal-gated coercion plan
(isConstString + data_ptr), and the reserved-spelling set. std gains
cstring_len/from_cstring/to_cstring (fmt.sx, re-exported); the old
cstring(size) allocator helper is renamed alloc_string everywhere;
getenv migrates to (name: cstring) -> ?cstring as the canonical user
and env() drops its manual strlen/memcpy.

Pinned: examples/1222 (FFI both directions, literal coercion,
?cstring null paths, round trip) and examples/1173 (both coercion
diagnostics); FAIL pre-feature. The alloc_string rename + getenv
signature shift the .ir snapshots — regenerated. zig build test
426/426; run_examples 604/604.

Spec: reserved spelling + cstring section + C-interop rows.
2026-06-12 14:50:53 +03:00

269 lines
8.7 KiB
Plaintext

#import "modules/std.sx";
// =====================================================================
// fs.sx — file system stdlib (POSIX backend, macOS values).
//
// Allocation contract: every returned `string` or slice is allocated
// from `context.allocator`. Callers are responsible for releasing it
// (typically via an arena reset).
//
// Handle ownership: `File` is a small value-typed handle wrapping the
// POSIX file descriptor. Methods are provided for read/write/close;
// the value is invalid (fd == -1) after `close()`.
//
// Scope (Phase 1A): file I/O + directory creation/deletion + path
// helpers needed for `.app` bundling. Recursive walkers, `stat`, and
// the full path module land in subsequent phases.
// =====================================================================
libc :: #library "c";
// ── Low-level libc bindings ─────────────────────────────────────────
// These declare the actual libc symbols and must use the libc names
// verbatim (no prefix), so they live at module top-level. The public
// API below wraps them. Users should not call these directly.
//
// macOS `open` is variadic in C (`int open(const char*, int, ...)`);
// declared with `..args: []i32` so the mode is passed via the C
// variadic tail. Without that, the mode arg goes to the wrong
// register on arm64 and the file ends up with mode 0.
open :: (path: [:0]u8, flags: i32, ..args: []i32) -> i32 #foreign libc;
close :: (fd: i32) -> i32 #foreign libc;
read :: (fd: i32, buf: [*]u8, count: usize) -> isize #foreign libc;
write :: (fd: i32, buf: [*]u8, count: usize) -> isize #foreign libc;
lseek :: (fd: i32, offset: i64, whence: i32) -> i64 #foreign libc;
unlink :: (path: [:0]u8) -> i32 #foreign libc;
rmdir :: (path: [:0]u8) -> i32 #foreign libc;
mkdir :: (path: [:0]u8, mode: u32) -> i32 #foreign libc;
access :: (path: [:0]u8, mode: i32) -> i32 #foreign libc;
chmod :: (path: [:0]u8, mode: u32) -> i32 #foreign libc;
rename :: (oldp: [:0]u8, newp: [:0]u8) -> i32 #foreign libc;
// macOS POSIX constants. Linux values differ; split into platform-
// conditional includes when we gain a Linux host.
O_RDONLY :i32: 0x0000;
O_WRONLY :i32: 0x0001;
O_RDWR :i32: 0x0002;
O_APPEND :i32: 0x0008;
O_CREAT :i32: 0x0200;
O_TRUNC :i32: 0x0400;
SEEK_SET :i32: 0;
SEEK_CUR :i32: 1;
SEEK_END :i32: 2;
F_OK :i32: 0;
// ── Public types ─────────────────────────────────────────────────────
OpenMode :: enum {
read; // O_RDONLY
write; // O_WRONLY | O_CREAT | O_TRUNC
append; // O_WRONLY | O_CREAT | O_APPEND
read_write; // O_RDWR
}
SeekFrom :: enum { set; current; end; }
File :: struct {
fd: i32 = -1;
is_valid :: (self: *File) -> bool { self.fd >= 0 }
close :: (self: *File) -> bool {
if self.fd < 0 { return false; }
rc := close(self.fd);
self.fd = -1;
rc == 0
}
read :: (self: *File, buf: string) -> i64 {
if self.fd < 0 { return -1; }
n := read(self.fd, buf.ptr, xx buf.len);
cast(i64) n
}
write :: (self: *File, data: string) -> i64 {
if self.fd < 0 { return -1; }
n := write(self.fd, data.ptr, xx data.len);
cast(i64) n
}
seek :: (self: *File, offset: i64, whence: SeekFrom) -> i64 {
if self.fd < 0 { return -1; }
w := SEEK_SET;
if whence == .current { w = SEEK_CUR; }
if whence == .end { w = SEEK_END; }
lseek(self.fd, offset, w)
}
}
// ── High-level file API ─────────────────────────────────────────────
// Named `open_file` (not `open`) so they don't shadow libc's `open`
// symbol; the latter is needed for `#foreign libc` to resolve. Same
// idea for `delete_file`/`delete_dir` vs libc's `unlink`/`rmdir`,
// `set_mode` vs libc's `chmod`, etc.
mode_to_flags :: (m: OpenMode) -> i32 {
if m == .read { return O_RDONLY; }
if m == .write { return O_WRONLY | O_CREAT | O_TRUNC; }
if m == .append { return O_WRONLY | O_CREAT | O_APPEND; }
if m == .read_write { return O_RDWR; }
O_RDONLY
}
open_file :: (path: [:0]u8, mode: OpenMode) -> ?File {
fd := open(path, mode_to_flags(mode), 420); // 0o644 = 420
if fd < 0 { return null; }
File.{ fd = fd }
}
// One-shot read: opens, slurps the whole file into a fresh buffer,
// closes. Returns null on any failure. Uses libc directly (not File
// methods) so it remains callable from the post-link IR interpreter,
// which doesn't yet handle `*Self` method dispatch on locally-
// unwrapped optionals.
read_file :: (path: [:0]u8) -> ?string {
fd := open(path, O_RDONLY, 0);
if fd < 0 { return null; }
size := lseek(fd, 0, SEEK_END);
if size < 0 { close(fd); return null; }
lseek(fd, 0, SEEK_SET);
buf := alloc_string(size);
n := read(fd, buf.ptr, xx size);
close(fd);
if cast(i64) n != size { return null; }
buf
}
// One-shot write: creates / truncates and writes the whole buffer.
write_file :: (path: [:0]u8, data: string) -> bool {
fd := open(path, O_WRONLY | O_CREAT | O_TRUNC, 420); // 0o644
if fd < 0 { return false; }
n := write(fd, data.ptr, xx data.len);
close(fd);
cast(i64) n == cast(i64) data.len
}
append_file :: (path: [:0]u8, data: string) -> bool {
fd := open(path, O_WRONLY | O_CREAT | O_APPEND, 420);
if fd < 0 { return false; }
n := write(fd, data.ptr, xx data.len);
close(fd);
cast(i64) n == cast(i64) data.len
}
// ── Single-syscall ops ───────────────────────────────────────────────
exists :: (path: [:0]u8) -> bool {
access(path, F_OK) == 0
}
delete_file :: (path: [:0]u8) -> bool {
unlink(path) == 0
}
delete_dir :: (path: [:0]u8) -> bool {
rmdir(path) == 0
}
create_dir :: (path: [:0]u8) -> bool {
mkdir(path, 493) == 0 // 0o755 = 493
}
set_mode :: (path: [:0]u8, mode: u32) -> bool {
chmod(path, mode) == 0
}
move :: (oldp: [:0]u8, newp: [:0]u8) -> bool {
rename(oldp, newp) == 0
}
// Recursive mkdir -p. Walks the path and creates each missing
// segment. Treats existing directories as success.
create_dir_all :: (path: [:0]u8) -> bool {
if path.len == 0 { return true; }
if exists(path) { return true; }
last := path.len - 1;
while last > 0 {
if path[last] == 47 { break; }
last -= 1;
}
if last > 0 {
parent := alloc_string(last);
memcpy(parent.ptr, path.ptr, last);
if !create_dir_all(parent) { return false; }
}
create_dir(path)
}
// Copy a file by streaming through a 64KB buffer. Uses libc directly
// (not File methods) — same interpreter-compat reason as read_file.
// No metadata is preserved beyond what `open` creates (mode 0644).
// Caller is responsible for setting executable bits with `set_mode`.
copy_file :: (src: [:0]u8, dst: [:0]u8) -> bool {
src_fd := open(src, O_RDONLY, 0);
if src_fd < 0 { return false; }
dst_fd := open(dst, O_WRONLY | O_CREAT | O_TRUNC, 420);
if dst_fd < 0 {
close(src_fd);
return false;
}
ok := true;
buf := alloc_string(65536);
loop := true;
while loop {
n := read(src_fd, buf.ptr, 65536);
if n < 0 { ok = false; loop = false; }
if n == 0 { loop = false; }
if n > 0 {
w := write(dst_fd, buf.ptr, xx n);
if w != cast(isize) n { ok = false; loop = false; }
}
}
close(src_fd);
close(dst_fd);
ok
}
// ── Path helpers ─────────────────────────────────────────────────────
// `path_join` is in std.sx (used widely beyond fs). These are the
// fs-adjacent helpers — basename/dirname operate purely on text.
basename :: (p: string) -> string {
if p.len == 0 { return ""; }
last := p.len - 1;
while last > 0 {
if p[last] != 47 { break; }
last -= 1;
}
end := last + 1;
while last > 0 {
if p[last - 1] == 47 { return substr(p, last, end - last); }
last -= 1;
}
substr(p, 0, end)
}
dirname :: (p: string) -> string {
if p.len == 0 { return ""; }
last := p.len - 1;
while last > 0 {
if p[last] != 47 { break; }
last -= 1;
}
while last > 0 {
if p[last] == 47 {
while last > 0 {
if p[last - 1] != 47 { break; }
last -= 1;
}
return substr(p, 0, last);
}
last -= 1;
}
if p[0] == 47 { return "/"; }
"."
}