checkin for bug 76293. Moves cache directory aside on startup if disk cache is unable to read or delete files. Cache service disables disk cache device if it fails to initialize, and defaults to using memory cache instead. r=beard, sr=darin.

git-svn-id: svn://10.0.0.236/trunk@93110 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
gordon%netscape.com 2001-04-26 05:07:28 +00:00
parent 2c027ee6d4
commit 417e742581
2 changed files with 73 additions and 13 deletions

View File

@ -433,7 +433,13 @@ nsCacheService::CreateDiskDevice()
nsresult rv = mDiskDevice->Init();
if (NS_FAILED(rv)) {
// XXX log error
#if DEBUG
printf("###\n");
printf("### mDiskDevice->Init() failed (0x%.8x)\n", rv);
printf("### - disabling disk cache for this session.\n");
printf("###\n");
#endif
mEnableDiskDevice = PR_FALSE;
delete mDiskDevice;
mDiskDevice = nsnull;
}
@ -732,28 +738,36 @@ nsCacheService::EnsureEntryHasDevice(nsCacheEntry * entry)
{
nsCacheDevice * device = entry->CacheDevice();
if (device) return device;
nsresult rv = NS_OK;
if (entry->IsStreamData() && entry->IsAllowedOnDisk() && mEnableDiskDevice) {
// this is the default
if (!mDiskDevice) {
nsresult rv = CreateDiskDevice();
if (NS_FAILED(rv))
return nsnull;
rv = CreateDiskDevice(); // ignore the error (check for mDiskDevice instead)
}
device = mDiskDevice;
} else if (mEnableMemoryDevice) {
if (mDiskDevice) {
entry->MarkBinding(); // XXX
rv = mDiskDevice->BindEntry(entry);
entry->ClearBinding(); // XXX
if (NS_SUCCEEDED(rv))
device = mDiskDevice;
}
}
// if we can't use mDiskDevice, try mMemoryDevice
if (!device && mEnableMemoryDevice) {
NS_ASSERTION(entry->IsAllowedInMemory(), "oops.. bad flags");
device = mMemoryDevice;
entry->MarkBinding(); // XXX
rv = mMemoryDevice->BindEntry(entry);
entry->ClearBinding(); // XXX
if (NS_SUCCEEDED(rv))
device = mMemoryDevice;
}
if (device == nsnull) return nsnull;
entry->MarkBinding(); // XXX
nsresult rv = device->BindEntry(entry);
entry->ClearBinding(); // XXX
if (NS_FAILED(rv)) return nsnull;
entry->SetCacheDevice(device);
return device;
}

View File

@ -633,6 +633,14 @@ nsDiskCacheDevice::Init()
gFileTransportService = do_GetService("@mozilla.org/network/file-transport-service;1", &rv);
if (NS_FAILED(rv)) return rv;
// delete "Cache.Trash" folder
nsCOMPtr<nsIFile> cacheTrashDir;
rv = mCacheDirectory->Clone(getter_AddRefs(cacheTrashDir));
if (NS_FAILED(rv)) return rv;
rv = cacheTrashDir->SetLeafName("Cache.Trash");
if (NS_FAILED(rv)) return rv;
(void) cacheTrashDir->Delete(PR_TRUE); // ignore errors, we tried...
// XXX read in persistent information about the cache. this can fail, if
// no cache directory has ever existed before.
rv = readCacheMap();
@ -1502,7 +1510,45 @@ nsresult nsDiskCacheDevice::clobberDiskCache()
// recursively delete the disk cache directory.
rv = mCacheDirectory->Delete(PR_TRUE);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv)) {
// try moving it aside
// get parent directory of cache directory
nsCOMPtr<nsIFile> cacheParentDir;
rv = mCacheDirectory->GetParent(getter_AddRefs(cacheParentDir));
if (NS_FAILED(rv)) return rv;
// create "Cache.Trash" directory if necessary
nsCOMPtr<nsIFile> oldCacheDir;
rv = cacheParentDir->Clone(getter_AddRefs(oldCacheDir));
if (NS_FAILED(rv)) return rv;
rv = oldCacheDir->Append("Cache.Trash");
if (NS_FAILED(rv)) return rv;
PRBool exists = PR_FALSE;
rv = oldCacheDir->Exists(&exists);
if (NS_FAILED(rv)) return rv;
if (!exists) {
// create the "Cache.Trash" directory
rv = oldCacheDir->Create(nsIFile::DIRECTORY_TYPE,0777);
if (NS_FAILED(rv)) return rv;
}
// create a directory with unique name to contain existing cache directory
rv = oldCacheDir->Append("Cache");
if (NS_FAILED(rv)) return rv;
rv = oldCacheDir->CreateUnique(nsnull,nsIFile::DIRECTORY_TYPE, 0777);
if (NS_FAILED(rv)) return rv;
// move existing cache directory into profileDir/Cache.Trash/CacheUnique
nsCOMPtr<nsIFile> existingCacheDir;
rv = mCacheDirectory->Clone(getter_AddRefs(existingCacheDir));
if (NS_FAILED(rv)) return rv;
rv = existingCacheDir->MoveTo(oldCacheDir, nsnull);
if (NS_FAILED(rv)) return rv;
}
rv = mCacheDirectory->Create(nsIFile::DIRECTORY_TYPE, 0777);
if (NS_FAILED(rv)) return rv;