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.

259 lines
7.8 KiB

  1. @echo on
  2. setlocal
  3. set __TEST=0
  4. if "%1" == "/n" set __TEST=1
  5. if "%1" == "/n" shift
  6. if "%1" == "-n" set __TEST=1
  7. if "%1" == "-n" shift
  8. if "%1" == "" goto usage
  9. if "%2" == "" goto usage
  10. rem -- detect whether 4Dos/NT or CMD is the shell
  11. if %@eval[2+2] == 4 goto altshell
  12. rem -----------------------------------------------------------------------
  13. rem -- iterate thru specified files, with CMD
  14. set SDMIRRORTMPFILE=%temp%\~sdmirror.tmp
  15. set __SRCCLIENT=%1
  16. set __SRCROOT=%2
  17. rem -- check for opened files
  18. sd opened > %SDMIRRORTMPFILE% 2>nul
  19. if errorlevel 1 goto dest_has_files_open
  20. findstr /b /r . %SDMIRRORTMPFILE% > nul
  21. if not errorlevel 1 goto dest_has_files_open
  22. rem -- setup: determine translation from source client paths to specified
  23. rem -- source path that's actually accessible. also determines
  24. rem -- destination client local path.
  25. sd client -o | findstr /b Root: > %SDMIRRORTMPFILE%
  26. if errorlevel 1 goto error_dest_client
  27. for /f "tokens=2" %%i in ( %SDMIRRORTMPFILE% ) do set __DESTROOT=%%i
  28. sd -c %__SRCCLIENT% client -o | findstr /b Root: > %SDMIRRORTMPFILE%
  29. if errorlevel 1 goto error_source_client
  30. for /f "tokens=2" %%i in ( %SDMIRRORTMPFILE% ) do set __SRCXLATE=%%i
  31. echo Destination root is: %__DESTROOT%
  32. echo Source root is: %__SRCROOT% (accessible)
  33. echo Translation root is: %__SRCXLATE% (actual client root)
  34. rem -- sync to the source client
  35. echo Sync to client %1...
  36. if %__TEST% == 1 echo ( sd sync %__DESTROOT%\...@%1 )
  37. if %__TEST% == 0 sd sync %__DESTROOT%\...@%1
  38. rem -- open same files for edit on the destination as on the source
  39. sd -c %__SRCCLIENT% opened -l > %SDMIRRORTMPFILE% 2>nul
  40. for /f "tokens=1,2 delims=#" %%i in ( %SDMIRRORTMPFILE% ) do call :cmd_perfile_open "%%i" "%%j"
  41. del %SDMIRRORTMPFILE%
  42. goto :eof
  43. rem -- subroutine: open file on destination for same operation as on source
  44. :cmd_perfile_open
  45. @echo off
  46. set SDMIRRORTMPFILE2=%temp%\~sdmirror2.cmd
  47. set SDMIRRORTMPFILE3=%temp%\~sdmirror3.tmp
  48. rem translate source client local filename to locally accessible source and destination filenames
  49. echo set _xlate_=%11>%SDMIRRORTMPFILE2%
  50. echo set _xlate_src_=^%%_xlate_:%__SRCXLATE%=%__SRCROOT%%%>> %SDMIRRORTMPFILE2%
  51. echo set _xlate_dest_=^%%_xlate_:%__SRCXLATE%=%__DESTROOT%%%>> %SDMIRRORTMPFILE2%
  52. echo echo %%_xlate_src_%%#%%_xlate_dest_%%^> %SDMIRRORTMPFILE3% >> %SDMIRRORTMPFILE2%
  53. call %SDMIRRORTMPFILE2%
  54. for /f "tokens=1,2 delims=#" %%i in ( %SDMIRRORTMPFILE3% ) do ( set _xlate_src_=%%i& set _xlate_dest_=%%j)
  55. for /f "tokens=3" %%i in ( %2 ) do set __word=%%i
  56. if "%__word%" == "delete" goto cpo_delete
  57. if "%__word%" == "edit" goto cpo_edit
  58. if "%__word%" == "add" goto cpo_add
  59. if "%__word%" == "branch" goto cpo_add2
  60. if "%__word%" == "integrate" goto cpo_edit2
  61. echo warning: unknown action '%__word%', no action performed.
  62. goto cpo_cleanup
  63. :cpo_delete
  64. if %__TEST% == 1 echo delete %_xlate_dest_%
  65. if %__TEST% == 1 goto cpo_cleanup
  66. sd delete %_xlate_dest_%
  67. goto cpo_cleanup
  68. :cpo_edit2
  69. echo warning: substituting action 'edit' instead of action '%__word%'.
  70. :cpo_edit
  71. if %__TEST% == 1 echo edit %_xlate_dest_%
  72. if %__TEST% == 1 echo copy %_xlate_src_% -^> %_xlate_dest_%
  73. if %__TEST% == 1 goto cpo_cleanup
  74. sd edit %_xlate_dest_%
  75. copy %_xlate_src_% %_xlate_dest_%
  76. goto cpo_cleanup
  77. :cpo_add2
  78. echo warning: substituting action 'add' instead of action '%__word%'.
  79. :cpo_add
  80. if %__TEST% == 1 echo add %_xlate_dest_%
  81. if %__TEST% == 1 echo copy %_xlate_src_% -^> %_xlate_dest_%
  82. if %__TEST% == 1 goto cpo_cleanup
  83. copy %_xlate_src_% %_xlate_dest_%
  84. sd add %_xlate_dest_%
  85. goto cpo_cleanup
  86. :cpo_cleanup
  87. rem del %SDMIRRORTMPFILE2%
  88. del %SDMIRRORTMPFILE3%
  89. goto :eof
  90. rem -----------------------------------------------------------------------
  91. :altshell
  92. rem -- iterate thru specified files, with 4Dos/NT
  93. set SDMIRRORTMPFILE=%temp%\~sdmirror.tmp
  94. set __SRCCLIENT=%1
  95. set __SRCROOT=%2
  96. rem -- check for opened files
  97. sd opened > %SDMIRRORTMPFILE% 2>nul
  98. if errorlevel 1 goto dest_has_files_open
  99. if %@filesize[%SDMIRRORTMPFILE%] NE 0 goto dest_has_files_open
  100. rem -- setup
  101. gosub calc_xlation
  102. rem -- sync to the source client
  103. echo Sync to client %1...
  104. if %__TEST% == 1 echo ( sd sync %__DESTROOT%\...@%1 )
  105. if %__TEST% == 0 sd sync %__DESTROOT%\...@%1
  106. rem -- open same files for edit on the destination as on the source
  107. sd -c %__SRCCLIENT% opened -l > %SDMIRRORTMPFILE% 2>nul
  108. for /f "tokens=1,2 delims=#" %%i in ( %SDMIRRORTMPFILE% ) do gosub perfile_open
  109. *del /q %SDMIRRORTMPFILE%
  110. goto :eof
  111. rem -- subroutine: open file on destination for same operation as on source
  112. :perfile_open
  113. @echo off
  114. set _xlate_=%i
  115. gosub xlate_filename
  116. switch "%@word[2,%j]"
  117. case "delete"
  118. iff %__TEST% == 1 then
  119. echo delete %_xlate_dest_%
  120. else
  121. sd delete "%_xlate_dest_%"
  122. endiff
  123. case "edit" .or. "integrate"
  124. if not "%@word[2,%j]" == "edit" echo warning: substituting action 'edit' instead of action '%@word[2,%j]'.
  125. iff %__TEST% == 1 then
  126. echo edit %_xlate_dest_%
  127. echo copy %_xlate_src_% `->` %_xlate_dest_%
  128. else
  129. sd edit "%_xlate_dest_%"
  130. *copy "%_xlate_src_%" "%_xlate_dest_%"
  131. endiff
  132. case "add" .or. "branch"
  133. if not "%@word[2,%j]" == "add" echo warning: substituting action 'add' instead of action '%@word[2,%j]'.
  134. iff %__TEST% == 1 then
  135. echo add "%_xlate_dest_%"
  136. echo copy %_xlate_src_% `->` %_xlate_dest_%
  137. else
  138. *copy "%_xlate_src_%" "%_xlate_dest_%"
  139. sd add "%_xlate_dest_%"
  140. endiff
  141. default
  142. echo warning: unknown action '%@word[2,%j]', no action performed.
  143. endswitch
  144. return
  145. rem -- subroutine: translate source client local filename to locally accessible source and destination filenames
  146. :xlate_filename
  147. @echo off
  148. if "%_xlate_%" == "" goto error_xlate
  149. set _xlate_=%@instr[%@len[%__SRCXLATE%],,%_xlate_%]
  150. set _xlate_src_=%__SRCROOT%%_xlate_%
  151. set _xlate_dest_=%__DESTROOT%%_xlate_%
  152. unset _xlate_
  153. return
  154. rem -- subroutine: determine translation from source client paths to specified
  155. rem -- source path that's actually accessible. also determines
  156. rem -- destination client local path.
  157. :calc_xlation
  158. @echo off
  159. sd client -o | findstr /b Root: > %SDMIRRORTMPFILE%
  160. if errorlevel 1 goto error_dest_client
  161. set __DESTROOT=%@word[1,%@line[%SDMIRRORTMPFILE%,0]]
  162. sd -c %__SRCCLIENT% client -o | findstr /b Root: > %SDMIRRORTMPFILE%
  163. if errorlevel 1 goto error_source_client
  164. set __SRCXLATE=%@word[1,%@line[%SDMIRRORTMPFILE%,0]]
  165. echo.
  166. echo Destination root is: %__DESTROOT%
  167. echo Source root is: %__SRCROOT% (accessible)
  168. echo Translation root is: %__SRCXLATE% (actual client root)
  169. echo.
  170. return
  171. :dest_has_files_open
  172. echo Destination client has files opened, or error listing opened files. Aborting.
  173. goto :eof
  174. :error_xlate
  175. echo Internal script error: The translation routine needs a filename. Aborting.
  176. goto :eof
  177. :error_dest_client
  178. echo Error trying to find local root of destination client. Aborting.
  179. goto :eof
  180. :error_source_client
  181. echo Error trying to find local root of source client. Aborting.
  182. goto :eof
  183. :usage
  184. echo SDMIRROR - mirror one client to another (revisions and edits).
  185. echo.
  186. echo Syntax: SDMIRROR [/n] source_client source_root
  187. echo.
  188. echo Determines the destination client based on the environment or SD.INI file.
  189. echo.
  190. echo Parameters:
  191. echo /n - show what would be done, but do nothing
  192. echo source_client - name of source client
  193. echo source_root - locally accessible path to the source client's files
  194. echo.
  195. echo Algorithm:
  196. echo 1. Sync the destination client based on the source client.
  197. echo 2. Open the same files for edit on the destination as on the source.
  198. echo 3. Delete any files opened for delete.
  199. echo 4. Copy any opened for edit or add.
  200. echo.
  201. echo Assumes the destination client does not have any files opened.