Windows: official port -- UCRT static build, UTF-8 throughout, Low IL sandbox, ncurses TUI, installer; v0.6.0

Build
  * cross build switches to pkgsCross.ucrt64 (shell-win.nix); curl (Schannel),
    winpthreads and ncurses 6.6 are linked statically, so the release is
    cst-agent.exe + busybox.exe (busybox-w32 Unicode) with no extra DLLs
  * windres embeds a manifest with activeCodePage=UTF-8 and a version resource
  * ncurses 6.6 Windows console driver patches (mouse button state, A_DIM,
    wide-char declarations): scripts/patches/
  * scripts/bootstrap_msys2.sh builds the same release natively in MSYS2
    UCRT64; PR #1's MINGW64 notes now point there
  * vendor/tre-regex (musl's TRE) provides <regex.h> on MinGW

UTF-8
  * paths cross the system call boundary as UTF-16 (*W APIs); console code
    pages set to 65001 and restored on exit; files are read and written in
    binary mode
  * child output that is not valid UTF-8 is decoded from the console, OEM or
    ANSI system code page
  * read_file hides CR; edit_file, apply_patch and write_file keep CRLF line
    endings and a UTF-8 BOM

OS layer and processes
  * os_dir_* and cst_fnmatch replace the POSIX-only directory walks in
    subagent, plugins, skills, undo and fs_tools; realpath / path_under and
    symlink rejection work on Windows
  * every child goes through CreateProcessW with its own environment block
    inside a Job Object, so timeouts and Esc end the whole process tree

Sandbox (auto mode, "auto · lowil")
  * run_shell runs with a restricted Low integrity token; the workspace gets
    an inheritable Low label once, the sandbox TEMP is labeled Low, and the
    credentials file gets a no-read-up label
  * drive roots, the profile and system folders are never labeled
  * network commands still ask, since Low IL does not block the network;
    Access denied and MSYS2's 0xc0000142 offer a rerun outside the sandbox
  * /sandbox lists labeled folders, /sandbox reset-labels restores them

TUI
  * raster console fonts are switched to Consolas / NSimSun while running
  * the home logo falls back to text when the console font lacks U+2580
  * mouse drag copy goes to the Windows clipboard

Distribution
  * scripts/install_windows.ps1 installs a Gitea release into
    %LOCALAPPDATA%\Programs\cst-agent and adds it to the user PATH;
    cst-agent-update.cmd reruns it; scripts/release_windows.sh uploads the
    zip with the token taken from the environment
  * scripts/run_windows.bat is gone; docs/README-windows.md is rewritten

Tests
  * new test_lowil, test_win_utf8 and test_os_dir; tests made portable
    (cst_test_util.h)
  * scripts/win_test.sh and scripts/winvm/ run the suite on English and
    Chinese Windows Server VMs as Administrator, a standard user and a
    Chinese user name: 29/29 on all six
This commit is contained in:
WHC
2026-10-06 23:08:14 +08:00
parent 9862c29f58
commit 2a43ae6622
84 changed files with 9784 additions and 1083 deletions
+208
View File
@@ -0,0 +1,208 @@
/* Portable helpers for the test programs: scratch dirs, env, recursive
* delete, symlinks and (with CST_TEST_SOCKETS) a loopback socket layer. */
#ifndef CST_TEST_UTIL_H
#define CST_TEST_UTIL_H
#include "os_compat.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#ifdef CST_TEST_SOCKETS
#include <winsock2.h> /* before windows.h */
#include <ws2tcpip.h>
#endif
#include <direct.h>
#include <io.h>
#include <windows.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#if defined(__GNUC__)
#define CST_TEST_UNUSED __attribute__((unused))
#else
#define CST_TEST_UNUSED
#endif
/* mkdir(path, mode) that also builds on Windows. */
#define test_mkdir(p, mode) os_mkdir((p), (mode))
/* Fresh empty directory "<tmp>/<tag>-<pid>-<n>"; 0 on success. */
static CST_TEST_UNUSED int test_tmpdir(char *out, size_t n, const char *tag) {
static int seq;
char base[1024];
if (os_temp_dir(base, sizeof(base)) != 0)
snprintf(base, sizeof(base), ".");
for (int i = 0; i < 100; i++) {
int k = snprintf(out, n, "%s/%s-%d-%d", base, tag, os_getpid(), seq++);
if (k < 0 || (size_t)k >= n)
return -1;
#ifdef _WIN32
int rc = _mkdir(out);
#else
int rc = mkdir(out, 0755);
#endif
if (rc == 0)
return 0;
if (errno != EEXIST)
return -1;
}
return -1;
}
/* rm -rf without a shell; links are removed, never followed. */
static CST_TEST_UNUSED int test_rmrf(const char *path) {
os_stat_t st;
if (os_lstat(path, &st) != 0)
return 0;
if (st.is_link) {
#ifdef _WIN32
if (RemoveDirectoryA(path) || DeleteFileA(path))
return 0;
return -1;
#else
return unlink(path);
#endif
}
if (!st.is_dir) {
#ifdef _WIN32
SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
#endif
return remove(path);
}
os_dir_t *d = os_dir_open(path);
if (d) {
const os_dirent_t *e;
while ((e = os_dir_next(d))) {
if (!strcmp(e->name, ".") || !strcmp(e->name, ".."))
continue;
size_t L = strlen(path) + strlen(e->name) + 2;
char *c = malloc(L);
if (!c)
break;
snprintf(c, L, "%s/%s", path, e->name);
test_rmrf(c);
free(c);
}
os_dir_close(d);
}
#ifdef _WIN32
return _rmdir(path);
#else
return rmdir(path);
#endif
}
/* 0 on success; -1 when the platform or account cannot make links (Windows
* without Developer Mode or admin rights): callers skip those checks. */
static CST_TEST_UNUSED int test_symlink(const char *target, const char *link) {
#ifdef _WIN32
wchar_t *wt = os_utf8_to_wide(target), *wl = os_utf8_to_wide(link);
int ok = 0;
if (wt && wl) {
for (wchar_t *p = wt; *p; p++)
if (*p == L'/')
*p = L'\\';
DWORD attr = GetFileAttributesW(wt);
if (attr == INVALID_FILE_ATTRIBUTES) {
/* relative targets resolve against the link's directory */
wchar_t *slash = wcsrchr(wl, L'/');
if (!slash)
slash = wcsrchr(wl, L'\\');
if (slash && wt[0] != L'\\' && !(wt[0] && wt[1] == L':')) {
size_t dl = (size_t)(slash - wl) + 1, tl = wcslen(wt);
wchar_t *full = malloc((dl + tl + 1) * sizeof(wchar_t));
if (full) {
memcpy(full, wl, dl * sizeof(wchar_t));
memcpy(full + dl, wt, (tl + 1) * sizeof(wchar_t));
attr = GetFileAttributesW(full);
free(full);
}
}
}
DWORD flags = 0x2; /* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE */
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
ok = CreateSymbolicLinkW(wl, wt, flags) != 0;
}
free(wt);
free(wl);
return ok ? 0 : -1;
#else
return symlink(target, link);
#endif
}
/* rand_r replacement (same sequence everywhere). */
static CST_TEST_UNUSED int test_rand(unsigned *seed) {
*seed = *seed * 1103515245u + 12345u;
return (int)((*seed >> 16) & 0x7fff);
}
/* getline replacement: -1 at EOF. */
static CST_TEST_UNUSED long test_getline(char **line, size_t *cap, FILE *fp) {
if (!*line || *cap == 0) {
*cap = 256;
*line = malloc(*cap);
if (!*line)
return -1;
}
size_t n = 0;
int c;
while ((c = fgetc(fp)) != EOF) {
if (n + 2 > *cap) {
size_t nc = *cap * 2;
char *nl = realloc(*line, nc);
if (!nl)
return -1;
*line = nl;
*cap = nc;
}
(*line)[n++] = (char)c;
if (c == '\n')
break;
}
if (n == 0)
return -1;
(*line)[n] = 0;
return (long)n;
}
static CST_TEST_UNUSED void test_sleep_ms(int ms) {
#ifdef _WIN32
Sleep((DWORD)ms);
#else
usleep((useconds_t)ms * 1000);
#endif
}
#ifdef CST_TEST_SOCKETS
#ifdef _WIN32
typedef SOCKET test_sock_t;
#define TEST_BAD_SOCK INVALID_SOCKET
#define test_sock_close closesocket
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
static CST_TEST_UNUSED void test_sock_init(void) {
WSADATA w;
WSAStartup(MAKEWORD(2, 2), &w);
}
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/socket.h>
typedef int test_sock_t;
#define TEST_BAD_SOCK (-1)
#define test_sock_close close
static CST_TEST_UNUSED void test_sock_init(void) { signal(SIGPIPE, SIG_IGN); }
#endif
#endif /* CST_TEST_SOCKETS */
#endif