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.

166 lines
5.7 KiB

  1. #include <windows.h>
  2. #include <winioctl.h>
  3. #include <ntddsnap.h>
  4. #include <stdio.h>
  5. #include <objbase.h>
  6. void __cdecl
  7. main(
  8. int argc,
  9. char** argv
  10. )
  11. {
  12. WCHAR driveName[10];
  13. HANDLE handle, h;
  14. BOOL b;
  15. DWORD bytes;
  16. PVOLSNAP_NAME name;
  17. WCHAR buffer[MAX_PATH];
  18. WCHAR originalVolumeName[MAX_PATH];
  19. VOLSNAP_NAMES names;
  20. PVOLSNAP_NAMES pnames, pn;
  21. PWCHAR p;
  22. VOLSNAP_DIFF_AREA_SIZES sizes;
  23. WCHAR globalrootName[MAX_PATH];
  24. WCHAR diffAreaName[MAX_PATH];
  25. FILETIME fileTime, localFileTime;
  26. SYSTEMTIME systemTime;
  27. VOLSNAP_CONFIG_INFO configInfo;
  28. if (argc != 2) {
  29. printf("usage: %s drive:\n", argv[0]);
  30. return;
  31. }
  32. swprintf(driveName, L"\\\\?\\%c:", toupper(argv[1][0]));
  33. handle = CreateFile(driveName, 0,
  34. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  35. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  36. INVALID_HANDLE_VALUE);
  37. if (handle == INVALID_HANDLE_VALUE) {
  38. printf("Could not open the given volume %d\n", GetLastError());
  39. return;
  40. }
  41. pnames = &names;
  42. b = DeviceIoControl(handle, IOCTL_VOLSNAP_QUERY_NAMES_OF_SNAPSHOTS,
  43. NULL, 0, &names, sizeof(names), &bytes, NULL);
  44. if (!b && GetLastError() == ERROR_MORE_DATA) {
  45. pnames = LocalAlloc(0, names.MultiSzLength + sizeof(VOLSNAP_NAMES));
  46. b = DeviceIoControl(handle, IOCTL_VOLSNAP_QUERY_NAMES_OF_SNAPSHOTS,
  47. NULL, 0, pnames, names.MultiSzLength +
  48. sizeof(VOLSNAP_NAMES), &bytes, NULL);
  49. }
  50. if (!b) {
  51. printf("Query names of snapshots failed with %d\n", GetLastError());
  52. return;
  53. }
  54. printf("Snapshots of this volume:\n\n");
  55. p = pnames->Names;
  56. while (*p) {
  57. swprintf(globalrootName, L"\\\\?\\GLOBALROOT%s", p);
  58. h = CreateFile(globalrootName, 0,
  59. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  60. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  61. INVALID_HANDLE_VALUE);
  62. if (h == INVALID_HANDLE_VALUE) {
  63. printf("Could not open %ws, failed with %d\n", globalrootName,
  64. GetLastError());
  65. return;
  66. }
  67. pn = (PVOLSNAP_NAMES) diffAreaName;
  68. b = DeviceIoControl(h, IOCTL_VOLSNAP_QUERY_DIFF_AREA, NULL, 0,
  69. pn, MAX_PATH*sizeof(WCHAR), &bytes, NULL);
  70. if (!b) {
  71. printf("QUERY_DIFF_AREA failed with %d\n", GetLastError());
  72. return;
  73. }
  74. b = DeviceIoControl(h, IOCTL_VOLSNAP_QUERY_DIFF_AREA_SIZES,
  75. NULL, 0, &sizes, sizeof(sizes), &bytes, NULL);
  76. if (!b) {
  77. printf("QUERY_DIFF_AREA_SIZES failed with %d\n", GetLastError());
  78. return;
  79. }
  80. b = DeviceIoControl(h, IOCTL_VOLSNAP_QUERY_CONFIG_INFO, NULL, 0,
  81. &configInfo, sizeof(configInfo), &bytes, NULL);
  82. if (!b) {
  83. printf("VOLSNAP_QUERY_CONFIG_INFO failed with %d\n", GetLastError());
  84. return;
  85. }
  86. CopyMemory(&fileTime, &configInfo.SnapshotCreationTime,
  87. sizeof(fileTime));
  88. b = FileTimeToLocalFileTime(&fileTime, &localFileTime);
  89. if (!b) {
  90. printf("FileTimeToLocalFileTime failed with %d\n", GetLastError());
  91. return;
  92. }
  93. b = FileTimeToSystemTime(&localFileTime, &systemTime);
  94. if (!b) {
  95. printf("FileTimeToSystemTime failed with %d\n", GetLastError());
  96. return;
  97. }
  98. printf(" %ws", p);
  99. if (configInfo.Attributes&VOLSNAP_ATTRIBUTE_PERSISTENT) {
  100. printf(" (PERSISTENT)");
  101. } else {
  102. printf(" (VOLATILE)");
  103. }
  104. printf(" %02d/%02d/%d %02d:%02d:%02d\n",
  105. systemTime.wMonth, systemTime.wDay, systemTime.wYear,
  106. systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
  107. printf(" %ws (%I64d, %I64d)\n\n", pn->Names,
  108. sizes.UsedVolumeSpace, sizes.AllocatedVolumeSpace);
  109. while (*p++) {
  110. }
  111. }
  112. printf("\n");
  113. pnames = &names;
  114. b = DeviceIoControl(handle, IOCTL_VOLSNAP_QUERY_DIFF_AREA,
  115. NULL, 0, &names, sizeof(names), &bytes, NULL);
  116. if (!b && GetLastError() == ERROR_MORE_DATA) {
  117. pnames = LocalAlloc(0, names.MultiSzLength + sizeof(VOLSNAP_NAMES));
  118. b = DeviceIoControl(handle, IOCTL_VOLSNAP_QUERY_DIFF_AREA,
  119. NULL, 0, pnames, names.MultiSzLength +
  120. sizeof(VOLSNAP_NAMES), &bytes, NULL);
  121. }
  122. if (!b) {
  123. printf("Query diff area failed with %d\n", GetLastError());
  124. return;
  125. }
  126. printf("Next Diff Area for this volume:\n");
  127. p = pnames->Names;
  128. while (*p) {
  129. printf(" %ws\n", p);
  130. while (*p++) {
  131. }
  132. }
  133. printf("\n");
  134. b = DeviceIoControl(handle, IOCTL_VOLSNAP_QUERY_DIFF_AREA_SIZES,
  135. NULL, 0, &sizes, sizeof(sizes), &bytes, NULL);
  136. if (!b) {
  137. printf("Query diff area sizes failed with %d\n", GetLastError());
  138. return;
  139. }
  140. printf("Total UsedVolumeSpace = %I64d\n", sizes.UsedVolumeSpace);
  141. printf("Total AllocatedVolumeSpace = %I64d\n", sizes.AllocatedVolumeSpace);
  142. printf("Total MaximumVolumeSpace = %I64d\n", sizes.MaximumVolumeSpace);
  143. }