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.

153 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1989-1997 Microsoft Corporation
  3. Module Name:
  4. bkup.c - test backup read / write
  5. Abstract:
  6. Driver for backup read/write
  7. --*/
  8. extern "C" {
  9. #include <nt.h>
  10. #include <ntioapi.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. }
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <tchar.h>
  18. //
  19. //
  20. //
  21. #define SHIFTBYN(c,v,n) ((c) -= (n), (v) += (n))
  22. #define SHIFT(c,v) SHIFTBYN( (c), (v), 1 )
  23. void Usage( void )
  24. {
  25. fprintf( stderr, "Usage: bkup [-r file] performs backup read, output to stdout\n" );
  26. fprintf( stderr, " [-w backupimage newfile] performs backup write\n" );
  27. }
  28. int __cdecl
  29. main (
  30. int argc,
  31. char **argv
  32. )
  33. {
  34. SHIFT( argc, argv );
  35. if (argc == 2 && !strcmp( *argv, "-r" )) {
  36. SHIFT( argc, argv );
  37. HANDLE h = CreateFile( *argv, // file name
  38. FILE_READ_DATA | FILE_READ_ATTRIBUTES,
  39. // desired access
  40. FILE_SHARE_READ, // share mode
  41. NULL, // security
  42. OPEN_EXISTING, // disposition
  43. 0, // flags and attributes
  44. 0 ); // template file
  45. if (h == INVALID_HANDLE_VALUE) {
  46. fprintf( stderr, "CreateFile returned %x\n", GetLastError( ));
  47. return 1;
  48. }
  49. BYTE *p = new BYTE[65536];
  50. ULONG Length, Written;
  51. PVOID Context = NULL;
  52. while (TRUE) {
  53. if (!BackupRead( h, p, 65536, &Length, FALSE, TRUE, &Context )) {
  54. fprintf( stderr, "BackupRead returned %x\n", GetLastError( ));
  55. return 1;
  56. }
  57. if (Length == 0) {
  58. break;
  59. }
  60. fprintf( stderr, "Transferring %x\n", Length );
  61. if (!WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), p, Length, &Written, NULL ))
  62. {
  63. fprintf( stderr, "WriteFile returned %x\n", GetLastError( ));
  64. return 1;
  65. }
  66. }
  67. } else if (argc == 3 && !strcmp( *argv, "-w" )) {
  68. SHIFT( argc, argv );
  69. HANDLE hOld = CreateFile( *argv, // file name
  70. FILE_READ_DATA, // desired access
  71. FILE_SHARE_READ, // share mode
  72. NULL, // security
  73. OPEN_EXISTING, // disposition
  74. 0, // flags and attributes
  75. 0 ); // template file
  76. if (hOld == INVALID_HANDLE_VALUE) {
  77. fprintf( stderr, "CreateFile destination returned %x\n", GetLastError( ));
  78. return 1;
  79. }
  80. SHIFT( argc, argv );
  81. HANDLE hNew = CreateFile( *argv, // file name
  82. FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES,
  83. // desired access
  84. FALSE, // share mode
  85. NULL, // security
  86. CREATE_ALWAYS, // disposition
  87. 0, // flags and attributes
  88. 0 ); // template file
  89. if (hNew == INVALID_HANDLE_VALUE) {
  90. fprintf( stderr, "CreateFile destination returned %x\n", GetLastError( ));
  91. return 1;
  92. }
  93. BYTE *p = new BYTE[127];
  94. ULONG Length, Written;
  95. PVOID Context = NULL;
  96. while (TRUE) {
  97. if (!ReadFile( hOld, p, 127, &Length, NULL )) {
  98. fprintf( stderr, "ReadFile returned %x\n", GetLastError( ));
  99. return 1;
  100. }
  101. if (Length == 0) {
  102. break;
  103. }
  104. fprintf( stderr, "Transferring %x\n", Length );
  105. if (!BackupWrite( hNew, p, Length, &Written, FALSE, TRUE, &Context )) {
  106. fprintf( stderr, "BackupWrite returned %x\n", GetLastError( ));
  107. return 1;
  108. }
  109. }
  110. } else {
  111. Usage( );
  112. return 1;
  113. }
  114. return 0;
  115. }