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.

306 lines
12 KiB

  1. // This is an includefile containing the common
  2. // stuff for using RoboCopy.
  3. // To use, you must call RoboCopyInit().
  4. // It creates the global, "g_robocopy" which contains the ActiveX object
  5. // pointing to RoboCopy.
  6. // When you are finished, you must call g_robocopy.UnRegister()
  7. //
  8. // You must have a "function [script]_js::OnEventSourceEvent(RemoteObj, DispID, cmd, params)"
  9. // and filter messages to robocopy like this:
  10. // if (g_robocopy == null || !g_robocopy.OnEventSource(arguments))
  11. // {
  12. // do your processing here...
  13. // }
  14. //
  15. //
  16. // Aside from RoboCopyInit(), all RoboCopy function may throw exceptions - you must be prepared to handle this.
  17. //
  18. // If you wany copy status, override the following functions:
  19. // StatusStarted();
  20. // StatusFile();
  21. // StatusProgress(nPercent, nSize, nCopiedBytes);
  22. // StatusError();
  23. // StatusDone()
  24. // StatusMessage(nErrorCode, strErrorMessage, strRoboCopyMessage, strFileName)
  25. function RoboCopyInit()
  26. {
  27. try
  28. {
  29. g_robocopy = new RoboCopy();
  30. }
  31. catch(ex)
  32. {
  33. SimpleErrorDialog(LocalMachine + ": Cannot initialize RoboCopy", "RoboCopy() init failed, " + ex, false);
  34. return false;
  35. }
  36. return true;
  37. }
  38. function RoboCopyUnRegister()
  39. {
  40. if (this.objRemote != null)
  41. {
  42. UnregisterEventSource(this.objRemote);
  43. this.objRemote = null;
  44. }
  45. }
  46. function RoboCopyCopyFile(srcdir, dstdir, file)
  47. {
  48. try
  49. {
  50. var n = file.lastIndexOf("\\");
  51. if (n != -1)
  52. {
  53. var filepath = file.substr(0, n + 1);
  54. file = file.substr(n + 1);
  55. srcdir += filepath;
  56. dstdir += filepath;
  57. }
  58. this.nErrorCode = this.RCERR_NOT_INITIALIZED;
  59. JAssert(this.objRemote != null);
  60. if (this.objRemote != null)
  61. {
  62. LogMsg("RoboCopy file " + srcdir + file + " to " + dstdir + file);
  63. this.strSrcDir = srcdir;
  64. this.strDstDir = dstdir;
  65. this.strSrcFile = srcdir + file;
  66. this.strDstFile = dstdir + file;
  67. this.objRemote.SetCopyOptions(srcdir, dstdir, file, "/ETA /R:100 /V");
  68. this.objRemote.DoCopy();
  69. }
  70. }
  71. catch(ex)
  72. {
  73. ex.detail = "Copyfile('" + srcdir + "', '" + dstdir + "', '" + file + "')";
  74. this.TranslateException(ex);
  75. }
  76. return this.nErrorCode;
  77. }
  78. function RoboCopySetExcludeFiles(aFiles)
  79. {
  80. this.aExcludeFiles = aFiles;
  81. }
  82. function RoboCopyCopyDir(srcdir, dstdir, fRecursive)
  83. {
  84. try
  85. {
  86. var options;
  87. var char;
  88. var i;
  89. this.nErrorCode = this.RCERR_NOT_INITIALIZED;
  90. JAssert(this.objRemote != null);
  91. if (this.objRemote != null)
  92. {
  93. options = "/Z /ETA /R:100 /V";
  94. if (fRecursive)
  95. options += " /E ";
  96. if (this.aExcludeFiles.length != 0)
  97. options += " /XF ";
  98. char = srcdir.charAt(srcdir.length - 1);
  99. if (char != '\\' && char != ':')
  100. srcdir += '\\';
  101. char = dstdir.charAt(dstdir.length - 1);
  102. if (char != '\\' && char != ':')
  103. dstdir += '\\';
  104. LogMsg("RoboCopy dir " + srcdir + " to " + dstdir );
  105. this.strSrcDir = srcdir;
  106. this.strDstDir = dstdir;
  107. this.strSrcFile = srcdir;
  108. this.strDstFile = dstdir;
  109. if (this.aExcludeFiles.length != 0)
  110. {
  111. // MTRCopy.SetCopyOptions will only accept one file exclude
  112. // pattern.
  113. // If you call SetCopyOptions multiple times, it keeps a list
  114. // of all of the file patterns to exclude.
  115. for(i = 0; i < this.aExcludeFiles.length; ++i)
  116. {
  117. LogMsg("EXCLUDING FILE " + this.aExcludeFiles[i]);
  118. this.objRemote.SetCopyOptions(srcdir, dstdir, this.aExcludeFiles[i], options);
  119. }
  120. }
  121. else
  122. this.objRemote.SetCopyOptions(srcdir, dstdir, "*.*", options);
  123. this.objRemote.DoCopy();
  124. }
  125. }
  126. catch(ex)
  127. {
  128. ex.detail = "CopyDir('" + srcdir + "', '" + dstdir + "')";
  129. this.TranslateException(ex);
  130. }
  131. return this.nErrorCode;
  132. }
  133. function RoboCopyTranslateException(ex)
  134. {
  135. if (!ex.number)
  136. ex.number = "<undefined>";
  137. ex.description = "Unknown RoboCopy exception: " + ex.number;
  138. if ( this.ErrorMessages[ex.number] != null)
  139. ex.description = this.ErrorMessages[ex.number];
  140. throw ex;
  141. }
  142. function RoboCopyOnEventSource(objRet, aArgs)
  143. {
  144. var strMsg;
  145. var strText;
  146. if (aArgs[0] != this.objRemote)
  147. return false;
  148. switch(aArgs[1]) // dispid
  149. {
  150. case this.RC_COPYSTARTED: // ()
  151. objRet.rc = this.StatusStarted(aArgs[2]);
  152. break;
  153. case this.RC_COPYFILE : // ([in] BSTR FileName)
  154. this.strSrcFile = aArgs[2];
  155. objRet.rc = this.StatusFile();
  156. break;
  157. case this.RC_PROGRESS : // ([in] int iPercent, [in] long lSizeLow, [in] long lSizeHigh, [in] long lCopiedLow, [in] long lCopiedHigh)
  158. objRet.rc = this.StatusProgress(aArgs[2], aArgs[3], aArgs[5]);
  159. break;
  160. case this.RC_COPYERROR : // ([in] long lReason)
  161. this.nErrorCode = aArgs[2];
  162. if (this.ErrorMessages[aArgs[2]])
  163. strMsg = this.ErrorMessages[aArgs[2]];
  164. else
  165. strMsg = 'Unknown';
  166. objRet.rc = this.StatusError(strMsg);
  167. break;
  168. case this.RC_COPYDONE : // ()
  169. objRet.rc = this.StatusDone();
  170. break;
  171. case this.RC_ERRORMESSAGE : // ([in] long lErrorCode, [in] BSTR bstrErrorText, [in] BSTR bstrMessage, [in] BSTR bstrFileName);
  172. objRet.rc = this.StatusMessage(aArgs[2], aArgs[3], aArgs[4], aArgs[5]);
  173. break;
  174. default:
  175. LogMsg("robocopy Unknown event (" + aArgs[1] + ") copying file " + this.strSrcFile + " to " + this.strDstFile);
  176. objRet.rc = 0;
  177. break;
  178. }
  179. return true;
  180. }
  181. function RoboStatusError(strMsg)
  182. {
  183. LogMsg("Robocopy copy error " + this.nErrorCode + " (" + strMsg + ") copying file " + this.strSrcFile + " to " + this.strDstFile);
  184. return this.RC_CONTINUE
  185. }
  186. function RoboStatusMessage(nErrorCode, strErrorMessage, strRoboCopyMessage, strFileName)
  187. {
  188. LogMsg("Robocopy error message (" + nErrorCode + ") " + strErrorMessage + " " + strRoboCopyMessage + " " + strFileName);
  189. return this.RC_CONTINUE
  190. }
  191. function RoboCopy()
  192. {
  193. if (!RoboCopy.prototype.UnRegister)
  194. {
  195. RoboCopy.prototype.UnRegister = RoboCopyUnRegister;
  196. RoboCopy.prototype.OnEventSource = RoboCopyOnEventSource;
  197. RoboCopy.prototype.CopyFile = RoboCopyCopyFile;
  198. RoboCopy.prototype.TranslateException = RoboCopyTranslateException;
  199. RoboCopy.prototype.CopyDir = RoboCopyCopyDir;
  200. RoboCopy.prototype.SetExcludeFiles = RoboCopySetExcludeFiles;
  201. RoboCopy.prototype.StatusStarted = function(args) { return true;}
  202. RoboCopy.prototype.StatusFile = function(args) { return true;}
  203. RoboCopy.prototype.StatusProgress = function(args) { return this.PROGRESS_QUIET;}
  204. RoboCopy.prototype.StatusError = RoboStatusError;
  205. RoboCopy.prototype.StatusDone = function(args) { return true;}
  206. RoboCopy.prototype.StatusMessage = RoboStatusMessage;
  207. // Possible Return values for the progress function
  208. RoboCopy.prototype.PROGRESS_CONTINUE = 0;
  209. RoboCopy.prototype.PROGRESS_CANCEL = 1;
  210. RoboCopy.prototype.PROGRESS_STOP = 2;
  211. RoboCopy.prototype.PROGRESS_QUIET = 3;
  212. // Possible return values for the error function
  213. RoboCopy.prototype.RC_FAIL = 0;
  214. RoboCopy.prototype.RC_CONTINUE = 1;
  215. RoboCopy.prototype.RC_COPYSTARTED = 1;
  216. RoboCopy.prototype.RC_COPYFILE = 2;
  217. RoboCopy.prototype.RC_PROGRESS = 3;
  218. RoboCopy.prototype.RC_COPYERROR = 4;
  219. RoboCopy.prototype.RC_COPYDONE = 5;
  220. RoboCopy.prototype.RC_ERRORMESSAGE = 6;
  221. RoboCopy.prototype.RCERR_NOT_INITIALIZED = -1
  222. RoboCopy.prototype.RCERR_SRC_CANT_BE_WILDCARD = -1000
  223. RoboCopy.prototype.RCERR_SRC_CANT_EXPAND = -1001
  224. RoboCopy.prototype.RCERR_NO_SRC_DIR = -1002
  225. RoboCopy.prototype.RCERR_DST_CANT_BE_WILDCARD = -1003
  226. RoboCopy.prototype.RCERR_DST_CANT_EXPAND = -1004
  227. RoboCopy.prototype.RCERR_NO_DST_DIR = -1005
  228. RoboCopy.prototype.RCERR_INVALID_OPTION = -1006
  229. RoboCopy.prototype.RCERR_PATH_CANT_BE_WILDCARD = -1007
  230. RoboCopy.prototype.RCERR_FILESPEC_CANT_BE_PATH = -1008
  231. RoboCopy.prototype.RCERR_CANT_ACCESS_SRC = -1009
  232. RoboCopy.prototype.RCERR_CANT_ACCESS_DST = -1010
  233. RoboCopy.prototype.RCERR_CANT_CREATE_DST_DIR = -1011
  234. RoboCopy.prototype.RCERR_CANT_GET_MODULE_HANDLE = -1012
  235. RoboCopy.prototype.RCERR_OPTIONS_NOT_SET = -1013
  236. RoboCopy.prototype.RCERR_RETRY_LIMIT = -1014 // Fatal - no more retries
  237. RoboCopy.prototype.RCERR_WAITING_FOR_RETRY = -1015 // NON-Fatal - information
  238. RoboCopy.prototype.RCERR_RETRYING = -1016 // NON-Fatal - information
  239. RoboCopy.prototype.ErrorMessages = new Object();
  240. RoboCopy.prototype.ErrorMessages[this.RCERR_NOT_INITIALIZED] = "RoboCopy Error: NOT_INITIALIZED";
  241. RoboCopy.prototype.ErrorMessages[this.RCERR_SRC_CANT_BE_WILDCARD] = "RoboCopy Error: SRC_CANT_BE_WILDCARD";
  242. RoboCopy.prototype.ErrorMessages[this.RCERR_SRC_CANT_EXPAND] = "RoboCopy Error: SRC_CANT_EXPAND";
  243. RoboCopy.prototype.ErrorMessages[this.RCERR_NO_SRC_DIR] = "RoboCopy Error: NO_SRC_DIR";
  244. RoboCopy.prototype.ErrorMessages[this.RCERR_DST_CANT_BE_WILDCARD] = "RoboCopy Error: DST_CANT_BE_WILDCARD";
  245. RoboCopy.prototype.ErrorMessages[this.RCERR_DST_CANT_EXPAND] = "RoboCopy Error: DST_CANT_EXPAND";
  246. RoboCopy.prototype.ErrorMessages[this.RCERR_NO_DST_DIR] = "RoboCopy Error: NO_DST_DIR";
  247. RoboCopy.prototype.ErrorMessages[this.RCERR_INVALID_OPTION] = "RoboCopy Error: INVALID_OPTION";
  248. RoboCopy.prototype.ErrorMessages[this.RCERR_PATH_CANT_BE_WILDCARD] = "RoboCopy Error: PATH_CANT_BE_WILDCARD";
  249. RoboCopy.prototype.ErrorMessages[this.RCERR_FILESPEC_CANT_BE_PATH] = "RoboCopy Error: FILESPEC_CANT_BE_PATH";
  250. RoboCopy.prototype.ErrorMessages[this.RCERR_CANT_ACCESS_SRC] = "RoboCopy Error: CANT_ACCESS_SRC";
  251. RoboCopy.prototype.ErrorMessages[this.RCERR_CANT_ACCESS_DST] = "RoboCopy Error: CANT_ACCESS_DST";
  252. RoboCopy.prototype.ErrorMessages[this.RCERR_CANT_CREATE_DST_DIR] = "RoboCopy Error: CANT_CREATE_DST_DIR";
  253. RoboCopy.prototype.ErrorMessages[this.RCERR_CANT_GET_MODULE_HANDLE] = "RoboCopy Error: CANT_GET_MODULE_HANDLE";
  254. RoboCopy.prototype.ErrorMessages[this.RCERR_OPTIONS_NOT_SET] = "RoboCopy Error: OPTIONS_NOT_SET";
  255. RoboCopy.prototype.ErrorMessages[this.RCERR_RETRY_LIMIT] = "RoboCopy Error: RETRY_LIMIT";
  256. RoboCopy.prototype.ErrorMessages[this.RCERR_WAITING_FOR_RETRY] = "RoboCopy: WAITING_FOR_RETRY";
  257. RoboCopy.prototype.ErrorMessages[this.RCERR_RETRYING] = "RoboCopy: RETRYING";
  258. }
  259. this.objRemote = new ActiveXObject('MTScript.RoboCopy');
  260. this.nErrorCode = 0;
  261. this.strSrcDir = '';
  262. this.strDstDir = '';
  263. this.strSrcFile = "<notset>";
  264. this.strDstFile = "<notset>";
  265. this.aExcludeFiles = new Array();
  266. RegisterEventSource(this.objRemote, "MTScript.RoboCopyEvents");
  267. }