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.

359 lines
8.2 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. size_t cchDest
  191. )
  192. {
  193. TCHAR *desc;
  194. TCHAR szVolumeName[MAX_PATH];
  195. TCHAR szDrive[MAX_PATH];
  196. BOOL bRet = TRUE;
  197. *szVolumeName = 0;
  198. CreateStringFromDrive(dre, szDrive, sizeof(szDrive));
  199. GetVolumeInformation(szDrive, szVolumeName, ARRAYSIZE(szVolumeName), NULL, NULL, NULL, NULL, 0);
  200. desc = SHFormatMessage( MSG_VOL_NAME_DRIVE_LETTER, szVolumeName, (TCHAR)(dre + 'A'));
  201. if (!SUCCEEDED(StringCchCopy(psz, cchDest, desc)))
  202. {
  203. bRet = FALSE;
  204. }
  205. LocalFree (desc);
  206. return bRet;
  207. }
  208. /*
  209. **------------------------------------------------------------------------------
  210. ** GetHardwareType
  211. **
  212. ** Purpose:
  213. ** Parameters:
  214. ** hwHardware - hardware type of drive
  215. ** Return: TRUE if compatible with our needs
  216. ** FALSE otherwise
  217. ** Notes:
  218. ** Mod Log: Created by Jason Cobb (2/97)
  219. **------------------------------------------------------------------------------
  220. */
  221. BOOL
  222. GetHardwareType(
  223. drenum dre,
  224. hardware &hwType
  225. )
  226. {
  227. TCHAR szDrive[4];
  228. hwType = hwINVALID;
  229. //
  230. //Get drive string from drive number
  231. //
  232. if (!CreateStringFromDrive(dre, szDrive, 4))
  233. return FALSE;
  234. UINT uiType = GetDriveType(szDrive);
  235. switch (uiType)
  236. {
  237. case 0:
  238. hwType = hwUnknown;
  239. return FALSE;
  240. case 1:
  241. hwType = hwINVALID;
  242. return FALSE;
  243. case DRIVE_REMOVABLE:
  244. hwType = hwRemoveable;
  245. break;
  246. case DRIVE_FIXED:
  247. hwType = hwFixed;
  248. break;
  249. case DRIVE_REMOTE:
  250. hwType = hwNetwork;
  251. return FALSE;
  252. case DRIVE_CDROM:
  253. hwType = hwCDROM;
  254. return FALSE;
  255. case DRIVE_RAMDISK:
  256. hwType = hwRamDrive;
  257. break;
  258. default:
  259. hwType = hwUnknown;
  260. return FALSE;
  261. }
  262. return TRUE;
  263. }
  264. /*
  265. **------------------------------------------------------------------------------
  266. ** CreateStringFromDrive
  267. **
  268. ** Purpose: creates a drive string from a drive number
  269. ** Mod Log: Created by Jason Cobb (4/97)
  270. **------------------------------------------------------------------------------
  271. */
  272. BOOL
  273. CreateStringFromDrive(
  274. drenum dre,
  275. TCHAR * szDrive,
  276. ULONG cLen
  277. )
  278. {
  279. if ((szDrive == NULL) || (cLen < 4))
  280. return FALSE;
  281. if (dre == Drive_INV)
  282. return FALSE;
  283. TCHAR ch = (CHAR)(dre + 'A');
  284. //
  285. //Drive string = Drive letter, colon, slash = "C:\"
  286. //
  287. szDrive[0] = ch;
  288. szDrive[1] = ':';
  289. szDrive[2] = '\\';
  290. szDrive[3] = 0;
  291. return TRUE;
  292. }
  293. ULARGE_INTEGER
  294. GetFreeSpaceRatio(
  295. WORD dwDrive,
  296. ULARGE_INTEGER cbTotal
  297. )
  298. {
  299. // for now, hardcode it as a percentage...
  300. ULARGE_INTEGER cbMin;
  301. // for now use 1% as the test to go into aggressive mode...
  302. cbMin.QuadPart = cbTotal.QuadPart / 100;
  303. return cbMin;
  304. }