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.

210 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. logdrive.cpp
  5. Abstract:
  6. SIS Groveler logging drive class
  7. Authors:
  8. John Douceur, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "all.hxx"
  14. static const _TCHAR registry_parameter_path[] = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Groveler\\Parameters");
  15. static const _TCHAR *log_drive_key_name = _T("log drive");
  16. LogDrive::LogDrive()
  17. {
  18. _TCHAR *guid_name;
  19. part_initted = 0;
  20. registry_written = false;
  21. log_drive_index = 0;
  22. _TCHAR reg_name[MAX_PATH + 1];
  23. read_registry(reg_name,sizeof(reg_name));
  24. int partition_count = sis_drives.partition_count();
  25. ASSERT(partition_count > 0);
  26. part_initted = new bool[partition_count];
  27. for (int index = 0; index < partition_count; index++)
  28. {
  29. part_initted[index] = false;
  30. }
  31. for (index = 0; index < partition_count; index++)
  32. {
  33. guid_name = sis_drives.partition_guid_name(index);
  34. if (_tcsicmp(reg_name, guid_name) == 0)
  35. {
  36. log_drive_index = index;
  37. write_registry(_T(""));
  38. return;
  39. }
  40. }
  41. __int64 log_drive_space = 0;
  42. for (index = 0; index < partition_count; index++)
  43. {
  44. guid_name = sis_drives.partition_guid_name(index);
  45. ULARGE_INTEGER my_free_bytes;
  46. ULARGE_INTEGER total_bytes;
  47. ULARGE_INTEGER free_bytes;
  48. int ok = GetDiskFreeSpaceEx(
  49. guid_name, &my_free_bytes, &total_bytes, &free_bytes);
  50. if (ok)
  51. {
  52. __int64 space = free_bytes.QuadPart;
  53. if (space > log_drive_space)
  54. {
  55. log_drive_space = space;
  56. log_drive_index = index;
  57. }
  58. }
  59. else
  60. {
  61. DWORD err = GetLastError();
  62. PRINT_DEBUG_MSG((_T("GROVELER: GetDiskFreeSpaceEx() failed with error %d\n"),
  63. err));
  64. }
  65. }
  66. write_registry(_T(""));
  67. }
  68. LogDrive::~LogDrive()
  69. {
  70. if (part_initted != 0)
  71. {
  72. delete[] part_initted;
  73. part_initted = 0;
  74. }
  75. }
  76. void
  77. LogDrive::partition_initialized(
  78. int partition_index)
  79. {
  80. if (!registry_written)
  81. {
  82. int partition_count = sis_drives.partition_count();
  83. ASSERT(partition_index >= 0);
  84. ASSERT(partition_index < partition_count);
  85. part_initted[partition_index] = true;
  86. for (int index = 0; index < partition_count; index++)
  87. {
  88. if (!part_initted[index])
  89. {
  90. return;
  91. }
  92. }
  93. write_registry(sis_drives.partition_guid_name(log_drive_index));
  94. registry_written = true;
  95. }
  96. }
  97. bool
  98. LogDrive::read_registry(
  99. _TCHAR *name,
  100. DWORD size)
  101. {
  102. DWORD type;
  103. try
  104. {
  105. HKEY path_key = 0;
  106. Registry::open_key_ex(HKEY_LOCAL_MACHINE,
  107. registry_parameter_path, REG_OPTION_NON_VOLATILE,
  108. KEY_ALL_ACCESS, &path_key);
  109. try
  110. {
  111. Registry::query_value_ex(path_key, log_drive_key_name, 0,
  112. &type, (BYTE *)name, &size);
  113. }
  114. catch (DWORD result)
  115. {
  116. ASSERT(result != ERROR_SUCCESS);
  117. PRINT_DEBUG_MSG((
  118. _T("GROVELER: Registry::query_value_ex() failed with error %d\n"),
  119. result));
  120. ASSERT(path_key != 0);
  121. Registry::close_key(path_key);
  122. path_key = 0;
  123. *name = 0;
  124. return false;
  125. }
  126. ASSERT(path_key != 0);
  127. Registry::close_key(path_key);
  128. path_key = 0;
  129. }
  130. catch (DWORD result)
  131. {
  132. ASSERT(result != ERROR_SUCCESS);
  133. PRINT_DEBUG_MSG((_T("GROVELER: Registry::open_key_ex() or Registry::close_key() ")
  134. _T("failed with error %d\n"), result));
  135. *name = 0;
  136. return false;
  137. }
  138. if (type != REG_EXPAND_SZ)
  139. {
  140. *name = 0;
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool
  146. LogDrive::write_registry(
  147. _TCHAR *name)
  148. {
  149. try
  150. {
  151. HKEY path_key = 0;
  152. DWORD disp;
  153. Registry::create_key_ex(HKEY_LOCAL_MACHINE,
  154. registry_parameter_path, 0, 0, REG_OPTION_NON_VOLATILE,
  155. KEY_ALL_ACCESS, 0, &path_key, &disp);
  156. try
  157. {
  158. Registry::set_value_ex(path_key, log_drive_key_name, 0,
  159. REG_EXPAND_SZ, (BYTE *)name,
  160. (_tcslen(name) + 1) * sizeof(_TCHAR));
  161. }
  162. catch (DWORD result)
  163. {
  164. ASSERT(result != ERROR_SUCCESS);
  165. PRINT_DEBUG_MSG((
  166. _T("GROVELER: Registry::set_value_ex() failed with error %d\n"),
  167. result));
  168. ASSERT(path_key != 0);
  169. Registry::close_key(path_key);
  170. path_key = 0;
  171. return false;
  172. }
  173. ASSERT(path_key != 0);
  174. Registry::close_key(path_key);
  175. path_key = 0;
  176. }
  177. catch (DWORD result)
  178. {
  179. ASSERT(result != ERROR_SUCCESS);
  180. PRINT_DEBUG_MSG((_T("GROVELER: Registry::create_key_ex() or Registry::close_key() ")
  181. _T("failed with error %d\n"), result));
  182. return false;
  183. }
  184. return true;
  185. }