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.

252 lines
5.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. volume.c
  5. Abstract:
  6. This file contains code for all commands that effect a volume
  7. Author:
  8. Wesley Witt [wesw] 1-March-2000
  9. Revision History:
  10. --*/
  11. #include <precomp.h>
  12. INT
  13. VolumeHelp(
  14. IN INT argc,
  15. IN PWSTR argv[]
  16. )
  17. {
  18. DisplayMsg( MSG_USAGE_VOLUME );
  19. return EXIT_CODE_SUCCESS;
  20. }
  21. DWORD
  22. QueryHardDiskNumber(
  23. IN UCHAR DriveLetter
  24. )
  25. {
  26. WCHAR driveName[10];
  27. HANDLE h;
  28. BOOL b;
  29. STORAGE_DEVICE_NUMBER number;
  30. DWORD bytes;
  31. driveName[0] = '\\';
  32. driveName[1] = '\\';
  33. driveName[2] = '.';
  34. driveName[3] = '\\';
  35. driveName[4] = DriveLetter;
  36. driveName[5] = ':';
  37. driveName[6] = 0;
  38. h = CreateFile(
  39. driveName,
  40. GENERIC_READ,
  41. FILE_SHARE_READ | FILE_SHARE_WRITE,
  42. NULL,
  43. OPEN_EXISTING,
  44. FILE_ATTRIBUTE_NORMAL,
  45. INVALID_HANDLE_VALUE
  46. );
  47. if (h == INVALID_HANDLE_VALUE) {
  48. return (DWORD) -1;
  49. }
  50. b = DeviceIoControl(
  51. h,
  52. IOCTL_STORAGE_GET_DEVICE_NUMBER,
  53. NULL,
  54. 0,
  55. &number,
  56. sizeof(number),
  57. &bytes,
  58. NULL
  59. );
  60. CloseHandle(h);
  61. if (!b) {
  62. return (DWORD) -1;
  63. }
  64. return number.DeviceNumber;
  65. }
  66. INT
  67. DismountVolume(
  68. IN INT argc,
  69. IN PWSTR argv[]
  70. )
  71. /*++
  72. Routine Description:
  73. This routine dismounts the volume.
  74. Arguments:
  75. argc - The argument count.
  76. argv - Array of Strings of the form :
  77. ' fscutl dismountv <volume pathname>'.
  78. Return Value:
  79. None
  80. --*/
  81. {
  82. BOOL Status;
  83. HANDLE FileHandle = INVALID_HANDLE_VALUE;
  84. DWORD BytesReturned;
  85. WCHAR FileName[MAX_PATH];
  86. INT ExitCode = EXIT_CODE_SUCCESS;
  87. try {
  88. if (argc != 1) {
  89. DisplayMsg( MSG_USAGE_DISMOUNTV );
  90. if (argc != 0) {
  91. ExitCode = EXIT_CODE_FAILURE;
  92. }
  93. leave;
  94. }
  95. if (!IsVolumeLocalNTFS( argv[0][0] )) {
  96. DisplayMsg( MSG_NTFS_REQUIRED );
  97. ExitCode = EXIT_CODE_FAILURE;
  98. leave;
  99. }
  100. if (wcslen( DotPrefix ) + wcslen( argv[0] ) + 1 > MAX_PATH) {
  101. DisplayMsg( MSG_FILENAME_TOO_LONG );
  102. ExitCode = EXIT_CODE_FAILURE;
  103. leave;
  104. }
  105. wcscpy( FileName, DotPrefix );
  106. wcscat( FileName, argv[0] );
  107. FileHandle = CreateFile(
  108. FileName,
  109. GENERIC_WRITE,
  110. FILE_SHARE_READ | FILE_SHARE_WRITE,
  111. NULL,
  112. OPEN_EXISTING,
  113. FILE_ATTRIBUTE_NORMAL,
  114. NULL
  115. );
  116. if (FileHandle == INVALID_HANDLE_VALUE) {
  117. DisplayError();
  118. ExitCode = EXIT_CODE_FAILURE;
  119. leave;
  120. }
  121. Status = DeviceIoControl(
  122. FileHandle,
  123. FSCTL_DISMOUNT_VOLUME,
  124. NULL,
  125. 0,
  126. NULL,
  127. 0,
  128. &BytesReturned,
  129. (LPOVERLAPPED)NULL
  130. );
  131. if (!Status) {
  132. DisplayError();
  133. ExitCode = EXIT_CODE_FAILURE;
  134. }
  135. } finally {
  136. if (FileHandle != INVALID_HANDLE_VALUE) {
  137. CloseHandle( FileHandle );
  138. }
  139. }
  140. return ExitCode;
  141. }
  142. INT
  143. DiskFreeSpace(
  144. IN INT argc,
  145. IN PWSTR argv[]
  146. )
  147. /*++
  148. Routine Description:
  149. This routine provides information about free disk space in the
  150. directory path passed in.
  151. Arguments:
  152. argc - The argument count
  153. argv - Array of Strings of the form :
  154. 'fscutl df <drive name>'.
  155. Return Value:
  156. None
  157. --*/
  158. {
  159. ULARGE_INTEGER FreeBytesAvailableToCaller; // receives the number of bytes on disk available to the caller
  160. ULARGE_INTEGER TotalNumberOfBytes; // receives the number of bytes on disk
  161. ULARGE_INTEGER TotalNumberOfFreeBytes; // receives the free bytes on disk
  162. BOOL Status; // return status
  163. INT ExitCode = EXIT_CODE_SUCCESS;
  164. try {
  165. if (argc != 1) {
  166. DisplayMsg( MSG_USAGE_DF );
  167. if (argc != 0) {
  168. ExitCode = EXIT_CODE_FAILURE;
  169. }
  170. leave ;
  171. }
  172. if (!IsVolumeLocalNTFS( argv[0][0] )) {
  173. DisplayMsg( MSG_NTFS_REQUIRED );
  174. ExitCode = EXIT_CODE_FAILURE;
  175. leave;
  176. }
  177. Status = GetDiskFreeSpaceEx(
  178. argv[0],
  179. &FreeBytesAvailableToCaller,
  180. &TotalNumberOfBytes,
  181. &TotalNumberOfFreeBytes
  182. );
  183. if (!Status) {
  184. DisplayError();
  185. ExitCode = EXIT_CODE_FAILURE;
  186. leave;
  187. }
  188. DisplayMsg( MSG_DISKFREE,
  189. QuadToDecimalText( TotalNumberOfFreeBytes.QuadPart ),
  190. QuadToDecimalText( TotalNumberOfBytes.QuadPart ),
  191. QuadToDecimalText( FreeBytesAvailableToCaller.QuadPart ));
  192. } finally {
  193. }
  194. return ExitCode;
  195. }