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.

375 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. fndquota.c
  5. Abstract:
  6. Test program to fill you cache to just under your quota.
  7. Author:
  8. Vince Roggero (vincentr) 27-Jun-1997
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <wininet.h>
  20. #include <winineti.h>
  21. //=================================================================================
  22. #define MAX_COMMAND_ARGS 32
  23. #define DEFAULT_BUFFER_SIZE 1024 // 1k
  24. #define URL_NAME_SIZE (16 + 1)
  25. #define CACHE_ENTRY_BUFFER_SIZE (1024 * 5)
  26. #define CACHE_DATA_BUFFER_SIZE 1024
  27. #define CACHE_HEADER_INFO_SIZE 2048
  28. #define CACHE_HEADER_INFO_SIZE_NORMAL_MAX 256
  29. #define CACHE_HEADER_INFO_SIZE_BIG_MAX 512
  30. //=================================================================================
  31. BYTE GlobalCacheEntryInfoBuffer[CACHE_ENTRY_BUFFER_SIZE];
  32. BYTE GlobalCacheDataBuffer[CACHE_DATA_BUFFER_SIZE];
  33. BYTE GlobalCacheHeaderInfo[CACHE_HEADER_INFO_SIZE];
  34. DWORD g_dwFileSize = 16384;
  35. DWORD g_dwNumEntries = 1;
  36. DWORD g_dwInitEntries = 0;
  37. BOOL g_bVerbose = FALSE;
  38. //=================================================================================
  39. DWORD SetFileSizeByName(LPCTSTR FileName, DWORD FileSize)
  40. /*++
  41. Routine Description:
  42. Set the size of the specified file.
  43. Arguments:
  44. FileName : full path name of the file whose size is asked for.
  45. FileSize : new size of the file.
  46. Return Value:
  47. Windows Error Code.
  48. --*/
  49. {
  50. HANDLE FileHandle;
  51. DWORD FilePointer;
  52. DWORD Error = ERROR_SUCCESS;
  53. DWORD dwFlags = 0;
  54. DWORD dwCreate;
  55. BOOL BoolError;
  56. //
  57. // get the size of the file being cached.
  58. //
  59. dwFlags = 0;
  60. dwCreate = CREATE_ALWAYS;
  61. FileHandle = CreateFile(
  62. FileName,
  63. GENERIC_WRITE,
  64. 0, //FILE_SHARE_READ | FILE_SHARE_WRITE,
  65. NULL,
  66. dwCreate,
  67. FILE_ATTRIBUTE_NORMAL | dwFlags,
  68. NULL );
  69. if( FileHandle == INVALID_HANDLE_VALUE ) {
  70. return( GetLastError() );
  71. }
  72. FilePointer = SetFilePointer(FileHandle, FileSize, NULL, FILE_BEGIN );
  73. if( FilePointer != 0xFFFFFFFF )
  74. {
  75. if(!SetEndOfFile( FileHandle ))
  76. Error = GetLastError();
  77. }
  78. else
  79. {
  80. Error = GetLastError();
  81. }
  82. CloseHandle( FileHandle );
  83. return( Error );
  84. }
  85. //=================================================================================
  86. FILETIME
  87. GetGmtTime(
  88. VOID
  89. )
  90. {
  91. SYSTEMTIME SystemTime;
  92. FILETIME Time;
  93. GetSystemTime( &SystemTime );
  94. SystemTimeToFileTime( &SystemTime, &Time );
  95. return( Time );
  96. }
  97. //=================================================================================
  98. DWORD EnumUrlCacheEntries(DWORD *pdwTotal)
  99. {
  100. DWORD BufferSize, dwSmall=0, dwLarge=0;
  101. HANDLE EnumHandle;
  102. DWORD Index = 1, len;
  103. DWORD dwTotal = 0;
  104. LPINTERNET_CACHE_ENTRY_INFO lpCEI = (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer;
  105. BOOL bRC;
  106. char Str[256];
  107. //
  108. // start enum.
  109. //
  110. memset(GlobalCacheEntryInfoBuffer, 0, CACHE_ENTRY_BUFFER_SIZE);
  111. BufferSize = CACHE_ENTRY_BUFFER_SIZE;
  112. EnumHandle = FindFirstUrlCacheEntryEx (
  113. NULL, // search pattern
  114. 0, // flags
  115. 0xffffffff, // filter
  116. 0, // groupid
  117. (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer,
  118. &BufferSize,
  119. NULL,
  120. NULL,
  121. NULL
  122. );
  123. if( EnumHandle == NULL )
  124. {
  125. return( GetLastError() );
  126. }
  127. ++dwTotal;
  128. //
  129. // get more entries.
  130. //
  131. for ( ;; )
  132. {
  133. memset(GlobalCacheEntryInfoBuffer, 0, CACHE_ENTRY_BUFFER_SIZE);
  134. BufferSize = CACHE_ENTRY_BUFFER_SIZE;
  135. if( !FindNextUrlCacheEntryEx(
  136. EnumHandle,
  137. (LPINTERNET_CACHE_ENTRY_INFO)GlobalCacheEntryInfoBuffer,
  138. &BufferSize, NULL, NULL, NULL))
  139. {
  140. DWORD Error;
  141. Error = GetLastError();
  142. if( Error != ERROR_NO_MORE_ITEMS ) {
  143. return( Error );
  144. }
  145. break;
  146. }
  147. ++dwTotal;
  148. }
  149. *pdwTotal = dwTotal;
  150. FindCloseUrlCache(EnumHandle);
  151. return(ERROR_SUCCESS);
  152. }
  153. //=================================================================================
  154. DWORD ProcessSimulateCache(DWORD dwNumUrls)
  155. {
  156. static DWORD dwUrlNum = 0;
  157. DWORD Error;
  158. DWORD i, j;
  159. CHAR UrlName[ URL_NAME_SIZE ];
  160. TCHAR LocalFileName[MAX_PATH];
  161. DWORD FileSize;
  162. LONGLONG ExpireTime;
  163. FILETIME LastModTime;
  164. CHAR TimeBuffer[MAX_PATH];
  165. DWORD UrlLife;
  166. DWORD BufferSize;
  167. DWORD CacheHeaderInfoSize;
  168. for( i = dwUrlNum; i < (dwUrlNum + dwNumUrls); i++ )
  169. {
  170. //
  171. // make a new url name.
  172. //
  173. sprintf(UrlName, "http://serv/URL%ld", i);
  174. //
  175. // create url file.
  176. //
  177. if( !CreateUrlCacheEntry(UrlName, 0, "tmp", LocalFileName, 0 ) )
  178. {
  179. Error = GetLastError();
  180. printf( "CreateUrlFile call failed, %ld.\n", Error );
  181. return( Error );
  182. }
  183. //
  184. // set file size.
  185. //
  186. Error = SetFileSizeByName(LocalFileName, g_dwFileSize);
  187. if( Error != ERROR_SUCCESS )
  188. {
  189. printf( "SetFileSizeByName call failed, %ld.\n", Error );
  190. return( Error );
  191. }
  192. UrlLife = rand() % 48;
  193. ExpireTime = (LONGLONG)UrlLife * (LONGLONG)36000000000;
  194. // in 100 of nano seconds.
  195. LastModTime = GetGmtTime();
  196. ExpireTime += *((LONGLONG *)&LastModTime);
  197. CacheHeaderInfoSize = CACHE_HEADER_INFO_SIZE_NORMAL_MAX;
  198. //
  199. // cache this file.
  200. //
  201. if( !CommitUrlCacheEntryA(
  202. UrlName,
  203. LocalFileName,
  204. *((FILETIME *)&ExpireTime),
  205. LastModTime,
  206. NORMAL_CACHE_ENTRY,
  207. (LPBYTE)GlobalCacheHeaderInfo,
  208. CacheHeaderInfoSize,
  209. TEXT("tst"),
  210. 0 ) ) {
  211. Error = GetLastError();
  212. printf( "CreateUrlFile call failed, %ld.\n", Error );
  213. return( Error );
  214. }
  215. }
  216. dwUrlNum = i; // Save last for next call
  217. return( ERROR_SUCCESS );
  218. }
  219. //---------------------------------------------------------------------
  220. void Display_Usage(const char *szApp)
  221. {
  222. printf("Usage: %s [Options]\r\n\n", szApp);
  223. printf("Options:\r\n");
  224. printf("\t-f# File size of cache entries in bytes.\r\n");
  225. printf("\t-i# Initial number of entries to create\r\n");
  226. printf("\t-n# Number of entries to create before checking total.\r\n");
  227. printf("\t-v Turn on verbose output.\r\n");
  228. }
  229. //---------------------------------------------------------------------
  230. BOOL ParseCommandLine(int argcIn, char *argvIn[])
  231. {
  232. BOOL bRC = TRUE;
  233. int argc = argcIn;
  234. char **argv = argvIn;
  235. argv++; argc--;
  236. while( argc > 0 && argv[0][0] == '-' )
  237. {
  238. switch (argv[0][1])
  239. {
  240. case 'f':
  241. g_dwFileSize = atoi(&argv[0][2]);
  242. break;
  243. case 'i':
  244. g_dwInitEntries= atoi(&argv[0][2]);
  245. break;
  246. case 'n':
  247. g_dwNumEntries = atoi(&argv[0][2]);
  248. break;
  249. case 'v':
  250. g_bVerbose = TRUE;
  251. break;
  252. case '?':
  253. bRC = FALSE;
  254. break;
  255. default:
  256. bRC = FALSE;
  257. break;
  258. }
  259. argv++; argc--;
  260. }
  261. if(bRC == FALSE)
  262. {
  263. Display_Usage(argvIn[0]);
  264. bRC = FALSE;
  265. }
  266. return(bRC);
  267. }
  268. //=================================================================================
  269. void __cdecl main(int argc,char *argv[])
  270. {
  271. DWORD Error;
  272. DWORD i;
  273. DWORD dwEntries = 0;
  274. DWORD dwOldEntries = 0;
  275. if(!ParseCommandLine(argc, argv))
  276. return;
  277. //
  278. // init GlobalCacheHeaderInfo buffer.
  279. //
  280. for( i = 0; i < CACHE_HEADER_INFO_SIZE; i++) {
  281. GlobalCacheHeaderInfo[i] = (BYTE)((DWORD)'0' + i % 10);
  282. }
  283. if(g_bVerbose)
  284. printf("FileSize=%d InitEntries=%d NumEntries=%d\r\n", g_dwFileSize, g_dwInitEntries, g_dwNumEntries);
  285. if(g_dwInitEntries)
  286. ProcessSimulateCache(g_dwInitEntries);
  287. while(TRUE)
  288. {
  289. ProcessSimulateCache(g_dwNumEntries);
  290. dwOldEntries = dwEntries;
  291. EnumUrlCacheEntries(&dwEntries);
  292. if(dwEntries < dwOldEntries) // Quota has been exceeded
  293. break;
  294. if(g_bVerbose)
  295. printf("Entries=%d\r\n", dwEntries);
  296. }
  297. printf("setperfmode on\n");
  298. printf("setquietmode on\n");
  299. printf("setfilesize %d\n", g_dwFileSize);
  300. printf("simcache %d\n", dwOldEntries - 4);
  301. return;
  302. }