Source code of Windows XP (NT5)
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.

246 lines
4.8 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. wcscpy( FileName, L"\\\\.\\" );
  101. wcsncat( FileName, argv[0], (sizeof(FileName)/sizeof(WCHAR))-wcslen(FileName) );
  102. FileHandle = CreateFile(
  103. FileName,
  104. GENERIC_WRITE,
  105. FILE_SHARE_READ | FILE_SHARE_WRITE,
  106. NULL,
  107. OPEN_EXISTING,
  108. FILE_ATTRIBUTE_NORMAL,
  109. NULL
  110. );
  111. if (FileHandle == INVALID_HANDLE_VALUE) {
  112. DisplayError();
  113. ExitCode = EXIT_CODE_FAILURE;
  114. leave;
  115. }
  116. Status = DeviceIoControl(
  117. FileHandle,
  118. FSCTL_DISMOUNT_VOLUME,
  119. NULL,
  120. 0,
  121. NULL,
  122. 0,
  123. &BytesReturned,
  124. (LPOVERLAPPED)NULL
  125. );
  126. if (!Status) {
  127. DisplayError();
  128. ExitCode = EXIT_CODE_FAILURE;
  129. }
  130. } finally {
  131. if (FileHandle != INVALID_HANDLE_VALUE) {
  132. CloseHandle( FileHandle );
  133. }
  134. }
  135. return ExitCode;
  136. }
  137. INT
  138. DiskFreeSpace(
  139. IN INT argc,
  140. IN PWSTR argv[]
  141. )
  142. /*++
  143. Routine Description:
  144. This routine provides information about free disk space in the
  145. directory path passed in.
  146. Arguments:
  147. argc - The argument count
  148. argv - Array of Strings of the form :
  149. 'fscutl df <drive name>'.
  150. Return Value:
  151. None
  152. --*/
  153. {
  154. ULARGE_INTEGER FreeBytesAvailableToCaller; // receives the number of bytes on disk available to the caller
  155. ULARGE_INTEGER TotalNumberOfBytes; // receives the number of bytes on disk
  156. ULARGE_INTEGER TotalNumberOfFreeBytes; // receives the free bytes on disk
  157. BOOL Status; // return status
  158. INT ExitCode = EXIT_CODE_SUCCESS;
  159. try {
  160. if (argc != 1) {
  161. DisplayMsg( MSG_USAGE_DF );
  162. if (argc != 0) {
  163. ExitCode = EXIT_CODE_FAILURE;
  164. }
  165. leave ;
  166. }
  167. if (!IsVolumeLocalNTFS( argv[0][0] )) {
  168. DisplayMsg( MSG_NTFS_REQUIRED );
  169. ExitCode = EXIT_CODE_FAILURE;
  170. leave;
  171. }
  172. Status = GetDiskFreeSpaceEx(
  173. argv[0],
  174. &FreeBytesAvailableToCaller,
  175. &TotalNumberOfBytes,
  176. &TotalNumberOfFreeBytes
  177. );
  178. if (!Status) {
  179. DisplayError();
  180. ExitCode = EXIT_CODE_FAILURE;
  181. leave;
  182. }
  183. DisplayMsg( MSG_DISKFREE,
  184. QuadToDecimalText( TotalNumberOfFreeBytes.QuadPart ),
  185. QuadToDecimalText( TotalNumberOfBytes.QuadPart ),
  186. QuadToDecimalText( FreeBytesAvailableToCaller.QuadPart ));
  187. } finally {
  188. }
  189. return ExitCode;
  190. }