Bug 330842: "ASSERT: Can't serialize: file doesn't exist!" at startup, r+a=mconnor

This commit is contained in:
gavin%gavinsharp.com 2006-04-06 23:11:36 +00:00
Родитель dbcbdf34e7
Коммит a8d71dabdc
1 изменённых файлов: 55 добавлений и 38 удалений

Просмотреть файл

@ -1712,15 +1712,16 @@ SearchService.prototype = {
// Check whether aDir is the user profile dir
var isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR));
var files = aDir.directoryEntries;
var files = aDir.directoryEntries
.QueryInterface(Ci.nsIDirectoryEnumerator);
var ios = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
while (files.hasMoreElements()) {
var file = files.getNext().QueryInterface(Ci.nsILocalFile);
var file = files.nextFile;
// Ignore hidden and empty files, and directories
if (!file.isFile() || file.isHidden() || file.fileSize == 0)
if (file.fileSize == 0 || !file.isFile() || file.isHidden())
continue;
var fileURL = ios.newFileURI(file).QueryInterface(Ci.nsIURL);
@ -1741,7 +1742,7 @@ SearchService.prototype = {
case SHERLOCK_FILE_EXT:
addedEngine = new Engine(file, Ci.nsISearchEngine.DATA_TEXT,
!isWritable);
if (isInProfile && file.isWritable()) {
if (isWritable) {
// Convert engines in the profile to the new format
try {
this._convertSherlockFile(addedEngine, fileURL.fileBaseName);
@ -1749,12 +1750,16 @@ SearchService.prototype = {
// If the engine couldn't be converted, mark it as read-only
addedEngine._readOnly = true;
}
} else {
// Try to find the engine's icon
}
// If the engine still doesn't have an icon, then either the
// conversion failed
if (!addedEngine._iconURI) {
var icon = this._findSherlockIcon(file, fileURL.fileBaseName);
if (icon)
addedEngine._iconURI = ios.newFileURI(icon);
}
this._addEngineToStore(addedEngine);
break;
default:
@ -1782,6 +1787,46 @@ SearchService.prototype = {
* @see nsIURL::fileBaseName
*/
_convertSherlockFile: function SRCH_SVC_convertSherlock(aEngine, aBaseName) {
var oldSherlockFile = aEngine._file;
// Back up the old file
try {
var backupDir = oldSherlockFile.parent;
backupDir.append("searchplugins-backup");
if (backupDir.exists() && !backupDir.isDirectory()) {
// Can't create the backup directory, just bail
throw Cr.NS_ERROR_FAILURE;
}
if (!backupDir.exists())
backupDir.create(Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
oldSherlockFile.copyTo(backupDir, null);
} catch (ex) {
// Just bail. Engines that can't be backed up won't be converted, but
// engines that aren't converted are loaded as readonly.
LOG("_convertSherlockFile: Couldn't back up " + oldSherlockFile.path);
throw Cr.NS_ERROR_FAILURE;
}
// Rename the file, but don't clobber existing files
var newXMLFile = oldSherlockFile.parent.clone();
newXMLFile.append(aBaseName + "." + XML_FILE_EXT);
if (newXMLFile.exists()) {
// There is an existing file with this name, create a unique file
newXMLFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
}
// Rename the .src file to .xml
oldSherlockFile.moveTo(null, newXMLFile.leafName);
aEngine._file = newXMLFile;
// Write the converted engine to disk
aEngine._serializeToFile();
// See if it has a corresponding icon
try {
var icon = this._findSherlockIcon(aEngine._file, aBaseName);
@ -1807,41 +1852,13 @@ SearchService.prototype = {
LOG("_importSherlockEngine: Set sherlock iconURI to: \"" +
aEngine._iconURL + "\"");
// Delete the icon
// Write the engine to disk to save changes
aEngine._serializeToFile();
// Delete the icon now that we're sure everything's been saved
icon.remove(false);
}
} catch (ex) { LOG("_convertSherlockFile: Error setting icon: " + ex); }
var oldFile = aEngine._file;
// Back up the old file
try {
var backupDir = oldFile.parent;
backupDir.append("searchplugins-backup");
if (!backupDir.exists())
backupDir.create(Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
oldFile.copyTo(backupDir, null);
} catch (ex) {
// Just bail. Engines that can't be backed up won't be converted, but
// engines that aren't converted are loaded as readonly.
LOG("_convertSherlockFile: Couldn't back up " + oldFile.path + ".");
return;
}
// Rename the file, but don't clobber existing files
var newFile = oldFile.parent.clone();
newFile.append(aBaseName + "." + XML_FILE_EXT);
if (!newFile.exists())
oldFile.moveTo(null, aBaseName + "." + XML_FILE_EXT);
else {
newFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
oldFile.moveTo(null, newFile.leafName);
}
// Write the converted engine to disk
aEngine._serializeToFile();
},
/**