feat(core): add append, positional writes, truncate, permissions, times, links, realpath and statfs to Environment files, and full stat metadata

This commit is contained in:
Aiden Cline 2026-11-17 00:35:52 -06:00
parent f5086ef8a2
commit c06be89dec
7 changed files with 498 additions and 24 deletions

View file

@ -20,9 +20,31 @@ const NOT_FOUND = 44
const WRONG_KIND = 45
const FAILED = 46
const TAB = "\t"
// Kind, size and mtime first, as the read and list scripts expect, then the rest of stat(2): device, inode, raw mode
// (hex), links, owner, group, device major and minor (hex), I/O block size, blocks, and access, change and birth times.
const STAT_FORMAT = [
"%F",
"%s",
"%Y",
"%d",
"%i",
"%f",
"%h",
"%u",
"%g",
"%t",
"%T",
"%o",
"%b",
"%X",
"%Z",
"%W",
].join(TAB)
// Exits NOT_FOUND unless "$1" names something, a dangling symlink included.
const exists = `[ -e "$1" ] || [ -L "$1" ] || exit ${NOT_FOUND}`
const loadMetadata = (flags = "") => `
metadata=$(stat ${flags} -c '%F${TAB}%s${TAB}%Y' -- "$1" 2>&1) || {
metadata=$(stat ${flags} -c '${STAT_FORMAT}' -- "$1" 2>&1) || {
case "$metadata" in
*'No such file or directory'*|*'Not a directory'*) exit ${NOT_FOUND} ;;
*) printf '%s' "$metadata" >&2; exit ${FAILED} ;;
@ -117,6 +139,10 @@ export const execDefaults = (spawner: ChildProcessSpawner["Service"]): FilesImpl
const complete = (path: string, result: Result) =>
result.exitCode === 0 ? Effect.void : Effect.fail(processFailure(path, result))
// A script with no output that may report NOT_FOUND.
const plain = (path: string, script: string, args: ReadonlyArray<string>) =>
run(path, script, args).pipe(Effect.flatMap((result) => classifyPlain(path, result, () => undefined)))
return {
stat: (path) => run(path, statScript).pipe(Effect.flatMap((result) => classifyPlain(path, result, parseInfo))),
read: (path, range) =>
@ -145,6 +171,47 @@ export const execDefaults = (spawner: ChildProcessSpawner["Service"]): FilesImpl
move: (from, to) =>
run(from, moveScript, [to]).pipe(Effect.flatMap((result) => classifyPlain(from, result, () => undefined))),
mkdir: (path) => run(path, `mkdir -p -- "$1"`).pipe(Effect.flatMap((result) => complete(path, result))),
append: (path, bytes) =>
run(path, `cat >> "$1"`, [], bytes).pipe(Effect.flatMap((result) => complete(path, result))),
writeAt: (path, bytes, offset) =>
run(path, `dd of="$1" oflag=seek_bytes seek="$2" conv=notrunc status=none`, [String(offset)], bytes).pipe(
Effect.flatMap((result) => complete(path, result)),
),
truncate: (path, length) =>
plain(path, `[ -e "$1" ] || exit ${NOT_FOUND}; truncate -s "$2" -- "$1"`, [String(length)]),
chmod: (path, mode) =>
plain(path, `[ -e "$1" ] || exit ${NOT_FOUND}; chmod "$2" -- "$1"`, [(mode & 0o7777).toString(8)]),
chown: (path, uid, gid, follow) =>
plain(
path,
`${follow ? `[ -e "$1" ] || exit ${NOT_FOUND}` : exists}; chown ${follow ? "" : "-h"} "$2:$3" -- "$1"`,
[String(uid), String(gid)],
),
utimes: (path, atimeMs, mtimeMs, follow) =>
// -c never creates the file; one touch per time, as -a and -m take a single -d.
plain(
path,
`${follow ? `[ -e "$1" ] || exit ${NOT_FOUND}` : exists}
touch -c ${follow ? "" : "-h"} -a -d "@$2" -- "$1" && touch -c ${follow ? "" : "-h"} -m -d "@$3" -- "$1"`,
[String(atimeMs / 1_000), String(mtimeMs / 1_000)],
),
symlink: (target, path) =>
run(path, `ln -s -- "$2" "$1"`, [target]).pipe(Effect.flatMap((result) => complete(path, result))),
readlink: (path) =>
run(
path,
`if [ -L "$1" ]; then readlink -- "$1"; elif [ -e "$1" ]; then stat -c '%F' -- "$1" >&2; exit ${WRONG_KIND}; else exit ${NOT_FOUND}; fi`,
).pipe(Effect.flatMap((result) => classify(path, result, (stdout) => trimNewline(stdout)))),
realpath: (path) =>
run(path, `[ -e "$1" ] || exit ${NOT_FOUND}; realpath -e -- "$1"`).pipe(
Effect.flatMap((result) => classifyPlain(path, result, (stdout) => trimNewline(stdout))),
),
link: (existing, path) => plain(existing, `[ -e "$1" ] || exit ${NOT_FOUND}; ln -- "$1" "$2"`, [path]),
statfs: (path) =>
run(
path,
`[ -e "$1" ] || exit ${NOT_FOUND}; stat -f -c '${["%t", "%S", "%b", "%f", "%a", "%c", "%d"].join(TAB)}' -- "$1"`,
).pipe(Effect.flatMap((result) => classifyPlain(path, result, parseFilesystem))),
}
}
@ -166,13 +233,65 @@ const processFailure = (path: string, result: Result) =>
})
const parseInfo = (bytes: Uint8Array): FileInfo => {
const [rawType, rawSize, rawMtime] = new TextDecoder().decode(bytes).trim().split(TAB)
const [
rawType,
rawSize,
rawMtime,
dev,
ino,
mode,
nlink,
uid,
gid,
major,
minor,
blksize,
blocks,
atime,
ctime,
birth,
] = new TextDecoder().decode(bytes).trim().split(TAB)
const size = Number(rawSize)
const mtimeMs = Number(rawMtime) * 1_000
if (!rawType || !Number.isFinite(size) || !Number.isFinite(mtimeMs)) throw new Error("Invalid stat output")
return { type: parseType(rawType), size, mtimeMs }
const majorNumber = Number.parseInt(major ?? "0", 16)
const minorNumber = Number.parseInt(minor ?? "0", 16)
return {
type: parseType(rawType),
size,
mtimeMs,
dev: Number(dev),
ino: Number(ino),
mode: Number.parseInt(mode ?? "0", 16),
nlink: Number(nlink),
uid: Number(uid),
gid: Number(gid),
// glibc's makedev, the encoding Node reports as rdev on Linux.
rdev: ((majorNumber & 0xfff) << 8) | (minorNumber & 0xff) | ((minorNumber & ~0xff) << 12),
blksize: Number(blksize),
blocks: Number(blocks),
atimeMs: Number(atime) * 1_000,
ctimeMs: Number(ctime) * 1_000,
// GNU stat prints 0 when the filesystem doesn't record birth times; Node reports the change time's epoch then too.
birthtimeMs: Number(birth) * 1_000,
}
}
const parseFilesystem = (bytes: Uint8Array) => {
const [type, bsize, blocks, bfree, bavail, files, ffree] = new TextDecoder().decode(bytes).trim().split(TAB)
return {
type: Number.parseInt(type ?? "0", 16),
bsize: Number(bsize),
blocks: Number(blocks),
bfree: Number(bfree),
bavail: Number(bavail),
files: Number(files),
ffree: Number(ffree),
}
}
const trimNewline = (bytes: Uint8Array) => new TextDecoder().decode(bytes).replace(/\n$/, "")
const parseType = (value: string): FileType => {
if (value === "regular file" || value === "regular empty file" || value === "f") return "file"
if (value === "directory" || value === "d") return "directory"

View file

@ -7,6 +7,31 @@ export interface FileInfo {
readonly type: FileType
readonly size: number
readonly mtimeMs: number
/** The rest of POSIX stat, where the environment reports it. */
readonly dev?: number
readonly ino?: number
/** File type and permission bits, as in `st_mode`. */
readonly mode?: number
readonly nlink?: number
readonly uid?: number
readonly gid?: number
readonly rdev?: number
readonly blksize?: number
readonly blocks?: number
readonly atimeMs?: number
readonly ctimeMs?: number
readonly birthtimeMs?: number
}
/** Free and total space on the filesystem holding a path, in blocks of `bsize` bytes, as `statfs(2)` reports it. */
export interface FilesystemInfo {
readonly type: number
readonly bsize: number
readonly blocks: number
readonly bfree: number
readonly bavail: number
readonly files: number
readonly ffree: number
}
export interface DirEntry {
@ -47,6 +72,32 @@ export interface FilesImpl {
readonly remove: (path: string) => Effect.Effect<void, Failed>
readonly move: (from: string, to: string) => Effect.Effect<void, NotFound | Failed>
readonly mkdir: (path: string) => Effect.Effect<void, Failed>
/** Adds bytes to the end of a file, creating it when missing. */
readonly append: (path: string, bytes: Uint8Array) => Effect.Effect<void, Failed>
/** Writes bytes at `offset` without truncating, extending the file with zeros if needed; creates it when missing. */
readonly writeAt: (path: string, bytes: Uint8Array, offset: number) => Effect.Effect<void, Failed>
/** Cuts or zero-extends a file to `length` bytes. */
readonly truncate: (path: string, length: number) => Effect.Effect<void, NotFound | Failed>
/** Sets the permission bits (`mode & 0o7777`), following a final symlink. */
readonly chmod: (path: string, mode: number) => Effect.Effect<void, NotFound | Failed>
/** Sets the owner; `follow: false` changes a final symlink itself. */
readonly chown: (path: string, uid: number, gid: number, follow: boolean) => Effect.Effect<void, NotFound | Failed>
/** Sets access and modification times; `follow: false` changes a final symlink itself. */
readonly utimes: (
path: string,
atimeMs: number,
mtimeMs: number,
follow: boolean,
) => Effect.Effect<void, NotFound | Failed>
/** Creates a symlink at `path` holding `target` as given. */
readonly symlink: (target: string, path: string) => Effect.Effect<void, Failed>
/** The target a symlink holds; `WrongKind` for anything else. */
readonly readlink: (path: string) => Effect.Effect<string, NotFound | WrongKind | Failed>
/** The canonical absolute path with every symlink resolved. */
readonly realpath: (path: string) => Effect.Effect<string, NotFound | Failed>
/** Creates a hard link at `path` to the file at `existing`. */
readonly link: (existing: string, path: string) => Effect.Effect<void, NotFound | Failed>
readonly statfs: (path: string) => Effect.Effect<FilesystemInfo, NotFound | Failed>
}
export interface Files extends FilesImpl {}

View file

@ -8,6 +8,7 @@ export {
type FileInfo,
type Files,
type FilesImpl,
type FilesystemInfo,
type FileType,
NotFound,
typeFollowing,

View file

@ -1,3 +1,4 @@
import { constants } from "node:fs"
import fs from "node:fs/promises"
import path from "node:path"
import { Effect } from "effect"
@ -64,6 +65,43 @@ export const makeLocalDriver = (spawner: ChildProcessSpawner["Service"]): Driver
yield* attempt(from, () => fs.rename(from, destination))
}),
mkdir: (value) => attempt(value, () => fs.mkdir(value, { recursive: true }).then(() => undefined)),
append: (value, bytes) => attempt(value, () => fs.appendFile(value, bytes)),
writeAt: (value, bytes, offset) =>
attempt(value, async () => {
// Read-write and created when missing, without O_TRUNC, so existing bytes outside the range stay.
const handle = await fs.open(value, constants.O_RDWR | constants.O_CREAT)
try {
await handle.write(bytes, 0, bytes.length, offset)
} finally {
await handle.close()
}
}),
truncate: (value, length) => attempt(value, () => fs.truncate(value, length), true),
chmod: (value, mode) => attempt(value, () => fs.chmod(value, mode & 0o7777), true),
chown: (value, uid, gid, follow) => attempt(value, () => (follow ? fs.chown : fs.lchown)(value, uid, gid), true),
utimes: (value, atimeMs, mtimeMs, follow) =>
attempt(value, () => (follow ? fs.utimes : fs.lutimes)(value, atimeMs / 1_000, mtimeMs / 1_000), true),
symlink: (target, value) => attempt(value, () => fs.symlink(target, value)),
readlink: (value) =>
Effect.gen(function* () {
const info = yield* stat(value, false)
if (info.type !== "symlink") return yield* new WrongKind({ path: value, actual: info.type })
return yield* attempt(value, () => fs.readlink(value), true)
}),
realpath: (value) => attempt(value, () => fs.realpath(value), true),
link: (existing, value) => attempt(existing, () => fs.link(existing, value), true),
statfs: (value) =>
attempt(value, () => fs.statfs(value), true).pipe(
Effect.map((info) => ({
type: info.type,
bsize: info.bsize,
blocks: info.blocks,
bfree: info.bfree,
bavail: info.bavail,
files: info.files,
ffree: info.ffree,
})),
),
}
return { spawner, overrides }
@ -71,7 +109,25 @@ export const makeLocalDriver = (spawner: ChildProcessSpawner["Service"]): Driver
const stat = (value: string, follow: boolean) =>
attempt(value, () => (follow ? fs.stat(value) : fs.lstat(value)), true).pipe(
Effect.map((stats): FileInfo => ({ type: fileType(stats), size: stats.size, mtimeMs: stats.mtimeMs })),
Effect.map(
(stats): FileInfo => ({
type: fileType(stats),
size: stats.size,
mtimeMs: stats.mtimeMs,
dev: stats.dev,
ino: stats.ino,
mode: stats.mode,
nlink: stats.nlink,
uid: stats.uid,
gid: stats.gid,
rdev: stats.rdev,
blksize: stats.blksize,
blocks: stats.blocks,
atimeMs: stats.atimeMs,
ctimeMs: stats.ctimeMs,
birthtimeMs: stats.birthtimeMs,
}),
),
)
const fileType = (entry: { isFile(): boolean; isDirectory(): boolean; isSymbolicLink(): boolean }): FileType => {

View file

@ -4,10 +4,24 @@ import { make } from "effect/process/ChildProcessSpawner"
import type { Driver } from "./driver.js"
import { Failed, NotFound, WrongKind, type FileInfo, type FilesImpl, type FileType } from "./files.js"
/** What chmod, chown and utimes change, and what identifies a node. */
type Meta = {
readonly ino?: number
readonly mode?: number
readonly uid?: number
readonly gid?: number
readonly atimeMs?: number
readonly ctimeMs?: number
readonly birthtimeMs?: number
}
type Node =
| { readonly type: "file"; readonly bytes: Uint8Array; readonly mtimeMs: number }
| { readonly type: "directory"; readonly mtimeMs: number }
| { readonly type: "symlink"; readonly target: string; readonly mtimeMs: number }
| ({ readonly type: "file"; readonly bytes: Uint8Array; readonly mtimeMs: number } & Meta)
| ({ readonly type: "directory"; readonly mtimeMs: number } & Meta)
| ({ readonly type: "symlink"; readonly target: string; readonly mtimeMs: number } & Meta)
const typeBits = { file: 0o100000, directory: 0o040000, symlink: 0o120000 }
const defaultPermissions = { file: 0o644, directory: 0o755, symlink: 0o777 }
export interface MemoryDriver extends Driver {
readonly symlink: (target: string, path: string) => Effect.Effect<void, Failed>
@ -16,16 +30,40 @@ export interface MemoryDriver extends Driver {
export const makeMemoryDriver = (): MemoryDriver => {
const nodes = new Map<string, Node>([["/", { type: "directory", mtimeMs: Date.now() }]])
const key = (value: string) => path.posix.resolve("/", value)
const size = (node: Node) =>
node.type === "file"
? node.bytes.length
: node.type === "symlink"
? new TextEncoder().encode(node.target).length
: 0
const info = (node: Node): FileInfo => ({
type: node.type,
size:
node.type === "file"
? node.bytes.length
: node.type === "symlink"
? new TextEncoder().encode(node.target).length
: 0,
size: size(node),
mtimeMs: node.mtimeMs,
dev: 1,
ino: node.ino ?? 0,
mode: typeBits[node.type] | (node.mode ?? defaultPermissions[node.type]),
nlink: node.type === "directory" ? 2 : 1,
uid: node.uid ?? 0,
gid: node.gid ?? 0,
rdev: 0,
blksize: 4096,
blocks: Math.ceil(size(node) / 512),
atimeMs: node.atimeMs ?? node.mtimeMs,
ctimeMs: node.ctimeMs ?? node.mtimeMs,
birthtimeMs: node.birthtimeMs ?? node.mtimeMs,
})
let inodes = 1
// A new node gets the next inode and its birth time; a changed one keeps both.
const put = (target: string, node: Node) => {
const existing = nodes.get(target)
nodes.set(target, {
...node,
ino: node.ino ?? existing?.ino ?? ++inodes,
birthtimeMs: node.birthtimeMs ?? existing?.birthtimeMs ?? node.mtimeMs,
ctimeMs: Date.now(),
})
}
const resolveKey = (value: string, followFinal: boolean, seen = new Set<string>()): string | undefined => {
const normalized = key(value)
const parts = normalized.split("/").filter(Boolean)
@ -57,9 +95,44 @@ export const makeMemoryDriver = (): MemoryDriver => {
if (existing) throw new Error(`Path is not a directory: ${value}`)
const parent = path.posix.dirname(target)
if (parent !== target) mkdirSync(parent)
nodes.set(target, { type: "directory", mtimeMs: Date.now() })
put(target, { type: "directory", mtimeMs: Date.now() })
}
const failed = (value: string, cause: unknown) => new Failed({ path: value, cause })
// The existing node a path names after every symlink, or undefined.
const followed = (value: string) => {
const target = resolveKey(value, true)
return target !== undefined && nodes.has(target) ? target : undefined
}
const concat = (left: Uint8Array, right: Uint8Array) => {
const next = new Uint8Array(left.length + right.length)
next.set(left)
next.set(right, left.length)
return next
}
// Rewrites a file's bytes through symlinks, creating the file when missing.
const change = (value: string, update: (current: Uint8Array) => Uint8Array) =>
Effect.try({
try: () => {
const target = resolveKey(value, true) ?? key(value)
const current = nodes.get(target)
if (current && current.type !== "file") throw new Error(`Path is not a file: ${value}`)
if (!current) requireParent(target)
put(target, {
...current,
type: "file",
bytes: update(current?.bytes ?? new Uint8Array()),
mtimeMs: Date.now(),
})
},
catch: (cause) => failed(value, cause),
})
const meta = (value: string, follow: boolean, update: () => Meta & { readonly mtimeMs?: number }) =>
Effect.gen(function* () {
const target = follow ? followed(value) : (resolveKey(value, false) ?? key(value))
const node = target === undefined ? undefined : nodes.get(target)
if (!target || !node) return yield* new NotFound({ path: value })
put(target, { ...node, ...update() })
})
const overrides: FilesImpl = {
stat: (value) =>
Effect.suspend(() => {
@ -87,7 +160,13 @@ export const makeMemoryDriver = (): MemoryDriver => {
const target = existing?.type === "symlink" ? resolveKey(value, true) : resolveKey(value, false)
if (!target) throw new Error(`Cannot resolve symlink: ${value}`)
requireParent(target)
nodes.set(target, { type: "file", bytes: bytes.slice(), mtimeMs: Date.now() })
const previous = nodes.get(target)
put(target, {
...(previous?.type === "file" ? previous : {}),
type: "file",
bytes: bytes.slice(),
mtimeMs: Date.now(),
})
},
catch: (cause) => failed(value, cause),
}),
@ -137,6 +216,75 @@ export const makeMemoryDriver = (): MemoryDriver => {
})
}),
mkdir: (value) => Effect.try({ try: () => mkdirSync(value), catch: (cause) => failed(value, cause) }),
append: (value, bytes) => change(value, (current) => concat(current, bytes)),
writeAt: (value, bytes, offset) =>
change(value, (current) => {
const next = new Uint8Array(Math.max(current.length, offset + bytes.length))
next.set(current)
next.set(bytes, offset)
return next
}),
truncate: (value, length) =>
Effect.gen(function* () {
const target = followed(value)
if (!target) return yield* new NotFound({ path: value })
yield* change(value, (current) => {
const next = new Uint8Array(length)
next.set(current.subarray(0, length))
return next
})
}),
chmod: (value, mode) => meta(value, true, () => ({ mode: mode & 0o7777 })),
chown: (value, uid, gid, follow) => meta(value, follow, () => ({ uid, gid })),
utimes: (value, atimeMs, mtimeMs, follow) => meta(value, follow, () => ({ atimeMs, mtimeMs })),
symlink: (target, value) =>
Effect.try({
try: () => {
requireParent(value)
put(resolveKey(value, false) ?? key(value), { type: "symlink", target, mtimeMs: Date.now() })
},
catch: (cause) => failed(value, cause),
}),
readlink: (value) =>
Effect.gen(function* () {
const node = lookup(value)
if (!node) return yield* new NotFound({ path: value })
if (node.type !== "symlink") return yield* new WrongKind({ path: value, actual: node.type })
return node.target
}),
realpath: (value) =>
Effect.gen(function* () {
const target = followed(value)
if (!target) return yield* new NotFound({ path: value })
return target
}),
link: (existing, value) =>
Effect.gen(function* () {
const source = followed(existing)
const node = source === undefined ? undefined : nodes.get(source)
if (!node) return yield* new NotFound({ path: existing })
yield* Effect.try({
try: () => {
requireParent(value)
// A copy rather than a shared inode: the memory driver keeps no link counts.
nodes.set(key(value), { ...node })
},
catch: (cause) => failed(value, cause),
})
}),
statfs: (value) =>
Effect.gen(function* () {
if (!followed(value)) return yield* new NotFound({ path: value })
return {
type: 0x01021994,
bsize: 4096,
blocks: 1 << 20,
bfree: 1 << 19,
bavail: 1 << 19,
files: 1 << 16,
ffree: 1 << 15,
}
}),
}
const spawner = make((command) =>
@ -157,14 +305,7 @@ export const makeMemoryDriver = (): MemoryDriver => {
return {
spawner,
overrides,
symlink: (target, value) =>
Effect.try({
try: () => {
requireParent(value)
nodes.set(resolveKey(value, false) ?? key(value), { type: "symlink", target, mtimeMs: Date.now() })
},
catch: (cause) => failed(value, cause),
}),
symlink: overrides.symlink,
}
}

View file

@ -310,6 +310,18 @@ const layer = (options: Options) =>
remove: (path) => withFiles(path, (files) => files.remove(path)),
move: (from, to) => withFiles(from, (files) => files.move(from, to)),
mkdir: (path) => withFiles(path, (files) => files.mkdir(path)),
append: (path, bytes) => withFiles(path, (files) => files.append(path, bytes)),
writeAt: (path, bytes, offset) => withFiles(path, (files) => files.writeAt(path, bytes, offset)),
truncate: (path, length) => withFiles(path, (files) => files.truncate(path, length)),
chmod: (path, mode) => withFiles(path, (files) => files.chmod(path, mode)),
chown: (path, uid, gid, follow) => withFiles(path, (files) => files.chown(path, uid, gid, follow)),
utimes: (path, atimeMs, mtimeMs, follow) =>
withFiles(path, (files) => files.utimes(path, atimeMs, mtimeMs, follow)),
symlink: (target, path) => withFiles(path, (files) => files.symlink(target, path)),
readlink: (path) => withFiles(path, (files) => files.readlink(path)),
realpath: (path) => withFiles(path, (files) => files.realpath(path)),
link: (existing, path) => withFiles(path, (files) => files.link(existing, path)),
statfs: (path) => withFiles(path, (files) => files.statfs(path)),
}
return { spawner, overrides }
}),

View file

@ -45,7 +45,10 @@ export const environmentConformance = <E>(
expect(text(result.bytes)).toBe("hello")
expect(result.info.type).toBe("file")
expect(result.info.size).toBe(5)
expect(yield* harness.files.stat(target)).toEqual(result.info)
// Reading can move the access time, so compare what reading doesn't change.
const { atimeMs: _stat, ...stat } = yield* harness.files.stat(target)
const { atimeMs: _read, ...read } = result.info
expect(stat).toEqual(read)
}),
)
@ -178,6 +181,97 @@ export const environmentConformance = <E>(
}),
)
check("stats report POSIX metadata", (harness) =>
Effect.gen(function* () {
const target = `${harness.root}/meta.txt`
yield* harness.files.write(target, bytes("hello"))
const info = yield* harness.files.stat(target)
expect(info.mode! & 0o170000).toBe(0o100000)
expect(info.nlink).toBeGreaterThanOrEqual(1)
expect(info.ino).toBeGreaterThan(0)
for (const value of [
info.dev,
info.uid,
info.gid,
info.rdev,
info.blksize,
info.blocks,
info.atimeMs,
info.ctimeMs,
info.birthtimeMs,
])
expect(Number.isFinite(value)).toBe(true)
expect((yield* harness.files.stat(harness.root)).mode! & 0o170000).toBe(0o040000)
}),
)
check("appends, writes at an offset, and truncates", (harness) =>
Effect.gen(function* () {
const target = `${harness.root}/bytes.txt`
yield* harness.files.append(target, bytes("abc"))
yield* harness.files.append(target, bytes("def"))
yield* harness.files.writeAt(target, bytes("XY"), 1)
expect(text((yield* harness.files.read(target)).bytes)).toBe("aXYdef")
yield* harness.files.writeAt(target, bytes("!"), 8)
expect([...(yield* harness.files.read(target)).bytes]).toEqual([...bytes("aXYdef"), 0, 0, 33])
yield* harness.files.truncate(target, 2)
expect(text((yield* harness.files.read(target)).bytes)).toBe("aX")
yield* harness.files.truncate(target, 4)
expect([...(yield* harness.files.read(target)).bytes]).toEqual([97, 88, 0, 0])
yield* harness.files.writeAt(`${harness.root}/created.txt`, bytes("new"), 0)
expect(text((yield* harness.files.read(`${harness.root}/created.txt`)).bytes)).toBe("new")
expect(yield* Effect.flip(harness.files.truncate(`${harness.root}/missing`, 0))).toBeInstanceOf(NotFound)
}),
)
check("changes permission bits and times", (harness) =>
Effect.gen(function* () {
const target = `${harness.root}/mode.txt`
yield* harness.files.write(target, bytes("x"))
yield* harness.files.chmod(target, 0o100600)
expect((yield* harness.files.stat(target)).mode! & 0o7777).toBe(0o600)
yield* harness.files.utimes(target, 1_000_000_000, 2_000_000_000, true)
const info = yield* harness.files.stat(target)
expect(info.atimeMs).toBe(1_000_000_000)
expect(info.mtimeMs).toBe(2_000_000_000)
const owner = yield* harness.files.stat(target)
yield* harness.files.chown(target, owner.uid!, owner.gid!, true)
expect(yield* Effect.flip(harness.files.chmod(`${harness.root}/missing`, 0o644))).toBeInstanceOf(NotFound)
expect(yield* Effect.flip(harness.files.utimes(`${harness.root}/missing`, 0, 0, true))).toBeInstanceOf(NotFound)
}),
)
check("creates and reads symlinks and hard links, and resolves real paths", (harness) =>
Effect.gen(function* () {
if (!harness.symlink) return
const target = `${harness.root}/real.txt`
yield* harness.files.write(target, bytes("real"))
yield* harness.files.symlink("real.txt", `${harness.root}/sym`)
expect(yield* harness.files.readlink(`${harness.root}/sym`)).toBe("real.txt")
expect((yield* harness.files.stat(`${harness.root}/sym`)).type).toBe("symlink")
expect(yield* harness.files.realpath(`${harness.root}/sym`)).toBe(yield* harness.files.realpath(target))
const readlinkFile = yield* Effect.flip(harness.files.readlink(target))
expect(readlinkFile).toBeInstanceOf(WrongKind)
expect(yield* Effect.flip(harness.files.readlink(`${harness.root}/missing`))).toBeInstanceOf(NotFound)
expect(yield* Effect.flip(harness.files.realpath(`${harness.root}/missing`))).toBeInstanceOf(NotFound)
yield* harness.files.link(target, `${harness.root}/hard.txt`)
expect(text((yield* harness.files.read(`${harness.root}/hard.txt`)).bytes)).toBe("real")
expect(yield* Effect.flip(harness.files.link(`${harness.root}/missing`, `${harness.root}/x`))).toBeInstanceOf(
NotFound,
)
}),
)
check("reports filesystem space", (harness) =>
Effect.gen(function* () {
const info = yield* harness.files.statfs(harness.root)
expect(info.bsize).toBeGreaterThan(0)
expect(info.blocks).toBeGreaterThan(0)
expect(info.bavail).toBeLessThanOrEqual(info.blocks)
expect(yield* Effect.flip(harness.files.statfs(`${harness.root}/missing`))).toBeInstanceOf(NotFound)
}),
)
check("moves files and removes trees idempotently", (harness) =>
Effect.gen(function* () {
const source = `${harness.root}/source/file`