Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): keep a patch going when the file system refuses chmod

Yudhi Armyndharis0c96aabfe30b
diff --git a/scripts/atomic-write.js b/scripts/atomic-write.js
index a3f637484..f3ebdafdf 100644
--- a/scripts/atomic-write.js
+++ b/scripts/atomic-write.js
@@ -13,6 +13,9 @@ const path = require('path');
* including the rename leaves the target as it was, so a patcher may treat any write error as "this
* tree was not patched" and `--best-effort` may skip it.
*
+ * A temporary file left behind by a killed run carries that run's pid and is not removed here, since it
+ * could belong to a run still in progress; a later run on the same pid overwrites it.
+ *
* A patch that spans several files stages all of them before it replaces the first. If a rename then
* fails after an earlier one succeeded, the tree is half patched; that error is marked `partial` and
* `--best-effort` must not skip it.
@@ -24,9 +27,16 @@ function writeFilesAtomic(entries) {
// The real file: a link would otherwise be replaced by a regular file.
const target = fs.realpathSync(file);
const temp = path.join(path.dirname(target), `${path.basename(target)}.${process.pid}.tmp`);
+ const mode = fs.statSync(target).mode & 0o7777;
staged.push({ target, temp });
- fs.writeFileSync(temp, text, 'utf8');
- fs.chmodSync(temp, fs.statSync(target).mode & 0o7777);
+ fs.writeFileSync(temp, text, { encoding: 'utf8', mode });
+ try {
+ // The create applied the umask; this restores the original bits. A file system that refuses chmod
+ // (a bind mount, FAT) keeps the umasked mode rather than failing a write that in place would pass.
+ fs.chmodSync(temp, mode);
+ } catch {
+ // see above
+ }
}
staged.forEach(({ target, temp }, replaced) => {
try {
diff --git a/scripts/patch-write-failure.spec.js b/scripts/patch-write-failure.spec.js
index 2d5d395e4..5dee4b112 100644
--- a/scripts/patch-write-failure.spec.js
+++ b/scripts/patch-write-failure.spec.js
@@ -23,9 +23,9 @@ test.after(() => fs.rmSync(WORK, { recursive: true, force: true }));
const PREFIX_BYTES = 64;
const MARKER = 'FIXTURE_WRITE_FAULT';
const PRELOAD = path.join(WORK, 'write-fault.cjs');
-// Fails one write ('write': the temporary file beside FIXTURE_FAULT_FILE, or the file itself) or one
-// rename ('rename': onto FIXTURE_FAULT_FILE). A full disk or an I/O error lands a prefix first; a refused
-// or busy open writes nothing.
+// Fails one write ('write': the temporary file beside FIXTURE_FAULT_FILE, or the file itself), one
+// rename ('rename': onto FIXTURE_FAULT_FILE) or the chmod of the temporary file ('chmod'). A full disk or
+// an I/O error lands a prefix first; a refused or busy open writes nothing.
fs.writeFileSync(
PRELOAD,
`'use strict';
@@ -33,6 +33,7 @@ const fs = require('node:fs');
const path = require('node:path');
const write = fs.writeFileSync;
const rename = fs.renameSync;
+const chmod = fs.chmodSync;
const op = process.env.FIXTURE_FAULT_OP;
const file = path.resolve(process.env.FIXTURE_FAULT_FILE);
const code = process.env.FIXTURE_FAULT_CODE;
@@ -52,6 +53,10 @@ fs.renameSync = function (from, to) {
if (op === 'rename' && path.resolve(String(to)) === file) fail();
return rename.call(fs, from, to);
};
+fs.chmodSync = function (target, mode) {
+ if (op === 'chmod' && path.resolve(String(target)).startsWith(file + '.')) fail();
+ return chmod.call(fs, target, mode);
+};
`,
);
@@ -216,6 +221,36 @@ test('patch-baileys-pairing.js fails even under --best-effort when only the firs
assert.equal(result.status, 1);
});
+// A file system that refuses chmod (a bind mount, FAT) must not turn into a failed or skipped patch: the
+// in-place write this replaced needed none. The file is patched, keeps the mode it had, and no temporary
+// file is left, under --best-effort and, for the Puppeteer patch, without it.
+const refusedChmod = tree => ({ op: 'chmod', code: 'EPERM', file: Object.values(tree.targets)[0] });
+const patchedWithoutChmod = (tree, result, before) => {
+ assert.match(result.stderr, new RegExp(MARKER), 'the injected fault did not fire');
+ assert.equal(result.status, 0, result.stderr);
+ Object.entries(before).forEach(([relative, text]) => {
+ assert.notEqual(fs.readFileSync(tree.targets[relative], 'utf8'), text);
+ assert.equal(fs.statSync(tree.targets[relative]).mode & 0o777, 0o640);
+ });
+ assert.deepEqual(leftovers(tree), []);
+};
+
+for (const [name, relative, shapeOf] of PATCHERS) {
+ test(`${name} patches the file when the file system refuses chmod`, () => {
+ const shape = `// fixture\n${shapeOf(require(`./${name}`))}\n`;
+ const tree = fixture(name, { [relative]: shape });
+ patchedWithoutChmod(tree, patch(tree, { fault: refusedChmod(tree) }), { [relative]: shape });
+ });
+}
+
+test('patch-baileys-pairing.js patches both files when the file system refuses chmod', () => {
+ const tree = pairingTree();
+ const before = Object.fromEntries(
+ Object.entries(tree.targets).map(([key, file]) => [key, fs.readFileSync(file, 'utf8')]),
+ );
+ patchedWithoutChmod(tree, patch(tree, { fault: { op: 'chmod', code: 'EPERM', file: tree.targets[SOCKET] } }), before);
+});
+
// The Puppeteer patch has no --best-effort mode: any failure is fatal, and leaves the file untouched.
const puppeteer = require('./patch-wwebjs-puppeteer.js');
test('patch-wwebjs-puppeteer.js fails on a write error and leaves Client.js as it was', () => {
@@ -229,6 +264,13 @@ test('patch-wwebjs-puppeteer.js fails on a write error and leaves Client.js as i
assert.equal(result.status, 1);
});
+test('patch-wwebjs-puppeteer.js patches Client.js when the file system refuses chmod, without --best-effort', () => {
+ const relative = 'whatsapp-web.js/src/Client.js';
+ const shape = `// fixture\n${puppeteer.REPLACEMENTS.map(([before]) => before).join('\n')}\n`;
+ const tree = fixture('patch-wwebjs-puppeteer.js', { [relative]: shape });
+ patchedWithoutChmod(tree, patch(tree, { fault: refusedChmod(tree), bestEffort: false }), { [relative]: shape });
+});
+
test('a link is written through, not replaced', () => {
const { writeFileAtomic } = require('./atomic-write.js');
const real = path.join(WORK, 'real.txt');