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.

This commit is contained in:
gordon%netscape.com 2001-04-26 05:07:28 +00:00
Родитель c392e83131
Коммит 847ca8a31d
2 изменённых файлов: 73 добавлений и 13 удалений

38
netwerk/cache/src/nsCacheService.cpp поставляемый
Просмотреть файл

@ -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;
}

48
netwerk/cache/src/nsDiskCacheDevice.cpp поставляемый
Просмотреть файл

@ -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;