lib/os+test: add stat / lstat / fstat / exists

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.
This commit is contained in:
2026-05-16 02:42:41 +09:00
parent db2b05bbe5
commit 2f9d6dc43a
10 changed files with 1998 additions and 0 deletions

107
test/wcc/976_stat_run.c Normal file
View File

@@ -0,0 +1,107 @@
/*
* 976_stat_run — execute the lib/os stat fixture under the C-side
* `ww run` driver and assert exit 0.
*
* Workflow (mirrors 975_dirs_run's mkdtemp + setenv + rm-rf shape):
* 1. mkdtemp /tmp/wwstat-XXXXXX — fresh per run, no parallel-test
* contention.
* 2. Populate the scratch tree:
* <tmp>/regfile regular file, 11 bytes "hello world"
* <tmp>/symlink → ./regfile (relative symlink)
* <tmp>/subdir directory, mode 0700
* 3. setenv WW_TEST_STAT_REGFILE / SYMLINK / SUBDIR / NOENT with
* the absolute paths; lib/os/stattest.ww reads them via
* os.getenv and exercises stat/lstat/fstat/exists.
* 4. exec `ww run lib/os/stattest.ww`.
* 5. system("rm -rf <tmp>") regardless of pass/fail.
*
* Same wrapper shape as 970-975; cleanup-on-failure is rm-rf'd so a
* regression never leaves residue under /tmp.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char tmpl[64];
strcpy(tmpl, "/tmp/wwstat-XXXXXX");
if (mkdtemp(tmpl) == NULL) {
perror("mkdtemp");
return 1;
}
char regfile[256], symlink_path[256], subdir[256], noent[256];
snprintf(regfile, sizeof regfile, "%s/regfile", tmpl);
snprintf(symlink_path, sizeof symlink_path, "%s/symlink", tmpl);
snprintf(subdir, sizeof subdir, "%s/subdir", tmpl);
snprintf(noent, sizeof noent, "%s/does-not-exist", tmpl);
/* Populate. */
int fd = open(regfile, O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd < 0) { perror("open regfile"); goto cleanup_fail; }
if (write(fd, "hello world", 11) != 11) {
perror("write regfile"); close(fd); goto cleanup_fail;
}
close(fd);
if (symlink("./regfile", symlink_path) != 0) {
perror("symlink"); goto cleanup_fail;
}
if (mkdir(subdir, 0700) != 0) {
perror("mkdir subdir"); goto cleanup_fail;
}
setenv("WW_TEST_STAT_REGFILE", regfile, 1);
setenv("WW_TEST_STAT_SYMLINK", symlink_path, 1);
setenv("WW_TEST_STAT_SUBDIR", subdir, 1);
setenv("WW_TEST_STAT_NOENT", noent, 1);
const char *src = "lib/os/stattest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
int rc = runwait(cmd);
char rmcmd[256];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpl);
(void)system(rmcmd);
if (rc != 0) {
fprintf(stderr, "stat_run FAIL: %s exited %d\n", src, rc);
return 1;
}
printf("stat_run: %s ok\n", src);
return 0;
cleanup_fail: {
char rmcmd[256];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpl);
(void)system(rmcmd);
return 1;
} }