Hare-shaped filestat introspection. New types: filestat (80B, mirrors fs::filestat ref/hare/fs/types.ha:141), mode (31-member enum mirroring fs::mode ref/hare/fs/types.ha:63), stat_mask (7 bits mirroring fs::stat_mask ref/hare/fs/types.ha:129), timespec (i64+i64, layout-compatible with future lib/time::instant). APIs: stat / lstat / fstat (*filestat, *u8|i32) (void|oserror) over SYS_newfstatat (nr=262). The out-param shape sidesteps the cgreturn 24B ABI cap; commented inline. exists(*u8) bool goes through the syscall directly rather than wrapping stat()? — dodges task #22's 80B-scrutinee match-slot disagreement until that lands. Three latent cgen workarounds in tree, all pointer'd to filed tasks: #22: os.exists sidesteps the (void|oserror) match shape #24: `at` enum bundles AT_FDCWD/SYMLINK_NOFOLLOW/EMPTY_PATH instead of three top-level `def`s (negative-literal def DATA omit) #25: kstat.mode typed as `mode` (enum) rather than u32 to skip the redundant u32→enum cast emit Tests: 976_stat_run, 9 rows — stat/lstat/fstat × regfile/dir/symlink plus exists × {regfile,dir,noent}. Row 1 also pins perm-bit and atime/mtime/ctime!=0 to catch silent kstat→filestat offset miscompiles (kstat fields at 72/88/104). Graduation to lib/fs when it ships is noted inline; signatures stay rename-compatible.
186 lines
5.8 KiB
Plaintext
186 lines
5.8 KiB
Plaintext
// stattest — exercises [[os.stat]] / [[os.lstat]] / [[os.fstat]] /
|
|
// [[os.exists]] against a scratch tree pre-arranged by
|
|
// test/wcc/976_stat_run.c. The driver mkdtemp's a tmp dir, creates a
|
|
// regfile + symlink + subdir, exports paths through env vars, then
|
|
// exec's `ww run lib/os/stattest.ww`. We read the paths back via
|
|
// os.getenv and stat the targets.
|
|
//
|
|
// Pre-arranged scratch tree (set by 976_stat_run.c):
|
|
//
|
|
// WW_TEST_STAT_REGFILE = "<tmp>/regfile" regular file, 11 bytes
|
|
// WW_TEST_STAT_SYMLINK = "<tmp>/symlink" symlink → ./regfile
|
|
// WW_TEST_STAT_SUBDIR = "<tmp>/subdir" directory, 0700
|
|
// WW_TEST_STAT_NOENT = "<tmp>/does-not-exist"
|
|
//
|
|
// Test exit code follows the stdlib `_run` convention: a `signalled`
|
|
// global the main fn bumps before each row, so `WEXITSTATUS = signalled
|
|
// + 10` tells the harness which row tripped. Same shape as
|
|
// ostest / temptest / shlextest.
|
|
|
|
use os;
|
|
|
|
let signalled: i32 = 0;
|
|
|
|
fn fail() void = { os.exit(signalled + 10); };
|
|
|
|
// envpath — fetch an env var or abort; we want a clean signal if
|
|
// 976_stat_run.c didn't prime the env state.
|
|
fn envpath(name: str) str = {
|
|
match (os.getenv(name)) {
|
|
case void => { fail(); return "": str; };
|
|
case let s: str => return s;
|
|
};
|
|
};
|
|
|
|
// istype — mask the file-type bits (S_IFMT = 0o170000 = 61440)
|
|
// out of a mode and compare against the requested type bit.
|
|
fn istype(m: os.mode, t: os.mode) bool = {
|
|
return ((m as u32) & 61440u32) == (t as u32);
|
|
};
|
|
|
|
// ---- stat: regular file --------------------------------------------
|
|
//
|
|
// Pinned bytes are "hello world" (11 bytes). We verify:
|
|
// - mask is fully set (newfstatat fills everything)
|
|
// - mode's type bits == REG
|
|
// - sz == 11
|
|
// - inode is non-zero (real fs entry, not synthetic)
|
|
|
|
@test fn test_stat_regfile() void = {
|
|
let p = envpath("WW_TEST_STAT_REGFILE");
|
|
let fi: os.filestat;
|
|
// newfstatat populates every field, so the mask is the
|
|
// OR-fold of all 7 Hare stat_mask bits — mirrors what
|
|
// fillfilestat assigns.
|
|
let wantmask: u32 = (os.stat_mask.UID | os.stat_mask.GID
|
|
| os.stat_mask.SIZE | os.stat_mask.INODE
|
|
| os.stat_mask.ATIME | os.stat_mask.MTIME
|
|
| os.stat_mask.CTIME) as u32;
|
|
match (os.stat(&fi, p.ptr)) {
|
|
case let e: os.oserror => fail();
|
|
case void => {
|
|
if ((fi.mask as u32) != wantmask) { fail(); };
|
|
if (!istype(fi.mode, os.mode.REG)) { fail(); };
|
|
if (fi.sz != 11u64) { fail(); };
|
|
if (fi.inode == 0u64) { fail(); };
|
|
// Permission-bit smoke test — 976_stat_run.c open(2)s with
|
|
// mode 0644 so USER_R survives any reasonable umask. Catches
|
|
// a struct-field-offset miscompile on `fi.mode` that the
|
|
// type-bit istype() check could miss if perm bits aliased a
|
|
// neighbouring u32 (uid/gid).
|
|
if (((fi.mode as u32) & (os.mode.USER_R as u32)) == 0u32) {
|
|
fail();
|
|
};
|
|
// atime/mtime/ctime — kernel-set at create time, all
|
|
// post-epoch (>0). Three distinct kstat offsets (72/88/104)
|
|
// so a fillfilestat field-copy miscompile or a filestat
|
|
// timespec-offset bug surfaces here, not silently.
|
|
if (fi.atime.sec <= 0i64) { fail(); };
|
|
if (fi.mtime.sec <= 0i64) { fail(); };
|
|
if (fi.ctime.sec <= 0i64) { fail(); };
|
|
};
|
|
};
|
|
};
|
|
|
|
// ---- stat: directory ------------------------------------------------
|
|
|
|
@test fn test_stat_subdir() void = {
|
|
let p = envpath("WW_TEST_STAT_SUBDIR");
|
|
let fi: os.filestat;
|
|
match (os.stat(&fi, p.ptr)) {
|
|
case let e: os.oserror => fail();
|
|
case void => {
|
|
if (!istype(fi.mode, os.mode.DIR)) { fail(); };
|
|
};
|
|
};
|
|
};
|
|
|
|
// ---- stat: missing path → oserror ENOENT ---------------------------
|
|
|
|
@test fn test_stat_noent() void = {
|
|
let p = envpath("WW_TEST_STAT_NOENT");
|
|
let fi: os.filestat;
|
|
match (os.stat(&fi, p.ptr)) {
|
|
case void => fail();
|
|
case let e: os.oserror => {
|
|
// ENOENT = 2 → raw errno is -2.
|
|
if ((e: i64) != -2i64) { fail(); };
|
|
};
|
|
};
|
|
};
|
|
|
|
// ---- stat (follow) vs lstat (no-follow) on a symlink ----------------
|
|
//
|
|
// stat follows the link → reports the regfile (REG, 11 bytes).
|
|
// lstat does NOT follow → reports the link itself (LINK).
|
|
|
|
@test fn test_stat_symlink_follow() void = {
|
|
let p = envpath("WW_TEST_STAT_SYMLINK");
|
|
let fi: os.filestat;
|
|
match (os.stat(&fi, p.ptr)) {
|
|
case let e: os.oserror => fail();
|
|
case void => {
|
|
if (!istype(fi.mode, os.mode.REG)) { fail(); };
|
|
if (fi.sz != 11u64) { fail(); };
|
|
};
|
|
};
|
|
};
|
|
|
|
@test fn test_lstat_symlink_nofollow() void = {
|
|
let p = envpath("WW_TEST_STAT_SYMLINK");
|
|
let fi: os.filestat;
|
|
match (os.lstat(&fi, p.ptr)) {
|
|
case let e: os.oserror => fail();
|
|
case void => {
|
|
if (!istype(fi.mode, os.mode.LINK)) { fail(); };
|
|
};
|
|
};
|
|
};
|
|
|
|
// ---- fstat: open a file and stat by fd ------------------------------
|
|
|
|
@test fn test_fstat_regfile() void = {
|
|
let p = envpath("WW_TEST_STAT_REGFILE");
|
|
let fd: i32 = os.open(p.ptr, os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { fail(); };
|
|
let fi: os.filestat;
|
|
match (os.fstat(&fi, fd)) {
|
|
case let e: os.oserror => { os.close(fd); fail(); };
|
|
case void => {
|
|
if (!istype(fi.mode, os.mode.REG)) { os.close(fd); fail(); };
|
|
if (fi.sz != 11u64) { os.close(fd); fail(); };
|
|
};
|
|
};
|
|
os.close(fd);
|
|
};
|
|
|
|
// ---- exists: true on regfile/dir/symlink, false on noent ------------
|
|
|
|
@test fn test_exists_regfile() void = {
|
|
let p = envpath("WW_TEST_STAT_REGFILE");
|
|
if (!os.exists(p.ptr)) { fail(); };
|
|
};
|
|
|
|
@test fn test_exists_subdir() void = {
|
|
let p = envpath("WW_TEST_STAT_SUBDIR");
|
|
if (!os.exists(p.ptr)) { fail(); };
|
|
};
|
|
|
|
@test fn test_exists_noent() void = {
|
|
let p = envpath("WW_TEST_STAT_NOENT");
|
|
if (os.exists(p.ptr)) { fail(); };
|
|
};
|
|
|
|
export fn main() i32 = {
|
|
signalled = 1; test_stat_regfile();
|
|
signalled = 2; test_stat_subdir();
|
|
signalled = 3; test_stat_noent();
|
|
signalled = 4; test_stat_symlink_follow();
|
|
signalled = 5; test_lstat_symlink_nofollow();
|
|
signalled = 6; test_fstat_regfile();
|
|
signalled = 7; test_exists_regfile();
|
|
signalled = 8; test_exists_subdir();
|
|
signalled = 9; test_exists_noent();
|
|
return 0;
|
|
};
|