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.

150 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. sync.c
  5. Abstract:
  6. This is the main module for the Win32 sync command.
  7. Author:
  8. Mark Lucovsky (markl) 28-Jan-1991
  9. Revision History:
  10. --*/
  11. #include "sync.h"
  12. int
  13. __cdecl main( argc, argv )
  14. int argc;
  15. char *argv[];
  16. {
  17. BOOLEAN fEject;
  18. char *p;
  19. int i;
  20. char c;
  21. char DrivePath[ 4 ];
  22. if (argc > 1 &&
  23. (!_stricmp( argv[1], "-e" ) || !_stricmp( argv[1], "/e" ))
  24. ) {
  25. argc -= 1;
  26. argv += 1;
  27. fEject = TRUE;
  28. }
  29. else {
  30. fEject = FALSE;
  31. }
  32. if ( argc > 1 ) {
  33. while (--argc) {
  34. p = *++argv;
  35. if ( isalpha(*p) ) {
  36. sprintf( DrivePath, "%c:", *p );
  37. SyncVolume( DrivePath, fEject );
  38. }
  39. }
  40. }
  41. else {
  42. for(i=0;i<26;i++){
  43. c = (CHAR)i + (CHAR)'a';
  44. sprintf( DrivePath, "%c:", i+'A' );
  45. switch (GetDriveType( DrivePath )) {
  46. case DRIVE_REMOVABLE:
  47. if (i <2) {
  48. break;
  49. }
  50. case DRIVE_FIXED:
  51. SyncVolume( DrivePath, fEject );
  52. break;
  53. }
  54. }
  55. }
  56. return( 0 );
  57. }
  58. void
  59. SyncVolume(
  60. PCHAR DrivePath,
  61. BOOLEAN EjectMedia
  62. )
  63. {
  64. UCHAR VolumePath[16];
  65. HANDLE VolumeHandle;
  66. DWORD ReturnedByteCount;
  67. _strupr( DrivePath );
  68. sprintf( VolumePath, "\\\\.\\%s", DrivePath );
  69. VolumeHandle = CreateFile( VolumePath,
  70. GENERIC_READ | GENERIC_WRITE,
  71. FILE_SHARE_READ | FILE_SHARE_WRITE,
  72. NULL,
  73. OPEN_EXISTING,
  74. 0,
  75. NULL
  76. );
  77. if (VolumeHandle == INVALID_HANDLE_VALUE ) {
  78. fprintf( stderr, "SYNC: Unable to open %s volume (%u)\n", DrivePath, GetLastError() );
  79. return;
  80. }
  81. printf( "Syncing %s ... ", DrivePath );
  82. if (!FlushFileBuffers( VolumeHandle )) {
  83. printf( "flush failed (%u)\n", GetLastError() );
  84. }
  85. else
  86. if (!DeviceIoControl( VolumeHandle,
  87. FSCTL_LOCK_VOLUME,
  88. NULL,
  89. 0,
  90. NULL,
  91. 0,
  92. &ReturnedByteCount,
  93. NULL
  94. )
  95. ) {
  96. printf( "lock volume failed (%u)\n", GetLastError() );
  97. }
  98. else
  99. if (!DeviceIoControl( VolumeHandle,
  100. FSCTL_DISMOUNT_VOLUME,
  101. NULL,
  102. 0,
  103. NULL,
  104. 0,
  105. &ReturnedByteCount,
  106. NULL
  107. )
  108. ) {
  109. printf( "dismount volume failed (%u)\n", GetLastError() );
  110. }
  111. else
  112. if (EjectMedia && !DeviceIoControl( VolumeHandle,
  113. IOCTL_DISK_EJECT_MEDIA,
  114. NULL,
  115. 0,
  116. NULL,
  117. 0,
  118. &ReturnedByteCount,
  119. NULL
  120. )
  121. ) {
  122. printf( "eject media failed (%u)\n", GetLastError() );
  123. }
  124. else {
  125. printf( "done. Okay to remove drive.\n" );
  126. }
  127. CloseHandle( VolumeHandle );
  128. return;
  129. }