Files
Chris Tate db94b2d257 Add fs.cpSync, fs.cp and fs/promises cp
fs.cpSync, fs.cp and fs/promises cp compile with every option:
dereference, errorOnExist, filter, force, mode, preserveTimestamps,
recursive and verbatimSymlinks. They follow Node's two implementations.
cpSync follows cp-sync.js over the fs binding: copy errors are plain
Errors with a code, std::error_code failures throw an ErrnoException,
and links inside a copied directory point at their canonical targets.
fs.cp and fs/promises cp follow cp.js, whose copy errors are
SystemErrors with code, info, errno, syscall and path.

Options and paths validate in Node's order with Node's errors. filter
runs for the top-level paths and each entry in Node's order; an async
filter must return a promise that is already settled, and a pending one
throws SC2020. The callback and promise forms complete the copy before
the callback runs or the promise settles. Windows builds throw SC2020.

The copy lives in the new scr_fs_cp.c. Directory walks keep their frames
on the heap, so deep trees also copy on a fiber's stack. File contents
copy with sendfile on Linux, and COPYFILE_FICLONE uses FICLONE
where it exists. ScrError gains the SystemError context and the
ErrnoException key order, with matching fields in the compiler's Error
layout. The stored fs.cp callback uses the same implementation.

Corpus 5567-5571 cover the sync, callback and promise forms, errors,
symlinks and 1500-level trees.
2026-10-16 21:06:22 -05:00

94 lines
3.5 KiB
TypeScript

import { execFile } from "node:child_process";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import { afterAll, beforeAll, expect, test } from "vitest";
const execFileAsync = promisify(execFile);
const testDir = import.meta.dirname;
const bin = join(testDir, "build", "test_bytes");
let scratch: string;
// Compiled once with ASan + the RC audit: the assertions in test_bytes.c
// cover the ToUint8/ToUint32 coercion matrix, ToIndex construction,
// slice/set copies, the utf8/hex/base64 conversions (invalid-utf8
// replacement included), concat, the u32be pair, the fs Buffer round trip,
// zlib deflate/inflate, and the RC recursion through SCR_ELEM_BYTES arrays
// — the sanitized run proves no leak/double-free across all of them.
beforeAll(async () => {
await mkdir(join(testDir, "build"), { recursive: true });
await execFileAsync("clang", [
"-std=c11",
"-O1",
"-Wall",
"-Wextra",
"-fsanitize=address,undefined",
"-DSCR_RC_AUDIT",
"-I",
join(testDir, "../src"),
"-o",
bin,
join(testDir, "test_bytes.c"),
join(testDir, "../src/scr_bytes.test.c"),
join(testDir, "../src/scr_bytes.c"),
join(testDir, "../src/scr_shared.c"),
join(testDir, "../src/scr_bytes_io.c"),
join(testDir, "../src/scr_fs_cp.c"),
join(testDir, "../src/scr_zlib.c"),
join(testDir, "../src/scr_lib.c"),
join(testDir, "../src/scr_hash.c"),
join(testDir, "../src/scr_string.c"),
join(testDir, "../src/scr_array.c"),
join(testDir, "../src/scr_map.c"),
join(testDir, "../src/scr_exception.c"),
join(testDir, "../src/scr_error.c"),
join(testDir, "../src/scr_number.c"),
join(testDir, "../src/scr_console.c"),
join(testDir, "../src/scr_closure.c"),
join(testDir, "../src/scr_ffi.c"),
join(testDir, "../src/scr_object.c"),
join(testDir, "../src/scr_union.c"),
join(testDir, "../src/scr_cycle.c"),
join(testDir, "../src/scr_alloc.c"),
join(testDir, "../src/scr_json.c"),
join(testDir, "../src/scr_bigint.c"),
join(testDir, "../src/scr_async.c"),
join(testDir, "../src/scr_child.c"),
join(testDir, "../src/scr_path.c"),
join(testDir, "../src/scr_url.c"),
join(testDir, "../src/scr_url_params.c"),
"-Wno-deprecated-declarations",
"-lz",
...(process.platform === "linux" ? ["-D_GNU_SOURCE", "-lm"] : []),
]);
scratch = await mkdtemp(join(tmpdir(), "scriptc-bytes-"));
});
afterAll(async () => {
if (scratch) await rm(scratch, { recursive: true, force: true });
});
test("bytes runtime: coercions, encodings, zlib, fs, RC", async () => {
const { stderr } = await execFileAsync(bin, [scratch], {
env: { ...process.env, UBSAN_OPTIONS: "halt_on_error=1" },
});
expect(stderr.trim().split("\n").pop()).toMatch(/^(\d+)\/\1 cases passed$/);
});
// Statically typed numeric reads cannot represent undefined and still trap.
// Invalid writes are ignored and covered by the runtime assertions above.
test.each([
["--crash-get-oob", "typed array index 1 out of bounds (length 1)"],
["--crash-get-frac", "typed array index 0.5 out of bounds (length 1)"],
])("trap aborts (%s)", async (mode, message) => {
const err = await execFileAsync(bin, [mode]).then(
() => {
throw new Error(`expected ${mode} to abort`);
},
(e: Error & { signal?: string; stderr?: string }) => e,
);
expect(err.signal).toBe("SIGABRT");
expect(err.stderr).toContain(`scriptc: RangeError: ${message}`);
});