2014-05-14 02:39:25 +00:00
|
|
|
/*
|
2020-03-15 20:33:33 +00:00
|
|
|
* This is a debug tool. It checks all revisions for data corruption
|
|
|
|
*/
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
if (process.argv.length != 3) {
|
|
|
|
console.error("Use: node bin/checkPadDeltas.js $PADID");
|
2014-05-14 02:39:25 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
// get the padID
|
|
|
|
const padId = process.argv[2];
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
// load and initialize NPM;
|
2014-05-14 02:39:25 +00:00
|
|
|
var expect = require('expect.js')
|
|
|
|
var diff = require('diff')
|
2020-03-15 20:33:33 +00:00
|
|
|
var async = require('async')
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
let npm = require('../src/node_modules/npm');
|
|
|
|
var async = require("ep_etherpad-lite/node_modules/async");
|
|
|
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
npm.load({}, async function() {
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
try {
|
|
|
|
// initialize database
|
|
|
|
let settings = require('../src/node/utils/Settings');
|
|
|
|
let db = require('../src/node/db/DB');
|
|
|
|
await db.init();
|
|
|
|
|
|
|
|
// load modules
|
|
|
|
let Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
|
|
|
let padManager = require('../src/node/db/PadManager');
|
|
|
|
|
|
|
|
let exists = await padManager.doesPadExists(padId);
|
|
|
|
if (!exists) {
|
|
|
|
console.error("Pad does not exist");
|
2014-05-14 02:39:25 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
// get the pad
|
|
|
|
let pad = await padManager.getPad(padId);
|
2014-05-14 02:39:25 +00:00
|
|
|
|
|
|
|
//create an array with key revisions
|
|
|
|
//key revisions always save the full pad atext
|
|
|
|
var head = pad.getHeadRevisionNumber();
|
|
|
|
var keyRevisions = [];
|
|
|
|
for(var i=0;i<head;i+=100)
|
|
|
|
{
|
|
|
|
keyRevisions.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//create an array with all revisions
|
|
|
|
var revisions = [];
|
|
|
|
for(var i=0;i<=head;i++)
|
|
|
|
{
|
|
|
|
revisions.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
var atext = Changeset.makeAText("\n")
|
|
|
|
|
|
|
|
//run trough all revisions
|
|
|
|
async.forEachSeries(revisions, function(revNum, callback)
|
|
|
|
{
|
2020-03-15 20:33:33 +00:00
|
|
|
//console.log('Fetching', revNum)
|
|
|
|
db.db.get("pad:"+padId+":revs:" + revNum, function(err, revision)
|
|
|
|
{
|
|
|
|
if(err) return callback(err);
|
|
|
|
|
|
|
|
//check if there is a atext in the keyRevisions
|
|
|
|
if(~keyRevisions.indexOf(revNum) && (revision === undefined || revision.meta === undefined || revision.meta.atext === undefined)) {
|
|
|
|
console.error("No atext in key revision " + revNum);
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
//console.log("check revision ", revNum);
|
|
|
|
var cs = revision.changeset;
|
|
|
|
atext = Changeset.applyToAText(cs, atext, pad.pool);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.error("Bad changeset at revision " + revNum + " - " + e.message);
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(~keyRevisions.indexOf(revNum)) {
|
2014-05-14 02:39:25 +00:00
|
|
|
try {
|
2020-03-15 20:33:33 +00:00
|
|
|
expect(revision.meta.atext.text).to.eql(atext.text)
|
|
|
|
expect(revision.meta.atext.attribs).to.eql(atext.attribs)
|
|
|
|
}catch(e) {
|
|
|
|
console.error("Atext in key revision "+revNum+" doesn't match computed one.")
|
|
|
|
console.log(diff.diffChars(atext.text, revision.meta.atext.text).map(function(op) {if(!op.added && !op.removed) op.value = op.value.length; return op}))
|
|
|
|
//console.error(e)
|
|
|
|
//console.log('KeyRev. :', revision.meta.atext)
|
|
|
|
//console.log('Computed:', atext)
|
|
|
|
callback()
|
|
|
|
return
|
2014-05-14 02:39:25 +00:00
|
|
|
}
|
2020-03-15 20:33:33 +00:00
|
|
|
}
|
2014-05-14 02:39:25 +00:00
|
|
|
|
2020-03-15 20:33:33 +00:00
|
|
|
setImmediate(callback)
|
|
|
|
});
|
2014-05-14 02:39:25 +00:00
|
|
|
}, function(er) {
|
|
|
|
if(pad.atext.text == atext.text) console.log('ok')
|
|
|
|
else {
|
|
|
|
console.error('Pad AText doesn\'t match computed one! (Computed ',atext.text.length, ', db', pad.atext.text.length,')')
|
|
|
|
console.log(diff.diffChars(atext.text, pad.atext.text).map(function(op) {if(!op.added && !op.removed) op.value = op.value.length; return op}))
|
|
|
|
}
|
|
|
|
callback(er)
|
|
|
|
});
|
2020-03-15 20:33:33 +00:00
|
|
|
|
2014-05-14 02:39:25 +00:00
|
|
|
process.exit(0);
|
2020-03-15 20:33:33 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.trace(e);
|
|
|
|
process.exit(1);
|
2014-05-14 02:39:25 +00:00
|
|
|
}
|
2020-03-15 20:33:33 +00:00
|
|
|
|
2014-05-14 02:39:25 +00:00
|
|
|
});
|