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.

353 lines
7.8 KiB

  1. /*
  2. **------------------------------------------------------------------------------
  3. ** Module: Disk Cleanup Applet
  4. ** File: diskutil.cpp
  5. **
  6. ** Purpose: Disk utility functions
  7. ** Notes:
  8. ** Mod Log: Created by Jason Cobb (2/97)
  9. **
  10. ** Copyright (c)1997 Microsoft Corporation, All Rights Reserved
  11. **------------------------------------------------------------------------------
  12. */
  13. /*
  14. **------------------------------------------------------------------------------
  15. ** Project include files
  16. **------------------------------------------------------------------------------
  17. */
  18. #include "common.h"
  19. #include "diskutil.h"
  20. #include "msprintf.h"
  21. #include "resource.h"
  22. #define Not_VxD
  23. #include <vwin32.h>
  24. /*
  25. **------------------------------------------------------------------------------
  26. ** fIsSingleDrive
  27. **
  28. ** Purpose: gets a drive letter from a drive string
  29. ** Mod Log: Created by Jason Cobb (2/97)
  30. **------------------------------------------------------------------------------
  31. */
  32. BOOL
  33. fIsSingleDrive (
  34. LPTSTR lpDrive
  35. )
  36. {
  37. //
  38. //Is it a valid drive string ?!?
  39. //
  40. if (!fIsValidDriveString(lpDrive))
  41. return FALSE;
  42. //
  43. //Is it a valid drive ?!?
  44. //
  45. BOOL rc = FALSE;
  46. UINT driveType = GetDriveType (lpDrive);
  47. switch (driveType)
  48. {
  49. case 0: // Unknown drive type
  50. case 1: // Invalid drive type
  51. break;
  52. case DRIVE_REMOVABLE: // Removable drive (floppy,bernoulli,syquest,etc)
  53. case DRIVE_FIXED: // Hard disk
  54. // We support removeable and fixed drives
  55. rc = TRUE;
  56. break;
  57. case DRIVE_REMOTE: // Network
  58. case DRIVE_CDROM: // CD-ROM
  59. break;
  60. case DRIVE_RAMDISK: // RAM disk
  61. // We support ram drives, even though it's rather dubious
  62. rc = TRUE;
  63. break;
  64. default: // Unknown drive type
  65. break;
  66. }
  67. return rc;
  68. }
  69. /*
  70. **------------------------------------------------------------------------------
  71. ** fIsValidDriveString
  72. **
  73. ** Purpose: determines if a drive is a valid drive string
  74. ** Note: assumes a drive string consists of a drive letter,
  75. ** colon, and slash characters and nothing else.
  76. ** Example: "C:\"
  77. ** Mod Log: Created by Jason Cobb (2/97)
  78. **------------------------------------------------------------------------------
  79. */
  80. BOOL
  81. fIsValidDriveString(
  82. const TCHAR * szDrive
  83. )
  84. {
  85. //
  86. //Make sure we have a valid string
  87. //
  88. if ((szDrive == NULL) || (szDrive[0] == 0))
  89. return FALSE;
  90. //
  91. //Make sure length is equal to length of valid drive string "C:\"
  92. //
  93. INT iLen = lstrlen(szDrive);
  94. if (iLen != 3)
  95. return FALSE;
  96. //
  97. //Check drive letter
  98. //
  99. TCHAR ch = szDrive[0];
  100. if ((ch >= 'a') && (ch <= 'z'))
  101. ;
  102. else if ((ch >= 'A') && (ch <= 'Z'))
  103. ;
  104. else
  105. return FALSE;
  106. //
  107. //Check colon
  108. //
  109. ch = szDrive[1];
  110. if (ch != ':')
  111. return FALSE;
  112. //
  113. //Check slash
  114. //
  115. ch = szDrive[2];
  116. if (ch != '\\')
  117. return FALSE;
  118. //
  119. //Check zero terminating byte
  120. //
  121. ch = szDrive[3];
  122. if (ch != 0)
  123. return FALSE;
  124. return TRUE;
  125. }
  126. /*
  127. **------------------------------------------------------------------------------
  128. ** GetDriveFromString
  129. **
  130. ** Purpose: gets a drive letter from a drive string
  131. ** Mod Log: Created by Jason Cobb (4/97)
  132. **------------------------------------------------------------------------------
  133. */
  134. BOOL
  135. GetDriveFromString(
  136. const TCHAR * szDrive,
  137. drenum & dre
  138. )
  139. {
  140. dre = Drive_INV;
  141. //
  142. //Make sure we have a valid string
  143. //
  144. if ((szDrive == NULL) || (szDrive[0] == 0))
  145. return FALSE;
  146. //
  147. //Get drive number from drive letter
  148. //
  149. TCHAR chDrive = szDrive[0];
  150. if ((chDrive >= TCHAR('a')) && (chDrive <= TCHAR('z')))
  151. dre = (drenum)(chDrive - TCHAR('a'));
  152. else if ((chDrive >= TCHAR('A')) && (chDrive <= TCHAR('Z')))
  153. dre = (drenum)(chDrive - TCHAR('A'));
  154. else
  155. return FALSE;
  156. return TRUE;
  157. }
  158. /*
  159. **------------------------------------------------------------------------------
  160. ** GetDriveIcon
  161. **
  162. ** Purpose:
  163. ** Parameters:
  164. ** dre - driver letter
  165. ** bSmallIcon - TRUE if small icon is desired.
  166. ** Return: Drive Icon returned by the shell
  167. ** Notes:
  168. ** Mod Log: Created by Jason Cobb (2/97)
  169. **------------------------------------------------------------------------------
  170. */
  171. HICON
  172. GetDriveIcon(
  173. drenum dre,
  174. BOOL bSmallIcon
  175. )
  176. {
  177. TCHAR szDrive[10];
  178. SHFILEINFO fi;
  179. CreateStringFromDrive(dre, szDrive, sizeof(szDrive));
  180. if (bSmallIcon)
  181. SHGetFileInfo(szDrive, 0, &fi, sizeof(fi), SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_SMALLICON);
  182. else
  183. SHGetFileInfo(szDrive, 0, &fi, sizeof(fi), SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_LARGEICON);
  184. return fi.hIcon;
  185. }
  186. BOOL
  187. GetDriveDescription(
  188. drenum dre,
  189. TCHAR *psz
  190. )
  191. {
  192. TCHAR *desc;
  193. TCHAR szVolumeName[MAX_PATH];
  194. TCHAR szDrive[MAX_PATH];
  195. *szVolumeName = 0;
  196. CreateStringFromDrive(dre, szDrive, sizeof(szDrive));
  197. GetVolumeInformation(szDrive, szVolumeName, ARRAYSIZE(szVolumeName), NULL, NULL, NULL, NULL, 0);
  198. desc = SHFormatMessage( MSG_VOL_NAME_DRIVE_LETTER, szVolumeName, (TCHAR)(dre + 'A'));
  199. // desc = msprintf (nszVolNameDriveLetter, "%s%c", szVolumeName, (TCHAR)(dre + 'A'));
  200. StrCpy (psz, desc);
  201. LocalFree (desc);
  202. return TRUE;
  203. }
  204. /*
  205. **------------------------------------------------------------------------------
  206. ** GetHardwareType
  207. **
  208. ** Purpose:
  209. ** Parameters:
  210. ** hwHardware - hardware type of drive
  211. ** Return: TRUE if compatible with our needs
  212. ** FALSE otherwise
  213. ** Notes:
  214. ** Mod Log: Created by Jason Cobb (2/97)
  215. **------------------------------------------------------------------------------
  216. */
  217. BOOL
  218. GetHardwareType(
  219. drenum dre,
  220. hardware &hwType
  221. )
  222. {
  223. TCHAR szDrive[4];
  224. hwType = hwINVALID;
  225. //
  226. //Get drive string from drive number
  227. //
  228. if (!CreateStringFromDrive(dre, szDrive, 4))
  229. return FALSE;
  230. UINT uiType = GetDriveType(szDrive);
  231. switch (uiType)
  232. {
  233. case 0:
  234. hwType = hwUnknown;
  235. return FALSE;
  236. case 1:
  237. hwType = hwINVALID;
  238. return FALSE;
  239. case DRIVE_REMOVABLE:
  240. hwType = hwRemoveable;
  241. break;
  242. case DRIVE_FIXED:
  243. hwType = hwFixed;
  244. break;
  245. case DRIVE_REMOTE:
  246. hwType = hwNetwork;
  247. return FALSE;
  248. case DRIVE_CDROM:
  249. hwType = hwCDROM;
  250. return FALSE;
  251. case DRIVE_RAMDISK:
  252. hwType = hwRamDrive;
  253. break;
  254. default:
  255. hwType = hwUnknown;
  256. return FALSE;
  257. }
  258. return TRUE;
  259. }
  260. /*
  261. **------------------------------------------------------------------------------
  262. ** CreateStringFromDrive
  263. **
  264. ** Purpose: creates a drive string from a drive number
  265. ** Mod Log: Created by Jason Cobb (4/97)
  266. **------------------------------------------------------------------------------
  267. */
  268. BOOL
  269. CreateStringFromDrive(
  270. drenum dre,
  271. TCHAR * szDrive,
  272. ULONG cLen
  273. )
  274. {
  275. if ((szDrive == NULL) || (cLen < 4))
  276. return FALSE;
  277. if (dre == Drive_INV)
  278. return FALSE;
  279. TCHAR ch = (CHAR)(dre + 'A');
  280. //
  281. //Drive string = Drive letter, colon, slash = "C:\"
  282. //
  283. szDrive[0] = ch;
  284. szDrive[1] = ':';
  285. szDrive[2] = '\\';
  286. szDrive[3] = 0;
  287. return TRUE;
  288. }
  289. ULARGE_INTEGER
  290. GetFreeSpaceRatio(
  291. WORD dwDrive,
  292. ULARGE_INTEGER cbTotal
  293. )
  294. {
  295. // for now, hardcode it as a percentage...
  296. ULARGE_INTEGER cbMin;
  297. // for now use 1% as the test to go into aggressive mode...
  298. cbMin.QuadPart = cbTotal.QuadPart / 100;
  299. return cbMin;
  300. }