#include <camnet.h>
Inheritance diagram for DownloadCache:
Static Public Member Functions | |
static BOOL | SetSize (UINT32 lSize) |
| |
static UINT32 | GetSize () |
static INT32 | GetUsage () |
static BOOL | SetPath (const String_256 &strCachePath) |
| |
static void | Flush () |
| |
static INT32 | RemoveFile (const TCHAR *szFilePath) |
| |
static INT32 | RenameFile (const TCHAR *szOldName, const TCHAR *szNewName) |
| |
static void | InsertFile (const TCHAR *szFilePath) |
| |
static void | Refresh () |
| |
static BOOL | IsCachePath (const TCHAR *szPath) |
| |
Static Public Attributes | |
static class DownloadCache::CacheMonitor | m_CacheMonitor |
Private Member Functions | |
CC_DECLARE_MEMDUMP (DownloadCache) | |
Static Private Member Functions | |
static void | RemoveEntry (CacheEntry &entry) |
static BOOL | Traverse (const String_256 &strPath, BOOL bFlush=FALSE) |
| |
Static Private Attributes | |
static UINT32 | m_lMaxSize = 5000 * 1024 |
static UINT32 | m_lCurrentSize = 0 |
static String_256 | m_strCachePath |
static INT32 | m_nInstanceCount = 0 |
static STL::priority_queue< CacheEntry, STL::vector< CacheEntry >, CacheRemovalAlgorithm > | m_CacheData |
static STL::priority_queue< CacheEntry, STL::vector< CacheEntry >, CacheRemovalAlgorithm > | m_TemporaryCacheData |
Friends | |
class | InternetManager |
class | LibraryGallery |
class | LibClipartSGallery |
Classes | |
class | CacheMonitor |
Definition at line 480 of file camnet.h.
|
|
|
Definition at line 1406 of file camnet.cpp. 01407 { 01408 if (m_strCachePath.IsEmpty()) 01409 return; 01410 Traverse(m_strCachePath, TRUE); 01411 }
|
|
Definition at line 488 of file camnet.h. 00488 {return m_lMaxSize;}
|
|
Definition at line 490 of file camnet.h. 00490 {return m_lMaxSize ? 100 * m_lCurrentSize/m_lMaxSize : 0;}
|
|
Definition at line 1572 of file camnet.cpp. 01573 { 01574 // make sure the path points to a file in the cache - if it doesn't we'll just return 01575 if (!IsCachePath(szPath)) 01576 return; 01577 CacheEntry entry(szPath); 01578 if (!entry.IsValid()) 01579 { 01580 #ifdef _DEBUG 01581 TCHAR szMsg[256]; 01582 TCHAR szError[128]; 01583 switch (errno) 01584 { 01585 case ENOENT: 01586 camStrcpy(szError, "path not found (ENOENT)"); 01587 break; 01588 default: 01589 wsprintf(szError, "errno = %d", errno); 01590 } 01591 wsprintf(szMsg, "Invalid cache entry: %s, %s", szPath, szError); 01592 ERROR3(szMsg); 01593 #endif 01594 return; 01595 } 01596 if (!entry.IsFile()) 01597 return; 01598 TRACEUSER( "adrian", _T("Cache size before inserting %s is %u bytes\n"), entry.szFilePath, m_lCurrentSize); 01599 ::EnterCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01600 m_CacheData.push(entry); 01601 m_lCurrentSize += entry.Size(); 01602 TRACEUSER( "adrian", _T("Cache size after inserting %s is %u bytes\n"), entry.szFilePath, m_lCurrentSize); 01603 TRACEUSER( "adrian", _T("Cache usage is %d percent\n"), GetUsage()); 01604 // if the maximum cache size has been exceeded, discard files until we are within the limit again 01605 while (m_lCurrentSize > m_lMaxSize && m_CacheData.size() && entry != m_CacheData.top()) 01606 { 01607 m_CacheMonitor.IgnoreEvents(1); 01608 if (!_tremove(m_CacheData.top().szFilePath)) 01609 { 01610 m_lCurrentSize -= m_CacheData.top().Size(); 01611 TRACEUSER( "adrian", _T("Discarded %s - new cache size is %u bytes\n"), m_CacheData.top().szFilePath, m_lCurrentSize); 01612 TRACEUSER( "adrian", _T("New cache usage is %d percent\n"), GetUsage()); 01613 } 01614 else 01615 m_CacheMonitor.IgnoreEvents(-1); 01616 m_CacheData.pop(); 01617 } 01618 #ifdef _DEBUG 01619 AssertCacheDataValid(); 01620 #endif 01621 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01622 }
|
|
> BOOL DownloadCache::IsCachePath(const TCHAR* szPath);
Definition at line 1549 of file camnet.cpp. 01550 { 01551 String_256 strPath(szPath); 01552 strPath.toLower(); 01553 String_256 cachePath(m_strCachePath); 01554 cachePath.toLower(); 01555 return (camStrstr(strPath, cachePath)) ? TRUE : FALSE; 01556 }
|
|
Definition at line 1428 of file camnet.cpp. 01429 { 01430 if (m_strCachePath.IsEmpty()) 01431 { 01432 ERROR3("Invalid cache path!"); 01433 return; 01434 } 01435 ::EnterCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01436 __try 01437 { 01438 while (!m_CacheData.empty()) 01439 m_CacheData.pop(); 01440 m_lCurrentSize = 0; 01441 if (!Traverse(m_strCachePath)) 01442 { 01443 ERROR3("Unknown cache error"); 01444 __leave; 01445 } 01446 TRACEUSER( "adrian", _T("Current cache size is %u bytes\n"), m_lCurrentSize); 01447 TRACEUSER( "adrian", _T("Cache usage is %d percent\n"), GetUsage()); 01448 if (!m_lMaxSize) 01449 { 01450 ERROR3("Cache size is 0!"); 01451 __leave; 01452 } 01453 while (m_lCurrentSize > m_lMaxSize && m_CacheData.size()) // discard old files until we are within the cache limit 01454 { 01455 m_CacheMonitor.IgnoreEvents(1); 01456 if (!_tremove(m_CacheData.top().szFilePath)) 01457 { 01458 m_lCurrentSize -= m_CacheData.top().Size(); 01459 TRACEUSER( "adrian", _T("Discarded %s - new cache size is %u bytes\n"), m_CacheData.top().szFilePath, m_lCurrentSize); 01460 TRACEUSER( "adrian", _T("New cache usage is %d percent\n"), GetUsage()); 01461 } 01462 else 01463 m_CacheMonitor.IgnoreEvents(-1); 01464 m_CacheData.pop(); 01465 } 01466 #ifdef _DEBUG 01467 AssertCacheDataValid(); 01468 #endif 01469 } 01470 __finally 01471 { 01472 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01473 } 01474 }
|
|
Definition at line 532 of file camnet.h. 00533 { 00534 while (!m_TemporaryCacheData.empty()) 00535 m_TemporaryCacheData.pop(); 00536 while (!m_CacheData.empty()) 00537 { 00538 if (entry != m_CacheData.top()) 00539 m_TemporaryCacheData.push(m_CacheData.top()); 00540 m_CacheData.pop(); 00541 } 00542 m_CacheData = m_TemporaryCacheData; 00543 }
|
|
Definition at line 1640 of file camnet.cpp. 01641 { 01642 ::EnterCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01643 __try 01644 { 01645 // make sure the path points to a file in the cache - if it doesn't we'll just return _tremove 01646 if (!IsCachePath(szPath)) 01647 return _tremove(szPath); 01648 CacheEntry entry(szPath); 01649 INT32 nRet; 01650 if (!entry.IsValid()) 01651 { 01652 if (errno != ENOENT) 01653 { 01654 ERROR3("Invalid cache entry"); 01655 m_CacheMonitor.IgnoreEvents(1); 01656 nRet = _tremove(szPath); 01657 if (nRet) 01658 m_CacheMonitor.IgnoreEvents(-1); 01659 } 01660 else 01661 nRet = -1; 01662 return nRet; 01663 01664 } 01665 if (!entry.IsFile()) 01666 { 01667 errno = ENOENT; 01668 return -1; 01669 } 01670 m_CacheMonitor.IgnoreEvents(1); 01671 nRet =_tremove(szPath); 01672 if (!nRet) 01673 { 01674 RemoveEntry(entry); 01675 m_lCurrentSize -= entry.Size(); 01676 } 01677 else 01678 { 01679 m_CacheMonitor.IgnoreEvents(-1); 01680 #ifdef _DEBUG 01681 TCHAR szMsg[256]; 01682 TCHAR szError[128]; 01683 switch (errno) 01684 { 01685 case EACCES: 01686 camStrcpy(szError, "access denied (EACCES)"); 01687 break; 01688 case ENOENT: 01689 camStrcpy(szError, "path not found (ENOENT)"); 01690 break; 01691 default: 01692 wsprintf(szError, "errno = %d", errno); 01693 } 01694 wsprintf(szMsg, "Failed to delete %s , %s", szPath, szError); 01695 ERROR3(szMsg); 01696 #endif 01697 } 01698 #ifdef _DEBUG 01699 AssertCacheDataValid(); 01700 #endif 01701 return nRet; 01702 } 01703 __finally 01704 { 01705 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01706 } 01707 01708 return 0; 01709 }
|
|
Definition at line 1727 of file camnet.cpp. 01728 { 01729 String_256 strOldName(szOldName), strNewName(szNewName); 01730 BOOL bIsCachePath = IsCachePath(szOldName); 01731 if (bIsCachePath) 01732 m_CacheMonitor.IgnoreEvents(1); 01733 INT32 nRetValue = _trename(strOldName, strNewName); 01734 if (nRetValue && bIsCachePath) 01735 m_CacheMonitor.IgnoreEvents(-1); 01736 // make sure the path points to a file in the cache - if it doesn't we'll just return nRetValue 01737 if (!bIsCachePath) 01738 return nRetValue; 01739 ::EnterCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01740 if (!nRetValue) 01741 { 01742 CacheEntry entry(strNewName); 01743 if (!entry.IsValid()) 01744 { 01745 ERROR3("Invalid cache entry"); 01746 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01747 return nRetValue; 01748 } 01749 if (!entry.IsFile()) 01750 { 01751 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01752 return nRetValue; 01753 } 01754 m_CacheData.push(entry); // push the new file entry on the cache data stack 01755 camStrcpy(entry.szFilePath, strOldName); 01756 RemoveEntry(entry); // remove old entry 01757 } 01758 #ifdef _DEBUG 01759 else 01760 { 01761 TCHAR szMsg[256]; 01762 TCHAR szError[128]; 01763 switch (errno) 01764 { 01765 case EACCES: 01766 camStrcpy(szError, "file already exists or could not be created (EACCES)"); 01767 break; 01768 case ENOENT: 01769 camStrcpy(szError, "path not found (ENOENT)"); 01770 break; 01771 default: 01772 wsprintf(szError, "errno = %d", errno); 01773 } 01774 wsprintf(szMsg, "Failed to rename %s to %s, %s", strOldName, strNewName, szError); 01775 ERROR3(szMsg); 01776 } 01777 AssertCacheDataValid(); 01778 #endif 01779 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01780 return nRetValue; 01781 }
|
|
Definition at line 1273 of file camnet.cpp. 01274 { 01275 m_strCachePath = rCachePath; 01276 // if the directory does not exist, we create it 01277 if (_taccess(m_strCachePath, 0) == -1) 01278 { 01279 PathNameEx cachePath(m_strCachePath); 01280 if (!cachePath.CreateLocation()) 01281 return FALSE; 01282 else 01283 { 01284 while (!m_CacheData.empty()) 01285 m_CacheData.pop(); 01286 m_lCurrentSize = 0; // the cache is currently empty 01287 return TRUE; 01288 } 01289 } 01290 else 01291 { 01292 Refresh(); // update cache data 01293 return TRUE; 01294 } 01295 #ifdef _DEBUG 01296 AssertCacheDataValid(); 01297 #endif 01298 }
|
|
Definition at line 1313 of file camnet.cpp. 01314 { 01315 ERROR2IF(m_strCachePath.IsEmpty(), FALSE, "Invalid cache path"); 01316 01317 // *********************************************************************** 01318 // Matt - 15/02/2001 01319 // Modified so that large hard disks don't report incorrect disk space... 01320 // *********************************************************************** 01321 01322 TCHAR szDrive[_MAX_DRIVE + 1]; 01323 _tsplitpath(m_strCachePath, szDrive, NULL, NULL, NULL); 01324 camStrcat(szDrive, _T("\\")); 01325 01326 // Get pointer to the GetDiskFreeSpaceEx function if it exists on this platform... 01327 BOOL fResult = FALSE; 01328 P_GDFSE pGetDiskFreeSpaceEx = NULL; 01329 pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle ("kernel32.dll"), "GetDiskFreeSpaceExA"); 01330 01331 unsigned __int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; 01332 DWORD dwSectorsPerCluster, dwBytesPerSector, dwNumberOfFreeClusters, dwTotalNumberOfClusters; 01333 01334 if (pGetDiskFreeSpaceEx) 01335 { 01336 fResult = pGetDiskFreeSpaceEx(szDrive, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes); 01337 01338 // Test to see if our requested cache size is larger than the current available space !!! 01339 if (((__int64)lSize >= i64FreeBytesToCaller) || (lSize < 500 * SIZEOFKILOBYTE)) { return FALSE; } 01340 } 01341 else 01342 { 01343 fResult = GetDiskFreeSpace (szDrive, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters); 01344 if (fResult) 01345 { 01346 // Force 64bit maths so that drives over 4Gb work properly !!! 01347 i64FreeBytes = (__int64)dwNumberOfFreeClusters * dwSectorsPerCluster * dwBytesPerSector; 01348 01349 // Test to see if our requested cache size is larger than the current available space !!! 01350 if (((__int64)lSize >= i64FreeBytes) || (lSize < 500 * SIZEOFKILOBYTE)) { return FALSE; } 01351 } 01352 } 01353 01354 // Just incase we failed to call either of the above functions... 01355 if (!fResult) { ERROR3("Can't GetDiskFreeSpace()!"); return FALSE; } 01356 01357 // Set the preference value to the new value 01358 InternetManager::g_CacheSize = lSize; 01359 /* 01360 // This code is an abortion and unmaintainable. Why not use the preference system? 01361 HKEY arhKeys[6]; 01362 DWORD dwDisposition; 01363 memset(arhKeys, 0, sizeof(arhKeys)); 01364 String_256 strProgramKey = GetProgramNameRegistryKey(); 01365 BOOL bResult = ((RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &arhKeys[0]) == ERROR_SUCCESS) && 01366 (RegCreateKeyEx(arhKeys[0], _tcstok(strProgramKey, _T("\\")), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &arhKeys[1], &dwDisposition) == ERROR_SUCCESS) && 01367 (RegCreateKeyEx(arhKeys[1], _tcstok(NULL, _T("\\")), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &arhKeys[2], &dwDisposition) == ERROR_SUCCESS) && 01368 (RegCreateKeyEx(arhKeys[2], _tcstok(NULL, _T("\\")), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &arhKeys[3], &dwDisposition) == ERROR_SUCCESS) && 01369 (RegCreateKeyEx(arhKeys[3], _T("Options"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &arhKeys[4], &dwDisposition) == ERROR_SUCCESS) && 01370 (RegCreateKeyEx(arhKeys[4], _T("Internet"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &arhKeys[5], &dwDisposition) == ERROR_SUCCESS) && 01371 (RegSetValueEx(arhKeys[5], _T("Cache Size"), NULL, REG_DWORD, (LPBYTE) &lSize, sizeof(lSize)) == ERROR_SUCCESS)); 01372 for (INT32 i = 5; i >= 0; i--) RegCloseKey(arhKeys[i]); 01373 ERROR3IF(!bResult, "Failed to save changes to registry"); 01374 */ 01375 01376 m_lMaxSize = lSize; 01377 while (m_lCurrentSize > m_lMaxSize && m_CacheData.size()) // discard old files until we are within the cache limit 01378 { 01379 if (!_tremove(m_CacheData.top().szFilePath)) 01380 { 01381 m_lCurrentSize -= m_CacheData.top().Size(); 01382 TRACEUSER( "adrian", _T("Discarded %s - new cache size is %u bytes\n"), m_CacheData.top().szFilePath, m_lCurrentSize); 01383 TRACEUSER( "adrian", _T("New cache usage is %d percent\n"), GetUsage()); 01384 } 01385 m_CacheData.pop(); 01386 } 01387 #ifdef _DEBUG 01388 AssertCacheDataValid(); 01389 #endif 01390 return TRUE; 01391 }
|
|
Definition at line 1488 of file camnet.cpp. 01489 { 01490 ::EnterCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01491 _finddata_t findData; 01492 String_256 strSearchPattern(strDirPath); 01493 if (strSearchPattern[strSearchPattern.Length() -1] == _T('\\')) 01494 strSearchPattern += _T('*'); 01495 else 01496 strSearchPattern += _T("\\*"); // add wildcard 01497 INT32 hSearch = _tfindfirst(strSearchPattern, &findData); 01498 if (hSearch == -1) 01499 return TRUE; // the directory is empty 01500 do 01501 { 01502 if (!(camStrcmp(findData.name, _T(".")) && camStrcmp(findData.name, _T("..")))) 01503 continue; // skip this directory (.) or its parent (..) 01504 String_256 strFilePath(strDirPath); 01505 if (strFilePath[strFilePath.Length() -1] != _T('\\')) 01506 strFilePath += _T('\\'); 01507 strFilePath += findData.name; 01508 strFilePath.toLower(); 01509 CacheEntry entry(strFilePath); 01510 if (!entry.IsValid()) 01511 { 01512 ERROR3("Invalid cache entry"); 01513 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01514 return FALSE; 01515 } 01516 BOOL bIsFolder = entry.IsFolder(); 01517 BOOL bIsFile = entry.IsFile(); 01518 if (entry.IsFolder()) 01519 Traverse(strFilePath, bFlush); 01520 else if (entry.IsFile()) 01521 { 01522 if (!bFlush) 01523 { 01524 m_CacheData.push(entry); 01525 m_lCurrentSize += entry.Size(); 01526 } 01527 else if (!camStrstr(strFilePath, _T(".txt"))) 01528 RemoveFile(strFilePath); 01529 } 01530 } 01531 while (_tfindnext(hSearch, &findData) == 0); 01532 _findclose(hSearch); 01533 ::LeaveCriticalSection(&m_CacheMonitor.m_CacheDataCriticalSection); 01534 return TRUE; 01535 }
|
|
|
|
|
|
|
|
|
|
Definition at line 1254 of file camnet.cpp. |
|
|
|
|
|
|
|
|
|
|