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.

200 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. vststvolinfo.cxx
  5. Abstract:
  6. Implementation of volume information class
  7. Brian Berkowitz [brianb] 06/06/2000
  8. TBD:
  9. Revision History:
  10. Name Date Comments
  11. brianb 06/06/2000 Created
  12. --*/
  13. #include <stdafx.h>
  14. #include <vststvolinfo.hxx>
  15. CVsTstVolumeInfo::CVsTstVolumeInfo() :
  16. m_wszVolumeName(NULL),
  17. m_wszFileSystemName(NULL),
  18. m_llTotalSize(0L),
  19. m_llTotalFreeSpace(0L),
  20. m_flags(0),
  21. m_driveType(0),
  22. m_pVolumeNext(NULL)
  23. {
  24. }
  25. CVsTstVolumeInfo::~CVsTstVolumeInfo()
  26. {
  27. delete m_wszVolumeName;
  28. delete m_wszFileSystemName;
  29. }
  30. void CVsTstVolumeList::FreeVolumeList()
  31. {
  32. CVsTstVolumeInfo *pVolume = m_pVolumeFirst;
  33. m_pVolumeFirst = NULL;
  34. while(pVolume)
  35. {
  36. CVsTstVolumeInfo *pVolumeNext = pVolume->m_pVolumeNext;
  37. delete pVolume;
  38. pVolume = pVolumeNext;
  39. }
  40. }
  41. HRESULT CVsTstVolumeList::RefreshVolumeList()
  42. {
  43. FreeVolumeList();
  44. WCHAR bufVolumeName[MAX_PATH];
  45. HANDLE hVolumes = FindFirstVolume(bufVolumeName, sizeof(bufVolumeName));
  46. if (hVolumes == INVALID_HANDLE_VALUE)
  47. return HRESULT_FROM_WIN32(GetLastError());
  48. HRESULT hr = S_OK;
  49. try
  50. {
  51. do
  52. {
  53. WCHAR bufFileSystemName[MAX_PATH];
  54. DWORD serialNumber = 0;
  55. DWORD maxFileNameLength = 0;
  56. DWORD flags = 0;
  57. ULARGE_INTEGER freeSpace;
  58. ULARGE_INTEGER totalSpace;
  59. ULARGE_INTEGER totalFreeSpace;
  60. freeSpace.QuadPart = 0i64;
  61. totalSpace.QuadPart = 0i64;
  62. totalFreeSpace.QuadPart = 0i64;
  63. bufFileSystemName[0] = L'\0';
  64. UINT driveType = GetDriveType(bufVolumeName);
  65. if (driveType == DRIVE_FIXED)
  66. {
  67. if (!GetVolumeInformation
  68. (
  69. bufVolumeName,
  70. NULL,
  71. 0,
  72. &serialNumber,
  73. &maxFileNameLength,
  74. &flags,
  75. bufFileSystemName,
  76. sizeof(bufFileSystemName)
  77. ))
  78. throw(HRESULT_FROM_WIN32(GetLastError()));
  79. if (!GetDiskFreeSpaceEx
  80. (
  81. bufVolumeName,
  82. &freeSpace,
  83. &totalSpace,
  84. &totalFreeSpace
  85. ))
  86. throw(HRESULT_FROM_WIN32(GetLastError()));
  87. }
  88. CVsTstVolumeInfo *pVolume = new CVsTstVolumeInfo;
  89. if (pVolume == NULL)
  90. throw(E_OUTOFMEMORY);
  91. pVolume->m_wszVolumeName = new WCHAR[wcslen(bufVolumeName) + 1];
  92. if (pVolume->m_wszVolumeName == NULL)
  93. {
  94. delete pVolume;
  95. throw(E_OUTOFMEMORY);
  96. }
  97. wcscpy(pVolume->m_wszVolumeName, bufVolumeName);
  98. pVolume->m_wszFileSystemName = new WCHAR[wcslen(bufFileSystemName) + 1];
  99. if (pVolume->m_wszFileSystemName == NULL)
  100. {
  101. delete pVolume;
  102. throw(E_OUTOFMEMORY);
  103. }
  104. wcscpy(pVolume->m_wszFileSystemName, bufFileSystemName);
  105. pVolume->m_llTotalSize = totalSpace.QuadPart;
  106. pVolume->m_llTotalFreeSpace = totalFreeSpace.QuadPart;
  107. pVolume->m_llUserFreeSpace = freeSpace.QuadPart;
  108. pVolume->m_flags = flags;
  109. pVolume->m_serialNumber = serialNumber;
  110. pVolume->m_driveType = driveType;
  111. pVolume->m_pVolumeNext = m_pVolumeFirst;
  112. m_pVolumeFirst = pVolume;
  113. } while(FindNextVolume(hVolumes, bufVolumeName, sizeof(bufVolumeName)));
  114. DWORD dwErr = GetLastError();
  115. if (dwErr != ERROR_NO_MORE_FILES)
  116. throw(HRESULT_FROM_WIN32(dwErr));
  117. }
  118. catch(HRESULT hrExcept)
  119. {
  120. hr = hrExcept;
  121. }
  122. catch(...)
  123. {
  124. hr = E_UNEXPECTED;
  125. }
  126. if (FAILED(hr))
  127. FreeVolumeList();
  128. FindVolumeClose(hVolumes);
  129. return hr;
  130. }
  131. UINT CVsTstVolumeList::GetVolumeCount()
  132. {
  133. UINT cVolumes = 0;
  134. CVsTstVolumeInfo *pVolume = m_pVolumeFirst;
  135. while(pVolume)
  136. {
  137. cVolumes++;
  138. pVolume = pVolume->m_pVolumeNext;
  139. }
  140. return cVolumes;
  141. }
  142. const CVsTstVolumeInfo *CVsTstVolumeList::GetVolumeInfo(UINT iVolume)
  143. {
  144. CVsTstVolumeInfo *pVolume = m_pVolumeFirst;
  145. while(pVolume && iVolume > 0)
  146. {
  147. pVolume = pVolume->m_pVolumeNext;
  148. iVolume--;
  149. }
  150. return pVolume;
  151. }