From 51c70eb9253a17fc6b732a61e573d86d45729e22 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 20 Jul 2023 05:34:09 -0400 Subject: [PATCH] Use system temp directory by default Might help for something like https://forums.zotero.org/discussion/106337/zotero-7-sync-error --- chrome/content/zotero/xpcom/zotero.js | 58 ++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 15818cf6c8..96d2f8e743 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -32,6 +32,10 @@ Components.classes["@mozilla.org/net/osfileconstantsservice;1"] .getService(Components.interfaces.nsIOSFileConstantsService) .init(); +XPCOMUtils.defineLazyModuleGetters(globalThis, { + AsyncShutdown: "resource://gre/modules/AsyncShutdown.jsm", +}); + Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); /* @@ -872,9 +876,6 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); } yield Promise.all(shutdownPromises); - // remove temp directory - yield Zotero.removeTempDirectory(); - if (Zotero.DB) { // close DB yield Zotero.DB.closeDatabase(true) @@ -911,13 +912,52 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); this.getTranslatorsDirectory = function () { return Zotero.File.pathToFile(Zotero.DataDirectory.getSubdirectory('translators', true)); } - - this.getTempDirectory = function () { - return Zotero.File.pathToFile(Zotero.DataDirectory.getSubdirectory('tmp', true)); - } - this.removeTempDirectory = function () { - return Zotero.DataDirectory.removeSubdirectory('tmp'); + var _tmpDir; + this.getTempDirectory = function () { + if (_tmpDir) { + return Zotero.File.pathToFile(_tmpDir); + } + var dir; + try { + dir = Services.dirsvc.get("TmpD", Ci.nsIFile); + let relDir; + if (Zotero.isWin) { + relDir = 'Zotero'; + } + else if (Zotero.isMac) { + relDir = 'org.zotero.zotero'; + } + else { + relDir = 'zotero'; + } + dir.append(relDir); + Zotero.File.createDirectoryIfMissing(dir); + } + // If we can't use the system temp dir, fall back to 'tmp' in the data dir + catch (e) { + Zotero.warn(e); + dir = Zotero.File.pathToFile(Zotero.DataDirectory.getSubdirectory('tmp', true)); + } + + AsyncShutdown.profileBeforeChange.addBlocker( + "Zotero: Removing temp directory", + () => this.removeTempDirectory() + ); + + _tmpDir = dir.path; + return dir; + }; + + this.removeTempDirectory = async function () { + if (!_tmpDir) return; + try { + Zotero.debug("Removing " + _tmpDir); + return IOUtils.remove(_tmpDir, { recursive: true }); + } + catch (e) { + Zotero.logError(e); + } }