From 42fa852b76de5a60af4fdfda78c4973c004c1f8c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 24 May 2017 10:03:53 +0100 Subject: [PATCH 1/2] check file exists before running synctex --- .../clsi/app/coffee/CompileManager.coffee | 22 +++++++++++++------ .../unit/coffee/CompileManagerTests.coffee | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/services/clsi/app/coffee/CompileManager.coffee b/services/clsi/app/coffee/CompileManager.coffee index e4cb452fd6..7caa20c0fc 100644 --- a/services/clsi/app/coffee/CompileManager.coffee +++ b/services/clsi/app/coffee/CompileManager.coffee @@ -201,16 +201,24 @@ module.exports = CompileManager = logger.log project_id: project_id, user_id:user_id, page: page, h: h, v:v, stdout: stdout, "synctex pdf output" callback null, CompileManager._parseSynctexFromPdfOutput(stdout, base_dir) + _checkFileExists: (path, callback = (error) ->) -> + fs.stat path, (err, stats) -> + return callback(err) if err? + return callback(new Error("not a file")) if not stats.isFile() + callback() + _runSynctex: (args, callback = (error, stdout) ->) -> bin_path = Path.resolve(__dirname + "/../../bin/synctex") seconds = 1000 - if Settings.clsi?.synctexCommandWrapper? - [bin_path, args] = Settings.clsi?.synctexCommandWrapper bin_path, args - child_process.execFile bin_path, args, timeout: 10 * seconds, (error, stdout, stderr) -> - if error? - logger.err err:error, args:args, "error running synctex" - return callback(error) - callback(null, stdout) + outputFilePath = args[1] + CompileManager._checkFileExists outputFilePath, (err) -> + if Settings.clsi?.synctexCommandWrapper? + [bin_path, args] = Settings.clsi?.synctexCommandWrapper bin_path, args + child_process.execFile bin_path, args, timeout: 10 * seconds, (error, stdout, stderr) -> + if error? + logger.err err:error, args:args, "error running synctex" + return callback(error) + callback(null, stdout) _parseSynctexFromCodeOutput: (output) -> results = [] diff --git a/services/clsi/test/unit/coffee/CompileManagerTests.coffee b/services/clsi/test/unit/coffee/CompileManagerTests.coffee index e3f0705dd3..68629a1c43 100644 --- a/services/clsi/test/unit/coffee/CompileManagerTests.coffee +++ b/services/clsi/test/unit/coffee/CompileManagerTests.coffee @@ -192,6 +192,7 @@ describe "CompileManager", -> describe "syncFromCode", -> beforeEach -> + @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true}) @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "") @CompileManager.syncFromCode @project_id, @user_id, @file_name, @line, @column, @callback @@ -216,6 +217,7 @@ describe "CompileManager", -> describe "syncFromPdf", -> beforeEach -> + @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true}) @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}\t#{@line}\t#{@column}\n", "") @CompileManager.syncFromPdf @project_id, @user_id, @page, @h, @v, @callback From 550979991f713630872b0948b5a3b0d62633569f Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 24 May 2017 10:03:53 +0100 Subject: [PATCH 2/2] check directory exists and bail out on error --- .../clsi/app/coffee/CompileManager.coffee | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/services/clsi/app/coffee/CompileManager.coffee b/services/clsi/app/coffee/CompileManager.coffee index 7caa20c0fc..27aa394e72 100644 --- a/services/clsi/app/coffee/CompileManager.coffee +++ b/services/clsi/app/coffee/CompileManager.coffee @@ -202,16 +202,25 @@ module.exports = CompileManager = callback null, CompileManager._parseSynctexFromPdfOutput(stdout, base_dir) _checkFileExists: (path, callback = (error) ->) -> - fs.stat path, (err, stats) -> - return callback(err) if err? - return callback(new Error("not a file")) if not stats.isFile() - callback() + synctexDir = Path.dirname(path) + synctexFile = Path.join(synctexDir, "output.synctex.gz") + fs.stat synctexDir, (error, stats) -> + if error?.code is 'ENOENT' + return callback(new Error("called synctex with no output directory")) + return callback(error) if error? + fs.stat synctexFile, (error, stats) -> + if error?.code is 'ENOENT' + return callback(new Error("called synctex with no output file")) + return callback(error) if error? + return callback(new Error("not a file")) if not stats?.isFile() + callback() _runSynctex: (args, callback = (error, stdout) ->) -> bin_path = Path.resolve(__dirname + "/../../bin/synctex") seconds = 1000 outputFilePath = args[1] - CompileManager._checkFileExists outputFilePath, (err) -> + CompileManager._checkFileExists outputFilePath, (error) -> + return callback(error) if error? if Settings.clsi?.synctexCommandWrapper? [bin_path, args] = Settings.clsi?.synctexCommandWrapper bin_path, args child_process.execFile bin_path, args, timeout: 10 * seconds, (error, stdout, stderr) ->