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.

143 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. regsav.c
  5. Abstract:
  6. This module contains routine for compacting the hive file
  7. Author:
  8. Dragos C. Sambotin (dragoss) 30-Dec-1998
  9. Revision History:
  10. --*/
  11. #include "chkreg.h"
  12. #define TEMP_KEY_NAME TEXT("chkreg___$$Temp$Hive$$___")
  13. extern TCHAR *Hive;
  14. // to store the name of the compacted hive
  15. TCHAR CompactedHive[MAX_PATH];
  16. VOID
  17. DoCompactHive()
  18. /*
  19. Routine Description:
  20. Compacts a hive. It uses the LoadKey/SaveKey/UnloadKey sequence.
  21. The hive is temporary loaded under the key HKLM\TEMP_KEY_NAME.
  22. After compacting, the hive is unloaded (cleaning process).
  23. Arguments:
  24. None.
  25. Return Value:
  26. NONE.
  27. */
  28. {
  29. NTSTATUS Status;
  30. BOOLEAN OldPrivState;
  31. LONG Err;
  32. HKEY hkey;
  33. // construct the file name for the compacted hive
  34. if(!lstrcpy(CompactedHive,Hive) ) {
  35. fprintf(stderr,"Unable to generate new Hive file name\n");
  36. return;
  37. }
  38. if(!lstrcat(CompactedHive,TEXT(".BAK")) ) {
  39. fprintf(stderr,"Unable to generate new Hive file name\n");
  40. return;
  41. }
  42. // Attempt to get restore privilege
  43. Status = RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE,
  44. TRUE,
  45. FALSE,
  46. &OldPrivState);
  47. if (!NT_SUCCESS(Status)) {
  48. printf("Could not adjust privilege; status = 0x%lx\n",Status);
  49. return;
  50. }
  51. // Load the hive into registry
  52. Err = RegLoadKey(HKEY_LOCAL_MACHINE,TEMP_KEY_NAME,Hive);
  53. if( Err != ERROR_SUCCESS ) {
  54. fprintf(stderr,"Failed to load the Hive; error 0x%lx \n",Err);
  55. } else {
  56. Err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  57. TEMP_KEY_NAME,
  58. REG_OPTION_RESERVED,
  59. KEY_READ,
  60. &hkey);
  61. if (Err == ERROR_SUCCESS) {
  62. // Restore old privilege if necessary.
  63. if (!OldPrivState) {
  64. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE,
  65. FALSE,
  66. FALSE,
  67. &OldPrivState);
  68. }
  69. RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE,
  70. TRUE,
  71. FALSE,
  72. &OldPrivState);
  73. // Save the key into the new file name.
  74. // The CmpCopyTree function will take care of compacting also.
  75. Err = RegSaveKey(hkey,CompactedHive,NULL);
  76. if( Err != ERROR_SUCCESS ) {
  77. fprintf(stderr,"Failed to Save the Hive; error 0x%lx \n",Err);
  78. }
  79. if (!OldPrivState) {
  80. RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE,
  81. FALSE,
  82. FALSE,
  83. &OldPrivState);
  84. }
  85. RegCloseKey(hkey);
  86. }
  87. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE,
  88. TRUE,
  89. FALSE,
  90. &OldPrivState);
  91. // cleanup the registry machine hive.
  92. Err = RegUnLoadKey(HKEY_LOCAL_MACHINE,TEMP_KEY_NAME);
  93. if( Err != ERROR_SUCCESS ) {
  94. fprintf(stderr,"Failed to unload the Hive; error 0x%lx \n",Err);
  95. }
  96. if (!OldPrivState) {
  97. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE,
  98. FALSE,
  99. FALSE,
  100. &OldPrivState);
  101. }
  102. }
  103. }