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.

202 lines
5.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. //--------------------------------------------------------------------------
  7. #include <pch.cxx>
  8. #pragma hdrstop
  9. #include <ole2.h>
  10. #include "trkwks.hxx"
  11. #include "trksvr.hxx"
  12. #include "dltadmin.hxx"
  13. BOOL
  14. DltAdminBackupRead( ULONG cArgs, const TCHAR * const rgptszArgs[], ULONG *pcEaten )
  15. {
  16. NTSTATUS status;
  17. HRESULT hr = S_OK;;
  18. HANDLE hSource = NULL;
  19. HANDLE hBackup = NULL;
  20. BYTE rgb[ 8 * 1024 ];
  21. ULONG cbRead, cbWritten;
  22. void *pvBackup = NULL;
  23. if( 1 <= cArgs && IsHelpArgument( rgptszArgs[0] ))
  24. {
  25. printf( "\nOption BackupRead\n"
  26. " Purpose: Run the BackupRead API on a file\n"
  27. " Usage: -backupread <file to be read> <read data>\n"
  28. " E.g.: -backupread file.tst file.tst.bak\n" );
  29. *pcEaten = 1;
  30. return( TRUE );
  31. }
  32. if( 2 > cArgs )
  33. {
  34. printf( "Invalid parameters. Use -? for usage info\n" );
  35. *pcEaten = 0;
  36. return( FALSE );
  37. }
  38. // Open the source file
  39. hSource = CreateFile( rgptszArgs[0],
  40. GENERIC_READ,
  41. FILE_SHARE_READ,
  42. NULL,
  43. OPEN_EXISTING,
  44. 0,
  45. NULL );
  46. if( INVALID_HANDLE_VALUE == hSource )
  47. {
  48. printf( "Failed to open file (%lu)\n", GetLastError() );
  49. return FALSE;
  50. }
  51. // Open the backup file
  52. hBackup = CreateFile( rgptszArgs[1],
  53. GENERIC_READ | GENERIC_WRITE,
  54. 0,
  55. NULL,
  56. CREATE_ALWAYS,
  57. 0,
  58. NULL );
  59. if( INVALID_HANDLE_VALUE == hBackup )
  60. {
  61. printf( "Failed to open backup file (%lu)\n", GetLastError() );
  62. return FALSE;
  63. }
  64. while( TRUE )
  65. {
  66. if( !BackupRead( hSource,
  67. rgb,
  68. sizeof(rgb),
  69. &cbRead,
  70. FALSE,
  71. FALSE, //TRUE,
  72. &pvBackup ))
  73. {
  74. printf( "Failed BackupRead (%lu)\n", GetLastError() );
  75. return FALSE;
  76. }
  77. if( !WriteFile( hBackup, rgb, cbRead, &cbWritten, NULL ))
  78. {
  79. printf( "Failed WriteFile (%lu)\n", GetLastError() );
  80. return FALSE;
  81. }
  82. if( cbRead < sizeof(rgb) )
  83. break;
  84. }
  85. // Free resources
  86. BackupRead( hSource, rgb, sizeof(rgb), &cbRead, TRUE, TRUE, &pvBackup );
  87. CloseHandle( hSource );
  88. CloseHandle( hBackup );
  89. return TRUE;
  90. }
  91. BOOL
  92. DltAdminBackupWrite( ULONG cArgs, const TCHAR * const rgptszArgs[], ULONG *pcEaten )
  93. {
  94. NTSTATUS status;
  95. HRESULT hr = S_OK;;
  96. HANDLE hRestore = NULL;
  97. HANDLE hBackup = NULL;
  98. BYTE rgb[ 8 * 1024 ];
  99. ULONG cbRead, cbWritten;
  100. void *pvBackup = NULL;
  101. if( 1 <= cArgs && IsHelpArgument( rgptszArgs[0] ))
  102. {
  103. printf( "\nOption BackupWrite\n"
  104. " Purpose: Run the BackupWrite API on a file\n"
  105. " Usage: -backupread <backup file> <restored file>\n"
  106. " E.g.: -backupread file.tst.bak file.tst\n" );
  107. *pcEaten = 1;
  108. return( TRUE );
  109. }
  110. if( 2 > cArgs )
  111. {
  112. printf( "Invalid parameters. Use -? for usage info\n" );
  113. *pcEaten = 0;
  114. return( FALSE );
  115. }
  116. // Open the backup file
  117. hBackup = CreateFile( rgptszArgs[0],
  118. GENERIC_READ,
  119. FILE_SHARE_READ,
  120. NULL,
  121. OPEN_EXISTING,
  122. 0,
  123. NULL );
  124. if( INVALID_HANDLE_VALUE == hBackup )
  125. {
  126. printf( "Failed to open backup file (%lu)\n", GetLastError() );
  127. return FALSE;
  128. }
  129. // Open the restore file
  130. hRestore = CreateFile( rgptszArgs[1],
  131. GENERIC_READ | GENERIC_WRITE | WRITE_DAC,
  132. 0,
  133. NULL,
  134. CREATE_ALWAYS,
  135. FILE_FLAG_BACKUP_SEMANTICS,
  136. NULL );
  137. if( INVALID_HANDLE_VALUE == hRestore )
  138. {
  139. printf( "Failed to open restore file (%lu)\n", GetLastError() );
  140. return FALSE;
  141. }
  142. while( TRUE )
  143. {
  144. if( !ReadFile( hBackup, rgb, sizeof(rgb), &cbRead, NULL ))
  145. {
  146. printf( "Failed ReadFile (%lu)\n", GetLastError() );
  147. return FALSE;
  148. }
  149. if( !BackupWrite( hRestore,
  150. rgb,
  151. cbRead,
  152. &cbWritten,
  153. FALSE,
  154. FALSE, //TRUE,
  155. &pvBackup ))
  156. {
  157. printf( "Failed BackupWrite (%lu)\n", GetLastError() );
  158. return FALSE;
  159. }
  160. if( cbRead < sizeof(rgb) )
  161. break;
  162. }
  163. // Free resources
  164. BackupWrite( hRestore, rgb, 0, &cbWritten, TRUE, TRUE, &pvBackup );
  165. CloseHandle( hRestore );
  166. CloseHandle( hBackup );
  167. return TRUE;
  168. }