Fix case of data dir not being created for new profile

If there was an existing ~/Zotero directory, another profile was already
pointing to it, and there was a Firefox profile pointing to a custom
data directory or with an embedded 'zotero' directory, the Firefox data
directory would be used instead of creating a new data directory named
after the new profile.

This skips the Firefox logic for new profiles when there's an existing
profile pointing at ~/Zotero and just creates the new data directory.
This commit is contained in:
Dan Stillman 2020-11-17 15:46:44 -05:00
parent e575097e34
commit 7c0cfd9420

View file

@ -180,10 +180,15 @@ Zotero.DataDirectory = {
// New installation of 5.0+ with no data directory specified, so check all the places the data
// could be
else {
Zotero.fxProfileAccessError = false;
let useProfile = false;
let useFirefoxProfile = false;
let useFirefoxProfileCustom = false;
let dataDirNamedAfterProfile = false;
dataDir = this.defaultDir;
Zotero.fxProfileAccessError = false;
// If there's already a profile pointing to the default location, use a different
// data directory named after the profile, as long as one either doesn't exist yet or
// one does and it contains a database
@ -194,6 +199,7 @@ Zotero.DataDirectory = {
if (!(yield OS.File.exists(newDataDir))
|| (yield OS.File.exists(OS.Path.join(newDataDir, dbFilename)))) {
dataDir = newDataDir;
dataDirNamedAfterProfile = true;
}
}
}
@ -213,10 +219,6 @@ Zotero.DataDirectory = {
return dataDir;
}
let useProfile = false;
let useFirefoxProfile = false;
let useFirefoxProfileCustom = false;
// Check for <profile dir>/zotero/zotero.sqlite
let profileSubdirModTime;
try {
@ -236,109 +238,111 @@ Zotero.DataDirectory = {
//
// Check Firefox directory
//
let profilesParent = OS.Path.dirname(Zotero.Profile.getOtherAppProfilesDir());
Zotero.debug("Looking for Firefox profile in " + profilesParent);
// get default profile
var defProfile;
try {
defProfile = yield Zotero.Profile.getDefaultInProfilesDir(profilesParent);
}
catch (e) {
Zotero.debug("An error occurred locating the Firefox profile; "
+ "not attempting to migrate from Zotero for Firefox");
Zotero.logError(e);
Zotero.fxProfileAccessError = true;
}
if (defProfile) {
let profileDir = defProfile[0];
Zotero.debug("Found default profile at " + profileDir);
if (!dataDirNamedAfterProfile) {
let profilesParent = OS.Path.dirname(Zotero.Profile.getOtherAppProfilesDir());
Zotero.debug("Looking for Firefox profile in " + profilesParent);
// Read in prefs
let prefsFile = OS.Path.join(profileDir, "prefs.js");
if (yield OS.File.exists(prefsFile)) {
let prefs = yield Zotero.Profile.readPrefsFromFile(prefsFile);
// get default profile
let defProfile;
try {
defProfile = yield Zotero.Profile.getDefaultInProfilesDir(profilesParent);
}
catch (e) {
Zotero.debug("An error occurred locating the Firefox profile; "
+ "not attempting to migrate from Zotero for Firefox");
Zotero.logError(e);
Zotero.fxProfileAccessError = true;
}
if (defProfile) {
let profileDir = defProfile[0];
Zotero.debug("Found default profile at " + profileDir);
// Check for data dir pref
if (prefs['extensions.zotero.dataDir'] && prefs['extensions.zotero.useDataDir']) {
Zotero.debug(`Found custom dataDir of ${prefs['extensions.zotero.dataDir']}`);
let nsIFile;
try {
nsIFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
nsIFile.persistentDescriptor = prefs['extensions.zotero.dataDir'];
}
catch (e) {
Zotero.logError(e);
if (!useProfile) {
Zotero.debug("Persistent descriptor in extensions.zotero.dataDir "
+ "did not resolve", 1);
throw { name: "NS_ERROR_FILE_NOT_FOUND" };
// Read in prefs
let prefsFile = OS.Path.join(profileDir, "prefs.js");
if (yield OS.File.exists(prefsFile)) {
let prefs = yield Zotero.Profile.readPrefsFromFile(prefsFile);
// Check for data dir pref
if (prefs['extensions.zotero.dataDir'] && prefs['extensions.zotero.useDataDir']) {
Zotero.debug(`Found custom dataDir of ${prefs['extensions.zotero.dataDir']}`);
let nsIFile;
try {
nsIFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
nsIFile.persistentDescriptor = prefs['extensions.zotero.dataDir'];
}
}
try {
let dbFile = OS.Path.join(nsIFile.path, dbFilename);
let mtime = (yield OS.File.stat(dbFile)).lastModificationDate;
Zotero.debug(`Database found at ${dbFile}, last modified ${mtime}`);
// If custom location has a newer DB, use that
if (!useProfile || mtime > profileSubdirModTime) {
dataDir = nsIFile.path;
useFirefoxProfileCustom = true;
useProfile = false;
}
}
catch (e) {
Zotero.logError(e);
// If we have a DB in the Zotero profile and get an error trying to
// access the custom location in Firefox, use the Zotero profile, since
// there's at least some chance it's right. Otherwise, throw an error.
if (!useProfile) {
// The error message normally gets the path from the pref, but
// we got it from the prefs file, so include it here
e.dataDir = nsIFile.path;
throw e;
}
Zotero.fxProfileAccessError = true;
}
}
// If no custom dir specified, check for a subdirectory
else {
try {
let dir = OS.Path.join(profileDir, this.legacyDirName);
let dbFile = OS.Path.join(dir, dbFilename);
let mtime = (yield OS.File.stat(dbFile)).lastModificationDate;
Zotero.debug(`Database found at ${dbFile}, last modified ${mtime}`);
// If newer than Zotero profile directory, use this one
if (!useProfile || mtime > profileSubdirModTime) {
dataDir = dir;
useFirefoxProfile = true;
useProfile = false;
}
}
// Legacy subdirectory doesn't exist or there was a problem accessing it, so
// just fall through to default location
catch (e) {
if (!(e instanceof OS.File.Error && e.becauseNoSuchFile)) {
catch (e) {
Zotero.logError(e);
if (!useProfile) {
Zotero.debug("Persistent descriptor in extensions.zotero.dataDir "
+ "did not resolve", 1);
throw { name: "NS_ERROR_FILE_NOT_FOUND" };
}
}
try {
let dbFile = OS.Path.join(nsIFile.path, dbFilename);
let mtime = (yield OS.File.stat(dbFile)).lastModificationDate;
Zotero.debug(`Database found at ${dbFile}, last modified ${mtime}`);
// If custom location has a newer DB, use that
if (!useProfile || mtime > profileSubdirModTime) {
dataDir = nsIFile.path;
useFirefoxProfileCustom = true;
useProfile = false;
}
}
catch (e) {
Zotero.logError(e);
// If we have a DB in the Zotero profile and get an error trying to
// access the custom location in Firefox, use the Zotero profile, since
// there's at least some chance it's right. Otherwise, throw an error.
if (!useProfile) {
// The error message normally gets the path from the pref, but
// we got it from the prefs file, so include it here
e.dataDir = nsIFile.path;
throw e;
}
Zotero.fxProfileAccessError = true;
}
}
}
// If using data directory from Zotero for Firefox, transfer those prefs, because
// the fact that that DB was more recent and wasn't set in the Zotero profile prefs
// means that they were using Firefox.
if (useFirefoxProfile || useFirefoxProfileCustom) {
for (let key in prefs) {
if (key.substr(0, ZOTERO_CONFIG.PREF_BRANCH.length) === ZOTERO_CONFIG.PREF_BRANCH
&& key !== "extensions.zotero.firstRun2") {
Zotero.Prefs.set(key.substr(ZOTERO_CONFIG.PREF_BRANCH.length), prefs[key]);
// If no custom dir specified, check for a subdirectory
else {
try {
let dir = OS.Path.join(profileDir, this.legacyDirName);
let dbFile = OS.Path.join(dir, dbFilename);
let mtime = (yield OS.File.stat(dbFile)).lastModificationDate;
Zotero.debug(`Database found at ${dbFile}, last modified ${mtime}`);
// If newer than Zotero profile directory, use this one
if (!useProfile || mtime > profileSubdirModTime) {
dataDir = dir;
useFirefoxProfile = true;
useProfile = false;
}
}
// Legacy subdirectory doesn't exist or there was a problem accessing it, so
// just fall through to default location
catch (e) {
if (!(e instanceof OS.File.Error && e.becauseNoSuchFile)) {
Zotero.logError(e);
Zotero.fxProfileAccessError = true;
}
}
}
// If data directory setting was transferred, use that
if (Zotero.Prefs.get('useDataDir')) {
return this.init();
// If using data directory from Zotero for Firefox, transfer those prefs, because
// the fact that that DB was more recent and wasn't set in the Zotero profile prefs
// means that they were using Firefox.
if (useFirefoxProfile || useFirefoxProfileCustom) {
for (let key in prefs) {
if (key.substr(0, ZOTERO_CONFIG.PREF_BRANCH.length) === ZOTERO_CONFIG.PREF_BRANCH
&& key !== "extensions.zotero.firstRun2") {
Zotero.Prefs.set(key.substr(ZOTERO_CONFIG.PREF_BRANCH.length), prefs[key]);
}
}
// If data directory setting was transferred, use that
if (Zotero.Prefs.get('useDataDir')) {
return this.init();
}
}
}
}