diff --git a/app.js b/app.js index f6d5632b..f4bcd2e6 100755 --- a/app.js +++ b/app.js @@ -29,7 +29,7 @@ var nopt = require('nopt'), os = require('os'), props = require('./lib/properties'), compileHandler = require('./lib/compile').compileHandler, - diffHandler = require('./lib/diff').diffHandler, + buildDiffHandler = require('./lib/diff').buildDiffHandler, express = require('express'), child_process = require('child_process'), path = require('path'), @@ -76,6 +76,11 @@ props.initialize(rootDir + '/config', propHierarchy); // in hidden object props.properties var gccProps = props.propsFor("gcc-explorer"); +// Read from gccexplorer's config the wdiff configuration +// that will be used to configure lib/diff.js +var wdiffConfig = {wdiffExe: gccProps('wdiff_exe'," /usr/bin/wdiff"), + wdiffTmpDir: gccProps('wdiff_tmp_dir',"/tmp")}; + // Instantiate a function to access records concerning the chosen language // in hidden object props.properties var compilerPropsFunc = props.propsFor(language.toLowerCase()); @@ -415,7 +420,8 @@ findCompilers().then(function (compilers) { bodyParser = require('body-parser'), logger = require('morgan'), compression = require('compression'), - restreamer = require('./lib/restreamer'); + restreamer = require('./lib/restreamer'), + diffHandler = buildDiffHandler(wdiffConfig); webServer .use(logger('combined')) diff --git a/etc/config/gcc-explorer.defaults.properties b/etc/config/gcc-explorer.defaults.properties index 1e362363..cd03738d 100644 --- a/etc/config/gcc-explorer.defaults.properties +++ b/etc/config/gcc-explorer.defaults.properties @@ -10,3 +10,6 @@ optionsWhitelistRe=.* optionsBlacklistRe=^(-W[alp],)?((-wrapper|-fplugin.*|-specs|-load|-plugin|(@.*)|-I|-i)(=.*)?|--)$ allowedShortUrlHostRe=^([a-z]+\.)?(xania|godbolt)\.org$ googleShortLinkRewrite=^https?://goo.gl/(.*)$|https://godbolt.org/g/$1 + +wdiff_exe=../external/wdiff-1.2.2/src/wdiff +wdiff_tmp_dir=/tmp diff --git a/lib/diff.js b/lib/diff.js index 1f320324..8d109751 100644 --- a/lib/diff.js +++ b/lib/diff.js @@ -137,62 +137,65 @@ function cleanAndGetIndexes(text) { return {text: finalText, zones: zones}; } -function diffHandler(req, res, next) { - // console.log("req: "+JSON.stringify(JSON.decycle(req))); - // console.log(""); - // console.log("res: "+JSON.stringify(JSON.decycle(res))); - // console.log(""); - // console.log("next: "+JSON.stringify(JSON.decycle(next))); - var before = req.body.before; - var after = req.body.after; - if (before === undefined) { - console.log("Warning : Bad request : wrong \"before\""); - //return next(new Error("Bad request : wrong \"before\"")); - } - if (after === undefined) { - console.log("Warning : Bad request : wrong \"after\""); - //return next(new Error("Bad request : wrong \"after\"")); - } - //console.log("Before: "); - //console.log(before); - //console.log("After: "); - //console.log(after); - // TODO : make async the two creation of temp files + call to wdiff - var before_temp_file = "/tmp/gcc-explorer-before" - fs.writeFileSync(before_temp_file, before); - - var after_temp_file = "/tmp/gcc-explorer-after" - fs.writeFileSync(after_temp_file, after); - - var wdiff_exe = "/work1/gdevillers/compiler-explorer/external/wdiff-1.2.2/src/wdiff"; - var maxSize = 100000; - var wdiffResult = child_process.spawnSync( - "/work1/gdevillers/compiler-explorer/external/wdiff-1.2.2/src/wdiff", - ["/tmp/gcc-explorer-before", "/tmp/gcc-explorer-after"], - {maxBuffer: 100000}); - - res.set('Content-Type', 'application/json'); - var cleaned = cleanAndGetIndexes(wdiffResult.stdout.toString()); - if (cleaned == null) { - res.end(JSON.stringify({ - computedDiff: "Failed to clean the diff", - zones: null - })); - } else { - res.end(JSON.stringify({ - computedDiff: cleaned.text, - //computedDiff: cleaned.text+ - // "\n//// for reference: ////\n"+ // for debug only - // wdiffResult.stdout.toString(), - zones: cleaned.zones - //computedDiff: "aaa\nbbb[-ccc-]\n[-ddd-]eee\n[-fff-]\nsafe" - //computedDiff: "[-aaa-]" - //computedDiff: "aaa" - //computedDiff: "aa[--]a" - //computedDiff: "aa[-b-]" - })); +function buildDiffHandler(config) { + return function diffHandler(req, res, next) { + // console.log("req: "+JSON.stringify(JSON.decycle(req))); + // console.log(""); + // console.log("res: "+JSON.stringify(JSON.decycle(res))); + // console.log(""); + // console.log("next: "+JSON.stringify(JSON.decycle(next))); + var before = req.body.before; + var after = req.body.after; + if (before === undefined) { + console.log("Warning : Bad request : wrong \"before\""); + //return next(new Error("Bad request : wrong \"before\"")); + } + if (after === undefined) { + console.log("Warning : Bad request : wrong \"after\""); + //return next(new Error("Bad request : wrong \"after\"")); + } + //console.log("Before: "); + //console.log(before); + //console.log("After: "); + //console.log(after); + + // TODO : make async the two creation of temp files + call to wdiff ? + + var wdiffExe = config.wdiffExe; + + var tempBeforePath = config.wdiffTmpDir + "/gcc-explorer-wdiff-before"; + fs.writeFileSync(tempBeforePath, before); + + var tempAfterPath = config.wdiffTmpDir + "/gcc-explorer-wdiff-after"; + fs.writeFileSync(tempAfterPath, after); + + // TODO : get rid of this buffer or calculate it... + var maxSize = 100000; + var wdiffResult = child_process.spawnSync( + wdiffExe, + [tempBeforePath, tempAfterPath], + {maxBuffer: 100000}); + + res.set('Content-Type', 'application/json'); + var cleaned = cleanAndGetIndexes(wdiffResult.stdout.toString()); + if (cleaned == null) { + res.end(JSON.stringify({ + computedDiff: "Failed to clean the diff", + zones: null + })); + } else { + res.end(JSON.stringify({ + computedDiff: cleaned.text, + // for debug only: + //computedDiff: cleaned.text+ + // "\n//// for reference: ////\n"+ + // wdiffResult.stdout.toString(), + zones: cleaned.zones + })); + } } } + module.exports = { - diffHandler: diffHandler, + buildDiffHandler: buildDiffHandler, };