Program.SelectCommand.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using DiskAccessLibrary;
  6. using Utilities;
  7. namespace ISCSIConsole
  8. {
  9. public partial class Program
  10. {
  11. private static Disk m_selectedDisk;
  12. private static Volume m_selectedVolume;
  13. public static void SelectCommand(string[] args)
  14. {
  15. if (args.Length == 1)
  16. {
  17. HelpSelect();
  18. return;
  19. }
  20. switch (args[1].ToLower())
  21. {
  22. case "disk":
  23. {
  24. if (args.Length == 3)
  25. {
  26. int diskIndex = Conversion.ToInt32(args[2], -1);
  27. if (diskIndex >= 0)
  28. {
  29. PhysicalDisk disk = null;
  30. try
  31. {
  32. disk = new PhysicalDisk(diskIndex);
  33. }
  34. catch
  35. {
  36. Console.WriteLine();
  37. Console.WriteLine("Error: Invalid disk number");
  38. }
  39. if (disk != null)
  40. {
  41. m_selectedDisk = disk;
  42. }
  43. }
  44. }
  45. else
  46. {
  47. Console.WriteLine();
  48. Console.WriteLine("Error: Invalid number of arguments");
  49. }
  50. break;
  51. }
  52. case "vdisk":
  53. {
  54. if (args.Length == 3)
  55. {
  56. KeyValuePairList<string, string> parameters = ParseParameters(args, 2);
  57. if (parameters.ContainsKey("file"))
  58. {
  59. string path = parameters.ValueOf("file");
  60. if (new FileInfo(path).Exists)
  61. {
  62. try
  63. {
  64. m_selectedDisk = DiskImage.GetDiskImage(path);
  65. }
  66. catch (InvalidDataException)
  67. {
  68. Console.WriteLine("Invalid virtual disk format");
  69. }
  70. catch (NotImplementedException)
  71. {
  72. Console.WriteLine("Unsupported virtual disk format");
  73. }
  74. }
  75. else
  76. {
  77. Console.WriteLine("File not found: \"{0}\"", path);
  78. }
  79. }
  80. else
  81. {
  82. Console.WriteLine();
  83. Console.WriteLine("Error: Invalid argument");
  84. }
  85. }
  86. else
  87. {
  88. Console.WriteLine("Error: Invalid number of arguments");
  89. }
  90. break;
  91. }
  92. case "partition":
  93. {
  94. if (m_selectedDisk != null)
  95. {
  96. if (args.Length == 3)
  97. {
  98. int partitionIndex = Conversion.ToInt32(args[2], -1);
  99. List<Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
  100. if (partitionIndex >= 0 && partitionIndex < partitions.Count)
  101. {
  102. m_selectedVolume = partitions[partitionIndex];
  103. }
  104. else
  105. {
  106. Console.WriteLine("Error: Invalid partition number");
  107. }
  108. }
  109. else
  110. {
  111. Console.WriteLine();
  112. Console.WriteLine("Error: Partition number was not specified");
  113. }
  114. }
  115. else
  116. {
  117. Console.WriteLine("No disk has been selected");
  118. }
  119. break;
  120. }
  121. case "volume":
  122. {
  123. if (args.Length == 3)
  124. {
  125. List<Volume> volumes;
  126. try
  127. {
  128. volumes = WindowsVolumeHelper.GetVolumes();
  129. }
  130. catch
  131. {
  132. volumes = new List<Volume>();
  133. }
  134. int volumeIndex = Conversion.ToInt32(args[2], -1);
  135. if (volumeIndex >= 0 && volumeIndex < volumes.Count)
  136. {
  137. m_selectedVolume = volumes[volumeIndex];
  138. }
  139. else
  140. {
  141. Console.WriteLine();
  142. Console.WriteLine("Error: Invalid volume number");
  143. }
  144. }
  145. else
  146. {
  147. Console.WriteLine();
  148. Console.WriteLine("Error: Volume number was not specified");
  149. }
  150. break;
  151. }
  152. default:
  153. HelpSelect();
  154. break;
  155. }
  156. }
  157. public static void HelpSelect()
  158. {
  159. Console.WriteLine();
  160. Console.WriteLine("SELECT DISK <N> - Select physical disk.");
  161. Console.WriteLine("SELECT VDISK FILE=<path> - Select virtual disk file.");
  162. Console.WriteLine("SELECT VOLUME <N> - Select volume.");
  163. Console.WriteLine("SELECT PARTITION <N> - Select partition.");
  164. }
  165. }
  166. }