Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 122 additions & 3 deletions packages/core/src/environment/exec-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} ;;
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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 -sT -- "$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 -T -- "$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))),
}
}

Expand All @@ -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"
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/environment/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/environment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
type FileInfo,
type Files,
type FilesImpl,
type FilesystemInfo,
type FileType,
NotFound,
typeFollowing,
Expand Down
58 changes: 57 additions & 1 deletion packages/core/src/environment/local.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constants } from "node:fs"
import fs from "node:fs/promises"
import path from "node:path"
import { Effect } from "effect"
Expand Down Expand Up @@ -64,14 +65,69 @@ 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 }
}

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 => {
Expand Down
Loading
Loading