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.

193 lines
5.3 KiB

  1. // Restore Remote Storage Engine database from backup directory
  2. //
  3. // Usage: RsTore backup_dir
  4. // backup_dir - location of backup directory
  5. // The database is restored to the current directory
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <wchar.h>
  10. #include "esent.h"
  11. // Local data
  12. WCHAR *backup_dir;
  13. WCHAR *usage = L"RsTore <backup-directory>";
  14. // Local functions
  15. HRESULT FileCount(WCHAR* Pattern, LONG* pCount);
  16. HRESULT parseCommand(int argc, wchar_t *argv[]);
  17. #define WsbCatch(hr) \
  18. catch(HRESULT catchHr) { \
  19. hr = catchHr; \
  20. }
  21. // FileCount - count files matching the pattern
  22. HRESULT FileCount(WCHAR* Pattern, LONG* pCount)
  23. {
  24. DWORD err;
  25. WIN32_FIND_DATA FindData;
  26. HANDLE hFind;
  27. HRESULT hr = S_OK;
  28. int nCount = 0;
  29. int nSkipped = 0;
  30. try {
  31. hFind = FindFirstFile(Pattern, &FindData);
  32. if (INVALID_HANDLE_VALUE == hFind) {
  33. err = GetLastError();
  34. throw(HRESULT_FROM_WIN32(err));
  35. }
  36. while (TRUE) {
  37. if (FindData.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY |
  38. FILE_ATTRIBUTE_HIDDEN)) {
  39. // Don't count system files (such as "." and "..")
  40. nSkipped++;
  41. } else {
  42. nCount++;
  43. }
  44. if (!FindNextFile(hFind, &FindData)) {
  45. err = GetLastError();
  46. if (ERROR_NO_MORE_FILES == err) break;
  47. throw(HRESULT_FROM_WIN32(err));
  48. }
  49. }
  50. } WsbCatch(hr);
  51. *pCount = nCount;
  52. return(hr);
  53. }
  54. // parseCommand - Parse the command line
  55. HRESULT parseCommand(int argc, wchar_t *argv[])
  56. {
  57. HRESULT hr = E_FAIL;
  58. try {
  59. int i;
  60. // There should be cmd name + one parameters.
  61. if (argc != 2) {
  62. throw (E_FAIL);
  63. }
  64. for (i = 1; i < argc; i++) {
  65. if (WCHAR('-') == argv[i][0]) {
  66. throw(E_FAIL);
  67. } else {
  68. backup_dir = argv[i];
  69. hr = S_OK;
  70. }
  71. }
  72. } WsbCatch(hr);
  73. return(hr);
  74. }
  75. // wmain - Main function
  76. extern "C"
  77. int _cdecl wmain(int argc, wchar_t *argv[])
  78. {
  79. HRESULT hr = S_OK;
  80. try {
  81. hr = parseCommand(argc, argv);
  82. if (!SUCCEEDED(hr)) {
  83. printf("Command line is incorrect\n%ls\n", usage);
  84. return -1;
  85. }
  86. try {
  87. PCHAR cbdir = NULL;
  88. LONG count;
  89. ULONG size;
  90. JET_ERR jstat;
  91. WCHAR *pattern;
  92. //
  93. // Allocate memory for the string
  94. //
  95. size = wcslen(backup_dir) + 20;
  96. pattern = new WCHAR[size];
  97. if (pattern == NULL) {
  98. throw(E_OUTOFMEMORY);
  99. }
  100. // Check that there's a HSM DB to restore
  101. wcscpy(pattern, backup_dir);
  102. wcscat(pattern, L"\\*.jet");
  103. hr = FileCount(pattern, &count);
  104. delete pattern;
  105. pattern = 0;
  106. if (S_OK != hr || count == 0) {
  107. printf("No Remote Storage databases were found in the given\n");
  108. printf("directory: %ls\n", static_cast<WCHAR*>(backup_dir));
  109. printf("Please enter the directory containing the backup files.\n");
  110. throw(E_FAIL);
  111. }
  112. // Check that the current directory is empty
  113. pattern = L".\\*";
  114. hr = FileCount(pattern, &count);
  115. if (S_OK != hr || count != 0) {
  116. printf("The current directory is not empty\n");
  117. printf("The database restore can only be done to an empty directory.\n");
  118. throw(E_FAIL);
  119. }
  120. // Set the log size to avoid an error JetRestore
  121. jstat = JetSetSystemParameter(0, 0, JET_paramLogFileSize,
  122. 64, NULL);
  123. if (JET_errSuccess != jstat) {
  124. printf("JetSetSystemParameter(JET_paramLogFileSize) failed, JET error = %ld\n", (LONG)jstat);
  125. throw(E_FAIL);
  126. }
  127. // Try the restore
  128. size_t tempsize = wcstombs(0, backup_dir, 0);
  129. if (tempsize == (size_t)-1) {
  130. throw(E_INVALIDARG);
  131. }
  132. size = tempsize + 1;
  133. cbdir = new CHAR[size];
  134. if (cbdir == NULL) {
  135. throw(E_OUTOFMEMORY);
  136. }
  137. wcstombs(cbdir, backup_dir, size);
  138. jstat = JetRestore(cbdir, NULL);
  139. if (JET_errSuccess == jstat) {
  140. printf("Restore succeeded\n");
  141. hr = S_OK;
  142. } else {
  143. printf("Restore failed, JET error = %ld\n", (LONG)jstat);
  144. hr = E_FAIL;
  145. }
  146. delete cbdir;
  147. cbdir = 0;
  148. } WsbCatch(hr);
  149. } WsbCatch(hr);
  150. if (SUCCEEDED(hr)) {
  151. return(0);
  152. }
  153. return -1;
  154. }