Program.SelectCommand.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. catch (IOException ex)
  75. {
  76. Console.WriteLine("Cannot read file: " + ex.Message);
  77. }
  78. }
  79. else
  80. {
  81. Console.WriteLine("File not found: \"{0}\"", path);
  82. }
  83. }
  84. else
  85. {
  86. Console.WriteLine();
  87. Console.WriteLine("Error: Invalid argument");
  88. }
  89. }
  90. else
  91. {
  92. Console.WriteLine("Error: Invalid number of arguments");
  93. }
  94. break;
  95. }
  96. case "partition":
  97. {
  98. if (m_selectedDisk != null)
  99. {
  100. if (args.Length == 3)
  101. {
  102. int partitionIndex = Conversion.ToInt32(args[2], -1);
  103. List<Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
  104. if (partitionIndex >= 0 && partitionIndex < partitions.Count)
  105. {
  106. m_selectedVolume = partitions[partitionIndex];
  107. }
  108. else
  109. {
  110. Console.WriteLine("Error: Invalid partition number");
  111. }
  112. }
  113. else
  114. {
  115. Console.WriteLine();
  116. Console.WriteLine("Error: Partition number was not specified");
  117. }
  118. }
  119. else
  120. {
  121. Console.WriteLine("No disk has been selected");
  122. }
  123. break;
  124. }
  125. case "volume":
  126. {
  127. if (args.Length == 3)
  128. {
  129. List<Volume> volumes;
  130. try
  131. {
  132. volumes = WindowsVolumeHelper.GetVolumes();
  133. }
  134. catch
  135. {
  136. volumes = new List<Volume>();
  137. }
  138. int volumeIndex = Conversion.ToInt32(args[2], -1);
  139. if (volumeIndex >= 0 && volumeIndex < volumes.Count)
  140. {
  141. m_selectedVolume = volumes[volumeIndex];
  142. }
  143. else
  144. {
  145. Console.WriteLine();
  146. Console.WriteLine("Error: Invalid volume number");
  147. }
  148. }
  149. else
  150. {
  151. Console.WriteLine();
  152. Console.WriteLine("Error: Volume number was not specified");
  153. }
  154. break;
  155. }
  156. default:
  157. HelpSelect();
  158. break;
  159. }
  160. }
  161. public static void HelpSelect()
  162. {
  163. Console.WriteLine();
  164. Console.WriteLine("SELECT DISK <N> - Select physical disk.");
  165. Console.WriteLine("SELECT VDISK FILE=<path> - Select virtual disk file.");
  166. Console.WriteLine("SELECT VOLUME <N> - Select volume.");
  167. Console.WriteLine("SELECT PARTITION <N> - Select partition.");
  168. }
  169. }
  170. }