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.

363 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. Implements a small utility that fills the disk for purposes of free space
  7. testing.
  8. Author:
  9. Jim Schmidt (jimschm) 18-Aug-2000
  10. Revision History:
  11. <full name> (<alias>) <date> <comments>
  12. --*/
  13. #include "pch.h"
  14. #include "wininet.h"
  15. #include <lm.h>
  16. HANDLE g_hHeap;
  17. HINSTANCE g_hInst;
  18. BOOL
  19. Init (
  20. VOID
  21. )
  22. {
  23. g_hHeap = GetProcessHeap();
  24. g_hInst = GetModuleHandle (NULL);
  25. UtInitialize (NULL);
  26. return TRUE;
  27. }
  28. VOID
  29. Terminate (
  30. VOID
  31. )
  32. {
  33. UtTerminate ();
  34. }
  35. VOID
  36. HelpAndExit (
  37. VOID
  38. )
  39. {
  40. //
  41. // This routine is called whenever command line args are wrong
  42. //
  43. fprintf (
  44. stderr,
  45. "Command Line Syntax:\n\n"
  46. " filler <free_space> [/D:<drive>] [/C:<cmdline> [/M]]\n"
  47. " filler /Q [/D:<drive>]\n"
  48. "\nDescription:\n\n"
  49. " filler creates a file (bigfile.dat) on the current or specified\n"
  50. " drive, leaving only the specified amount of free space on the drive.\n"
  51. "\nArguments:\n\n"
  52. " free_space Specifies the amount of free space to leave on\n"
  53. " disk.\n"
  54. " /D Specifies the drive letter to fill (i.e. /D:C)\n"
  55. " /Q Queries the free space on the disk\n"
  56. " /C Executes command line specified in <cmdline>\n"
  57. " /M Issue message box if command line alters disk space\n"
  58. );
  59. exit (1);
  60. }
  61. INT
  62. __cdecl
  63. _tmain (
  64. INT argc,
  65. PCTSTR argv[]
  66. )
  67. {
  68. INT i;
  69. TCHAR drive;
  70. TCHAR curDir[MAX_PATH];
  71. LONGLONG freeSpace = -1;
  72. PCTSTR p;
  73. BOOL qSpecified = FALSE;
  74. PCTSTR cmdLine = NULL;
  75. BOOL mSpecified = FALSE;
  76. GetCurrentDirectory (ARRAYSIZE(curDir), curDir);
  77. drive = 0;
  78. //
  79. // TODO: Parse command line here
  80. //
  81. for (i = 1 ; i < argc ; i++) {
  82. if (argv[i][0] == TEXT('/') || argv[i][0] == TEXT('-')) {
  83. switch (_totlower (_tcsnextc (&argv[i][1]))) {
  84. case TEXT('d'):
  85. if (drive) {
  86. HelpAndExit();
  87. }
  88. if (argv[i][2] == TEXT(':')) {
  89. if (!argv[i][3] || argv[i][4]) {
  90. HelpAndExit();
  91. }
  92. drive = argv[i][3];
  93. } else if (i + 1 < argc) {
  94. i++;
  95. if (!argv[i][0] || argv[i][1]) {
  96. HelpAndExit();
  97. }
  98. drive = argv[i][0];
  99. } else {
  100. HelpAndExit();
  101. }
  102. if (!_istalpha (drive)) {
  103. HelpAndExit();
  104. }
  105. break;
  106. case TEXT('c'):
  107. if (cmdLine) {
  108. HelpAndExit();
  109. }
  110. if (argv[i][2] == TEXT(':')) {
  111. if (!argv[i][3]) {
  112. HelpAndExit();
  113. }
  114. cmdLine = &argv[i][3];
  115. } else if (i + 1 < argc) {
  116. i++;
  117. if (!argv[i][0]) {
  118. HelpAndExit();
  119. }
  120. cmdLine = argv[i];
  121. } else {
  122. HelpAndExit();
  123. }
  124. break;
  125. case TEXT('m'):
  126. if (mSpecified) {
  127. HelpAndExit();
  128. }
  129. mSpecified = TRUE;
  130. break;
  131. case TEXT('q'):
  132. if (qSpecified || freeSpace != -1) {
  133. HelpAndExit();
  134. }
  135. qSpecified = TRUE;
  136. break;
  137. default:
  138. HelpAndExit();
  139. }
  140. } else {
  141. //
  142. // Parse other args that don't require / or -
  143. //
  144. if (qSpecified || freeSpace != -1) {
  145. HelpAndExit();
  146. }
  147. freeSpace = StringToInt64 (argv[i], &p);
  148. if (*p != 0 || freeSpace < 0) {
  149. HelpAndExit();
  150. }
  151. }
  152. }
  153. if (cmdLine && qSpecified) {
  154. HelpAndExit();
  155. }
  156. if (mSpecified && !cmdLine) {
  157. HelpAndExit();
  158. }
  159. if (!drive) {
  160. drive = curDir[0];
  161. }
  162. if (!qSpecified) {
  163. if (freeSpace == -1) {
  164. HelpAndExit();
  165. }
  166. }
  167. //
  168. // Begin processing
  169. //
  170. if (!Init()) {
  171. return 0;
  172. }
  173. printf ("---------------------------------------------------------\n");
  174. //
  175. // Do work here
  176. //
  177. {
  178. HANDLE file;
  179. TCHAR path[] = TEXT("?:\\bigfile.dat");
  180. TCHAR rootDir[] = TEXT("?:\\");
  181. ULARGE_INTEGER freeBytes;
  182. ULARGE_INTEGER totalBytes;
  183. ULARGE_INTEGER totalFreeBytes;
  184. ULARGE_INTEGER freeBytesAfter;
  185. ULARGE_INTEGER totalBytesAfter;
  186. ULARGE_INTEGER totalFreeBytesAfter;
  187. BOOL b;
  188. HANDLE h;
  189. path[0] = drive;
  190. rootDir[0] = drive;
  191. if (qSpecified) {
  192. b = GetDiskFreeSpaceEx (rootDir, &freeBytes, &totalBytes, &totalFreeBytes);
  193. if (b) {
  194. _ftprintf (stderr, TEXT("Drive %c has %I64u bytes free\n"), drive, freeBytes.QuadPart);
  195. } else {
  196. _ftprintf (stderr, TEXT("Can't get free space from drive %c\n"), drive);
  197. }
  198. } else {
  199. _tprintf (TEXT("FILLER: Deleting %s..."), path);
  200. SetFileAttributes (path, FILE_ATTRIBUTE_NORMAL);
  201. DeleteFile (path);
  202. printf ("\n");
  203. b = GetDiskFreeSpaceEx (rootDir, &freeBytes, &totalBytes, &totalFreeBytes);
  204. if (b) {
  205. if (freeBytes.QuadPart <= (ULONGLONG) freeSpace) {
  206. _ftprintf (stderr, TEXT("ERROR: Drive %c only has %I64u bytes available\n"), drive, freeBytes.QuadPart);
  207. b = FALSE;
  208. }
  209. }
  210. if (b) {
  211. file = BfCreateFile (path);
  212. if (!file) {
  213. _ftprintf (stderr, TEXT("ERROR: Can't create file %s\n"), path);
  214. } else {
  215. printf ("FILLER: Allocating disk space...");
  216. GetDiskFreeSpaceEx (rootDir, &freeBytes, &totalBytes, &totalFreeBytes);
  217. freeBytes.QuadPart -= (ULONGLONG) freeSpace;
  218. if (!BfSetFilePointer (file, freeBytes.QuadPart)) {
  219. DEBUGMSG ((DBG_ERROR, "BfSetFilePointer failed"));
  220. }
  221. if (!SetEndOfFile (file)) {
  222. DEBUGMSG ((DBG_ERROR, "SetEndOfFile failed"));
  223. }
  224. CloseHandle (file);
  225. printf ("done\n");
  226. b = GetDiskFreeSpaceEx (rootDir, &freeBytes, &totalBytes, &totalFreeBytes);
  227. if (b) {
  228. _ftprintf (stderr, TEXT("FILLER: Drive %c now has %I64u bytes available\n"), drive, freeBytes.QuadPart);
  229. } else {
  230. fprintf (stderr, "ERROR: Can't get free space again.\n");
  231. }
  232. if (cmdLine) {
  233. h = StartProcess (cmdLine);
  234. if (!h) {
  235. _ftprintf (stderr, TEXT("\nERROR: Can't start process %s\n"), cmdLine);
  236. } else {
  237. _tprintf (TEXT("FILLER: Running\n\n %s\n\n"), cmdLine);
  238. WaitForSingleObject (h, INFINITE);
  239. CloseHandle (h);
  240. printf ("\n\nFILLER: Process done.\n");
  241. b = GetDiskFreeSpaceEx (rootDir, &freeBytesAfter, &totalBytesAfter, &totalFreeBytesAfter);
  242. if (b) {
  243. _ftprintf (stderr, TEXT("FILLER: Drive %c has %I64u bytes available after cmdline\n"), drive, freeBytesAfter.QuadPart);
  244. if (freeBytesAfter.QuadPart != freeBytes.QuadPart) {
  245. if (!mSpecified) {
  246. fprintf (stderr, "\nWARNING: Command line altered disk space\n\n");
  247. } else {
  248. TCHAR msg[1024];
  249. _stprintf (msg, "Command line altered disk space:\n\n%s\n\nSize: %I64i", cmdLine, freeSpace);
  250. MessageBox (NULL, msg, TEXT("filler.exe"), MB_OK);
  251. }
  252. }
  253. } else {
  254. fprintf (stderr, "ERROR: Can't get free space again.\n");
  255. }
  256. }
  257. }
  258. }
  259. } else {
  260. _ftprintf (stderr, TEXT("ERROR: Can't get free space for drive %c\n"), drive);
  261. }
  262. }
  263. }
  264. printf ("---------------------------------------------------------\n\n");
  265. //
  266. // End of processing
  267. //
  268. Terminate();
  269. return 0;
  270. }