Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corp.
  3. Module Name: urlcache.cxx
  4. Abstract:
  5. Urlcache API enhanced and optimized for internal use by wininet.
  6. Author:
  7. Rajeev Dujari (rajeevd) 10-Apr-97
  8. --*/
  9. #include <wininetp.h>
  10. #include <cache.hxx>
  11. DWORD
  12. UrlCacheRetrieve
  13. (
  14. IN LPSTR pszUrl,
  15. IN BOOL fOffline,
  16. OUT HANDLE* phStream,
  17. OUT CACHE_ENTRY_INFOEX** ppCEI
  18. )
  19. {
  20. BOOL fLocked = FALSE;
  21. HANDLE hStream = NULL;
  22. HANDLE hFile = INVALID_HANDLE_VALUE;
  23. DWORD dwErr;
  24. if (!InitGlobals())
  25. {
  26. INET_ASSERT(FALSE);
  27. return ERROR_WINHTTP_INTERNAL_ERROR;
  28. }
  29. DWORD dwLookupFlags = fOffline? LOOKUP_URL_TRANSLATE
  30. : (LOOKUP_BIT_SPARSE | LOOKUP_URL_NOCREATE);
  31. DWORD cbCEI;
  32. // Find the container and search the index.
  33. dwErr = GlobalUrlContainers->RetrieveUrl(
  34. pszUrl,
  35. (CACHE_ENTRY_INFO **) ppCEI,
  36. &cbCEI,
  37. dwLookupFlags,
  38. RETRIEVE_WITHOUT_CHECKS | RETRIEVE_WITH_ALLOCATION);
  39. if (dwErr != ERROR_SUCCESS)
  40. goto Cleanup;
  41. fLocked = TRUE;
  42. if ((*ppCEI)->CacheEntryType & SPARSE_CACHE_ENTRY)
  43. {
  44. *phStream = NULL;
  45. }
  46. else
  47. {
  48. // Allocate a stream handle.
  49. CACHE_STREAM_CONTEXT_HANDLE* pStream;
  50. LOCK_CACHE();
  51. hStream = HandleMgr.Alloc (sizeof(CACHE_STREAM_CONTEXT_HANDLE));
  52. if (hStream)
  53. {
  54. pStream = (CACHE_STREAM_CONTEXT_HANDLE*) HandleMgr.Map (hStream);
  55. INET_ASSERT (pStream);
  56. }
  57. UNLOCK_CACHE();
  58. if (!hStream)
  59. {
  60. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  61. goto Cleanup;
  62. }
  63. // Open the file.
  64. hFile = CreateFile
  65. (
  66. (*ppCEI)->lpszLocalFileName,
  67. GENERIC_READ,
  68. FILE_SHARE_READ,
  69. NULL,
  70. OPEN_EXISTING,
  71. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  72. NULL
  73. );
  74. if( hFile == INVALID_HANDLE_VALUE )
  75. {
  76. dwErr = GetLastError();
  77. goto Cleanup;
  78. }
  79. DWORD dwFileSize = GetFileSize(hFile, NULL);
  80. if (dwFileSize != (*ppCEI)->dwSizeLow)
  81. {
  82. dwErr = (dwFileSize==0xFFFFFFFF) ? GetLastError() : ERROR_INVALID_DATA;
  83. goto Cleanup;
  84. }
  85. pStream->FileHandle = hFile;
  86. // Copy URL name storage.
  87. pStream->SourceUrlName = NewString(pszUrl);
  88. if( !pStream->SourceUrlName)
  89. {
  90. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  91. goto Cleanup;
  92. }
  93. *phStream = hStream;
  94. }
  95. dwErr = ERROR_SUCCESS;
  96. Cleanup:
  97. if( dwErr != ERROR_SUCCESS )
  98. {
  99. if (*ppCEI)
  100. {
  101. FREE_MEMORY (*ppCEI);
  102. *ppCEI = NULL;
  103. }
  104. if (hStream)
  105. HandleMgr.Free (hStream);
  106. if (hFile)
  107. CloseHandle (hFile);
  108. if (fLocked)
  109. GlobalUrlContainers->UnlockUrl(pszUrl);
  110. }
  111. return dwErr;
  112. }
  113. void UrlCacheFlush (void)
  114. {
  115. DWORD fPersist;
  116. REGISTRY_OBJ regCache (HKEY_CURRENT_USER, OLD_CACHE_KEY);
  117. if ( ERROR_SUCCESS == regCache.GetStatus()
  118. && ERROR_SUCCESS == regCache.GetValue (CACHE_PERSISTENT, &fPersist)
  119. && !fPersist
  120. )
  121. {
  122. FreeUrlCacheSpace (NULL, 100, STICKY_CACHE_ENTRY);
  123. }
  124. }
  125. DWORD UrlCacheCreateFile(LPCSTR szUrl, LPTSTR szExt, LPTSTR szFile, HANDLE *phfHandle, BOOL fCreatePerUser, DWORD dwExpectedLength)
  126. {
  127. if (!InitGlobals())
  128. return ERROR_WINHTTP_INTERNAL_ERROR;
  129. else
  130. return GlobalUrlContainers->CreateUniqueFile(szUrl, dwExpectedLength, szExt, szFile, phfHandle, fCreatePerUser);
  131. }
  132. DWORD UrlCacheCommitFile(AddUrlArg* pArgs)
  133. {
  134. if (!InitGlobals())
  135. return ERROR_WINHTTP_INTERNAL_ERROR;
  136. else
  137. return GlobalUrlContainers->AddUrl(pArgs);
  138. }
  139. DWORD UrlCacheSendNotification(DWORD dwOp)
  140. {
  141. DWORD Error;
  142. // Initialize globals.
  143. if (!InitGlobals())
  144. {
  145. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  146. goto Cleanup;
  147. }
  148. Error = GlobalUrlContainers->SendCacheNotification(dwOp);
  149. Cleanup:
  150. return Error;
  151. }
  152. DWORD UrlCacheAddLeakFile (IN LPCSTR pszFile)
  153. {
  154. if (!InitGlobals())
  155. return ERROR_WINHTTP_INTERNAL_ERROR;
  156. else
  157. return GlobalUrlContainers->AddLeakFile (pszFile);
  158. }