From 489cee6b2411b425be4cb3a6cfeda31b7c2db6c2 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Wed, 27 Mar 2024 13:52:15 -0400 Subject: [PATCH] FileHandlers: Throw error when handler is missing on Windows/Linux Not awaiting exec() has the side effect that we no longer get errors if the executable is missing / isn't actually executable. Extract those checks to prevent problems of the sort fixed in 63f54d3 in the future. --- chrome/content/zotero/xpcom/fileHandlers.js | 27 +++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/xpcom/fileHandlers.js b/chrome/content/zotero/xpcom/fileHandlers.js index f9027233d9..62e023540f 100644 --- a/chrome/content/zotero/xpcom/fileHandlers.js +++ b/chrome/content/zotero/xpcom/fileHandlers.js @@ -278,7 +278,7 @@ Zotero.FileHandlers = { // PDF-XChange: http://help.tracker-software.com/eu/default.aspx?pageid=PDFXView25:command_line_options args.unshift('/A', 'page=' + page); } - Zotero.Utilities.Internal.exec(appPath, args); + await Zotero.FileHandlers._checkAndExecWithoutBlocking(appPath, args); } } ], @@ -293,7 +293,7 @@ Zotero.FileHandlers = { if (location?.position?.value) { args.push('--open-at=' + location.position.value); } - Zotero.Utilities.Internal.exec(appPath, args); + await Zotero.FileHandlers._checkAndExecWithoutBlocking(appPath, args); } } ] @@ -333,7 +333,7 @@ Zotero.FileHandlers = { if (page !== undefined) { args.unshift('-p', page); } - Zotero.Utilities.Internal.exec(appPath, args); + await Zotero.FileHandlers._checkAndExecWithoutBlocking(appPath, args); } } ], @@ -348,7 +348,7 @@ Zotero.FileHandlers = { if (location?.position?.value) { args.push('--open-at=' + location.position.value); } - Zotero.Utilities.Internal.exec(appPath, args); + await Zotero.FileHandlers._checkAndExecWithoutBlocking(appPath, args); } } ] @@ -483,7 +483,24 @@ Zotero.FileHandlers = { return handler.defaultDescription; } return false; - } + }, + + /** + * Check that a command exists and is executable, and run without waiting + * for it to finish + */ + async _checkAndExecWithoutBlocking(command, args) { + // Run the same checks that exec() runs so that we reject if the + // executable doesn't exist or isn't actually executable + if (!await OS.File.exists(command)) { + throw new Error(`${command} not found`); + } + if (!Zotero.File.pathToFile(command).isExecutable()) { + throw new Error(`${command} is not an executable`); + } + // Do not await + Zotero.Utilities.Internal.exec(command, args); + }, }; Zotero.OpenPDF = {