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.

173 lines
4.5 KiB

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. namespace Microsoft.Fusion.ADF
  5. {
  6. class MainApp
  7. {
  8. public static void Main(string[] args)
  9. {
  10. string sourceDir, targetDir, paramFile, extraXmlFile;
  11. ManifestGenerator mg = null;
  12. MGParamParser mgPP = null;
  13. Stream paramFileStream = null, extraXmlStream = null;
  14. if(args.Length < 2 || args.Length > 3)
  15. {
  16. Console.WriteLine("usage: mg <source_dir> <param_file> [extra_xml]");
  17. Console.WriteLine("<> indicates a parameter that is required.");
  18. Console.WriteLine("[] indicates a parameter that is optional.");
  19. return;
  20. }
  21. if(args.Length == 2)
  22. {
  23. sourceDir = args[0];
  24. paramFile = args[1];
  25. targetDir = args[0];
  26. extraXmlFile = null;
  27. }
  28. else
  29. {
  30. sourceDir = args[0];
  31. paramFile = args[1];
  32. targetDir = args[0];
  33. extraXmlFile = args[2];
  34. }
  35. try
  36. {
  37. paramFileStream = File.Open(paramFile, FileMode.Open);
  38. }
  39. catch(FileNotFoundException fnfe)
  40. {
  41. Console.WriteLine(fnfe.ToString());
  42. }
  43. try
  44. {
  45. if(extraXmlFile != null) extraXmlStream = File.Open(extraXmlFile, FileMode.Open);
  46. }
  47. catch(FileNotFoundException fnfe)
  48. {
  49. Console.WriteLine(fnfe.ToString());
  50. }
  51. try
  52. {
  53. mgPP = new MGParamParser(paramFileStream);
  54. }
  55. catch(MGParseErrorException mgpee)
  56. {
  57. Console.WriteLine(mgpee.ToString());
  58. }
  59. string appManFilePath = "";
  60. string subManFilePath = "";
  61. if(mgPP != null)
  62. {
  63. appManFilePath = Path.Combine(targetDir, String.Concat(mgPP.AppName, ".manifest"));
  64. subManFilePath = Path.Combine(targetDir, String.Concat(mgPP.AppName, ".subscription.manifest"));
  65. // DEAL WITH THIS MORE ELEGANTLY ... EITHER SAVE THESE FILES IN MEMORY OR MOVE TO TEMP DIRECTORY?
  66. if(File.Exists(appManFilePath)) File.Delete(appManFilePath);
  67. if(File.Exists(subManFilePath)) File.Delete(subManFilePath);
  68. try
  69. {
  70. mg = new ManifestGenerator(sourceDir, mgPP, extraXmlStream);
  71. }
  72. catch(ArgumentNullException ane)
  73. {
  74. Console.WriteLine(ane.ToString());
  75. }
  76. catch(ArgumentException ae)
  77. {
  78. Console.WriteLine(ae.ToString());
  79. }
  80. catch(MGParseErrorException mgpee)
  81. {
  82. Console.WriteLine(mgpee.ToString());
  83. }
  84. catch(MGDependencyException mgde)
  85. {
  86. Console.WriteLine(mgde.ToString());
  87. }
  88. catch(XmlException xmle)
  89. {
  90. Console.WriteLine(xmle.ToString());
  91. }
  92. }
  93. if(mg != null)
  94. {
  95. // Take the streams and make files out of them
  96. FileStream appManFile = File.Create(appManFilePath);
  97. FileStream subManFile = File.Create(subManFilePath);
  98. // Set up copy buffer
  99. int copyBufferSize = 32768;
  100. byte[] copyBuffer = new byte[copyBufferSize];
  101. int bytesRead;
  102. // Write the streams out
  103. Stream appManContents = mg.AppManStream;
  104. Stream subManContents = mg.SubManStream;
  105. while((bytesRead = appManContents.Read(copyBuffer, 0, copyBufferSize)) > 0) appManFile.Write(copyBuffer, 0, bytesRead);
  106. while((bytesRead = subManContents.Read(copyBuffer, 0, copyBufferSize)) > 0) subManFile.Write(copyBuffer, 0, bytesRead);
  107. // Close streams
  108. appManFile.Flush();
  109. appManFile.Close();
  110. Console.WriteLine("Created " + appManFilePath + " successfully");
  111. subManFile.Flush();
  112. subManFile.Close();
  113. Console.WriteLine("Created " + subManFilePath + " successfully");
  114. mg.CloseStreams();
  115. // Convert sourceDir and targetDir to absolute paths so that we can check if they are the same
  116. // Then check and store value
  117. bool dirsEqual = false;
  118. if(targetDir != null)
  119. {
  120. string absSourceDir = Path.GetFullPath(sourceDir).ToLower();
  121. string absTargetDir = Path.GetFullPath(targetDir).ToLower();
  122. dirsEqual = absSourceDir.Equals(absTargetDir);
  123. }
  124. // Now do the file copying if necessary
  125. if(!dirsEqual && targetDir != null)
  126. {
  127. if(!Directory.Exists(targetDir))
  128. {
  129. try
  130. {
  131. Directory.CreateDirectory(targetDir);
  132. }
  133. catch(IOException ioe)
  134. {
  135. throw new ArgumentException("Target directory " + targetDir + " could not be created.", ioe);
  136. }
  137. catch(ArgumentException ae)
  138. {
  139. throw new ArgumentException("Target directory " + targetDir + " could not be created.", ae);
  140. }
  141. }
  142. MGFileCopier mgFC = new MGFileCopier(targetDir);
  143. DirScanner.BeginScan((IFileOperator) mgFC, sourceDir);
  144. }
  145. }
  146. }
  147. }
  148. }