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.

149 lines
3.4 KiB

  1. //
  2. // Universal Resource Consumer: Just an innocent stress program
  3. // Copyright (c) Microsoft Corporation, 1997, 1998, 1999
  4. //
  5. //
  6. // module: disk.cxx
  7. // author: silviuc
  8. // created: Mon Apr 13 14:02:12 1998
  9. //
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <windows.h>
  14. #include "error.hxx"
  15. #include "disk.hxx"
  16. #include "consume.hxx"
  17. //
  18. // Table of contents (local functions)
  19. //
  20. static DWORD ConvertToMbytes (PULARGE_INTEGER Size);
  21. //
  22. // Function:
  23. //
  24. // ConsumeAllDiskSpace
  25. //
  26. // Description:
  27. //
  28. // This function consumes all disk space on the current
  29. // partition (the one from which `consume' has been launched).
  30. //
  31. void ConsumeAllDiskSpace ()
  32. {
  33. ULARGE_INTEGER TotalDiskSize;
  34. ULARGE_INTEGER FreeDiskSize;
  35. ULARGE_INTEGER UserFreeSize;
  36. ULARGE_INTEGER RequestSize;
  37. HANDLE File;
  38. HANDLE Mapping;
  39. BOOL Result;
  40. ULONG uLastBitMask;
  41. //
  42. // Get disk space information
  43. //
  44. Result = GetDiskFreeSpaceEx (
  45. NULL, // current directory
  46. & UserFreeSize,
  47. & TotalDiskSize,
  48. & FreeDiskSize);
  49. if (Result == FALSE) {
  50. Error ("GetDiskFreeSpaceEx() failed: error %u",
  51. GetLastError());
  52. }
  53. Message ("Total disk space: %u Mb", ConvertToMbytes (& TotalDiskSize));
  54. Message ("Free disk space: %u Mb", ConvertToMbytes (& FreeDiskSize));
  55. Message ("Free per user space: %u Mb", ConvertToMbytes (& UserFreeSize));
  56. //
  57. // Compute the request size
  58. //
  59. RequestSize.QuadPart = UserFreeSize.QuadPart;
  60. Message ("Attempting to use: %u Mb", ConvertToMbytes (& RequestSize));
  61. //
  62. // Don't allow a size that assigned to a LARGE_INTEGER will result
  63. // in a negative number
  64. //
  65. uLastBitMask = 1 << ( sizeof( uLastBitMask ) * 8 - 1 );
  66. RequestSize.HighPart &= ~uLastBitMask;
  67. //
  68. // Create a file
  69. //
  70. File = CreateFile (
  71. TEXT ("consume.dsk"),
  72. GENERIC_READ | GENERIC_WRITE,
  73. 0, // no sharing
  74. NULL, // default security
  75. CREATE_ALWAYS,
  76. FILE_FLAG_DELETE_ON_CLOSE,
  77. NULL); // default template file
  78. if (File == INVALID_HANDLE_VALUE)
  79. Error ("CreateFile (`consume.dsk') failed: error %u",
  80. GetLastError());
  81. //
  82. // Create a mapping file on top of the file.
  83. //
  84. for ( ; RequestSize.QuadPart > 0 ; ) {
  85. Mapping = CreateFileMapping (
  86. File,
  87. NULL, // default security
  88. PAGE_READWRITE, // protection
  89. RequestSize.HighPart,
  90. RequestSize.LowPart,
  91. NULL); // no name
  92. if (Mapping != NULL)
  93. break;
  94. if ( RequestSize.QuadPart > 0x10000 ) {
  95. RequestSize.QuadPart -= 0x10000;
  96. }
  97. else {
  98. break;
  99. }
  100. Message ("Reattempting to use: %u Mb", ConvertToMbytes (& RequestSize));
  101. }
  102. }
  103. //
  104. // Function:
  105. //
  106. // ConvertToMbytes
  107. //
  108. // Description:
  109. //
  110. // Converts a size to Mbytes so that we can print more user-friendly
  111. // values.
  112. //
  113. static DWORD
  114. ConvertToMbytes (
  115. PULARGE_INTEGER Size)
  116. {
  117. return(DWORD)(Size->QuadPart >> 20);
  118. }
  119. //
  120. // end of module: disk.cxx
  121. //