Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): skip a patch whose file refuses the write

Yudhi Armyndharis260389bd8ec8
diff --git a/scripts/patch-baileys-appstate.js b/scripts/patch-baileys-appstate.js
index 8a716ae4b..fdb2355c8 100644
--- a/scripts/patch-baileys-appstate.js
+++ b/scripts/patch-baileys-appstate.js
@@ -69,8 +69,9 @@ function applyAppStatePatch(baileysDir = DEFAULT_BAILEYS) {
try {
fs.writeFileSync(file, source.replace(LOOP_FIND, LOOP_REPLACE), 'utf8');
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { note: 'bounded the app-state resync loop on an empty decode' };
diff --git a/scripts/patch-baileys-newsletter-create.js b/scripts/patch-baileys-newsletter-create.js
index bf51fba3e..5d464f397 100644
--- a/scripts/patch-baileys-newsletter-create.js
+++ b/scripts/patch-baileys-newsletter-create.js
@@ -98,8 +98,9 @@ function applyNewsletterCreatePatch(baileysDir = DEFAULT_BAILEYS) {
try {
fs.writeFileSync(file, source.replace(PARSE_FIND, PARSE_REPLACE), 'utf8');
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { note: 'made the newsletter-create parse tolerate an absent picture, keeping the new channel id' };
diff --git a/scripts/patch-write-failure.spec.js b/scripts/patch-write-failure.spec.js
index afa71178d..1dfd847a8 100644
--- a/scripts/patch-write-failure.spec.js
+++ b/scripts/patch-write-failure.spec.js
@@ -10,7 +10,8 @@ const { spawnSync } = require('node:child_process');
/**
* `--best-effort` lets a source install carry on without a patch it cannot place. A write that fails
* part-way is not that case: fs.writeFileSync has already truncated the target, so the dependency is
- * left broken and no later run can tell. Each patcher must exit non-zero then, and only then.
+ * left broken and no later run can tell. Each patcher must exit non-zero then. An open that is refused
+ * writes nothing, and is skipped like any other tree the patcher cannot change.
*/
// realpath: os.tmpdir() is a symlink on macOS and the patcher resolves its own location through it,
@@ -28,11 +29,13 @@ const fs = require('node:fs');
const path = require('node:path');
const write = fs.writeFileSync;
const target = path.resolve(process.env.FIXTURE_WRITE_TARGET);
+const code = process.env.FIXTURE_WRITE_FAULT;
fs.writeFileSync = function (file, data, ...args) {
if (typeof file !== 'number' && path.resolve(String(file)) === target) {
- write.call(fs, file, Buffer.from(String(data)).subarray(0, ${PREFIX_BYTES}));
+ // A refused open writes nothing; any other failure lands a prefix first, as a full disk does.
+ if (code !== 'EACCES') write.call(fs, file, Buffer.from(String(data)).subarray(0, ${PREFIX_BYTES}));
process.stderr.write('${MARKER}\\n');
- throw Object.assign(new Error('fixture write failure'), { code: 'ENOSPC' });
+ throw Object.assign(new Error('fixture write failure'), { code });
}
return write.call(fs, file, data, ...args);
};
@@ -79,7 +82,7 @@ function fixture(name, relative, content) {
function patch({ root, script, target }, fault) {
const result = spawnSync(process.execPath, [...(fault ? ['--require', PRELOAD] : []), script, '--best-effort'], {
cwd: root,
- env: { PATH: process.env.PATH, FIXTURE_WRITE_TARGET: target },
+ env: { PATH: process.env.PATH, FIXTURE_WRITE_TARGET: target, FIXTURE_WRITE_FAULT: fault },
encoding: 'utf8',
timeout: 10000,
});
@@ -104,9 +107,17 @@ for (const [name, relative, shapeOf] of PATCHERS) {
test(`${name} fails when the write breaks part-way, even under --best-effort`, () => {
const tree = fixture(name, relative, shape);
- const result = patch(tree, true);
+ const result = patch(tree, 'ENOSPC');
assert.match(result.stderr, new RegExp(MARKER), 'the injected fault did not fire');
assert.equal(fs.statSync(tree.target).size, PREFIX_BYTES, 'the target keeps only the written prefix');
assert.notEqual(result.status, 0);
});
+
+ test(`${name} skips a file it is not permitted to write under --best-effort`, () => {
+ const tree = fixture(name, relative, shape);
+ const result = patch(tree, 'EACCES');
+ assert.match(result.stderr, new RegExp(MARKER), 'the injected fault did not fire');
+ assert.equal(fs.readFileSync(tree.target, 'utf8'), shape);
+ assert.equal(result.status, 0);
+ });
}
diff --git a/scripts/patch-wwebjs-block.js b/scripts/patch-wwebjs-block.js
index e85f2d417..6120a8da0 100644
--- a/scripts/patch-wwebjs-block.js
+++ b/scripts/patch-wwebjs-block.js
@@ -184,8 +184,9 @@ function applyBlockPatches({ wwjsDir = DEFAULT_WWJS } = {}) {
try {
fs.writeFileSync(contactFile, source);
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
}
diff --git a/scripts/patch-wwebjs-group-description.js b/scripts/patch-wwebjs-group-description.js
index 300d98fd9..1b20382c7 100644
--- a/scripts/patch-wwebjs-group-description.js
+++ b/scripts/patch-wwebjs-group-description.js
@@ -100,8 +100,9 @@ function applyGroupDescriptionFix(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(groupChatFile, source.replace(POSITIONAL_CALL, OPTIONS_CALL));
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { skipped: false, note: 'group description job called with an options object' };
diff --git a/scripts/patch-wwebjs-media-id.js b/scripts/patch-wwebjs-media-id.js
index 335bd7c8c..586d2dce7 100644
--- a/scripts/patch-wwebjs-media-id.js
+++ b/scripts/patch-wwebjs-media-id.js
@@ -54,8 +54,9 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(utilsFile, source.replace(ANCHOR, FIX + ANCHOR));
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { skipped: false, note: 'media model id stripped from outgoing messages' };
diff --git a/scripts/patch-wwebjs-newsletter-preview.js b/scripts/patch-wwebjs-newsletter-preview.js
index 8b916fc92..c031a8ec4 100644
--- a/scripts/patch-wwebjs-newsletter-preview.js
+++ b/scripts/patch-wwebjs-newsletter-preview.js
@@ -50,8 +50,9 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(utilsFile, source.replace(LEGACY_CALL, CONTEXT_CALL));
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { skipped: false, note: 'destination chat context added to link previews' };
diff --git a/scripts/patch-wwebjs-participant-arity.js b/scripts/patch-wwebjs-participant-arity.js
index 630dfc8fd..1b00ba6d7 100644
--- a/scripts/patch-wwebjs-participant-arity.js
+++ b/scripts/patch-wwebjs-participant-arity.js
@@ -179,8 +179,9 @@ function applyParticipantArityPatches(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(groupChatFile, source);
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
}
diff --git a/scripts/patch-wwebjs-ready-sync.js b/scripts/patch-wwebjs-ready-sync.js
index 00cba03cf..339ad455d 100644
--- a/scripts/patch-wwebjs-ready-sync.js
+++ b/scripts/patch-wwebjs-ready-sync.js
@@ -118,8 +118,9 @@ function applyReadySyncPatch(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(clientFile, source);
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { skipped: false, note: 'readiness marker and hasSynced level-check applied' };
diff --git a/scripts/patch-wwebjs-send-error.js b/scripts/patch-wwebjs-send-error.js
index 122e3a7ab..40b9fbc39 100644
--- a/scripts/patch-wwebjs-send-error.js
+++ b/scripts/patch-wwebjs-send-error.js
@@ -160,8 +160,9 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(clientFile, source.replace(ANCHOR, FIX));
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
return { skipped: false, note: 'page-side send errors now carry what the page threw' };
diff --git a/scripts/patch-wwebjs-status.js b/scripts/patch-wwebjs-status.js
index 53b172660..57fabc411 100644
--- a/scripts/patch-wwebjs-status.js
+++ b/scripts/patch-wwebjs-status.js
@@ -173,8 +173,9 @@ function applyStatusPatches(wwjsDir = DEFAULT_WWJS) {
try {
fs.writeFileSync(utilsFile, source);
} catch (error) {
- // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
- error.partial = true;
+ // The write truncates before it writes, so a failure after the open leaves a partial file, which
+ // --best-effort must not skip. A refused open leaves the tree as it was.
+ error.partial = !['EACCES', 'EPERM', 'EROFS'].includes(error.code);
throw error;
}
}