From 7c3faeb58df87188370632f754adcc0c22d9290d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 26 Apr 2020 02:28:33 -0400 Subject: [PATCH] Don't allow setting data dir to 'storage' or linked attachment base dir If 'storage' (with a zotero.sqlite file above it) is chosen, offer to use parent instead. Addresses #538 --- chrome/content/zotero/xpcom/dataDirectory.js | 49 ++++++++++++++++++++ chrome/locale/en-US/zotero/zotero.properties | 2 + 2 files changed, 51 insertions(+) diff --git a/chrome/content/zotero/xpcom/dataDirectory.js b/chrome/content/zotero/xpcom/dataDirectory.js index 397d573989..e38f6fd85e 100644 --- a/chrome/content/zotero/xpcom/dataDirectory.js +++ b/chrome/content/zotero/xpcom/dataDirectory.js @@ -462,11 +462,35 @@ Zotero.DataDirectory = { let dialogText = ''; let dialogTitle = ''; + // If set to 'storage', offer to use the parent directory + if (await this.isStorageDirectory(file.path)) { + let buttonFlags = ps.STD_YES_NO_BUTTONS; + let parentPath = OS.Path.dirname(file.path); + let index = ps.confirmEx( + null, + Zotero.getString('general.error'), + Zotero.getString('dataDir.cannotBeSetWithAlternative', [parentPath]), + buttonFlags, + null, null, null, null, {} + ); + if (index == 1) { + continue; + } + file = Zotero.File.pathToFile(parentPath) + } + if (file.path == (Zotero.Prefs.get('lastDataDir') || Zotero.Prefs.get('dataDir'))) { Zotero.debug("Data directory hasn't changed"); return false; } + if (this.isLinkedAttachmentBaseDirectory(file.path)) { + let dialogTitle = Zotero.getString('general.error'); + let dialogText = Zotero.getString('dataDir.cannotBeLinkedAttachmentBaseDirectory'); + ps.alert(null, dialogTitle, dialogText); + continue; + } + // In dropbox folder if (Zotero.File.isDropboxDirectory(file.path)) { dialogTitle = Zotero.getString('general.warning'); @@ -648,6 +672,31 @@ Zotero.DataDirectory = { }), + isStorageDirectory: async function (dir) { + if (OS.Path.basename(dir) != 'storage') { + return false; + } + let sqlitePath = OS.Path.join(OS.Path.dirname(dir), 'zotero.sqlite'); + return OS.File.exists(sqlitePath); + }, + + + isLinkedAttachmentBaseDirectory: function (dir) { + var oldPath = Zotero.Prefs.get('baseAttachmentPath'); + if (!oldPath) return false; + + try { + oldPath = OS.Path.normalize(oldPath); + } + catch (e) { + Zotero.logError(e); + return false; + } + + return oldPath === OS.Path.normalize(dir); + }, + + // TODO: Remove after 5.0 upgrades checkForLostLegacy: async function () { var currentDir = this.dir; diff --git a/chrome/locale/en-US/zotero/zotero.properties b/chrome/locale/en-US/zotero/zotero.properties index 454ab06dec..dea7b66ce3 100644 --- a/chrome/locale/en-US/zotero/zotero.properties +++ b/chrome/locale/en-US/zotero/zotero.properties @@ -151,6 +151,8 @@ dataDir.selectDir = Select a Zotero data directory dataDir.selectNewDir = Select a new %S data directory dataDir.changeDataDirectory = Change Data Directory… dataDir.chooseNewDataDirectory = Choose New Data Directory… +dataDir.cannotBeSetWithAlternative = The data directory cannot be set to a “storage” directory. Did you mean %S? +dataDir.cannotBeLinkedAttachmentBaseDirectory = The data directory cannot be set to the same location as the Linked Attachment Base Directory. dataDir.unsafeLocation.selected.dropbox = Choosing a data directory within Dropbox may corrupt your database. dataDir.unsafeLocation.selected.useAnyway = Use this directory anyway? dataDir.unsafeLocation.existing.dropbox = Your Zotero data directory is within Dropbox, which may lead to data corruption.