| 13 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 14 |  | String fileName; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 15 |  | String forceFiles = ""; | 
 
 
 
 
 
 
 
 | 16 | + | bool globalNoBackups = false; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 17 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 18 | < | public XmlPatch(String file) | 
 
 
 
 
 
 
 
 
 | 18 | > | public XmlPatch(String file, bool noBackups) | 
 
 
 
 
 
 
 
 
 
 
 | 19 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 20 |  | fileName = file; | 
 
 
 
 
 
 
 
 | 21 | + | globalNoBackups = noBackups; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 22 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 23 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 24 | < | public XmlPatch(String file, String forceInFiles) | 
 
 
 
 
 
 
 
 
 | 24 | > | public XmlPatch(String file, String forceInFiles, bool noBackups) | 
 
 
 
 
 
 
 
 
 
 
 | 25 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 26 |  | fileName = file; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 27 |  | forceFiles = forceInFiles; //We support apply the operation in diverse forced files (NameOfFile parameter will be ignored) | 
 
 
 
 
 
 
 
 | 28 | + | globalNoBackups = noBackups; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 29 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 30 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 31 |  | /// <summary> | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 137 |  | //---------------------------------------------------XML Injection (start) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 138 |  | foreach (String currFile in filesToProcess) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 139 |  | { | 
 
 
 
 
 
 
 
 
 
 
 | 140 | < |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 141 | < | Util.backupFile(currFile); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 140 | > | if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file | 
 
 
 
 
 | 141 | > | { | 
 
 
 
 
 | 142 | > | Util.backupFile(currFile); | 
 
 
 
 
 | 143 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 144 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 145 |  | XmlDocument xdoc = new XmlDocument(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 146 |  | xdoc.Load(currFile); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 232 |  | foreach (String currFile in filesToProcess) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 233 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 234 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 235 | < | Util.backupFile(currFile); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 235 | > | if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file | 
 
 
 
 
 | 236 | > | { | 
 
 
 
 
 | 237 | > | Util.backupFile(currFile); | 
 
 
 
 
 | 238 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 239 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 240 |  | XmlDocument xdoc = new XmlDocument(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 241 |  | xdoc.Load(currFile); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 295 |  | // Add the filename if it doesn't exists | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 296 |  | else | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 297 |  | { | 
 
 
 
 
 
 
 
 
 
 
 | 298 | < | command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // -2 to be inside quotes | 
 
 
 
 
 
 
 
 
 | 298 | > | command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // -1 to be inside quotes | 
 
 
 
 
 
 
 
 
 
 
 | 299 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 300 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 301 |  | if (paramType != "") | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 312 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 313 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 314 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 315 | < | command = command.Replace("\"", ""); // remove quotes | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 308 | < |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 309 | < | ProcessStartInfo startInfo = new ProcessStartInfo(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 310 | < | if (!Util.IsRunningOnMono()) | 
 
 
 
 
 
 
 
 
 | 315 | > | if (this.globalNoBackups && !Util.ContainsIgnoreCase(command,"nobackups")) // add noBackup flag if provided as global parameter | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 316 |  | { | 
 
 
 
 
 
 
 
 
 
 
 | 317 | < | startInfo.FileName = Util.getExeFileName(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 313 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 314 | < | else{ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 315 | < | startInfo.FileName = "mono"; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 316 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 317 | < | if (!Util.IsRunningOnMono()) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 318 | < | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 319 | < | startInfo.Arguments = command; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 320 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 321 | < | else{ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 322 | < | startInfo.Arguments = Util.getExeFileName() + " " + command; | 
 
 
 
 
 
 
 
 
 | 317 | > | command = command.Insert(command.Length - 1, " -nobackups"); // -1 to be inside quotes | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 318 |  | } | 
 
 
 
 
 
 
 
 
 | 324 | – | startInfo.UseShellExecute = false; // necessary to redirect output | 
 
 
 
 
 
 
 
 
 | 325 | – | startInfo.RedirectStandardOutput = true; | 
 
 
 
 
 
 
 
 
 | 326 | – | startInfo.RedirectStandardError = true; | 
 
 
 
 
 
 
 
 
 | 327 | – | startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // hide new process window | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 319 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 320 | < | Process p = System.Diagnostics.Process.Start(startInfo); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 321 | < | p.OutputDataReceived += commandStdOutputReceived; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 322 | < | p.ErrorDataReceived += commandStdErrorReceived; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 332 | < | p.BeginOutputReadLine(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 333 | < | p.BeginErrorReadLine(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 334 | < | p.WaitForExit(); | 
 
 
 
 
 
 
 
 
 | 320 | > | command = command.Replace("\"", ""); // remove quotes | 
 
 
 
 
 | 321 | > |  | 
 
 
 
 
 | 322 | > | Program.Main(command.Split(' ')); // use the current process is more efficient than start a new one | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 323 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 324 |  | catch (Exception e) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 325 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 330 |  | return true; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 331 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 332 |  |  | 
 
 
 
 
 
 
 
 
 | 345 | – | /// <summary> | 
 
 
 
 
 
 
 
 
 | 346 | – | /// Reads asynchronously output from the new process where the command will be executed | 
 
 
 
 
 
 
 
 
 | 347 | – | /// </summary> | 
 
 
 
 
 
 
 
 
 | 348 | – | /// <param name="sender"></param> | 
 
 
 
 
 
 
 
 
 | 349 | – | /// <param name="e"></param> | 
 
 
 
 
 
 
 
 
 | 350 | – | private void commandStdOutputReceived(object sender, DataReceivedEventArgs e) | 
 
 
 
 
 
 
 
 
 | 351 | – | { | 
 
 
 
 
 
 
 
 
 | 352 | – |  | 
 
 
 
 
 
 
 
 
 | 353 | – | string myData = e.Data; | 
 
 
 
 
 
 
 
 
 | 354 | – |  | 
 
 
 
 
 
 
 
 
 | 355 | – | if (myData != null) | 
 
 
 
 
 
 
 
 
 | 356 | – | { | 
 
 
 
 
 
 
 
 
 | 357 | – | if (myData.EndsWith("\n")) | 
 
 
 
 
 
 
 
 
 | 358 | – | { | 
 
 
 
 
 
 
 
 
 | 359 | – | Console.Write(myData); | 
 
 
 
 
 
 
 
 
 | 360 | – | } | 
 
 
 
 
 
 
 
 
 | 361 | – | else | 
 
 
 
 
 
 
 
 
 | 362 | – | { | 
 
 
 
 
 
 
 
 
 | 363 | – | Console.WriteLine(myData); | 
 
 
 
 
 
 
 
 
 | 364 | – | } | 
 
 
 
 
 
 
 
 
 | 365 | – | } | 
 
 
 
 
 
 
 
 
 | 366 | – |  | 
 
 
 
 
 
 
 
 
 | 367 | – | } | 
 
 
 
 
 
 
 
 
 | 368 | – |  | 
 
 
 
 
 
 
 
 
 | 369 | – | private void commandStdErrorReceived(object sender, DataReceivedEventArgs e) | 
 
 
 
 
 
 
 
 
 | 370 | – | { | 
 
 
 
 
 
 
 
 
 | 371 | – |  | 
 
 
 
 
 
 
 
 
 | 372 | – | string myData = e.Data; | 
 
 
 
 
 
 
 
 
 | 373 | – |  | 
 
 
 
 
 
 
 
 
 | 374 | – | if (myData != null) | 
 
 
 
 
 
 
 
 
 | 375 | – | { | 
 
 
 
 
 
 
 
 
 | 376 | – | if (myData.EndsWith("\n")) | 
 
 
 
 
 
 
 
 
 | 377 | – | { | 
 
 
 
 
 
 
 
 
 | 378 | – | Console.Error.Write(myData); | 
 
 
 
 
 
 
 
 
 | 379 | – | } | 
 
 
 
 
 
 
 
 
 | 380 | – | else | 
 
 
 
 
 
 
 
 
 | 381 | – | { | 
 
 
 
 
 
 
 
 
 | 382 | – | Console.Error.WriteLine(myData); | 
 
 
 
 
 
 
 
 
 | 383 | – | } | 
 
 
 
 
 
 
 
 
 | 384 | – | } | 
 
 
 
 
 
 
 
 
 | 385 | – |  | 
 
 
 
 
 
 
 
 
 | 386 | – | } | 
 
 
 
 
 
 
 
 
 | 387 | – |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 333 |  | private string getPatchParameter(string line, string parameterName) | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 334 |  | { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 335 |  | string result = ""; |