launch-film 0.2.0 → 0.3.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/README.md +53 -28
- package/THIRD_PARTY.md +1 -1
- package/bin/launch-film.js +417 -91
- package/package.json +4 -4
- package/skills/launch-film/SKILL.md +23 -19
- package/src/audio.js +79 -7
- package/src/brand.js +123 -21
- package/src/check.js +57 -27
- package/src/chrome.js +471 -53
- package/src/confine.js +164 -0
- package/src/ffmpeg.js +334 -49
- package/src/film.js +488 -36
- package/src/fonts.js +36 -8
- package/src/frames.js +70 -27
- package/src/logo.js +24 -10
- package/src/motion.js +12 -4
- package/src/out.js +1362 -0
- package/src/plan.js +9 -3
- package/src/programs.js +422 -0
- package/src/scene.js +8 -5
- package/src/stills.js +166 -102
- package/src/store.js +3 -2
- package/src/words.js +53 -8
package/src/out.js
ADDED
|
@@ -0,0 +1,1362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ─── the out folder ────────────────────────────────────────────────────────
|
|
3
|
+
// A run makes its files in a part folder (.launch-film-part-<pid>-<hex>)
|
|
4
|
+
// and puts them in place only once it has ended well. The part folder is
|
|
5
|
+
// inside the folder the files go to when that folder is there, and next to
|
|
6
|
+
// it when the run makes it, so it is on the same disk and gets the same mode
|
|
7
|
+
// and default ACL (it is made with mkdirSync: mkdtemp's 0700 would shut out
|
|
8
|
+
// the others a default ACL lets in). To put the files in place, each earlier
|
|
9
|
+
// file or folder the run replaces is moved aside into the part folder, the
|
|
10
|
+
// new one is moved in, and the part folder, with the earlier files in it,
|
|
11
|
+
// is removed last: at no moment are both gone. A run that fails, is refused
|
|
12
|
+
// or is stopped removes its part folder and the folders it made for it, so
|
|
13
|
+
// the folder is left as it was and nothing is added to it.
|
|
14
|
+
//
|
|
15
|
+
// A run names its outputs as it writes them; anything else in its part
|
|
16
|
+
// folder is a work file and goes with it. A run started inside another's
|
|
17
|
+
// part folder (the render inside the one-go command) puts its files there,
|
|
18
|
+
// and they go in with the other run's when that one ends well.
|
|
19
|
+
//
|
|
20
|
+
// Two runs that put their files in one folder at once take turns (lock): the
|
|
21
|
+
// one that ends later replaces the other's files whole. A run that adds its
|
|
22
|
+
// files to a folder already there (frames/, stills/, check/) takes that
|
|
23
|
+
// folder's lock as well, so it takes turns with a run that puts its files in
|
|
24
|
+
// that folder alone. Only the wait for a turn gives way to a stop; the moves
|
|
25
|
+
// come after all of it, synchronously. A folder the files go into that is on
|
|
26
|
+
// another disk (one a link leads to) takes them through a part folder of its
|
|
27
|
+
// own, so every move is a rename on one disk. The earlier files a run moves
|
|
28
|
+
// aside go in the .old of a part folder that is in the folder they come
|
|
29
|
+
// from, by their paths from it.
|
|
30
|
+
//
|
|
31
|
+
// A stop (Ctrl-C, SIGTERM, SIGHUP) passes its signal on to the Chrome,
|
|
32
|
+
// ffmpeg and easybrandkit the runs started, kills those still running after
|
|
33
|
+
// STOP_WAIT, with the Chrome easybrandkit started (watch), removes every
|
|
34
|
+
// part folder and temp folder, and ends the process by the same signal. A
|
|
35
|
+
// failed run ends the process the same way, with SIGTERM to them and exit
|
|
36
|
+
// code 1 (exit). Either way the CLI's last line says which folder was left
|
|
37
|
+
// as it was (told). A run killed outright (SIGKILL) cannot remove its part
|
|
38
|
+
// folder: a later run that ends well in the same place removes it, once its
|
|
39
|
+
// process is gone and nothing in it has changed for STALE. One killed while
|
|
40
|
+
// it put its files in place can hold earlier files in its .old: that part
|
|
41
|
+
// folder is never removed, but kept under a name no run removes, with a
|
|
42
|
+
// note of what is in it (sweep). Only this user's own part folders and temp
|
|
43
|
+
// folders are looked at: in a folder others write to as well (/tmp), what
|
|
44
|
+
// another user left, or put there to look like a run's, is left alone.
|
|
45
|
+
|
|
46
|
+
const fs = require("fs");
|
|
47
|
+
const os = require("os");
|
|
48
|
+
const path = require("path");
|
|
49
|
+
const crypto = require("crypto");
|
|
50
|
+
|
|
51
|
+
const PART = /^\.launch-film-part-(\d+)-[0-9a-f]{8}$/;
|
|
52
|
+
const STALE = 15 * 60 * 1000;
|
|
53
|
+
const STOP_WAIT = 400;
|
|
54
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", ...(process.platform === "win32" ? ["SIGBREAK"] : [])];
|
|
55
|
+
// The commit lock, how long a run waits for another's commit to end (a
|
|
56
|
+
// commit takes a moment), and the age past which a lock was left by a run
|
|
57
|
+
// killed outright, whatever process now has its pid.
|
|
58
|
+
const LOCK = ".launch-film-commit";
|
|
59
|
+
const LOCK_WAIT = 10 * 1000;
|
|
60
|
+
const LOCK_STALE = 60 * 1000;
|
|
61
|
+
// The folder a run makes for the few moments it moves aside a lock left
|
|
62
|
+
// behind, how old it is once a run killed in those moments left it, and what
|
|
63
|
+
// such a run or one killed just after can leave next to the lock.
|
|
64
|
+
const TAKING = `${LOCK}-taking`;
|
|
65
|
+
const TAKING_STALE = 5 * 1000;
|
|
66
|
+
const LEFT = /^\.launch-film-commit-(?:taking|\d+-[0-9a-f]{8})$/;
|
|
67
|
+
|
|
68
|
+
// The runs begun and not ended, the temp folders (each with what to do
|
|
69
|
+
// before it is removed) and the processes to stop with them, and every part
|
|
70
|
+
// folder with the folder its files go to.
|
|
71
|
+
const open = new Set();
|
|
72
|
+
const temps = new Map();
|
|
73
|
+
const children = new Set();
|
|
74
|
+
const places = [];
|
|
75
|
+
// What became of the folders of the runs a stop or a failure ended, and
|
|
76
|
+
// what the runs that ended well found left by runs killed outright.
|
|
77
|
+
const told = [];
|
|
78
|
+
const found = [];
|
|
79
|
+
let stopping = null;
|
|
80
|
+
// A failed run is ending the process (exit).
|
|
81
|
+
let ending = false;
|
|
82
|
+
let hooked = false;
|
|
83
|
+
let listening = false;
|
|
84
|
+
|
|
85
|
+
// Undefined when nothing is there, a path through a file included.
|
|
86
|
+
const lstat = (at) => {
|
|
87
|
+
try {
|
|
88
|
+
return fs.lstatSync(at, { throwIfNoEntry: false });
|
|
89
|
+
} catch (err) {
|
|
90
|
+
if (err.code === "ENOTDIR") return undefined;
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const isDir = (at) => {
|
|
95
|
+
try {
|
|
96
|
+
return fs.statSync(at).isDirectory();
|
|
97
|
+
} catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
// Whether `at` is `folder` or in it. A name in it that begins with two dots,
|
|
102
|
+
// as ..notes.txt, is in it: only .. itself climbs out.
|
|
103
|
+
const inside = (at, folder) => {
|
|
104
|
+
const rel = path.relative(folder, at);
|
|
105
|
+
return rel === "" || (rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
106
|
+
};
|
|
107
|
+
const partName = () => `.launch-film-part-${process.pid}-${crypto.randomBytes(4).toString("hex")}`;
|
|
108
|
+
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
109
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
110
|
+
const uid = () => (typeof process.getuid === "function" ? process.getuid() : null);
|
|
111
|
+
|
|
112
|
+
// Whether `at` is a folder of this user's own, and not a link to one. A
|
|
113
|
+
// system without users (Windows) has every folder its user's.
|
|
114
|
+
function mine(at) {
|
|
115
|
+
const s = lstat(at);
|
|
116
|
+
return Boolean(s) && s.isDirectory() && (uid() === null || s.uid === uid());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A system error's reason without the call and the path Node adds to it:
|
|
120
|
+
// "no space left on device (ENOSPC)". Node's own message for a write that
|
|
121
|
+
// fails names no file, so what was written is said with it (save).
|
|
122
|
+
function reason(err) {
|
|
123
|
+
const text = String((err && err.message) || err);
|
|
124
|
+
const m = /^(E[A-Z0-9]+)[:,] (.+?)(?:, [a-z_]+(?: '.*)?| '.*)?$/s.exec(text);
|
|
125
|
+
return m ? `${m[2].charAt(0).toLowerCase()}${m[2].slice(1)} (${m[1]})` : text;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// What to do when the disk a file goes to is full, said after the reason:
|
|
129
|
+
// the disk of the folder it is in, or of the temp folder for one in a temp
|
|
130
|
+
// folder of the run's own (tempDir), as a render's clips are, which goes
|
|
131
|
+
// with the run. The folder is named as it is once the runs have ended well
|
|
132
|
+
// (shown), not as the new one: a run inside another's part folder, as the
|
|
133
|
+
// stills of an address are, has its folder in that part folder.
|
|
134
|
+
function full(file) {
|
|
135
|
+
const dir = path.dirname(path.resolve(file));
|
|
136
|
+
const temp = [...temps.keys()].some((folder) => inside(dir, folder));
|
|
137
|
+
return `The disk of ${shown(temp ? os.tmpdir() : dir)} is full: free some room and run again`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Writes a file, and fails with the file and the reason.
|
|
141
|
+
function save(file, data) {
|
|
142
|
+
try {
|
|
143
|
+
fs.writeFileSync(file, data);
|
|
144
|
+
} catch (err) {
|
|
145
|
+
throw new Error(`writing ${file}: ${reason(err)}${err.code === "ENOSPC" ? `. ${full(file)}` : ""}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The files and folders a plain copy of `from` holds, every link followed,
|
|
150
|
+
// at any depth: visit(source, rel, folder) for each, in the order of their
|
|
151
|
+
// names, a folder before what is in it. Left out are a link that leads nowhere or back to a folder the copy
|
|
152
|
+
// is inside of, sockets and the like, and what launch-film's own runs keep
|
|
153
|
+
// there.
|
|
154
|
+
function eachPlain(from, visit, rel = "", trail = [fs.realpathSync(from)]) {
|
|
155
|
+
for (const name of fs.readdirSync(from).sort()) {
|
|
156
|
+
if (name.startsWith(".launch-film-")) continue;
|
|
157
|
+
const source = path.join(from, name);
|
|
158
|
+
const at = rel ? path.join(rel, name) : name;
|
|
159
|
+
let s;
|
|
160
|
+
try {
|
|
161
|
+
s = fs.statSync(source);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
if (err.code === "ENOENT" || err.code === "ELOOP") continue;
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
if (s.isFile()) visit(source, at, false);
|
|
167
|
+
else if (s.isDirectory()) {
|
|
168
|
+
const real = fs.realpathSync(source);
|
|
169
|
+
if (trail.includes(real)) continue;
|
|
170
|
+
visit(source, at, true);
|
|
171
|
+
eachPlain(source, visit, at, [...trail, real]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Copies a folder as plain files and folders, every link in it followed and
|
|
177
|
+
// what it leads to copied, at any depth (fs.cpSync's dereference follows only
|
|
178
|
+
// a link at the top), so what works on the copy never reaches through a link.
|
|
179
|
+
// What eachPlain leaves out is left out. The folders take the mode and the
|
|
180
|
+
// default ACL of where they are made, as the part folder does. A folder
|
|
181
|
+
// cannot be copied into itself. With `held`, as LAUNCH_FILM_CONFINE asks of
|
|
182
|
+
// a kit (bin/launch-film.js), a file or a folder in it that a link leads to
|
|
183
|
+
// outside it is refused before anything is copied, as no file of the
|
|
184
|
+
// folder's: the error's `held` is its path from the folder. Links that stay
|
|
185
|
+
// in the folder are followed as ever.
|
|
186
|
+
function copyPlain(from, to, { held = false } = {}) {
|
|
187
|
+
const real = fs.realpathSync(from);
|
|
188
|
+
if (inside(path.join(fs.realpathSync(path.dirname(to)), path.basename(to)), real)) throw new Error(`${to} is inside ${from}`);
|
|
189
|
+
const out = (source, rel) => {
|
|
190
|
+
if (held && !inside(fs.realpathSync(source), real)) throw Object.assign(new Error(`${path.join(from, rel)} leads out of ${from} through a link`), { held: rel });
|
|
191
|
+
};
|
|
192
|
+
if (held) eachPlain(from, out);
|
|
193
|
+
fs.mkdirSync(to, { recursive: true });
|
|
194
|
+
eachPlain(from, (source, rel, folder) => {
|
|
195
|
+
// Looked at again as it is copied, in case a link changed since.
|
|
196
|
+
out(source, rel);
|
|
197
|
+
if (folder) fs.mkdirSync(path.join(to, rel), { recursive: true });
|
|
198
|
+
else fs.copyFileSync(source, path.join(to, rel));
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// A name on one line: a line break or another control character in it,
|
|
203
|
+
// and a backslash, are written escaped.
|
|
204
|
+
const ESCAPES = { "\\": "\\\\", "\n": "\\n", "\r": "\\r", "\t": "\\t" };
|
|
205
|
+
const plain = (name) => String(name).replace(/[\\\p{Cc}\u2028\u2029]/gu, (c) => ESCAPES[c] || `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
206
|
+
|
|
207
|
+
// Whether it is gone: a folder another program is still writing to, or
|
|
208
|
+
// holds open (on Windows), may stay, and why(err) is told the reason.
|
|
209
|
+
function remove(at, why = () => {}) {
|
|
210
|
+
try {
|
|
211
|
+
fs.rmSync(at, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
212
|
+
return true;
|
|
213
|
+
} catch (err) {
|
|
214
|
+
why(err);
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function stoppedError() {
|
|
220
|
+
return Object.assign(new Error("stopped"), { stopped: true });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// A path or a message with the part folders in it, as the paths will be
|
|
224
|
+
// once the runs have ended well: the innermost run's first.
|
|
225
|
+
function shown(text) {
|
|
226
|
+
let s = String(text);
|
|
227
|
+
for (let i = places.length - 1; i >= 0; i--) s = s.split(places[i][0]).join(places[i][1]);
|
|
228
|
+
return s;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// A failure's message, as shown(), with a file in a part folder named as
|
|
232
|
+
// the new one: the run made it, and the earlier file of that name, which
|
|
233
|
+
// it does not touch, is as it was. A path in quotes, as Node's own errors
|
|
234
|
+
// give it, is named so too, before its quote.
|
|
235
|
+
function said(text) {
|
|
236
|
+
const mark = "\u0001";
|
|
237
|
+
let s = String(text);
|
|
238
|
+
for (let i = places.length - 1; i >= 0; i--) {
|
|
239
|
+
const [part, target] = places[i];
|
|
240
|
+
s = s.split(part + path.sep).join(`${mark}${target}${path.sep}`).split(part).join(target);
|
|
241
|
+
}
|
|
242
|
+
return s.replace(/\u0001+/g, mark).replace(/(^|[\s(])(['"]?)\u0001/g, "$1the new $2").split(mark).join("");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// However the process ends, no part folder of a run that has not ended is
|
|
246
|
+
// left behind, no temp folder, and no process the runs started.
|
|
247
|
+
function hook() {
|
|
248
|
+
if (hooked) return;
|
|
249
|
+
hooked = true;
|
|
250
|
+
process.on("exit", endAll);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Begins a run that makes files for the folder `dir`. run.out(name) is
|
|
254
|
+
// where it writes the output `name` (a file, or a folder it adds its files
|
|
255
|
+
// to), run.folder(name) where it writes a folder that replaces the one of
|
|
256
|
+
// that name; run.part is its part folder, for work files.
|
|
257
|
+
function begin(dir) {
|
|
258
|
+
if (stopping) throw stoppedError();
|
|
259
|
+
const target = path.resolve(dir);
|
|
260
|
+
const there = lstat(target);
|
|
261
|
+
if (there && !isDir(target)) throw new Error(`${shown(target)} is a file, not a folder`);
|
|
262
|
+
const home = there ? target : path.dirname(target);
|
|
263
|
+
const made = [];
|
|
264
|
+
// Up to the root at most: on Windows a drive that is not there has none.
|
|
265
|
+
for (let at = home; !fs.existsSync(at) && path.dirname(at) !== at; at = path.dirname(at)) made.push(at);
|
|
266
|
+
// The first folder there, which the new ones go in: a file in its place
|
|
267
|
+
// is told of as for the folder itself.
|
|
268
|
+
const base = made.length ? path.dirname(made[made.length - 1]) : home;
|
|
269
|
+
if (lstat(base) && !isDir(base)) throw new Error(`${shown(base)} is a file, not a folder`);
|
|
270
|
+
const part = path.join(home, partName());
|
|
271
|
+
try {
|
|
272
|
+
fs.mkdirSync(home, { recursive: true });
|
|
273
|
+
fs.mkdirSync(part);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
// The folders it made go, and the refusal names the folder it could
|
|
276
|
+
// not write in, not the part folder, which is the run's own business.
|
|
277
|
+
for (const folder of made) {
|
|
278
|
+
try {
|
|
279
|
+
fs.rmdirSync(folder);
|
|
280
|
+
} catch {
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
throw new Error(`cannot write in ${shown(base)}: ${reason(err)}`);
|
|
285
|
+
}
|
|
286
|
+
places.push([part, target]);
|
|
287
|
+
const run = {
|
|
288
|
+
dir: target,
|
|
289
|
+
part,
|
|
290
|
+
existed: Boolean(there),
|
|
291
|
+
made,
|
|
292
|
+
outputs: new Map(),
|
|
293
|
+
// Part folders made in folders on another disk, as the run commits.
|
|
294
|
+
stages: [],
|
|
295
|
+
ended: false,
|
|
296
|
+
// Its failure has said what became of its folder.
|
|
297
|
+
said: false,
|
|
298
|
+
out: (name) => output(run, name, false),
|
|
299
|
+
folder: (name) => output(run, name, true),
|
|
300
|
+
commit: () => commit(run),
|
|
301
|
+
discard: () => discard(run),
|
|
302
|
+
};
|
|
303
|
+
open.add(run);
|
|
304
|
+
hook();
|
|
305
|
+
return run;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function output(run, name, whole) {
|
|
309
|
+
if (!name || name !== path.basename(name) || name.startsWith(".launch-film-")) throw new Error(`"${name}" cannot be an output's name`);
|
|
310
|
+
run.outputs.set(name, whole || run.outputs.get(name) || false);
|
|
311
|
+
return path.join(run.part, name);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Runs fn(run) and puts its outputs in place when it resolves; removes them
|
|
315
|
+
// when it throws.
|
|
316
|
+
async function within(dir, fn) {
|
|
317
|
+
const run = begin(dir);
|
|
318
|
+
try {
|
|
319
|
+
const result = await fn(run);
|
|
320
|
+
await run.commit();
|
|
321
|
+
return result;
|
|
322
|
+
} catch (err) {
|
|
323
|
+
run.discard();
|
|
324
|
+
throw err;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function ended(run) {
|
|
329
|
+
run.ended = true;
|
|
330
|
+
open.delete(run);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Renames; on Windows a file another program holds open (a virus scanner
|
|
334
|
+
// looking at a new file) may be let go of in a moment, so a refused rename
|
|
335
|
+
// is tried again a few times, unless `done()` says there is no point.
|
|
336
|
+
function rename(from, to, done = () => false) {
|
|
337
|
+
for (let tries = 0; ; tries++) {
|
|
338
|
+
try {
|
|
339
|
+
fs.renameSync(from, to);
|
|
340
|
+
return;
|
|
341
|
+
} catch (err) {
|
|
342
|
+
if (process.platform === "win32" && ["EPERM", "EBUSY", "EACCES"].includes(err.code) && tries < 5 && !done()) {
|
|
343
|
+
pause(100);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
throw err;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Moves one entry, and notes the move so it can be undone.
|
|
352
|
+
function move(from, to, moved, rel) {
|
|
353
|
+
try {
|
|
354
|
+
rename(from, to);
|
|
355
|
+
} catch (err) {
|
|
356
|
+
throw Object.assign(err, { at: rel });
|
|
357
|
+
}
|
|
358
|
+
moved.push([from, to]);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Puts one output in place: a file, or a folder named whole, replaces the
|
|
362
|
+
// one there (moved aside into the `old` folder of ctx, by its path from
|
|
363
|
+
// ctx.home); another folder takes its files in one by one, so the files
|
|
364
|
+
// there that this run does not make stay. A folder on another disk than the
|
|
365
|
+
// new files gets them through a part folder of its own in it, the earlier
|
|
366
|
+
// files aside there too.
|
|
367
|
+
function place(ctx, from, to, rel, whole) {
|
|
368
|
+
const isFolder = fs.lstatSync(from).isDirectory();
|
|
369
|
+
const there = lstat(to);
|
|
370
|
+
if (there && isFolder && !isDir(to)) throw Object.assign(new Error("a file is where this run puts a folder"), { at: rel });
|
|
371
|
+
if (there && !isFolder && there.isDirectory()) throw Object.assign(new Error("a folder is where this run puts a file"), { at: rel });
|
|
372
|
+
if (there && isFolder && !whole) {
|
|
373
|
+
let into = ctx;
|
|
374
|
+
let source = from;
|
|
375
|
+
if (fs.statSync(to).dev !== fs.statSync(from).dev) {
|
|
376
|
+
source = path.join(to, partName());
|
|
377
|
+
fs.mkdirSync(source);
|
|
378
|
+
ctx.run.stages.push(source);
|
|
379
|
+
for (const name of fs.readdirSync(from)) if (!name.startsWith(".")) fs.cpSync(path.join(from, name), path.join(source, name), { recursive: true });
|
|
380
|
+
into = { ...ctx, old: path.join(source, ".old"), home: to };
|
|
381
|
+
}
|
|
382
|
+
for (const name of fs.readdirSync(source)) if (!name.startsWith(".")) place(into, path.join(source, name), path.join(to, name), path.join(rel, name), false);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (there) {
|
|
386
|
+
const aside = path.join(ctx.old, path.relative(ctx.home, to));
|
|
387
|
+
fs.mkdirSync(path.dirname(aside), { recursive: true });
|
|
388
|
+
move(to, aside, ctx.moved, rel);
|
|
389
|
+
}
|
|
390
|
+
move(from, to, ctx.moved, rel);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// A process's fields in /proc/<pid>/stat after its name (Linux), else null:
|
|
394
|
+
// [1] is its parent's pid, [19] when it started in clock ticks since boot.
|
|
395
|
+
function statOf(pid) {
|
|
396
|
+
try {
|
|
397
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
398
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
399
|
+
} catch {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// When a process started, in ms, where /proc tells (Linux), else null.
|
|
405
|
+
function startOf(pid) {
|
|
406
|
+
try {
|
|
407
|
+
const ticks = Number((statOf(pid) || [])[19]);
|
|
408
|
+
const boot = Number(/^btime (\d+)$/m.exec(fs.readFileSync("/proc/stat", "utf8"))[1]);
|
|
409
|
+
// The clock ticks a hundred times a second on Linux.
|
|
410
|
+
return Number.isFinite(ticks) && boot ? (boot + ticks / 100) * 1000 : null;
|
|
411
|
+
} catch {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Whether a lock made at `made` and holding `pid` (0 when it never got one)
|
|
417
|
+
// was left by a process killed outright: its process is gone, or the pid is
|
|
418
|
+
// this process's own or that of a process started after the lock was made
|
|
419
|
+
// (a pid used again, as in a container where every run has the same one),
|
|
420
|
+
// or the lock is older than a commit takes.
|
|
421
|
+
function leftBehind(pid, made) {
|
|
422
|
+
if (Date.now() - made > LOCK_STALE) return true;
|
|
423
|
+
if (!pid) return Date.now() - made > 2000;
|
|
424
|
+
if (pid === process.pid || !running(pid)) return true;
|
|
425
|
+
const started = startOf(pid);
|
|
426
|
+
return started !== null && started > made + 2000;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// What is in the lock at `at`: the pid it holds (0 when it has none yet) and
|
|
430
|
+
// its stat, null once it is gone.
|
|
431
|
+
function readLock(at) {
|
|
432
|
+
let pid = 0;
|
|
433
|
+
try {
|
|
434
|
+
pid = Number(fs.readFileSync(path.join(at, "pid"), "utf8")) || 0;
|
|
435
|
+
} catch {
|
|
436
|
+
// Not written yet, or let go meanwhile.
|
|
437
|
+
}
|
|
438
|
+
return { pid, s: lstat(at) };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Moves aside a lock a run killed outright left. One run at a time does:
|
|
442
|
+
// another may have done it first and yet another have taken the lock since,
|
|
443
|
+
// and a run that moved "the" lock on what it read before would move that
|
|
444
|
+
// live lock and commit alongside it. So it makes the folder TAKING, reads the
|
|
445
|
+
// lock again, the pid and the time from the one lock, and moves it only if it
|
|
446
|
+
// is still one left behind. A folder TAKING that a run killed in those few
|
|
447
|
+
// moments left is removed by its age. Returns true when the lock is gone,
|
|
448
|
+
// null when it is still there (for this run to wait for), or, for a lock left
|
|
449
|
+
// behind that cannot be moved aside, why not: another user's in a folder with
|
|
450
|
+
// the sticky bit, or one a program holds open on Windows.
|
|
451
|
+
function takeOver(at) {
|
|
452
|
+
const taking = path.join(path.dirname(at), TAKING);
|
|
453
|
+
try {
|
|
454
|
+
fs.mkdirSync(taking);
|
|
455
|
+
} catch (err) {
|
|
456
|
+
if (err.code !== "EEXIST") return err.code || err.message;
|
|
457
|
+
const s = lstat(taking);
|
|
458
|
+
if (s && Date.now() - s.mtimeMs > TAKING_STALE) {
|
|
459
|
+
try {
|
|
460
|
+
fs.rmdirSync(taking);
|
|
461
|
+
} catch {
|
|
462
|
+
// Another run's by now, or not this user's to remove.
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
const aside = `${at}-${process.pid}-${crypto.randomBytes(4).toString("hex")}`;
|
|
468
|
+
let moved = false;
|
|
469
|
+
try {
|
|
470
|
+
const { pid, s } = readLock(at);
|
|
471
|
+
if (!s) return true;
|
|
472
|
+
if (!leftBehind(pid, s.mtimeMs)) return null;
|
|
473
|
+
fs.renameSync(at, aside);
|
|
474
|
+
moved = true;
|
|
475
|
+
} catch (err) {
|
|
476
|
+
// Gone meanwhile: the run that held it let go.
|
|
477
|
+
return err.code === "ENOENT" ? true : err.code || err.message;
|
|
478
|
+
} finally {
|
|
479
|
+
try {
|
|
480
|
+
fs.rmdirSync(taking);
|
|
481
|
+
} catch {
|
|
482
|
+
// Taken for one left behind: the run that did goes on regardless.
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (moved) remove(aside);
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Takes the commit lock of `dir`: a folder named LOCK in it, holding the pid
|
|
490
|
+
// of the process that has it. A lock left by a process killed outright is
|
|
491
|
+
// taken over (leftBehind, takeOver). Waits for its turn, and gives up when
|
|
492
|
+
// the run is stopped. Errors name the lock by its path from `base`, the
|
|
493
|
+
// run's folder. Returns what lets it go.
|
|
494
|
+
async function lock(dir, base = dir) {
|
|
495
|
+
const at = path.join(dir, LOCK);
|
|
496
|
+
const since = Date.now();
|
|
497
|
+
for (;;) {
|
|
498
|
+
if (stopping) throw stoppedError();
|
|
499
|
+
let made = false;
|
|
500
|
+
try {
|
|
501
|
+
fs.mkdirSync(at);
|
|
502
|
+
made = true;
|
|
503
|
+
fs.writeFileSync(path.join(at, "pid"), String(process.pid));
|
|
504
|
+
return () => remove(at);
|
|
505
|
+
} catch (err) {
|
|
506
|
+
// Taken over before it got its pid, its run held up for seconds: it
|
|
507
|
+
// is lost, and the next try is as good as the first.
|
|
508
|
+
if (!(made && err.code === "ENOENT")) {
|
|
509
|
+
// One that could not get its pid, as on a full disk, is not left.
|
|
510
|
+
if (made) remove(at);
|
|
511
|
+
if (made || err.code !== "EEXIST") throw Object.assign(err, { at: path.relative(base, at) });
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
const { pid, s } = readLock(at);
|
|
515
|
+
// Why a lock left behind could not be moved aside. Such a lock is waited
|
|
516
|
+
// for as one another run holds, and then refused.
|
|
517
|
+
let stuck = null;
|
|
518
|
+
if (s && leftBehind(pid, s.mtimeMs)) {
|
|
519
|
+
const taken = takeOver(at);
|
|
520
|
+
if (taken === true) continue;
|
|
521
|
+
stuck = taken;
|
|
522
|
+
}
|
|
523
|
+
if (Date.now() - since > LOCK_WAIT) {
|
|
524
|
+
const whose = s && uid() !== null && s.uid !== uid() ? `, which belongs to another user (uid ${s.uid}),` : "";
|
|
525
|
+
throw Object.assign(new Error(stuck ? `${shown(at)}${whose} was left by a launch-film run that is gone and cannot be moved aside (${stuck}). Remove it and run again`
|
|
526
|
+
: `another launch-film run has been putting its files in ${shown(dir)} for ${LOCK_WAIT / 1000} s. If no other launch-film run is at work there, remove ${shown(at)}${whose} and run again`), { at: null });
|
|
527
|
+
}
|
|
528
|
+
await sleep(20);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Whether this user may not move what other users own in the folder `dir`:
|
|
533
|
+
// it has the sticky bit, as /tmp has, and is not this user's, so only the
|
|
534
|
+
// owner of a file in it may move it, or root.
|
|
535
|
+
function sticky(dir) {
|
|
536
|
+
const me = uid();
|
|
537
|
+
if (me === null || me === 0) return false;
|
|
538
|
+
try {
|
|
539
|
+
const s = fs.statSync(dir);
|
|
540
|
+
return Boolean(s.mode & 0o1000) && s.uid !== me;
|
|
541
|
+
} catch {
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Why the earlier file `to`, whose lstat is `there`, is not replaced: it is
|
|
547
|
+
// this user's own and this user may not write it, as a file the user made
|
|
548
|
+
// read-only, which a rename would replace without a word where writing it
|
|
549
|
+
// would fail. `would` says what became of it. A file of another user's that
|
|
550
|
+
// this one may not write is replaced as the folder allows, when the folder
|
|
551
|
+
// lets this user write in it: makersmcp's worker copies an earlier job's
|
|
552
|
+
// files into a new job's folder as its own, which the job's user may read
|
|
553
|
+
// and not write, and a render there failed once its clips had rendered. In
|
|
554
|
+
// a folder with the sticky bit no rename of this user's moves it (sticky),
|
|
555
|
+
// and the commit failed at the move, once all the work was done. null when
|
|
556
|
+
// it is replaced.
|
|
557
|
+
function heldBack(to, there, would = "so it was not replaced") {
|
|
558
|
+
if (!there.isFile()) return null;
|
|
559
|
+
if (uid() !== null && there.uid !== uid()) {
|
|
560
|
+
if (!sticky(path.dirname(to))) return null;
|
|
561
|
+
return Object.assign(new Error(`${shown(to)} is another user's file in a folder with the sticky bit, ${would}: have that user remove it, or write the new files in another folder, and run again`), { at: null });
|
|
562
|
+
}
|
|
563
|
+
try {
|
|
564
|
+
fs.accessSync(to, fs.constants.W_OK);
|
|
565
|
+
return null;
|
|
566
|
+
} catch (err) {
|
|
567
|
+
return Object.assign(new Error(`${shown(to)} may not be written (${err.code}), ${would}: make it writable or remove it, and run again`), { at: null });
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// What the outputs find where they go, read before anything moves (found):
|
|
572
|
+
// the folders already there that take their files one by one, which a run
|
|
573
|
+
// takes turns for, and the first earlier file the run does not replace
|
|
574
|
+
// (heldBack).
|
|
575
|
+
function survey(from, to, whole, found) {
|
|
576
|
+
const there = lstat(to);
|
|
577
|
+
if (!there) return found;
|
|
578
|
+
const isFolder = fs.lstatSync(from).isDirectory();
|
|
579
|
+
if (isFolder && !whole && isDir(to)) {
|
|
580
|
+
found.folders.push(to);
|
|
581
|
+
for (const name of fs.readdirSync(from)) if (!name.startsWith(".")) survey(path.join(from, name), path.join(to, name), false, found);
|
|
582
|
+
} else if (!isFolder && !found.readOnly) found.readOnly = heldBack(to, there);
|
|
583
|
+
return found;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// The refusal the commit would give for an earlier file of `names` in the
|
|
587
|
+
// folder `dir` (heldBack), or for one of them that is a folder this user may
|
|
588
|
+
// not write in, as the run puts its files in it or moves it aside whole; or
|
|
589
|
+
// null. Asked before a run's long work, as a render's clips or a plan's read
|
|
590
|
+
// of the site, so that it comes first. What cannot be looked at, in a folder
|
|
591
|
+
// this user may not search or through a loop of links, is left to the run,
|
|
592
|
+
// which says why as it begins (begin).
|
|
593
|
+
function heldBackIn(dir, names) {
|
|
594
|
+
for (const name of names) {
|
|
595
|
+
const to = path.join(path.resolve(dir), name);
|
|
596
|
+
let there;
|
|
597
|
+
try {
|
|
598
|
+
there = lstat(to);
|
|
599
|
+
} catch {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
if (!there) continue;
|
|
603
|
+
if (there.isDirectory()) {
|
|
604
|
+
try {
|
|
605
|
+
fs.accessSync(to, fs.constants.W_OK);
|
|
606
|
+
} catch (err) {
|
|
607
|
+
return `${shown(to)} may not be written in (${err.code}), so the run would not change it: make it writable, and run again`;
|
|
608
|
+
}
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
const held = heldBack(to, there, "so the run would not replace it");
|
|
612
|
+
if (held) return held.message;
|
|
613
|
+
}
|
|
614
|
+
return null;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Puts the run's outputs in place. Only the wait for its turn gives way: the
|
|
618
|
+
// moves are synchronous, so no stop comes in the middle of them. A move that
|
|
619
|
+
// fails puts back what was moved.
|
|
620
|
+
async function commit(run) {
|
|
621
|
+
if (run.ended) return;
|
|
622
|
+
if (stopping) throw stoppedError();
|
|
623
|
+
const { part, dir } = run;
|
|
624
|
+
const names = [...run.outputs.keys()].filter((name) => lstat(path.join(part, name)));
|
|
625
|
+
if (!lstat(dir)) {
|
|
626
|
+
// The run makes the folder: its part folder becomes it, less the work
|
|
627
|
+
// files.
|
|
628
|
+
for (const name of fs.readdirSync(part)) if (!run.outputs.has(name)) remove(path.join(part, name));
|
|
629
|
+
try {
|
|
630
|
+
rename(part, dir, () => Boolean(lstat(dir)));
|
|
631
|
+
return finish(run, names);
|
|
632
|
+
} catch (err) {
|
|
633
|
+
// Made meanwhile by another run: the files go in one by one, below.
|
|
634
|
+
if (!lstat(dir)) throw failed(run, err, [], []);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
// Another run putting its files here waits for this one, and this one for
|
|
638
|
+
// it, and a run that puts its files in a folder here alone (frames, stills
|
|
639
|
+
// or check on their own) takes turns with this one: what each finds there
|
|
640
|
+
// is read once it has its turn, and read again until no new folder has
|
|
641
|
+
// come up meanwhile. All the locks are held before the first move.
|
|
642
|
+
const letGo = [];
|
|
643
|
+
const free = () => {
|
|
644
|
+
for (const go of letGo.reverse()) go();
|
|
645
|
+
letGo.length = 0;
|
|
646
|
+
};
|
|
647
|
+
let found;
|
|
648
|
+
try {
|
|
649
|
+
letGo.push(await lock(dir));
|
|
650
|
+
const taken = new Set();
|
|
651
|
+
for (;;) {
|
|
652
|
+
found = { folders: [], readOnly: null };
|
|
653
|
+
for (const name of names) survey(path.join(part, name), path.join(dir, name), run.outputs.get(name), found);
|
|
654
|
+
const more = found.folders.filter((folder) => !taken.has(folder));
|
|
655
|
+
if (!more.length) break;
|
|
656
|
+
for (const folder of more) {
|
|
657
|
+
letGo.push(await lock(folder, dir));
|
|
658
|
+
taken.add(folder);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
} catch (err) {
|
|
662
|
+
free();
|
|
663
|
+
if (err.stopped) throw err;
|
|
664
|
+
throw failed(run, err, [], []);
|
|
665
|
+
}
|
|
666
|
+
try {
|
|
667
|
+
if (found.readOnly) throw failed(run, found.readOnly, [], []);
|
|
668
|
+
const ctx = { run, old: null, home: dir, moved: [] };
|
|
669
|
+
try {
|
|
670
|
+
// The earlier files go aside in a part folder in this folder: the
|
|
671
|
+
// run's own, or, when the run's is next to it (the folder was made
|
|
672
|
+
// meanwhile), one made here for them.
|
|
673
|
+
let aside = part;
|
|
674
|
+
if (path.dirname(part) !== dir) {
|
|
675
|
+
aside = path.join(dir, partName());
|
|
676
|
+
fs.mkdirSync(aside);
|
|
677
|
+
run.stages.push(aside);
|
|
678
|
+
}
|
|
679
|
+
ctx.old = path.join(aside, ".old");
|
|
680
|
+
fs.mkdirSync(ctx.old, { recursive: true });
|
|
681
|
+
for (const name of names) place(ctx, path.join(part, name), path.join(dir, name), name, run.outputs.get(name));
|
|
682
|
+
} catch (err) {
|
|
683
|
+
// Undone in reverse: each new file back to where it came from, each
|
|
684
|
+
// earlier one back from where it was put aside.
|
|
685
|
+
const asides = [part, ...run.stages];
|
|
686
|
+
const stuck = [];
|
|
687
|
+
const left = [];
|
|
688
|
+
for (const [from, to] of ctx.moved.reverse()) {
|
|
689
|
+
try {
|
|
690
|
+
fs.renameSync(to, from);
|
|
691
|
+
} catch {
|
|
692
|
+
if (asides.some((a) => inside(to, a))) stuck.push(to);
|
|
693
|
+
else left.push(shown(to));
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
throw failed(run, err, stuck, left);
|
|
697
|
+
}
|
|
698
|
+
} finally {
|
|
699
|
+
free();
|
|
700
|
+
}
|
|
701
|
+
finish(run, names);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// The folder a lock or a move refused with EACCES needed to write in: the
|
|
705
|
+
// folder the lock is in, a folder moved aside whole, or the folder a file is
|
|
706
|
+
// in, by `rel`, its path from the run's folder `dir`.
|
|
707
|
+
function writtenIn(dir, rel) {
|
|
708
|
+
const at = path.join(dir, rel);
|
|
709
|
+
if (path.basename(rel) === LOCK) return path.dirname(at);
|
|
710
|
+
const s = lstat(at);
|
|
711
|
+
return s && s.isDirectory() ? at : path.dirname(at);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Why a commit failed, said for the person who runs it again. On Windows a
|
|
715
|
+
// file another program holds open, as a virus scanner may, is not moved
|
|
716
|
+
// until that program lets go of it. Elsewhere EACCES is a folder this user
|
|
717
|
+
// may not write in, named as the user's own (writtenIn), and any other
|
|
718
|
+
// reason is the system's.
|
|
719
|
+
function failed(run, err, stuck, left) {
|
|
720
|
+
run.said = true;
|
|
721
|
+
const at = shown(path.join(run.dir, err.at || ""));
|
|
722
|
+
const held = process.platform === "win32" && ["EBUSY", "EPERM", "EACCES"].includes(err.code);
|
|
723
|
+
// A message that names its own paths (a lock another run holds) is said
|
|
724
|
+
// as it is.
|
|
725
|
+
const why = err.at === null ? err.message : held ? `${at} is held open by another program, or may not be changed (${err.code})`
|
|
726
|
+
: err.code === "EACCES" ? `${shown(writtenIn(run.dir, err.at || ""))} may not be written in (EACCES): make it writable, and run again` : `${at}: ${reason(err)}`;
|
|
727
|
+
const start = `could not put the new files in ${shown(run.dir)}: ${why}.`;
|
|
728
|
+
if (!stuck.length && !left.length) {
|
|
729
|
+
// Told of again by a stop that comes before this failure is: a commit
|
|
730
|
+
// waits for a lock for a while.
|
|
731
|
+
if (![...open].some((r) => r !== run && inside(run.dir, r.part))) told.push(`${shown(run.dir)} is as it was`);
|
|
732
|
+
return new Error(`${start} ${shown(run.dir)} is as it was${held ? "; close the file and run again" : ""}.`);
|
|
733
|
+
}
|
|
734
|
+
return new Error(`${start}${stuck.length ? ` Some of the earlier files could not be put back; they are in ${keep(run, stuck)}.` : ""}` +
|
|
735
|
+
`${left.length ? ` These new files could not be taken out again: ${left.join(", ")}.` : ""}`);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// A part folder holding earlier files that could not be put back is kept,
|
|
739
|
+
// under a name no run removes, with a note of what is in it: the run's own,
|
|
740
|
+
// or one it made in the folder or in a folder on another disk.
|
|
741
|
+
function keep(run, stuck) {
|
|
742
|
+
ended(run);
|
|
743
|
+
const kept = [];
|
|
744
|
+
for (const folder of [run.part, ...run.stages]) {
|
|
745
|
+
const mine = stuck.filter((f) => inside(f, path.join(folder, ".old"))).map((f) => path.relative(path.join(folder, ".old"), f));
|
|
746
|
+
if (mine.length) kept.push(keepFolder(folder, mine, `launch-film could not put these earlier files back in ${shown(path.dirname(folder))}`));
|
|
747
|
+
}
|
|
748
|
+
// The rest of what the run made goes.
|
|
749
|
+
for (const folder of [run.part, ...run.stages]) if (lstat(folder) && !kept.includes(folder)) remove(folder);
|
|
750
|
+
// A folder kept under its own name stays where it is: said by that path.
|
|
751
|
+
const at = places.findIndex(([part]) => part === run.part);
|
|
752
|
+
if (at >= 0) places.splice(at, 1);
|
|
753
|
+
return kept.join(" and ");
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// Keeps a part folder whose .old holds earlier files `files` (their paths
|
|
757
|
+
// from the folder it is in), under a name no run removes, with a note that
|
|
758
|
+
// starts with `what`. Returns where the files are: a folder that cannot be
|
|
759
|
+
// renamed keeps its own name, and sweep never removes it either. The note
|
|
760
|
+
// is made as a new file, so a link or a file of its name already there is
|
|
761
|
+
// never written through; the folder is kept without it then. Each file is
|
|
762
|
+
// listed on a line of its own, whatever its name holds.
|
|
763
|
+
function keepFolder(folder, files, what) {
|
|
764
|
+
const home = shown(path.dirname(folder));
|
|
765
|
+
try {
|
|
766
|
+
fs.writeFileSync(path.join(folder, "launch-film-kept.txt"), `${what}:\n${files.map((f) => ` ${plain(f)}`).join("\n")}\n` +
|
|
767
|
+
`They are in .old here, each by its path from ${home}. Put back the ones you want, then remove this folder: launch-film leaves it alone.\n`, { flag: "wx" });
|
|
768
|
+
} catch {
|
|
769
|
+
// Kept all the same.
|
|
770
|
+
}
|
|
771
|
+
const name = path.join(path.dirname(folder), path.basename(folder).replace(/^\.launch-film-part-/, "launch-film-kept-"));
|
|
772
|
+
try {
|
|
773
|
+
fs.renameSync(folder, name);
|
|
774
|
+
return name;
|
|
775
|
+
} catch {
|
|
776
|
+
return folder;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// Makes a folder and the folders in it writable by their owner, none through
|
|
781
|
+
// a link. What is not this user's to change stays as it is.
|
|
782
|
+
function makeWritable(folder) {
|
|
783
|
+
try {
|
|
784
|
+
const s = lstat(folder);
|
|
785
|
+
if (!s || !s.isDirectory()) return;
|
|
786
|
+
fs.chmodSync(folder, s.mode | 0o700);
|
|
787
|
+
for (const name of fs.readdirSync(folder)) makeWritable(path.join(folder, name));
|
|
788
|
+
} catch {
|
|
789
|
+
// The removal says why.
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// Removes a part folder or a folder of earlier files once its run has ended
|
|
794
|
+
// well. A folder the user made read-only inside one replaced whole (brand/)
|
|
795
|
+
// stops the removal, so the folders among the earlier files are made writable
|
|
796
|
+
// and it is tried again. One that still stays is kept like earlier files that
|
|
797
|
+
// could not be put back, and said, so no later run takes it for what a killed
|
|
798
|
+
// run left.
|
|
799
|
+
function tidy(folder) {
|
|
800
|
+
if (remove(folder)) return;
|
|
801
|
+
makeWritable(path.join(folder, ".old"));
|
|
802
|
+
let why = "";
|
|
803
|
+
if (remove(folder, (err) => (why = reason(err)))) return;
|
|
804
|
+
const home = shown(path.dirname(folder));
|
|
805
|
+
const files = earlier(folder);
|
|
806
|
+
if (!files.length) {
|
|
807
|
+
found.push(`${folder}, a folder of this run's in ${home}, could not be removed: ${why}`);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const kept = keepFolder(folder, files, `launch-film replaced these earlier files in ${home} and could not remove them (${why})`);
|
|
811
|
+
found.push(`the earlier files this run replaced in ${home} could not be removed (${why}), so they are in ${kept}; launch-film-kept.txt there lists them`);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function finish(run, names) {
|
|
815
|
+
ended(run);
|
|
816
|
+
for (const folder of [run.part, ...run.stages]) tidy(folder);
|
|
817
|
+
// A run inside another's part folder: its outputs go in with that run's,
|
|
818
|
+
// and what runs killed outright left is seen to once that run has ended
|
|
819
|
+
// well, so a run that fails after it leaves the folder as it was.
|
|
820
|
+
const outer = [...open].filter((r) => inside(run.dir, r.part)).sort((a, b) => b.part.length - a.part.length)[0];
|
|
821
|
+
if (outer) {
|
|
822
|
+
const top = path.relative(outer.part, run.dir).split(path.sep)[0];
|
|
823
|
+
if (top) output(outer, top, false);
|
|
824
|
+
else for (const name of names) output(outer, name, run.outputs.get(name));
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
sweep(run.dir);
|
|
828
|
+
sweep(path.dirname(run.dir));
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// Removes a run's part folder and the folders it made for it.
|
|
832
|
+
function discard(run) {
|
|
833
|
+
if (run.ended) return;
|
|
834
|
+
// A run inside another's goes with it, and the outer one is told of; a
|
|
835
|
+
// run whose commit failed has told of its folder itself.
|
|
836
|
+
if (!run.said && ![...open].some((r) => r !== run && inside(run.dir, r.part))) told.push(run.existed ? `${shown(run.dir)} is as it was` : `${shown(run.dir)} was not made`);
|
|
837
|
+
ended(run);
|
|
838
|
+
remove(run.part);
|
|
839
|
+
for (const stage of run.stages) remove(stage);
|
|
840
|
+
for (const folder of run.made) {
|
|
841
|
+
try {
|
|
842
|
+
// Empty ones only: another run may be at work in one.
|
|
843
|
+
fs.rmdirSync(folder);
|
|
844
|
+
} catch {
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// What became of the folders of the runs that failed, said once: the CLI
|
|
851
|
+
// ends its failure with it.
|
|
852
|
+
function aftermath() {
|
|
853
|
+
const said = [...new Set(told)];
|
|
854
|
+
told.length = 0;
|
|
855
|
+
return said;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// ─── temp folders and processes ────────────────────────────────────────────
|
|
859
|
+
// A folder in the system's temp folder, named for the process that made it
|
|
860
|
+
// (<prefix><pid>-<random>), removed with release(), and on a stop or at
|
|
861
|
+
// exit when it has not been (or could not be). before(dir), when given,
|
|
862
|
+
// runs each time just before it is removed: Chrome's profile ends what
|
|
863
|
+
// still uses it and takes the socket folder Chrome links from it along
|
|
864
|
+
// (chrome.js). The first time a process makes one of a kind, it removes the
|
|
865
|
+
// ones of that kind whose process is gone, or whose pid is its own now,
|
|
866
|
+
// the same way: a run killed outright leaves them. For a kind whose name
|
|
867
|
+
// other programs may give their folders too, ours(dir) tells launch-film's
|
|
868
|
+
// from theirs, and only those go.
|
|
869
|
+
const swept = new Set();
|
|
870
|
+
function tempDir(prefix, before = null, ours = null) {
|
|
871
|
+
if (!swept.has(prefix)) {
|
|
872
|
+
swept.add(prefix);
|
|
873
|
+
sweepTemps(prefix, before, ours);
|
|
874
|
+
}
|
|
875
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), `${prefix}${process.pid}-`));
|
|
876
|
+
temps.set(dir, before);
|
|
877
|
+
hook();
|
|
878
|
+
return dir;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// Whether the socket Chrome makes in a temp folder, at
|
|
882
|
+
// <folder>/com.google.Chrome.XXXXXX/SingletonSocket, fits under `folder`
|
|
883
|
+
// on Linux: a socket's path holds 107 bytes at most, and Chrome given a
|
|
884
|
+
// longer one stops as it starts. SOCKET_ROOM is the longest such folder.
|
|
885
|
+
const SOCKET_PATH = "/com.google.Chrome.XXXXXX/SingletonSocket";
|
|
886
|
+
const SOCKET_ROOM = 107 - SOCKET_PATH.length;
|
|
887
|
+
const socketFits = (folder) => Buffer.byteLength(folder) <= SOCKET_ROOM;
|
|
888
|
+
|
|
889
|
+
// The environment for a program whose own temp files should go with the
|
|
890
|
+
// temp folder `folder` of this run's. Chrome makes folders of its own in
|
|
891
|
+
// TMPDIR, and one stopped as it starts leaves some there that nothing ties
|
|
892
|
+
// to it, as does a program that is stopped before it has tidied up. On Linux
|
|
893
|
+
// only, and when Chrome's socket there still fits (socketFits): on a Mac
|
|
894
|
+
// that path is tighter, and elsewhere there is no TMPDIR to set.
|
|
895
|
+
function tempEnv(folder) {
|
|
896
|
+
return folder && process.platform === "linux" && socketFits(folder) ? { ...process.env, TMPDIR: folder } : process.env;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// The temp folders of a kind that runs killed outright left: this user's
|
|
900
|
+
// own, whose process is gone or has this process's pid now, and that
|
|
901
|
+
// ours(), where given, takes for launch-film's. A folder another user put
|
|
902
|
+
// in a shared temp folder under such a name is left alone, and before()
|
|
903
|
+
// never runs on it: a profile's link there can lead anywhere.
|
|
904
|
+
function sweepTemps(prefix, before, ours) {
|
|
905
|
+
const named = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(\\d+)-[A-Za-z0-9]{6}$`);
|
|
906
|
+
let entries = [];
|
|
907
|
+
try {
|
|
908
|
+
entries = fs.readdirSync(os.tmpdir(), { withFileTypes: true });
|
|
909
|
+
} catch {
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
for (const entry of entries) {
|
|
913
|
+
const m = named.exec(entry.name);
|
|
914
|
+
const at = path.join(os.tmpdir(), entry.name);
|
|
915
|
+
if (!m || !entry.isDirectory() || temps.has(at) || !mine(at)) continue;
|
|
916
|
+
const pid = Number(m[1]);
|
|
917
|
+
if (pid !== process.pid && running(pid)) continue;
|
|
918
|
+
if (ours && !ours(at)) continue;
|
|
919
|
+
if (before) {
|
|
920
|
+
try {
|
|
921
|
+
before(at);
|
|
922
|
+
} catch {
|
|
923
|
+
// The folder goes all the same.
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
remove(at);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
function clear(dir) {
|
|
931
|
+
const before = temps.get(dir);
|
|
932
|
+
if (before) {
|
|
933
|
+
try {
|
|
934
|
+
before(dir);
|
|
935
|
+
} catch {
|
|
936
|
+
// The folder goes all the same.
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return remove(dir);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function release(dir) {
|
|
943
|
+
if (clear(dir)) temps.delete(dir);
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
function signal(child, name) {
|
|
947
|
+
try {
|
|
948
|
+
child.kill(name);
|
|
949
|
+
} catch {
|
|
950
|
+
// Gone already.
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
function kill(pid) {
|
|
955
|
+
try {
|
|
956
|
+
process.kill(pid, "SIGKILL");
|
|
957
|
+
} catch {
|
|
958
|
+
// Gone already.
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// Whether a process runs, an ended one that waits to be reaped aside.
|
|
963
|
+
function alive(pid) {
|
|
964
|
+
try {
|
|
965
|
+
process.kill(pid, 0);
|
|
966
|
+
} catch {
|
|
967
|
+
return false;
|
|
968
|
+
}
|
|
969
|
+
const stat = statOf(pid);
|
|
970
|
+
return !stat || stat[0] !== "Z";
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
// ─── Chrome's profiles ─────────────────────────────────────────────────────
|
|
974
|
+
// The processes that use a Chrome profile: Chrome names it in the command
|
|
975
|
+
// line of each of them, its helpers' too. Read from /proc, so on Linux only.
|
|
976
|
+
function usersOf(profile) {
|
|
977
|
+
const flag = `--user-data-dir=${profile}`;
|
|
978
|
+
const pids = [];
|
|
979
|
+
let names = [];
|
|
980
|
+
try {
|
|
981
|
+
names = fs.readdirSync("/proc");
|
|
982
|
+
} catch {
|
|
983
|
+
return pids;
|
|
984
|
+
}
|
|
985
|
+
for (const name of names) {
|
|
986
|
+
if (!/^\d+$/.test(name) || Number(name) === process.pid) continue;
|
|
987
|
+
try {
|
|
988
|
+
const cmd = fs.readFileSync(`/proc/${name}/cmdline`, "utf8");
|
|
989
|
+
const at = cmd.indexOf(flag);
|
|
990
|
+
if (at >= 0 && /^[\0 ]?$/.test(cmd.charAt(at + flag.length))) pids.push(Number(name));
|
|
991
|
+
} catch {
|
|
992
|
+
// Gone meanwhile.
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
return pids;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// Whether a folder is right in the system's temp folder, or in a temp folder
|
|
999
|
+
// of this run's that a program was given as its own (tempEnv).
|
|
1000
|
+
function inTemp(folder) {
|
|
1001
|
+
if (temps.has(path.dirname(folder))) return true;
|
|
1002
|
+
try {
|
|
1003
|
+
return fs.realpathSync(path.dirname(folder)) === fs.realpathSync(os.tmpdir());
|
|
1004
|
+
} catch {
|
|
1005
|
+
return false;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
// Chrome links SingletonSocket in its profile to a socket in a temp folder
|
|
1010
|
+
// of its own (com.google.Chrome.XXXXXX), and removes both as it closes. One
|
|
1011
|
+
// stopped while it closes, or killed outright, leaves that folder behind,
|
|
1012
|
+
// so the folder is read from the link while Chrome is up, and goes with the
|
|
1013
|
+
// profile. Returns it, or null. Only a profile of this user's own is read,
|
|
1014
|
+
// and only a folder of this user's own is given: a profile another user
|
|
1015
|
+
// left in a shared temp folder can link anywhere.
|
|
1016
|
+
function socketFolder(profile) {
|
|
1017
|
+
try {
|
|
1018
|
+
if (!mine(profile)) return null;
|
|
1019
|
+
const folder = path.dirname(fs.readlinkSync(path.join(profile, "SingletonSocket")));
|
|
1020
|
+
if (/^\.?(com\.google\.Chrome|org\.chromium\.Chromium)\.\w+$/.test(path.basename(folder)) && inTemp(folder) && mine(folder)) return folder;
|
|
1021
|
+
} catch {
|
|
1022
|
+
// No such link.
|
|
1023
|
+
}
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// The folders Chrome's component updater downloads and unpacks into, which
|
|
1028
|
+
// Chrome makes in its TMPDIR in its first seconds when it may use the
|
|
1029
|
+
// network (com.google.Chrome.chrome_chrome_url_fetcher_.XXXXXX and
|
|
1030
|
+
// com.google.Chrome.chrome_chrome_Unpacker_BeginUnzipping.XXXXXX, or the
|
|
1031
|
+
// same after org.chromium.Chromium. for Chromium): a Chrome that ends
|
|
1032
|
+
// before it is done leaves them, empty or with what it fetched. Those in
|
|
1033
|
+
// the folder `dir`, as { dir, names }, or null where it cannot be read.
|
|
1034
|
+
const FETCHING = /^\.?(com\.google\.Chrome|org\.chromium\.Chromium)\.\w*(url_fetcher_|Unpacker_BeginUnzipping)\.\w+$/;
|
|
1035
|
+
function fetching(dir = os.tmpdir()) {
|
|
1036
|
+
try {
|
|
1037
|
+
return { dir, names: new Set(fs.readdirSync(dir).filter((name) => FETCHING.test(name))) };
|
|
1038
|
+
} catch {
|
|
1039
|
+
return null;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// Run just before a profile is removed: what still uses it is ended first,
|
|
1044
|
+
// since Chrome's helpers, still closing or left by a Chrome killed outright,
|
|
1045
|
+
// wrote into it after it was removed and so kept it, and the socket folder
|
|
1046
|
+
// Chrome made for it (`socket`, else read from its link) goes with it. For a
|
|
1047
|
+
// Chrome that had the system's temp folder as its own, `fetched` is what
|
|
1048
|
+
// fetching() found there as it started: the folders of that kind of this
|
|
1049
|
+
// user's that came since go too, with what is in them. Another Chrome that
|
|
1050
|
+
// fetched a component there at that moment loses only that download.
|
|
1051
|
+
function closeProfile(profile, socket = null, fetched = null) {
|
|
1052
|
+
const pids = usersOf(profile);
|
|
1053
|
+
for (const pid of pids) kill(pid);
|
|
1054
|
+
for (let i = 0; i < 100 && pids.some(alive); i++) pause(10);
|
|
1055
|
+
const folder = socket || socketFolder(profile);
|
|
1056
|
+
if (folder) remove(folder);
|
|
1057
|
+
const now = fetched && fetching(fetched.dir);
|
|
1058
|
+
for (const name of now ? now.names : []) {
|
|
1059
|
+
const at = path.join(now.dir, name);
|
|
1060
|
+
if (!fetched.names.has(name) && mine(at)) remove(at);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// The processes below `pids`, all the way down: [{ pid, start }]. Read from
|
|
1065
|
+
// /proc (Linux); elsewhere none.
|
|
1066
|
+
function below(pids) {
|
|
1067
|
+
if (!pids.length) return [];
|
|
1068
|
+
let names = [];
|
|
1069
|
+
try {
|
|
1070
|
+
names = fs.readdirSync("/proc");
|
|
1071
|
+
} catch {
|
|
1072
|
+
return [];
|
|
1073
|
+
}
|
|
1074
|
+
const kids = new Map();
|
|
1075
|
+
for (const name of names) {
|
|
1076
|
+
const stat = /^\d+$/.test(name) ? statOf(name) : null;
|
|
1077
|
+
if (!stat) continue;
|
|
1078
|
+
if (!kids.has(stat[1])) kids.set(stat[1], []);
|
|
1079
|
+
kids.get(stat[1]).push({ pid: Number(name), start: stat[19] });
|
|
1080
|
+
}
|
|
1081
|
+
const found = [];
|
|
1082
|
+
for (const queue = pids.map(String); queue.length; ) {
|
|
1083
|
+
for (const kid of kids.get(queue.shift()) || []) {
|
|
1084
|
+
found.push(kid);
|
|
1085
|
+
queue.push(String(kid.pid));
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
return found;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// easybrandkit runs a Chrome of its own, and 0.4.0 on a stop neither ends
|
|
1092
|
+
// it nor removes its profile: a stop sent to launch-film alone left that
|
|
1093
|
+
// Chrome running for good, and Ctrl-C left its profile in the temp folder.
|
|
1094
|
+
// 0.4.1, which launch-film runs through npx, ends its own on a stop, but one
|
|
1095
|
+
// on PATH or named by LAUNCH_FILM_EASYBRANDKIT can be older, and a stop
|
|
1096
|
+
// ends easybrandkit once STOP_WAIT has passed, whatever it is doing.
|
|
1097
|
+
// So the processes below one started with { watch: true } are looked at
|
|
1098
|
+
// while it runs, four times a second, and again as a stop or a failure
|
|
1099
|
+
// begins, and the profiles in the temp folder that the Chromes among them
|
|
1100
|
+
// use are noted with their socket folders: Ctrl-C ends easybrandkit at once,
|
|
1101
|
+
// and its Chrome is out of its tree before launch-film can look. The stop
|
|
1102
|
+
// or the failure ends those processes, and every process that uses one of
|
|
1103
|
+
// those profiles wherever it has been moved since, and removes the profiles
|
|
1104
|
+
// (Linux, where /proc tells).
|
|
1105
|
+
const watched = new Set();
|
|
1106
|
+
const theirs = new Map();
|
|
1107
|
+
const below0 = [];
|
|
1108
|
+
let watcher = null;
|
|
1109
|
+
|
|
1110
|
+
// The Chrome profile a process names on its command line, or null. Chrome
|
|
1111
|
+
// writes its command line over once it runs, as one line with spaces, so
|
|
1112
|
+
// the value ends at a NUL, or in that form at a space.
|
|
1113
|
+
function profileOf(pid) {
|
|
1114
|
+
let cmd = "";
|
|
1115
|
+
try {
|
|
1116
|
+
cmd = fs.readFileSync(`/proc/${pid}/cmdline`, "utf8");
|
|
1117
|
+
} catch {
|
|
1118
|
+
return null;
|
|
1119
|
+
}
|
|
1120
|
+
const m = /(?:^|[\0 ])--user-data-dir=([^\0]+)/.exec(cmd);
|
|
1121
|
+
if (!m) return null;
|
|
1122
|
+
return path.resolve(cmd.replace(/\0+$/, "").includes("\0") ? m[1] : m[1].split(" ")[0]);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
function note(list) {
|
|
1126
|
+
for (const p of list) {
|
|
1127
|
+
const profile = profileOf(p.pid);
|
|
1128
|
+
if (!profile || temps.has(profile) || theirs.get(profile) || !inTemp(profile)) continue;
|
|
1129
|
+
theirs.set(profile, socketFolder(profile));
|
|
1130
|
+
}
|
|
1131
|
+
return list;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function watch(child) {
|
|
1135
|
+
if (!fs.existsSync("/proc/self/stat")) return;
|
|
1136
|
+
watched.add(child);
|
|
1137
|
+
child.once("exit", () => {
|
|
1138
|
+
watched.delete(child);
|
|
1139
|
+
// One that ended well took its Chrome and profile with it.
|
|
1140
|
+
for (const profile of theirs.keys()) if (!lstat(profile)) theirs.delete(profile);
|
|
1141
|
+
});
|
|
1142
|
+
if (watcher) return;
|
|
1143
|
+
watcher = setInterval(() => {
|
|
1144
|
+
if (watched.size) note(below([...watched].map((c) => c.pid)));
|
|
1145
|
+
else {
|
|
1146
|
+
clearInterval(watcher);
|
|
1147
|
+
watcher = null;
|
|
1148
|
+
}
|
|
1149
|
+
}, 250);
|
|
1150
|
+
watcher.unref();
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
// Whether a process noted as { pid, start } is still that process.
|
|
1154
|
+
function same(p) {
|
|
1155
|
+
const stat = statOf(p.pid);
|
|
1156
|
+
return Boolean(stat) && stat[19] === p.start;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
// Ends the processes `list` that are still the ones noted, and what uses
|
|
1160
|
+
// the profiles noted, and removes those profiles.
|
|
1161
|
+
function endTheirs(list) {
|
|
1162
|
+
for (const p of list) if (same(p)) kill(p.pid);
|
|
1163
|
+
below0.length = 0;
|
|
1164
|
+
for (const [profile, socket] of theirs) {
|
|
1165
|
+
closeProfile(profile, socket);
|
|
1166
|
+
if (mine(profile)) remove(profile);
|
|
1167
|
+
}
|
|
1168
|
+
theirs.clear();
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
// A process the runs stop with them: Chrome, ffmpeg, easybrandkit. With
|
|
1172
|
+
// watch, the processes it starts are stopped with it too (above).
|
|
1173
|
+
function track(child, { watch: tree = false } = {}) {
|
|
1174
|
+
if (!child || !child.pid) return child;
|
|
1175
|
+
children.add(child);
|
|
1176
|
+
hook();
|
|
1177
|
+
child.once("exit", () => children.delete(child));
|
|
1178
|
+
if (tree) watch(child);
|
|
1179
|
+
if (stopping) signal(child, "SIGKILL");
|
|
1180
|
+
else if (ending) signal(child, "SIGTERM");
|
|
1181
|
+
return child;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
// Asks the processes the runs started to stop with the signal `name`, so
|
|
1185
|
+
// Chrome takes its helpers with it and npx passes it on to easybrandkit,
|
|
1186
|
+
// and calls then() once they have gone or STOP_WAIT has passed. What runs
|
|
1187
|
+
// below the watched ones is noted first: once easybrandkit has ended, the
|
|
1188
|
+
// system moves its Chrome out of its tree.
|
|
1189
|
+
function settle(name, then) {
|
|
1190
|
+
below0.push(...note(below([...watched].map((c) => c.pid))));
|
|
1191
|
+
for (const child of children) signal(child, name);
|
|
1192
|
+
const since = Date.now();
|
|
1193
|
+
const wait = setInterval(() => {
|
|
1194
|
+
if (children.size && Date.now() - since < STOP_WAIT) return;
|
|
1195
|
+
clearInterval(wait);
|
|
1196
|
+
then();
|
|
1197
|
+
}, 20);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// Stops what is left: the processes, the runs not ended (the innermost
|
|
1201
|
+
// first) and the temp folders.
|
|
1202
|
+
function endAll() {
|
|
1203
|
+
// What runs below the watched processes is looked at before they are
|
|
1204
|
+
// killed, while their tree is whole, and what ran there as the stop began
|
|
1205
|
+
// again: a process that was only starting then can be a Chrome now.
|
|
1206
|
+
const now = note([...below0, ...below([...watched].map((c) => c.pid))].filter(same));
|
|
1207
|
+
for (const child of children) signal(child, "SIGKILL");
|
|
1208
|
+
endTheirs(now);
|
|
1209
|
+
for (const run of [...open].reverse()) discard(run);
|
|
1210
|
+
for (const dir of temps.keys()) clear(dir);
|
|
1211
|
+
temps.clear();
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// Ctrl-C, SIGTERM or SIGHUP: the signal goes on to the processes the runs
|
|
1215
|
+
// started, so easybrandkit under npx and Chrome can end on their own; those
|
|
1216
|
+
// still running after STOP_WAIT are killed. Then the runs and temp folders
|
|
1217
|
+
// are removed and the process ends by the same signal. A second signal in
|
|
1218
|
+
// the meantime (a cancel sends one to the whole process group, and a
|
|
1219
|
+
// supervisor may pass it on again) is let go, so the clean-up finishes.
|
|
1220
|
+
function onSignal(name) {
|
|
1221
|
+
if (stopping) return;
|
|
1222
|
+
stopping = name;
|
|
1223
|
+
settle(name, () => {
|
|
1224
|
+
endAll();
|
|
1225
|
+
// Written at once: the process ends by the signal right after.
|
|
1226
|
+
try {
|
|
1227
|
+
fs.writeSync(2, `launch-film: stopped${told.length ? `, so ${[...new Set(told)].join(" and ")}` : ""}\n`);
|
|
1228
|
+
} catch {
|
|
1229
|
+
// No one to tell.
|
|
1230
|
+
}
|
|
1231
|
+
for (const s of SIGNALS) process.removeListener(s, onSignal);
|
|
1232
|
+
// Where the signal does not end the process (Windows), the exit code
|
|
1233
|
+
// a shell gives for it.
|
|
1234
|
+
setTimeout(() => process.exit(128 + (os.constants.signals[name] || 0)), 500);
|
|
1235
|
+
try {
|
|
1236
|
+
process.kill(process.pid, name);
|
|
1237
|
+
} catch {
|
|
1238
|
+
// Not a signal this system sends.
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// Ends the process with `code` after a run failed, once the processes the
|
|
1244
|
+
// runs started have stopped as at Ctrl-C: Chrome killed outright left its
|
|
1245
|
+
// helpers writing to its profile after the profile was removed. The exit
|
|
1246
|
+
// removes the temp folders and what the runs left (endAll).
|
|
1247
|
+
function exit(code) {
|
|
1248
|
+
if (stopping || ending) return;
|
|
1249
|
+
ending = true;
|
|
1250
|
+
process.exitCode = code;
|
|
1251
|
+
if (!children.size) process.exit(code);
|
|
1252
|
+
settle("SIGTERM", () => process.exit(code));
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// Called once by the CLI: library use keeps its own signal handling.
|
|
1256
|
+
function listen() {
|
|
1257
|
+
if (listening) return;
|
|
1258
|
+
listening = true;
|
|
1259
|
+
for (const s of SIGNALS) process.on(s, onSignal);
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// ─── part folders left by runs killed outright ─────────────────────────────
|
|
1263
|
+
function running(pid) {
|
|
1264
|
+
try {
|
|
1265
|
+
process.kill(pid, 0);
|
|
1266
|
+
return true;
|
|
1267
|
+
} catch (err) {
|
|
1268
|
+
return err.code === "EPERM";
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// The newest time anything in a folder changed, two levels down.
|
|
1273
|
+
function touched(folder) {
|
|
1274
|
+
let newest = 0;
|
|
1275
|
+
const look = (at, depth) => {
|
|
1276
|
+
const s = lstat(at);
|
|
1277
|
+
if (!s) return;
|
|
1278
|
+
newest = Math.max(newest, s.mtimeMs);
|
|
1279
|
+
if (depth > 0 && s.isDirectory()) {
|
|
1280
|
+
let names = [];
|
|
1281
|
+
try {
|
|
1282
|
+
names = fs.readdirSync(at);
|
|
1283
|
+
} catch {
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
for (const name of names) look(path.join(at, name), depth - 1);
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
look(folder, 2);
|
|
1290
|
+
return newest;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
// What a part folder's .old holds: the paths of its entries, a folder's
|
|
1294
|
+
// with a / at its end.
|
|
1295
|
+
function earlier(part) {
|
|
1296
|
+
const old = path.join(part, ".old");
|
|
1297
|
+
const list = [];
|
|
1298
|
+
const walk = (at, rel) => {
|
|
1299
|
+
let names = [];
|
|
1300
|
+
try {
|
|
1301
|
+
names = fs.readdirSync(at, { withFileTypes: true });
|
|
1302
|
+
} catch {
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
for (const e of names) {
|
|
1306
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
1307
|
+
let inner = 0;
|
|
1308
|
+
try {
|
|
1309
|
+
inner = e.isDirectory() ? fs.readdirSync(path.join(at, e.name)).length : 0;
|
|
1310
|
+
} catch {
|
|
1311
|
+
// Listed as a folder.
|
|
1312
|
+
}
|
|
1313
|
+
if (inner) walk(path.join(at, e.name), r);
|
|
1314
|
+
else list.push(e.isDirectory() ? `${r}/` : r);
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
walk(old, "");
|
|
1318
|
+
return list;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// Removes the part folders in `folder` that runs killed outright left: this
|
|
1322
|
+
// user's own, not a run's of this process that has not ended, nothing in
|
|
1323
|
+
// them changed for STALE, and their process gone, or its pid now this
|
|
1324
|
+
// process's (in a container every run can have the same pid). One whose
|
|
1325
|
+
// .old holds earlier files (the run was killed while it put its files in
|
|
1326
|
+
// place) is kept for them instead, and said once the run ends (noticed).
|
|
1327
|
+
function sweep(folder) {
|
|
1328
|
+
let entries = [];
|
|
1329
|
+
try {
|
|
1330
|
+
entries = fs.readdirSync(folder, { withFileTypes: true });
|
|
1331
|
+
} catch {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const ours = new Set([...open].flatMap((r) => [r.part, ...r.stages]));
|
|
1335
|
+
for (const entry of entries) {
|
|
1336
|
+
const m = PART.exec(entry.name);
|
|
1337
|
+
const at = path.join(folder, entry.name);
|
|
1338
|
+
// What a run killed as it took over a lock left next to it.
|
|
1339
|
+
if (LEFT.test(entry.name) && mine(at) && Date.now() - touched(at) >= STALE) remove(at);
|
|
1340
|
+
if (!m || !entry.isDirectory() || ours.has(at) || !mine(at)) continue;
|
|
1341
|
+
const pid = Number(m[1]);
|
|
1342
|
+
if (Date.now() - touched(at) < STALE || (pid !== process.pid && running(pid))) continue;
|
|
1343
|
+
const files = earlier(at);
|
|
1344
|
+
if (!files.length) {
|
|
1345
|
+
remove(at);
|
|
1346
|
+
continue;
|
|
1347
|
+
}
|
|
1348
|
+
const kept = keepFolder(at, files, `A launch-film run (process ${m[1]}) was killed while it put its files in place in ${shown(folder)}, and left these earlier files it had moved aside`);
|
|
1349
|
+
found.push(`a launch-film run killed while it put its files in ${shown(folder)} left earlier files in ${shown(kept)}; launch-film-kept.txt there lists them`);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
// What the runs that ended well found left by runs killed outright, said
|
|
1354
|
+
// once: the CLI ends with it.
|
|
1355
|
+
function noticed() {
|
|
1356
|
+
const said = [...new Set(found)];
|
|
1357
|
+
found.length = 0;
|
|
1358
|
+
return said;
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
module.exports = { begin, within, heldBackIn, shown, said, aftermath, noticed, tempDir, tempEnv, release, track, listen, exit, stopping: () => stopping, sweep, reason, full, save, copyPlain,
|
|
1362
|
+
socketFolder, closeProfile, fetching, socketFits, STALE, LOCK, SOCKET_ROOM };
|