diff --git a/chrome/content/zotero/xpcom/dataDirectory.js b/chrome/content/zotero/xpcom/dataDirectory.js index 214df2c375..4d94b28cd9 100644 --- a/chrome/content/zotero/xpcom/dataDirectory.js +++ b/chrome/content/zotero/xpcom/dataDirectory.js @@ -301,6 +301,39 @@ Zotero.DataDirectory = { }, + /** + * Check that a directory can be created and written to, and show an error if not + * + * @param {String} dir + * @return {Promise} + */ + _checkWriteAccess: async function (dir) { + var existed = false; + try { + existed = await IOUtils.exists(dir); + await Zotero.File.createDirectoryIfMissingAsync(dir); + let testFile = PathUtils.join(dir, '.zotero-write-test'); + await IOUtils.writeUTF8(testFile, ''); + await IOUtils.remove(testFile); + } + catch (e) { + Zotero.logError(e); + Services.prompt.alert( + null, + Zotero.getString('general.accessDenied'), + Zotero.ftl.formatValueSync('data-dir-not-writable', { path: dir }) + + "\n\n" + + Zotero.ftl.formatValueSync( + 'data-dir-check-parent-write-access', + { path: existed ? dir : PathUtils.parent(dir) } + ) + ); + return false; + } + return true; + }, + + /** * @return {String|false} - New path or false if not changed */ @@ -310,6 +343,9 @@ Zotero.DataDirectory = { var newPath; if (useHomeDir) { + if (!await this._checkWriteAccess(this.defaultDir)) { + return false; + } let changed = this.set(this.defaultDir); if (!changed) { return false; @@ -356,6 +392,10 @@ Zotero.DataDirectory = { continue; } + if (!await this._checkWriteAccess(file.path)) { + continue; + } + // In a cloud storage folder (Dropbox, etc.) if (Zotero.File.isCloudStorageFolder(file.path)) { dialogTitle = Zotero.getString('general.warning'); diff --git a/chrome/locale/en-US/zotero/zotero.ftl b/chrome/locale/en-US/zotero/zotero.ftl index 9a3fb4634d..7c8f535e01 100644 --- a/chrome/locale/en-US/zotero/zotero.ftl +++ b/chrome/locale/en-US/zotero/zotero.ftl @@ -1192,6 +1192,7 @@ plugins-blocked-plugin = .message = This plugin has been disabled by { -app-name }. data-dir-unsupported-storage = This can happen if the { -app-name } data directory is in a cloud storage folder (OneDrive, Dropbox, etc.) or on a network share. +data-dir-not-writable = { -app-name } can’t write to { $path }. data-dir-check-parent-write-access = Make sure you have write access to { $path } and that security software isn’t preventing { -app-name } from writing to the disk. login-manager-reset = { -app-name } was unable to read your saved login information, so it has been reset. Please log in again in the { preferences-pane-account } pane of the { -app-name } settings. diff --git a/test/tests/dataDirectoryTest.js b/test/tests/dataDirectoryTest.js index e826074f6e..b2925f00a7 100644 --- a/test/tests/dataDirectoryTest.js +++ b/test/tests/dataDirectoryTest.js @@ -194,6 +194,35 @@ describe("Zotero.DataDirectory", function () { }); }); + describe("#choose()", function () { + it("should show an error and leave the data directory unchanged if the default location isn't writable", async function () { + if (Zotero.isWin) { + this.skip(); + } + + var parentDir = await getTempDirectory(); + var defaultDir = PathUtils.join(parentDir, 'Zotero'); + var defaultDirStub = sinon.stub(Zotero.DataDirectory, 'defaultDir').get(() => defaultDir); + var promptService = Services.prompt; + var alertStub = sinon.stub(); + var confirmStub = sinon.stub(); + Services.prompt = { alert: alertStub, confirmEx: confirmStub }; + await IOUtils.setPermissions(parentDir, 0o555); + try { + assert.isFalse(await Zotero.DataDirectory.choose(false, true)); + assert.isTrue(alertStub.calledOnce); + assert.include(alertStub.firstCall.args[2], defaultDir); + assert.isFalse(confirmStub.called); + assert.isFalse(stubs.setDataDir.called); + } + finally { + Services.prompt = promptService; + defaultDirStub.restore(); + await IOUtils.setPermissions(parentDir, 0o755); + } + }); + }); + describe("#checkForMigration()", function () { let fileMoveStub;