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.

152 lines
4.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: tbackup.cxx
  7. //
  8. // Contents: testing backup read/write
  9. //
  10. // History: 1-Aug-97 weiruc Created.
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #define TRKDATA_ALLOCATE
  16. #include <trkwks.hxx>
  17. #include <cfiletim.hxx>
  18. #include <ocidl.h>
  19. // DWORD g_Debug = TRKDBG_ERROR;
  20. #define BUFFERSIZE 1000
  21. EXTERN_C void __cdecl _tmain(int argc, TCHAR **argv)
  22. {
  23. HANDLE hFile = INVALID_HANDLE_VALUE;
  24. HANDLE hBackupFile = INVALID_HANDLE_VALUE;
  25. BYTE rgbBuffer[BUFFERSIZE];
  26. DWORD dwBytesRead = 0;
  27. DWORD dwBytesWritten = 0;
  28. LPVOID pReadContext = NULL;
  29. LPVOID pWriteContext = NULL;
  30. BOOL fReadSuccessful = TRUE;
  31. if(argc != 3)
  32. {
  33. _tprintf(TEXT("usage: %s <testfile> <backupfile>\n"), argv[0]);
  34. goto Exit;
  35. }
  36. EnablePrivilege( SE_RESTORE_NAME );
  37. // open test file
  38. hFile = CreateFile(argv[1],
  39. GENERIC_READ,
  40. FILE_SHARE_READ,
  41. NULL,
  42. OPEN_EXISTING,
  43. FILE_ATTRIBUTE_NORMAL,
  44. NULL);
  45. if(INVALID_HANDLE_VALUE == hFile)
  46. {
  47. _tprintf(TEXT("Can't open (%s), %08x\n"), argv[1], GetLastError());
  48. goto Exit;
  49. }
  50. // open backup file
  51. hBackupFile = CreateFile(argv[2],
  52. GENERIC_READ | GENERIC_WRITE,
  53. FILE_SHARE_READ | FILE_SHARE_WRITE,
  54. NULL,
  55. CREATE_NEW,
  56. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
  57. NULL);
  58. if(INVALID_HANDLE_VALUE == hBackupFile)
  59. {
  60. _tprintf(TEXT("Can't open (%s), %08x\n"), argv[2], GetLastError());
  61. goto Exit;
  62. }
  63. // All we are doing is to backup read a file and backup write the file to
  64. // a different file. We are assuming the file is smaller than the
  65. // BUFFERSIZE.
  66. while(TRUE)
  67. {
  68. if(!BackupRead(hFile,
  69. rgbBuffer,
  70. BUFFERSIZE,
  71. &dwBytesRead,
  72. FALSE,
  73. FALSE,
  74. &pReadContext))
  75. {
  76. _tprintf(TEXT("BackupRead failed, %08x\n"), GetLastError());
  77. break;
  78. }
  79. else
  80. {
  81. _tprintf(TEXT(" %d bytes read\n"), dwBytesRead);
  82. }
  83. if(0 == dwBytesRead)
  84. {
  85. break;
  86. }
  87. if(!BackupWrite(hBackupFile,
  88. rgbBuffer,
  89. dwBytesRead,
  90. &dwBytesWritten,
  91. FALSE,
  92. FALSE,
  93. &pWriteContext))
  94. {
  95. _tprintf(TEXT("BackupWrite failed, %08x\n"), GetLastError());
  96. break;
  97. }
  98. else
  99. {
  100. _tprintf(TEXT(" %d bytes wrote\n"), dwBytesWritten);
  101. }
  102. }
  103. // Deallocate data structures used by BackupRead/Write.
  104. if(!BackupRead(hFile,
  105. rgbBuffer,
  106. BUFFERSIZE,
  107. &dwBytesRead,
  108. TRUE,
  109. TRUE,
  110. &pReadContext))
  111. {
  112. _tprintf(TEXT("Last BackupRead failed, %08x\n"), GetLastError());
  113. }
  114. if(!BackupWrite(hBackupFile,
  115. rgbBuffer,
  116. dwBytesRead,
  117. &dwBytesWritten,
  118. TRUE,
  119. TRUE,
  120. &pWriteContext))
  121. {
  122. _tprintf(TEXT("Last BackupWrite failed, %08x\n"), GetLastError());
  123. }
  124. Exit:
  125. if(INVALID_HANDLE_VALUE != hFile)
  126. {
  127. if(!CloseHandle(hFile))
  128. {
  129. _tprintf(TEXT("Can't close (%s), %08x\n"), argv[1], GetLastError());
  130. }
  131. }
  132. if(INVALID_HANDLE_VALUE != hBackupFile)
  133. {
  134. if(!CloseHandle(hBackupFile))
  135. {
  136. _tprintf(TEXT("Can't close (%s), %08x\n"), argv[2], GetLastError());
  137. }
  138. }
  139. }