@brunosouzas/stackledger 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +261 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +97 -0
- package/dist/cli.js.map +1 -0
- package/dist/control.d.ts +2 -0
- package/dist/control.js +22 -0
- package/dist/control.js.map +1 -0
- package/dist/discover-command.d.ts +7 -0
- package/dist/discover-command.js +60 -0
- package/dist/discover-command.js.map +1 -0
- package/dist/discovery/annotations.d.ts +24 -0
- package/dist/discovery/annotations.js +37 -0
- package/dist/discovery/annotations.js.map +1 -0
- package/dist/discovery/host.d.ts +47 -0
- package/dist/discovery/host.js +56 -0
- package/dist/discovery/host.js.map +1 -0
- package/dist/discovery/index.d.ts +15 -0
- package/dist/discovery/index.js +30 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/machine.d.ts +14 -0
- package/dist/discovery/machine.js +17 -0
- package/dist/discovery/machine.js.map +1 -0
- package/dist/discovery/run.d.ts +39 -0
- package/dist/discovery/run.js +130 -0
- package/dist/discovery/run.js.map +1 -0
- package/dist/discovery/snapshot.d.ts +60 -0
- package/dist/discovery/snapshot.js +161 -0
- package/dist/discovery/snapshot.js.map +1 -0
- package/dist/discovery/sources/apps.d.ts +3 -0
- package/dist/discovery/sources/apps.js +136 -0
- package/dist/discovery/sources/apps.js.map +1 -0
- package/dist/discovery/sources/brew.d.ts +15 -0
- package/dist/discovery/sources/brew.js +69 -0
- package/dist/discovery/sources/brew.js.map +1 -0
- package/dist/discovery/sources/common.d.ts +39 -0
- package/dist/discovery/sources/common.js +70 -0
- package/dist/discovery/sources/common.js.map +1 -0
- package/dist/discovery/sources/index.d.ts +8 -0
- package/dist/discovery/sources/index.js +12 -0
- package/dist/discovery/sources/index.js.map +1 -0
- package/dist/discovery/sources/launchd.d.ts +3 -0
- package/dist/discovery/sources/launchd.js +65 -0
- package/dist/discovery/sources/launchd.js.map +1 -0
- package/dist/discovery/sources/mas.d.ts +9 -0
- package/dist/discovery/sources/mas.js +25 -0
- package/dist/discovery/sources/mas.js.map +1 -0
- package/dist/discovery/sources/mise.d.ts +5 -0
- package/dist/discovery/sources/mise.js +64 -0
- package/dist/discovery/sources/mise.js.map +1 -0
- package/dist/discovery/sources/npm.d.ts +12 -0
- package/dist/discovery/sources/npm.js +34 -0
- package/dist/discovery/sources/npm.js.map +1 -0
- package/dist/discovery/sources/pipx.d.ts +4 -0
- package/dist/discovery/sources/pipx.js +82 -0
- package/dist/discovery/sources/pipx.js.map +1 -0
- package/dist/discovery/types.d.ts +73 -0
- package/dist/discovery/types.js +10 -0
- package/dist/discovery/types.js.map +1 -0
- package/dist/discovery/write.d.ts +50 -0
- package/dist/discovery/write.js +196 -0
- package/dist/discovery/write.js.map +1 -0
- package/dist/drift-command.d.ts +62 -0
- package/dist/drift-command.js +375 -0
- package/dist/drift-command.js.map +1 -0
- package/dist/drift-formats.d.ts +3 -0
- package/dist/drift-formats.js +54 -0
- package/dist/drift-formats.js.map +1 -0
- package/dist/drift.d.ts +95 -0
- package/dist/drift.js +206 -0
- package/dist/drift.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +26 -0
- package/dist/errors.js.map +1 -0
- package/dist/graph.d.ts +103 -0
- package/dist/graph.js +181 -0
- package/dist/graph.js.map +1 -0
- package/dist/ics.d.ts +18 -0
- package/dist/ics.js +103 -0
- package/dist/ics.js.map +1 -0
- package/dist/identity.d.ts +18 -0
- package/dist/identity.js +36 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/map/badges.d.ts +20 -0
- package/dist/map/badges.js +95 -0
- package/dist/map/badges.js.map +1 -0
- package/dist/map/command.d.ts +8 -0
- package/dist/map/command.js +86 -0
- package/dist/map/command.js.map +1 -0
- package/dist/map/heap.d.ts +15 -0
- package/dist/map/heap.js +57 -0
- package/dist/map/heap.js.map +1 -0
- package/dist/map/ink.d.ts +11 -0
- package/dist/map/ink.js +10 -0
- package/dist/map/ink.js.map +1 -0
- package/dist/map/labels.d.ts +43 -0
- package/dist/map/labels.js +172 -0
- package/dist/map/labels.js.map +1 -0
- package/dist/map/layout.d.ts +64 -0
- package/dist/map/layout.js +228 -0
- package/dist/map/layout.js.map +1 -0
- package/dist/map/model.d.ts +46 -0
- package/dist/map/model.js +84 -0
- package/dist/map/model.js.map +1 -0
- package/dist/map/neighbourhood.d.ts +18 -0
- package/dist/map/neighbourhood.js +137 -0
- package/dist/map/neighbourhood.js.map +1 -0
- package/dist/map/parallel.d.ts +35 -0
- package/dist/map/parallel.js +261 -0
- package/dist/map/parallel.js.map +1 -0
- package/dist/map/pieces.d.ts +12 -0
- package/dist/map/pieces.js +15 -0
- package/dist/map/pieces.js.map +1 -0
- package/dist/map/relations.d.ts +39 -0
- package/dist/map/relations.js +122 -0
- package/dist/map/relations.js.map +1 -0
- package/dist/map/route.d.ts +25 -0
- package/dist/map/route.js +165 -0
- package/dist/map/route.js.map +1 -0
- package/dist/map/strip.d.ts +29 -0
- package/dist/map/strip.js +120 -0
- package/dist/map/strip.js.map +1 -0
- package/dist/map/suggest.d.ts +21 -0
- package/dist/map/suggest.js +97 -0
- package/dist/map/suggest.js.map +1 -0
- package/dist/map/svg.d.ts +29 -0
- package/dist/map/svg.js +203 -0
- package/dist/map/svg.js.map +1 -0
- package/dist/map/themes.d.ts +64 -0
- package/dist/map/themes.js +94 -0
- package/dist/map/themes.js.map +1 -0
- package/dist/map/xml.d.ts +6 -0
- package/dist/map/xml.js +57 -0
- package/dist/map/xml.js.map +1 -0
- package/dist/map/zones.d.ts +19 -0
- package/dist/map/zones.js +46 -0
- package/dist/map/zones.js.map +1 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +145 -0
- package/dist/mcp.js.map +1 -0
- package/dist/money.d.ts +32 -0
- package/dist/money.js +91 -0
- package/dist/money.js.map +1 -0
- package/dist/report-command.d.ts +3 -0
- package/dist/report-command.js +85 -0
- package/dist/report-command.js.map +1 -0
- package/dist/report-formats.d.ts +11 -0
- package/dist/report-formats.js +157 -0
- package/dist/report-formats.js.map +1 -0
- package/dist/reports.d.ts +105 -0
- package/dist/reports.js +247 -0
- package/dist/reports.js.map +1 -0
- package/dist/scaffold.d.ts +30 -0
- package/dist/scaffold.js +140 -0
- package/dist/scaffold.js.map +1 -0
- package/dist/schema.d.ts +345 -0
- package/dist/schema.js +120 -0
- package/dist/schema.js.map +1 -0
- package/dist/secrets.d.ts +7 -0
- package/dist/secrets.js +68 -0
- package/dist/secrets.js.map +1 -0
- package/dist/site/build.d.ts +22 -0
- package/dist/site/build.js +198 -0
- package/dist/site/build.js.map +1 -0
- package/dist/site/command.d.ts +2 -0
- package/dist/site/command.js +221 -0
- package/dist/site/command.js.map +1 -0
- package/dist/site/data.d.ts +225 -0
- package/dist/site/data.js +243 -0
- package/dist/site/data.js.map +1 -0
- package/dist/site/fs-ops.d.ts +35 -0
- package/dist/site/fs-ops.js +26 -0
- package/dist/site/fs-ops.js.map +1 -0
- package/dist/site/owners.d.ts +5 -0
- package/dist/site/owners.js +50 -0
- package/dist/site/owners.js.map +1 -0
- package/dist/site/paths.d.ts +34 -0
- package/dist/site/paths.js +48 -0
- package/dist/site/paths.js.map +1 -0
- package/dist/site/policy.d.ts +17 -0
- package/dist/site/policy.js +26 -0
- package/dist/site/policy.js.map +1 -0
- package/dist/site/publish.d.ts +14 -0
- package/dist/site/publish.js +90 -0
- package/dist/site/publish.js.map +1 -0
- package/dist/site/relative.d.ts +14 -0
- package/dist/site/relative.js +265 -0
- package/dist/site/relative.js.map +1 -0
- package/dist/site/skills-build.d.ts +22 -0
- package/dist/site/skills-build.js +126 -0
- package/dist/site/skills-build.js.map +1 -0
- package/dist/site/skills-data.d.ts +89 -0
- package/dist/site/skills-data.js +94 -0
- package/dist/site/skills-data.js.map +1 -0
- package/dist/site/staging.d.ts +19 -0
- package/dist/site/staging.js +76 -0
- package/dist/site/staging.js.map +1 -0
- package/dist/site/ui-icons.d.ts +4 -0
- package/dist/site/ui-icons.js +19 -0
- package/dist/site/ui-icons.js.map +1 -0
- package/dist/skills/command.d.ts +8 -0
- package/dist/skills/command.js +64 -0
- package/dist/skills/command.js.map +1 -0
- package/dist/skills/frontmatter.d.ts +37 -0
- package/dist/skills/frontmatter.js +219 -0
- package/dist/skills/frontmatter.js.map +1 -0
- package/dist/skills/graph.d.ts +39 -0
- package/dist/skills/graph.js +100 -0
- package/dist/skills/graph.js.map +1 -0
- package/dist/skills/ignore.d.ts +34 -0
- package/dist/skills/ignore.js +138 -0
- package/dist/skills/ignore.js.map +1 -0
- package/dist/skills/lines.d.ts +53 -0
- package/dist/skills/lines.js +221 -0
- package/dist/skills/lines.js.map +1 -0
- package/dist/skills/map-command.d.ts +28 -0
- package/dist/skills/map-command.js +533 -0
- package/dist/skills/map-command.js.map +1 -0
- package/dist/skills/mermaid.d.ts +29 -0
- package/dist/skills/mermaid.js +261 -0
- package/dist/skills/mermaid.js.map +1 -0
- package/dist/skills/metro.d.ts +8 -0
- package/dist/skills/metro.js +93 -0
- package/dist/skills/metro.js.map +1 -0
- package/dist/skills/read.d.ts +17 -0
- package/dist/skills/read.js +54 -0
- package/dist/skills/read.js.map +1 -0
- package/dist/skills/rules.d.ts +28 -0
- package/dist/skills/rules.js +57 -0
- package/dist/skills/rules.js.map +1 -0
- package/dist/skills/source.d.ts +66 -0
- package/dist/skills/source.js +281 -0
- package/dist/skills/source.js.map +1 -0
- package/dist/skills/walk.d.ts +64 -0
- package/dist/skills/walk.js +281 -0
- package/dist/skills/walk.js.map +1 -0
- package/dist/source.d.ts +40 -0
- package/dist/source.js +199 -0
- package/dist/source.js.map +1 -0
- package/dist/stdout.d.ts +2 -0
- package/dist/stdout.js +24 -0
- package/dist/stdout.js.map +1 -0
- package/dist/validate.d.ts +45 -0
- package/dist/validate.js +321 -0
- package/dist/validate.js.map +1 -0
- package/dist/views.d.ts +25 -0
- package/dist/views.js +58 -0
- package/dist/views.js.map +1 -0
- package/examples/catalogue-repository/.github/workflows/stackledger.yml +122 -0
- package/examples/catalogue-repository/README.md +96 -0
- package/examples/catalogue-repository/catalog/catalog-info.yaml +12 -0
- package/examples/hosting/Caddyfile +69 -0
- package/examples/northwind/catalog-info.yaml +680 -0
- package/examples/northwind/stackledger.yaml +16 -0
- package/examples/northwind-machine/snapshot.json +309 -0
- package/examples/northwind-skills/agents/builder.md +10 -0
- package/examples/northwind-skills/agents/reviewer.md +10 -0
- package/examples/northwind-skills/designer/SKILL.md +8 -0
- package/examples/northwind-skills/editor/SKILL.md +8 -0
- package/examples/northwind-skills/engineering/SKILL.md +12 -0
- package/examples/northwind-skills/linter/SKILL.md +8 -0
- package/examples/northwind-skills/linter/rules.md +5 -0
- package/examples/northwind-skills/publishing/SKILL.md +12 -0
- package/examples/northwind-skills/publishing/rules.md +5 -0
- package/examples/northwind-skills/spec-writer/SKILL.md +8 -0
- package/examples/northwind-skills/stackledger-skills.yaml +51 -0
- package/examples/northwind-skills/tester/SKILL.md +10 -0
- package/examples/northwind-skills/writer/SKILL.md +8 -0
- package/examples/northwind-skills/writer/rules.md +6 -0
- package/npm-shrinkwrap.json +5016 -0
- package/package.json +52 -4
- package/site/LICENSE-astroplate +21 -0
- package/site/README.md +18 -0
- package/site/package.json +6 -0
- package/site/public/brand-mark-dark.svg +7 -0
- package/site/public/favicon.svg +6 -0
- package/site/public/home-banner.svg +93 -0
- package/site/public/link-icons/email-dark.svg +7 -0
- package/site/public/link-icons/email-light.svg +6 -0
- package/site/public/link-icons/github-dark.svg +8 -0
- package/site/public/link-icons/github-light.svg +7 -0
- package/site/public/link-icons/linkedin-dark.svg +8 -0
- package/site/public/link-icons/linkedin-light.svg +7 -0
- package/site/public/link-icons/website-dark.svg +8 -0
- package/site/public/link-icons/website-light.svg +7 -0
- package/site/src/components/Footer.astro +25 -0
- package/site/src/components/Header.astro +35 -0
- package/site/src/components/Impact.astro +55 -0
- package/site/src/components/LinkOrText.astro +12 -0
- package/site/src/components/SkillItem.astro +141 -0
- package/site/src/components/SubNav.astro +40 -0
- package/site/src/components/ThemeSwitch.astro +13 -0
- package/site/src/components/Themed.astro +15 -0
- package/site/src/layouts/Base.astro +52 -0
- package/site/src/lib/site.ts +25 -0
- package/site/src/pages/[reports].astro +222 -0
- package/site/src/pages/agents/[file].astro +14 -0
- package/site/src/pages/index.astro +72 -0
- package/site/src/pages/journeys/[name].astro +50 -0
- package/site/src/pages/journeys/index.astro +37 -0
- package/site/src/pages/licences/[name].astro +56 -0
- package/site/src/pages/map/index.astro +78 -0
- package/site/src/pages/playbooks/[file].astro +14 -0
- package/site/src/pages/rules/[...rules].astro +61 -0
- package/site/src/pages/skill-map/[...map].astro +101 -0
- package/site/src/pages/skills/[...list].astro +75 -0
- package/site/src/pages/skills/[file].astro +14 -0
- package/site/src/pages/software/[name].astro +155 -0
- package/site/src/pages/software/index.astro +71 -0
- package/site/src/scripts/diagram.ts +8 -0
- package/site/src/scripts/links.ts +31 -0
- package/site/src/scripts/map.ts +133 -0
- package/site/src/scripts/software.ts +32 -0
- package/site/src/scripts/theme.ts +52 -0
- package/site/src/styles/base.css +481 -0
- package/site/src/styles/fonts.css +60 -0
- package/site/src/styles/override.css +1 -0
- package/site/src/styles/pages.css +315 -0
- package/site/src/styles/tokens.css +87 -0
- package/skills/stackledger/SKILL.md +155 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/** Write the snapshot to <root>/<machine>/snapshot.json (design D4, discover-command spec): the only
|
|
2
|
+
* place discovery writes.
|
|
3
|
+
*
|
|
4
|
+
* Steps: serialise first (over 32 MiB fails before any file system call); refuse a home folder that
|
|
5
|
+
* group or others can write; check, or create with mode 0700 one level at a time, the snapshot root
|
|
6
|
+
* and the machine folder (no link, a folder, owned by the user, no permission for group or others) and
|
|
7
|
+
* remember their device and inode; refuse a snapshot.json that is not a regular file; create a new
|
|
8
|
+
* temporary file with O_EXCL, O_NOFOLLOW and mode 0600; check the folders again; write, fsync, close;
|
|
9
|
+
* check again; rename over snapshot.json. A reader sees the previous complete file or the new one;
|
|
10
|
+
* two runs at once each rename a complete file and the last one wins.
|
|
11
|
+
*
|
|
12
|
+
* Threat model: this is a boundary against other users (they cannot write anything on the path). It
|
|
13
|
+
* only detects a swap by the user's own processes: Node has no openat/renameat to close the window
|
|
14
|
+
* between the last check and the rename. The temporary file is renamed, or removed after a failure, by
|
|
15
|
+
* its path only while that path still leads to the file written (a regular file with the device and
|
|
16
|
+
* inode fstat gave its descriptor); in a folder moved away, or replaced, it stays. When fstat fails
|
|
17
|
+
* the file's identity is unknown, so nothing is removed and the error line says the file was left.
|
|
18
|
+
*
|
|
19
|
+
* Every call goes through WriteOps, so tests inject a swap at a chosen call. The shipped code always
|
|
20
|
+
* uses NODE_WRITE_OPS. Failures throw InternalError (exit 1, one line).
|
|
21
|
+
*/
|
|
22
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
23
|
+
import { randomBytes } from 'node:crypto';
|
|
24
|
+
import { join } from 'node:path';
|
|
25
|
+
import { InternalError } from '../errors.js';
|
|
26
|
+
import { serialise } from './snapshot.js';
|
|
27
|
+
export const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;
|
|
28
|
+
export const SNAPSHOT_FILE = 'snapshot.json';
|
|
29
|
+
const PRIVATE_FOLDER = 0o700, PRIVATE_FILE = 0o600;
|
|
30
|
+
/** Mode bits; src/drift-command.ts checks the `--scaffold` folders with the same rules. */
|
|
31
|
+
export const WRITABLE_BY_OTHERS = 0o022;
|
|
32
|
+
const ANY_PERMISSION_FOR_OTHERS = 0o077;
|
|
33
|
+
export const NODE_WRITE_OPS = {
|
|
34
|
+
stat: path => statSync(path),
|
|
35
|
+
lstat: path => lstatSync(path),
|
|
36
|
+
mkdir: (path, mode) => { mkdirSync(path, { mode }); },
|
|
37
|
+
open: (path, flags, mode) => openSync(path, flags, mode),
|
|
38
|
+
fstat: fd => fstatSync(fd),
|
|
39
|
+
write: (fd, text) => writeFileSync(fd, text),
|
|
40
|
+
fsync: fd => {
|
|
41
|
+
// Under Node's permission model (`--permission`) fsync is refused even for a file the process may
|
|
42
|
+
// write. The rename stays atomic without it; only durability after a power loss is lost, so that
|
|
43
|
+
// one refusal is accepted on purpose and every other failure is reported.
|
|
44
|
+
try {
|
|
45
|
+
fsyncSync(fd);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code !== 'ERR_ACCESS_DENIED')
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
close: fd => closeSync(fd),
|
|
53
|
+
rename: (from, to) => renameSync(from, to),
|
|
54
|
+
unlink: path => unlinkSync(path),
|
|
55
|
+
};
|
|
56
|
+
const fail = (message) => new InternalError(`stackledger discover: ${message}`);
|
|
57
|
+
const code = (error) => error.code ?? 'unknown error';
|
|
58
|
+
/** Whether `status` (from lstat) is still the folder checked earlier: no link, a folder, same device and inode. */
|
|
59
|
+
export function sameFolder(status, folder) {
|
|
60
|
+
return !status.isSymbolicLink() && status.isDirectory() && status.dev === folder.dev && status.ino === folder.ino;
|
|
61
|
+
}
|
|
62
|
+
/** Check one snapshot folder, creating it when absent; refuses anything another user could change. */
|
|
63
|
+
function checkFolder(ops, path, label) {
|
|
64
|
+
let status;
|
|
65
|
+
try {
|
|
66
|
+
status = ops.lstat(path);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (code(error) !== 'ENOENT')
|
|
70
|
+
throw fail(`cannot read ${label} (${code(error)})`);
|
|
71
|
+
// EEXIST: another run created it meanwhile; it is checked like any existing folder.
|
|
72
|
+
try {
|
|
73
|
+
ops.mkdir(path, PRIVATE_FOLDER);
|
|
74
|
+
}
|
|
75
|
+
catch (inner) {
|
|
76
|
+
if (code(inner) !== 'EEXIST')
|
|
77
|
+
throw fail(`cannot create ${label} (${code(inner)})`);
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
status = ops.lstat(path);
|
|
81
|
+
}
|
|
82
|
+
catch (inner) {
|
|
83
|
+
throw fail(`cannot read ${label} (${code(inner)})`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (status.isSymbolicLink())
|
|
87
|
+
throw fail(`${label} is a symbolic link; nothing was written`);
|
|
88
|
+
if (!status.isDirectory())
|
|
89
|
+
throw fail(`${label} is not a folder; nothing was written`);
|
|
90
|
+
if (status.uid !== process.getuid())
|
|
91
|
+
throw fail(`${label} is not owned by the current user; nothing was written`);
|
|
92
|
+
if (status.mode & ANY_PERMISSION_FOR_OTHERS)
|
|
93
|
+
throw fail(`${label} must be private to the user (mode 0700); nothing was written`);
|
|
94
|
+
return { path, dev: status.dev, ino: status.ino };
|
|
95
|
+
}
|
|
96
|
+
function unchanged(ops, folders) {
|
|
97
|
+
return folders.every(folder => {
|
|
98
|
+
try {
|
|
99
|
+
return sameFolder(ops.lstat(folder.path), folder);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** Whether `path` still leads to the file written: a regular file (no link) with its device and inode. */
|
|
107
|
+
function stillTemporary(ops, path, file) {
|
|
108
|
+
try {
|
|
109
|
+
const status = ops.lstat(path);
|
|
110
|
+
return status.isFile() && !status.isSymbolicLink() && status.dev === file.dev && status.ino === file.ino;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Remove the temporary file only while its path still leads to the file written. */
|
|
117
|
+
function removeTemporary(ops, path, file) {
|
|
118
|
+
if (!stillTemporary(ops, path, file))
|
|
119
|
+
return; // Gone, moved away with its folder, or replaced: not this run's.
|
|
120
|
+
try {
|
|
121
|
+
ops.unlink(path);
|
|
122
|
+
}
|
|
123
|
+
catch { /* Gone meanwhile: there is nothing at this path to remove. */ }
|
|
124
|
+
}
|
|
125
|
+
/** Write the snapshot; returns the path of snapshot.json. */
|
|
126
|
+
export function writeSnapshot(snapshot, place, ops = NODE_WRITE_OPS) {
|
|
127
|
+
const text = serialise(snapshot);
|
|
128
|
+
if (Buffer.byteLength(text) > MAX_SNAPSHOT_BYTES)
|
|
129
|
+
throw fail('the snapshot is too large (over 32 MiB); nothing was written');
|
|
130
|
+
let home;
|
|
131
|
+
try {
|
|
132
|
+
home = ops.stat(place.home);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw fail(`cannot read the home folder (${code(error)})`);
|
|
136
|
+
}
|
|
137
|
+
if (home.mode & WRITABLE_BY_OTHERS)
|
|
138
|
+
throw fail('the home folder can be written by group or others; nothing was written');
|
|
139
|
+
const machine = join(place.root, snapshot.machine.name);
|
|
140
|
+
const folders = [checkFolder(ops, place.root, 'the snapshot folder'), checkFolder(ops, machine, 'the machine folder')];
|
|
141
|
+
const target = join(machine, SNAPSHOT_FILE);
|
|
142
|
+
try {
|
|
143
|
+
if (!ops.lstat(target).isFile())
|
|
144
|
+
throw fail(`${SNAPSHOT_FILE} is not a regular file; nothing was written`);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error instanceof InternalError)
|
|
148
|
+
throw error;
|
|
149
|
+
if (code(error) !== 'ENOENT')
|
|
150
|
+
throw fail(`cannot read ${SNAPSHOT_FILE} (${code(error)})`);
|
|
151
|
+
}
|
|
152
|
+
const temporary = join(machine, `snapshot.${process.pid}.${randomBytes(6).toString('hex')}.tmp`);
|
|
153
|
+
let fd, file;
|
|
154
|
+
try {
|
|
155
|
+
fd = ops.open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, PRIVATE_FILE);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
throw fail(`cannot create the snapshot file (${code(error)})`);
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const status = ops.fstat(fd);
|
|
162
|
+
file = { dev: status.dev, ino: status.ino };
|
|
163
|
+
const swapped = () => fail('a snapshot folder changed while writing; the previous snapshot was kept');
|
|
164
|
+
if (!unchanged(ops, folders))
|
|
165
|
+
throw swapped();
|
|
166
|
+
ops.write(fd, text);
|
|
167
|
+
ops.fsync(fd);
|
|
168
|
+
ops.close(fd);
|
|
169
|
+
fd = undefined;
|
|
170
|
+
if (!unchanged(ops, folders))
|
|
171
|
+
throw swapped();
|
|
172
|
+
// The rename moves whatever the path now holds: only the file written may become snapshot.json.
|
|
173
|
+
// A replacement is not this run's, so it is left where it is.
|
|
174
|
+
if (!stillTemporary(ops, temporary, file))
|
|
175
|
+
throw fail('the temporary snapshot file was replaced while writing; the previous snapshot was kept');
|
|
176
|
+
ops.rename(temporary, target);
|
|
177
|
+
return target;
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (fd !== undefined) {
|
|
181
|
+
try {
|
|
182
|
+
ops.close(fd);
|
|
183
|
+
}
|
|
184
|
+
catch { /* The write already failed; that failure is reported. */ }
|
|
185
|
+
}
|
|
186
|
+
// Without the identity fstat gives, a file at the temporary path may be a replacement: nothing is
|
|
187
|
+
// removed, and the one error line says the file was left.
|
|
188
|
+
if (file === undefined)
|
|
189
|
+
throw fail(`cannot write the snapshot (${code(error)}); the previous snapshot was kept, and the temporary file was left because its identity could not be read`);
|
|
190
|
+
removeTemporary(ops, temporary, file);
|
|
191
|
+
if (error instanceof InternalError)
|
|
192
|
+
throw error;
|
|
193
|
+
throw fail(`cannot write the snapshot (${code(error)}); the previous snapshot was kept`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=write.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.js","sourceRoot":"","sources":["../../src/discovery/write.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,E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AAC,CAAC;YAAC,CAAC;YAAC,MAAM,CAAC,CAAC,yDAAyD,CAAC,CAAC;QAAC,CAAC;QACpH,kGAAkG;QAClG,0DAA0D;QAC1D,IAAI,IAAI,KAAK,SAAS;YAAE,MAAM,IAAI,CAAC,8BAA8B,IAAI,CAAC,KAAK,CAAC,2GAA2G,CAAC,CAAC;QACzL,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;QACtC,IAAI,KAAK,YAAY,aAAa;YAAE,MAAM,KAAK,CAAC;QAChD,MAAM,IAAI,CAAC,8BAA8B,IAAI,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAC3F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** `stackledger drift <folder> [--snapshot <file>] [--format text|json] [--scaffold <dir>]`
|
|
2
|
+
* (drift-command spec, design D6 and D10). Called by src/cli.ts; run node dist/cli.js drift examples/northwind --snapshot
|
|
3
|
+
* examples/northwind-machine/snapshot.json.
|
|
4
|
+
*
|
|
5
|
+
* Like `stackledger report`: usage, folder and snapshot problems throw a plain Error before anything
|
|
6
|
+
* is written (exit 2 with usage, from src/cli.ts); a catalogue with errors is still compared, using the
|
|
7
|
+
* components the `full` view serves, with the counts line of src/map/command.ts on stderr. The whole
|
|
8
|
+
* output is built in memory first, so a failure while comparing or serialising leaves stdout empty and
|
|
9
|
+
* exits 1 with one line; a failure while writing stdout (a closed pipe) also exits 1 with one line.
|
|
10
|
+
*
|
|
11
|
+
* The snapshot: `--snapshot <file>`, or this machine's ~/.stackledger/<machine>/snapshot.json, whose
|
|
12
|
+
* machine name needs macOS and the sandbox (src/discovery/machine.ts through the sandboxed `scutil`).
|
|
13
|
+
* It must be a regular file of at most 32 MiB holding a `stackledger-snapshot/v1` snapshot that passes
|
|
14
|
+
* the consistency rules and the secret guard (src/discovery/snapshot.ts). Every reason is a fixed text:
|
|
15
|
+
* never the file's path or content.
|
|
16
|
+
*
|
|
17
|
+
* Without `--scaffold` it only reads the catalogue folder and the snapshot. With `--scaffold <dir>` it
|
|
18
|
+
* also writes the draft files of src/scaffold.ts, and only there: `dir` must not exist yet and must be
|
|
19
|
+
* neither inside the catalogue folder nor contain it (real paths compared; a usage error otherwise).
|
|
20
|
+
* Every folder on the real path of its parent, from / down to the parent, must not be writable by group
|
|
21
|
+
* or others unless it has the sticky bit (as /tmp has), and must be owned by the user or root: otherwise
|
|
22
|
+
* another user could swap `dir`, or the parent, for a link (usage error). Everything is built and
|
|
23
|
+
* checked in memory first; then `dir` is created with an exclusive mkdir (no `recursive`, so a folder
|
|
24
|
+
* created meanwhile is exit 2) and mode 0700, and its device and inode are remembered, as
|
|
25
|
+
* src/discovery/write.ts does for the snapshot folders. The real path of the folder made must be the
|
|
26
|
+
* one checked and outside the catalogue, else it is removed (still that folder and empty) and the
|
|
27
|
+
* command exits 1: the parent was replaced after the checks. Before each file is created
|
|
28
|
+
* (`wx`, mode 0644) and before stdout, `dir` must still be that folder and no link, else exit 1. A
|
|
29
|
+
* failed write exits 1 with one line and removes what this run created: each file whose exclusive
|
|
30
|
+
* create succeeded and that lstat still finds as a regular file with the device and inode its
|
|
31
|
+
* descriptor had (so a file another process put there, before or after, is kept), then `dir` when it is
|
|
32
|
+
* still the folder made (checked again right before the rmdir) and empty. Like write.ts this only detects a swap by the user's own
|
|
33
|
+
* processes: Node has no openat to close the window between the check and the create.
|
|
34
|
+
*
|
|
35
|
+
* `host` and `ops` exist for the tests (test/integration/drift-entry.mjs); src/cli.ts passes nothing,
|
|
36
|
+
* so the shipped command reads the real machine and writes through node:fs.
|
|
37
|
+
*/
|
|
38
|
+
import { type Stats } from 'node:fs';
|
|
39
|
+
import { type Host } from './discovery/host.js';
|
|
40
|
+
export declare const DRIFT_USAGE = "Usage: stackledger drift <folder> [--snapshot <file>] [--format text|json] [--scaffold <dir>]\n";
|
|
41
|
+
export declare const DRIFT_FORMATS: readonly ["text", "json"];
|
|
42
|
+
/** The file system calls of `--scaffold` (as src/site/fs-ops.ts does for build), so tests can inject a
|
|
43
|
+
* failure or a folder created between the checks and the mkdir. Synchronous; each throws what node:fs throws. */
|
|
44
|
+
export interface ScaffoldOps {
|
|
45
|
+
lstat(path: string): Stats;
|
|
46
|
+
realpath(path: string): string;
|
|
47
|
+
/** Create one folder, failing with EEXIST when it exists (never recursive). */
|
|
48
|
+
mkdir(path: string): void;
|
|
49
|
+
/** Create a new file exclusively (`wx`, mode 0644), failing with EEXIST when it exists; returns its
|
|
50
|
+
* descriptor and the device and inode the descriptor has (fstat), closing it when fstat fails. */
|
|
51
|
+
create(path: string): {
|
|
52
|
+
fd: number;
|
|
53
|
+
dev: number;
|
|
54
|
+
ino: number;
|
|
55
|
+
};
|
|
56
|
+
/** Write all of the text to a descriptor from `create`, then close it (also when the write fails). */
|
|
57
|
+
write(fd: number, text: string): void;
|
|
58
|
+
unlink(path: string): void;
|
|
59
|
+
rmdir(path: string): void;
|
|
60
|
+
}
|
|
61
|
+
export declare const NODE_SCAFFOLD_OPS: ScaffoldOps;
|
|
62
|
+
export declare function driftCommand(args: string[], host?: Host, ops?: ScaffoldOps): Promise<void>;
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/** `stackledger drift <folder> [--snapshot <file>] [--format text|json] [--scaffold <dir>]`
|
|
2
|
+
* (drift-command spec, design D6 and D10). Called by src/cli.ts; run node dist/cli.js drift examples/northwind --snapshot
|
|
3
|
+
* examples/northwind-machine/snapshot.json.
|
|
4
|
+
*
|
|
5
|
+
* Like `stackledger report`: usage, folder and snapshot problems throw a plain Error before anything
|
|
6
|
+
* is written (exit 2 with usage, from src/cli.ts); a catalogue with errors is still compared, using the
|
|
7
|
+
* components the `full` view serves, with the counts line of src/map/command.ts on stderr. The whole
|
|
8
|
+
* output is built in memory first, so a failure while comparing or serialising leaves stdout empty and
|
|
9
|
+
* exits 1 with one line; a failure while writing stdout (a closed pipe) also exits 1 with one line.
|
|
10
|
+
*
|
|
11
|
+
* The snapshot: `--snapshot <file>`, or this machine's ~/.stackledger/<machine>/snapshot.json, whose
|
|
12
|
+
* machine name needs macOS and the sandbox (src/discovery/machine.ts through the sandboxed `scutil`).
|
|
13
|
+
* It must be a regular file of at most 32 MiB holding a `stackledger-snapshot/v1` snapshot that passes
|
|
14
|
+
* the consistency rules and the secret guard (src/discovery/snapshot.ts). Every reason is a fixed text:
|
|
15
|
+
* never the file's path or content.
|
|
16
|
+
*
|
|
17
|
+
* Without `--scaffold` it only reads the catalogue folder and the snapshot. With `--scaffold <dir>` it
|
|
18
|
+
* also writes the draft files of src/scaffold.ts, and only there: `dir` must not exist yet and must be
|
|
19
|
+
* neither inside the catalogue folder nor contain it (real paths compared; a usage error otherwise).
|
|
20
|
+
* Every folder on the real path of its parent, from / down to the parent, must not be writable by group
|
|
21
|
+
* or others unless it has the sticky bit (as /tmp has), and must be owned by the user or root: otherwise
|
|
22
|
+
* another user could swap `dir`, or the parent, for a link (usage error). Everything is built and
|
|
23
|
+
* checked in memory first; then `dir` is created with an exclusive mkdir (no `recursive`, so a folder
|
|
24
|
+
* created meanwhile is exit 2) and mode 0700, and its device and inode are remembered, as
|
|
25
|
+
* src/discovery/write.ts does for the snapshot folders. The real path of the folder made must be the
|
|
26
|
+
* one checked and outside the catalogue, else it is removed (still that folder and empty) and the
|
|
27
|
+
* command exits 1: the parent was replaced after the checks. Before each file is created
|
|
28
|
+
* (`wx`, mode 0644) and before stdout, `dir` must still be that folder and no link, else exit 1. A
|
|
29
|
+
* failed write exits 1 with one line and removes what this run created: each file whose exclusive
|
|
30
|
+
* create succeeded and that lstat still finds as a regular file with the device and inode its
|
|
31
|
+
* descriptor had (so a file another process put there, before or after, is kept), then `dir` when it is
|
|
32
|
+
* still the folder made (checked again right before the rmdir) and empty. Like write.ts this only detects a swap by the user's own
|
|
33
|
+
* processes: Node has no openat to close the window between the check and the create.
|
|
34
|
+
*
|
|
35
|
+
* `host` and `ops` exist for the tests (test/integration/drift-entry.mjs); src/cli.ts passes nothing,
|
|
36
|
+
* so the shipped command reads the real machine and writes through node:fs.
|
|
37
|
+
*/
|
|
38
|
+
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, realpathSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
39
|
+
import { open } from 'node:fs/promises';
|
|
40
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
41
|
+
import { parseArgs } from 'node:util';
|
|
42
|
+
import { InternalError, internal } from './errors.js';
|
|
43
|
+
import { createHost } from './discovery/host.js';
|
|
44
|
+
import { machineName } from './discovery/machine.js';
|
|
45
|
+
import { checkSnapshot } from './discovery/snapshot.js';
|
|
46
|
+
import { MAX_SNAPSHOT_BYTES, sameFolder, WRITABLE_BY_OTHERS } from './discovery/write.js';
|
|
47
|
+
import { buildDrift } from './drift.js';
|
|
48
|
+
import { formatDriftJson, formatDriftText } from './drift-formats.js';
|
|
49
|
+
import { problemCounts } from './map/command.js';
|
|
50
|
+
import { buildScaffold, checkScaffold } from './scaffold.js';
|
|
51
|
+
import { loadFolder } from './source.js';
|
|
52
|
+
import { writeStdout } from './stdout.js';
|
|
53
|
+
import { validateLoaded } from './validate.js';
|
|
54
|
+
import { applyView, resolveHidden } from './views.js';
|
|
55
|
+
export const DRIFT_USAGE = 'Usage: stackledger drift <folder> [--snapshot <file>] [--format text|json] [--scaffold <dir>]\n';
|
|
56
|
+
export const DRIFT_FORMATS = ['text', 'json'];
|
|
57
|
+
export const NODE_SCAFFOLD_OPS = {
|
|
58
|
+
lstat: path => lstatSync(path),
|
|
59
|
+
realpath: path => realpathSync.native(path),
|
|
60
|
+
mkdir: path => { mkdirSync(path, { mode: 0o700 }); },
|
|
61
|
+
create: path => {
|
|
62
|
+
const fd = openSync(path, 'wx', 0o644);
|
|
63
|
+
try {
|
|
64
|
+
const { dev, ino } = fstatSync(fd);
|
|
65
|
+
return { fd, dev, ino };
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
closeSync(fd);
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
write: (fd, text) => { try {
|
|
73
|
+
writeFileSync(fd, text);
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
closeSync(fd);
|
|
77
|
+
} },
|
|
78
|
+
unlink: path => unlinkSync(path),
|
|
79
|
+
rmdir: path => rmdirSync(path),
|
|
80
|
+
};
|
|
81
|
+
const code = (error) => error.code ?? 'unknown';
|
|
82
|
+
const STICKY = 0o1000, ROOT = 0;
|
|
83
|
+
function parseDriftArgs(args) {
|
|
84
|
+
const { values, positionals } = parseArgs({ args, strict: true, allowPositionals: true,
|
|
85
|
+
options: { snapshot: { type: 'string' }, format: { type: 'string' }, scaffold: { type: 'string' } } });
|
|
86
|
+
const [, folder, ...extra] = positionals;
|
|
87
|
+
if (!folder)
|
|
88
|
+
throw new Error('A catalogue folder is required');
|
|
89
|
+
if (extra.length)
|
|
90
|
+
throw new Error('Only one folder is accepted');
|
|
91
|
+
const format = values.format ?? 'text';
|
|
92
|
+
if (!DRIFT_FORMATS.includes(format))
|
|
93
|
+
throw new Error(`Unknown format; allowed formats: ${DRIFT_FORMATS.join(', ')}`);
|
|
94
|
+
if (values.scaffold === '')
|
|
95
|
+
throw new Error('--scaffold needs a folder');
|
|
96
|
+
return { folder, snapshot: values.snapshot, format: format, scaffold: values.scaffold };
|
|
97
|
+
}
|
|
98
|
+
/** This machine's snapshot path; its name can only be found on macOS with the sandbox. */
|
|
99
|
+
async function defaultSnapshotPath(host) {
|
|
100
|
+
if (host.platform !== 'darwin' || !(await host.isFile(host.sandbox))) {
|
|
101
|
+
throw new Error("This machine's name cannot be found (it needs macOS and its sandbox); pass --snapshot <file>");
|
|
102
|
+
}
|
|
103
|
+
return join(host.snapshotRoot, await machineName(host.run), 'snapshot.json');
|
|
104
|
+
}
|
|
105
|
+
const SNAPSHOT_PROBLEMS = {
|
|
106
|
+
format: 'The snapshot format is not supported; expected stackledger-snapshot/v1',
|
|
107
|
+
shape: 'The snapshot does not have the stackledger-snapshot/v1 shape',
|
|
108
|
+
consistency: 'The snapshot is not consistent',
|
|
109
|
+
};
|
|
110
|
+
/** A snapshot that cannot be used: a usage error (exit 2) whose message is a fixed text. */
|
|
111
|
+
class SnapshotProblem extends Error {
|
|
112
|
+
}
|
|
113
|
+
/** Read the snapshot's bytes: a regular file of at most 32 MiB. */
|
|
114
|
+
async function readSnapshotBytes(path) {
|
|
115
|
+
let handle;
|
|
116
|
+
// O_NONBLOCK: a FIFO in place of the file must not block the open; it is refused as not a regular file.
|
|
117
|
+
try {
|
|
118
|
+
handle = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
const code = error.code;
|
|
122
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
123
|
+
throw new SnapshotProblem('No snapshot found; run stackledger discover to make one, or pass --snapshot <file>');
|
|
124
|
+
throw new SnapshotProblem('The snapshot cannot be read');
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
const status = await handle.stat();
|
|
128
|
+
if (!status.isFile())
|
|
129
|
+
throw new SnapshotProblem('The snapshot is not a regular file');
|
|
130
|
+
if (status.size > MAX_SNAPSHOT_BYTES)
|
|
131
|
+
throw new SnapshotProblem('The snapshot is larger than 32 MiB');
|
|
132
|
+
// Room for one byte more than the size: a file that grew after the stat is refused, not cut.
|
|
133
|
+
const buffer = Buffer.alloc(status.size + 1);
|
|
134
|
+
let length = 0;
|
|
135
|
+
while (length < buffer.length) {
|
|
136
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
137
|
+
if (bytesRead === 0)
|
|
138
|
+
break;
|
|
139
|
+
length += bytesRead;
|
|
140
|
+
}
|
|
141
|
+
if (length > status.size)
|
|
142
|
+
throw new SnapshotProblem('The snapshot changed while it was read');
|
|
143
|
+
return buffer.subarray(0, length);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
throw error instanceof SnapshotProblem ? error : new SnapshotProblem('The snapshot cannot be read');
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
await handle.close();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Read and check the snapshot; every failure is a usage error with a fixed reason. */
|
|
153
|
+
async function readSnapshot(path) {
|
|
154
|
+
const bytes = await readSnapshotBytes(path);
|
|
155
|
+
let value;
|
|
156
|
+
try {
|
|
157
|
+
value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
throw new SnapshotProblem('The snapshot is not JSON');
|
|
161
|
+
}
|
|
162
|
+
const checked = checkSnapshot(value);
|
|
163
|
+
if ('snapshot' in checked)
|
|
164
|
+
return checked.snapshot;
|
|
165
|
+
if (checked.problem === 'secret')
|
|
166
|
+
throw new SnapshotProblem(`The snapshot breaks a secret rule: ${checked.rule}`);
|
|
167
|
+
throw new SnapshotProblem(SNAPSHOT_PROBLEMS[checked.problem]);
|
|
168
|
+
}
|
|
169
|
+
/** The folders from `/` down to `folder` (a real path), the root first. */
|
|
170
|
+
function foldersDownTo(folder) {
|
|
171
|
+
const chain = [];
|
|
172
|
+
for (let current = folder;; current = dirname(current)) {
|
|
173
|
+
chain.unshift(current);
|
|
174
|
+
if (dirname(current) === current)
|
|
175
|
+
return chain;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Whether `target` is the catalogue, inside it, or contains it (real paths). */
|
|
179
|
+
function overlapsCatalogue(target, catalogueReal) {
|
|
180
|
+
if (target === catalogueReal || target.startsWith(catalogueReal + sep))
|
|
181
|
+
return 'inside';
|
|
182
|
+
return catalogueReal.startsWith(target + sep) ? 'contains' : undefined;
|
|
183
|
+
}
|
|
184
|
+
/** The real path `--scaffold` will create; usage errors when it exists, is inside the catalogue or contains
|
|
185
|
+
* it, or when another user could change a folder on the way to it. */
|
|
186
|
+
function scaffoldTarget(dir, catalogueFolder, ops) {
|
|
187
|
+
const absolute = resolve(dir);
|
|
188
|
+
let exists = true;
|
|
189
|
+
try {
|
|
190
|
+
ops.lstat(absolute);
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
if (code(error) !== 'ENOENT')
|
|
194
|
+
throw new Error('The scaffold folder cannot be checked');
|
|
195
|
+
exists = false;
|
|
196
|
+
}
|
|
197
|
+
if (exists)
|
|
198
|
+
throw new Error('The scaffold folder already exists; name a new folder');
|
|
199
|
+
let parent;
|
|
200
|
+
try {
|
|
201
|
+
parent = ops.realpath(dirname(absolute));
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
throw new Error('The folder that should hold the scaffold folder was not found');
|
|
205
|
+
}
|
|
206
|
+
// Another user who can write the parent could replace the new folder with a link, and one who can
|
|
207
|
+
// write any folder above it could replace the parent itself before the mkdir; so every folder on the
|
|
208
|
+
// parent's real path, from / down, must be the user's or root's and closed to others. The sticky
|
|
209
|
+
// bit (as on /tmp) stops other users renaming or removing a folder they do not own.
|
|
210
|
+
for (const folder of foldersDownTo(parent)) {
|
|
211
|
+
const which = folder === parent ? 'The folder that should hold the scaffold folder' : 'A folder above the one that should hold the scaffold folder';
|
|
212
|
+
let status;
|
|
213
|
+
try {
|
|
214
|
+
status = ops.lstat(folder);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
throw new Error(`${which} cannot be checked`);
|
|
218
|
+
}
|
|
219
|
+
if ((status.mode & WRITABLE_BY_OTHERS) && !(status.mode & STICKY))
|
|
220
|
+
throw new Error(`${which} can be written by other users; name a folder in a private place`);
|
|
221
|
+
if (status.uid !== process.getuid() && status.uid !== ROOT)
|
|
222
|
+
throw new Error(`${which} is not owned by the current user or root; name a folder in a private place`);
|
|
223
|
+
}
|
|
224
|
+
const target = join(parent, basename(absolute)), catalogueReal = ops.realpath(catalogueFolder);
|
|
225
|
+
const overlap = overlapsCatalogue(target, catalogueReal);
|
|
226
|
+
if (overlap === 'inside')
|
|
227
|
+
throw new Error('The scaffold folder must be outside the catalogue folder');
|
|
228
|
+
if (overlap === 'contains')
|
|
229
|
+
throw new Error('The scaffold folder must not contain the catalogue folder');
|
|
230
|
+
return { target, catalogueReal };
|
|
231
|
+
}
|
|
232
|
+
/** Every `metadata.name` of a catalogue document whose kind is Component, errors or not. */
|
|
233
|
+
function componentNames(loaded) {
|
|
234
|
+
return loaded.sources.flatMap(source => {
|
|
235
|
+
const metadata = source.value.metadata;
|
|
236
|
+
const name = metadata !== null && typeof metadata === 'object' ? metadata.name : undefined;
|
|
237
|
+
return source.value.kind === 'Component' && typeof name === 'string' ? [name] : [];
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/** A swap of the scaffold folder seen during the write (exit 1). */
|
|
241
|
+
class FolderReplaced extends InternalError {
|
|
242
|
+
constructor() { super('stackledger drift: the scaffold folder was replaced while writing; the command stopped and wrote nothing through the new path'); }
|
|
243
|
+
}
|
|
244
|
+
/** Create `target` and write every file in it; on a failure remove what was written, then throw. */
|
|
245
|
+
function writeScaffold({ target, catalogueReal }, files, ops) {
|
|
246
|
+
try {
|
|
247
|
+
ops.mkdir(target);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
if (code(error) === 'EEXIST')
|
|
251
|
+
throw new Error('The scaffold folder already exists; name a new folder');
|
|
252
|
+
throw new InternalError(`stackledger drift: cannot create the scaffold folder (${code(error)})`);
|
|
253
|
+
}
|
|
254
|
+
// The folder just made, by device and inode; every later step first checks `target` still is it.
|
|
255
|
+
let folder;
|
|
256
|
+
try {
|
|
257
|
+
const status = ops.lstat(target);
|
|
258
|
+
if (!status.isDirectory() || status.isSymbolicLink())
|
|
259
|
+
throw new FolderReplaced();
|
|
260
|
+
folder = { dev: status.dev, ino: status.ino };
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (error instanceof FolderReplaced)
|
|
264
|
+
throw error;
|
|
265
|
+
throw new InternalError(`stackledger drift: cannot check the scaffold folder (${code(error)})`);
|
|
266
|
+
}
|
|
267
|
+
// A parent swapped for a link between the checks and the mkdir puts the new folder somewhere else,
|
|
268
|
+
// possibly in the catalogue: its real path must be the one checked. Otherwise the folder this run
|
|
269
|
+
// made is removed by its real path, only while it is still that folder (rmdir keeps a non-empty one).
|
|
270
|
+
let real;
|
|
271
|
+
try {
|
|
272
|
+
real = ops.realpath(target);
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
real = undefined;
|
|
276
|
+
}
|
|
277
|
+
if (real !== target || overlapsCatalogue(real, catalogueReal) !== undefined) {
|
|
278
|
+
let removed = false;
|
|
279
|
+
if (real !== undefined) {
|
|
280
|
+
try {
|
|
281
|
+
if (sameFolder(ops.lstat(real), folder)) {
|
|
282
|
+
ops.rmdir(real);
|
|
283
|
+
removed = true;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
removed = false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
throw new InternalError(`stackledger drift: the folder that holds the scaffold folder was replaced before it was created; ${removed ? 'the folder made was removed and nothing was written' : 'nothing was written, but the folder made could not be removed'}`);
|
|
291
|
+
}
|
|
292
|
+
const stillOurs = () => { try {
|
|
293
|
+
return sameFolder(ops.lstat(target), folder);
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
return false;
|
|
297
|
+
} };
|
|
298
|
+
// Only files whose exclusive create succeeded, with the device and inode their descriptor had: a
|
|
299
|
+
// file that was already there (EEXIST) or was put in place of one later is not this run's to remove.
|
|
300
|
+
const created = [];
|
|
301
|
+
try {
|
|
302
|
+
for (const file of files) {
|
|
303
|
+
if (!stillOurs())
|
|
304
|
+
throw new FolderReplaced();
|
|
305
|
+
const path = join(target, file.file);
|
|
306
|
+
const { fd, dev, ino } = ops.create(path);
|
|
307
|
+
created.push({ path, dev, ino });
|
|
308
|
+
ops.write(fd, file.text);
|
|
309
|
+
}
|
|
310
|
+
if (!stillOurs())
|
|
311
|
+
throw new FolderReplaced();
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
// Created files may be partly written; one already gone needs no removal. A replaced folder is
|
|
315
|
+
// not this run's: it is left alone, and so is whatever its path now leads to.
|
|
316
|
+
if (!stillOurs())
|
|
317
|
+
throw error instanceof FolderReplaced ? error : new FolderReplaced();
|
|
318
|
+
for (const file of created) {
|
|
319
|
+
try {
|
|
320
|
+
const status = ops.lstat(file.path);
|
|
321
|
+
if (status.isFile() && status.dev === file.dev && status.ino === file.ino)
|
|
322
|
+
ops.unlink(file.path);
|
|
323
|
+
}
|
|
324
|
+
catch { /* already gone: nothing to remove */ }
|
|
325
|
+
}
|
|
326
|
+
// `dir` is checked again right before the rmdir: the removals above take time, and a folder put at
|
|
327
|
+
// its path meanwhile is not this run's. rmdir removes only an empty folder: one still holding
|
|
328
|
+
// another process's file is kept.
|
|
329
|
+
let removed = stillOurs();
|
|
330
|
+
if (removed) {
|
|
331
|
+
try {
|
|
332
|
+
ops.rmdir(target);
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
removed = false;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
throw new InternalError(`stackledger drift: cannot write the scaffold files (${code(error)}); ${removed ? 'nothing was kept' : 'the scaffold folder could not be removed'}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const leftOutLine = (count) => `stackledger drift: ${count} scaffold ${count === 1 ? 'file was' : 'files were'} left out because ${count === 1 ? 'it has' : 'they have'} errors; run stackledger validate on the drafts for details`;
|
|
342
|
+
export async function driftCommand(args, host = createHost(), ops = NODE_SCAFFOLD_OPS) {
|
|
343
|
+
if (args.includes('--help')) {
|
|
344
|
+
await writeStdout(DRIFT_USAGE, 'stackledger drift: cannot write to stdout');
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const options = parseDriftArgs(args);
|
|
348
|
+
const folder = resolve(options.folder);
|
|
349
|
+
const loaded = await loadFolder(folder);
|
|
350
|
+
const snapshot = await readSnapshot(options.snapshot === undefined ? await defaultSnapshotPath(host) : resolve(options.snapshot));
|
|
351
|
+
const target = options.scaffold === undefined ? undefined : scaffoldTarget(options.scaffold, folder, ops);
|
|
352
|
+
const catalogue = validateLoaded(loaded);
|
|
353
|
+
// Arguments, the folder, the snapshot and the scaffold folder are checked above: a failure from
|
|
354
|
+
// here on is exit 1, never usage (apart from a scaffold folder created meanwhile).
|
|
355
|
+
let output;
|
|
356
|
+
let scaffold;
|
|
357
|
+
try {
|
|
358
|
+
const drift = buildDrift(snapshot, applyView(catalogue.served, 'full', resolveHidden(catalogue)).components);
|
|
359
|
+
output = options.format === 'json' ? formatDriftJson(drift) : formatDriftText(drift);
|
|
360
|
+
if (target !== undefined)
|
|
361
|
+
scaffold = checkScaffold(buildScaffold(drift, snapshot, componentNames(loaded)));
|
|
362
|
+
}
|
|
363
|
+
catch (error) {
|
|
364
|
+
throw internal('drift', error);
|
|
365
|
+
}
|
|
366
|
+
if (target !== undefined)
|
|
367
|
+
writeScaffold(target, scaffold.files, ops);
|
|
368
|
+
const notes = problemCounts('drift', catalogue);
|
|
369
|
+
if (scaffold?.leftOut)
|
|
370
|
+
notes.push(leftOutLine(scaffold.leftOut));
|
|
371
|
+
if (notes.length)
|
|
372
|
+
process.stderr.write(`${notes.join('\n')}\n`);
|
|
373
|
+
await writeStdout(output, 'stackledger drift: cannot write the result to stdout');
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=drift-command.js.map
|