| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.ComponentModel; |
|---|
| 25 | using System.Drawing; |
|---|
| 26 | using System.Data; |
|---|
| 27 | using System.Linq; |
|---|
| 28 | using System.Text; |
|---|
| 29 | using System.Windows.Forms; |
|---|
| 30 | using System.IO; |
|---|
| 31 | using System.Text.RegularExpressions; |
|---|
| 32 | |
|---|
| 33 | using Eraser.Manager; |
|---|
| 34 | using Eraser.Util; |
|---|
| 35 | using Eraser.Util.ExtensionMethods; |
|---|
| 36 | using System.Globalization; |
|---|
| 37 | |
|---|
| 38 | namespace Eraser.DefaultPlugins |
|---|
| 39 | { |
|---|
| 40 | public partial class DriveErasureTargetConfigurer : UserControl, IErasureTargetConfigurer |
|---|
| 41 | { |
|---|
| 42 | /// <summary> |
|---|
| 43 | /// Represents an item in the list of drives. |
|---|
| 44 | /// </summary> |
|---|
| 45 | private class PartitionItem |
|---|
| 46 | { |
|---|
| 47 | public override string ToString() |
|---|
| 48 | { |
|---|
| 49 | if (!string.IsNullOrEmpty(Cache)) |
|---|
| 50 | return Cache; |
|---|
| 51 | |
|---|
| 52 | if (PhysicalDrive != null) |
|---|
| 53 | { |
|---|
| 54 | try |
|---|
| 55 | { |
|---|
| 56 | Cache = S._("Hard disk {0} ({1})", PhysicalDrive.Index, |
|---|
| 57 | new FileSize(PhysicalDrive.Size)); |
|---|
| 58 | } |
|---|
| 59 | catch (UnauthorizedAccessException) |
|---|
| 60 | { |
|---|
| 61 | Cache = S._("Hard disk {0}", PhysicalDrive.Index); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | else if (Volume != null) |
|---|
| 65 | { |
|---|
| 66 | try |
|---|
| 67 | { |
|---|
| 68 | if (Volume.IsMounted) |
|---|
| 69 | Cache = Volume.MountPoints[0].GetDescription(); |
|---|
| 70 | else if (Volume.PhysicalDrive != null) |
|---|
| 71 | Cache = S._("Partition {0} ({1})", |
|---|
| 72 | Volume.PhysicalDrive.Volumes.IndexOf(Volume) + 1, |
|---|
| 73 | new FileSize(Volume.TotalSize)); |
|---|
| 74 | else |
|---|
| 75 | Cache = S._("Partition ({0})", new FileSize(Volume.TotalSize)); |
|---|
| 76 | } |
|---|
| 77 | catch (UnauthorizedAccessException) |
|---|
| 78 | { |
|---|
| 79 | if (Volume.PhysicalDrive != null) |
|---|
| 80 | Cache = S._("Partition {0}", |
|---|
| 81 | Volume.PhysicalDrive.Volumes.IndexOf(Volume) + 1); |
|---|
| 82 | else |
|---|
| 83 | Cache = S._("Partition"); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | throw new InvalidOperationException(); |
|---|
| 88 | |
|---|
| 89 | return Cache; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /// <summary> |
|---|
| 93 | /// Stores the display text for rapid access. |
|---|
| 94 | /// </summary> |
|---|
| 95 | private string Cache; |
|---|
| 96 | |
|---|
| 97 | /// <summary> |
|---|
| 98 | /// The Physical drive this partition refers to. |
|---|
| 99 | /// </summary> |
|---|
| 100 | public PhysicalDriveInfo PhysicalDrive; |
|---|
| 101 | |
|---|
| 102 | /// <summary> |
|---|
| 103 | /// The volume this partition refers to. |
|---|
| 104 | /// </summary> |
|---|
| 105 | public VolumeInfo Volume; |
|---|
| 106 | |
|---|
| 107 | /// <summary> |
|---|
| 108 | /// The icon of the drive. |
|---|
| 109 | /// </summary> |
|---|
| 110 | public Icon Icon; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | public DriveErasureTargetConfigurer() |
|---|
| 114 | { |
|---|
| 115 | InitializeComponent(); |
|---|
| 116 | Theming.ApplyTheme(this); |
|---|
| 117 | |
|---|
| 118 | //Populate the drives list |
|---|
| 119 | List<VolumeInfo> volumes = new List<VolumeInfo>(); |
|---|
| 120 | foreach (PhysicalDriveInfo drive in PhysicalDriveInfo.Drives) |
|---|
| 121 | { |
|---|
| 122 | PartitionItem item = new PartitionItem(); |
|---|
| 123 | item.PhysicalDrive = drive; |
|---|
| 124 | partitionCmb.Items.Add(item); |
|---|
| 125 | |
|---|
| 126 | foreach (VolumeInfo volume in drive.Volumes) |
|---|
| 127 | { |
|---|
| 128 | item = new PartitionItem(); |
|---|
| 129 | item.Volume = volume; |
|---|
| 130 | |
|---|
| 131 | if (volume.IsMounted) |
|---|
| 132 | { |
|---|
| 133 | DirectoryInfo root = volume.MountPoints[0]; |
|---|
| 134 | item.Icon = root.GetIcon(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | partitionCmb.Items.Add(item); |
|---|
| 138 | volumes.Add(volume); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | //And then add volumes which aren't accounted for (notably, Dynamic volumes) |
|---|
| 143 | foreach (VolumeInfo volume in VolumeInfo.Volumes) |
|---|
| 144 | { |
|---|
| 145 | if (volumes.IndexOf(volume) == -1 && volume.VolumeType == DriveType.Fixed) |
|---|
| 146 | { |
|---|
| 147 | PartitionItem item = new PartitionItem(); |
|---|
| 148 | item.Volume = volume; |
|---|
| 149 | |
|---|
| 150 | if (volume.IsMounted) |
|---|
| 151 | { |
|---|
| 152 | DirectoryInfo root = volume.MountPoints[0]; |
|---|
| 153 | item.Icon = root.GetIcon(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | partitionCmb.Items.Insert(0, item); |
|---|
| 157 | volumes.Add(volume); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | if (partitionCmb.Items.Count != 0) |
|---|
| 162 | partitionCmb.SelectedIndex = 0; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | #region ICliConfigurer<ErasureTarget> Members |
|---|
| 166 | |
|---|
| 167 | public string Help() |
|---|
| 168 | { |
|---|
| 169 | return S._(@"drive Erases partitions, volumes or drives |
|---|
| 170 | arguments: |
|---|
| 171 | drive=\Device\Harddisk<index> |
|---|
| 172 | drive=\\.\PhysicalDrive<index> |
|---|
| 173 | drive=\\?\Volume<guid>"); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public bool ProcessArgument(string argument) |
|---|
| 177 | { |
|---|
| 178 | //The hard disk index |
|---|
| 179 | Regex hardDiskRegex = new Regex("^(drive=)?\\\\Device\\\\Harddisk(?<disk>[\\d]+)", |
|---|
| 180 | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
|---|
| 181 | |
|---|
| 182 | //PhysicalDrive index |
|---|
| 183 | Regex physicalDriveIndex = new Regex("^(drive=)?\\\\\\\\\\.\\\\PhysicalDrive(?<disk>[\\d]+)", |
|---|
| 184 | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
|---|
| 185 | |
|---|
| 186 | //The volume GUID |
|---|
| 187 | Regex volumeRegex = new Regex("^(drive=)?\\\\\\\\\\?\\\\Volume\\{(?<guid>([0-9a-f-]+))\\}", |
|---|
| 188 | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
|---|
| 189 | |
|---|
| 190 | //Try to get the hard disk index. |
|---|
| 191 | Match match = hardDiskRegex.Match(argument); |
|---|
| 192 | if (!match.Groups["disk"].Success) |
|---|
| 193 | match = physicalDriveIndex.Match(argument); |
|---|
| 194 | if (match.Groups["disk"].Success) |
|---|
| 195 | { |
|---|
| 196 | //Get the index of the disk. |
|---|
| 197 | int index = Convert.ToInt32(match.Groups["disk"].Value); |
|---|
| 198 | |
|---|
| 199 | //Create a physical drive info object for the target disk |
|---|
| 200 | PhysicalDriveInfo target = new PhysicalDriveInfo(index); |
|---|
| 201 | |
|---|
| 202 | //Select it in the GUI. |
|---|
| 203 | foreach (PartitionItem item in partitionCmb.Items) |
|---|
| 204 | if (item.PhysicalDrive != null && item.PhysicalDrive.Equals(target)) |
|---|
| 205 | partitionCmb.SelectedItem = item; |
|---|
| 206 | |
|---|
| 207 | return true; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | //Try to get the volume GUID |
|---|
| 211 | match = volumeRegex.Match(argument); |
|---|
| 212 | if (match.Groups["guid"].Success) |
|---|
| 213 | { |
|---|
| 214 | //Find the volume GUID |
|---|
| 215 | Guid guid = new Guid(match.Groups["guid"].Value); |
|---|
| 216 | |
|---|
| 217 | //Create a volume info object for the target volume |
|---|
| 218 | VolumeInfo target = new VolumeInfo(string.Format(CultureInfo.InvariantCulture, |
|---|
| 219 | "\\\\?\\Volume{{{0}}}\\", guid)); |
|---|
| 220 | |
|---|
| 221 | //Select it in the GUI. |
|---|
| 222 | foreach (PartitionItem item in partitionCmb.Items) |
|---|
| 223 | if (item.Volume != null && item.Volume.Equals(target)) |
|---|
| 224 | partitionCmb.SelectedItem = item; |
|---|
| 225 | |
|---|
| 226 | return true; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | #endregion |
|---|
| 233 | |
|---|
| 234 | #region IConfigurer<ErasureTarget> Members |
|---|
| 235 | |
|---|
| 236 | public void LoadFrom(ErasureTarget target) |
|---|
| 237 | { |
|---|
| 238 | DriveErasureTarget partition = target as DriveErasureTarget; |
|---|
| 239 | if (partition == null) |
|---|
| 240 | throw new ArgumentException("The provided erasure target type is not " + |
|---|
| 241 | "supported by this configurer."); |
|---|
| 242 | |
|---|
| 243 | foreach (PartitionItem item in partitionCmb.Items) |
|---|
| 244 | if ((item.PhysicalDrive != null && |
|---|
| 245 | item.PhysicalDrive.Equals(partition.PhysicalDrive)) || |
|---|
| 246 | (item.Volume != null && item.Volume.Equals(partition.Volume))) |
|---|
| 247 | { |
|---|
| 248 | partitionCmb.SelectedItem = item; |
|---|
| 249 | break; |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | public bool SaveTo(ErasureTarget target) |
|---|
| 254 | { |
|---|
| 255 | DriveErasureTarget partition = target as DriveErasureTarget; |
|---|
| 256 | if (partition == null) |
|---|
| 257 | throw new ArgumentException("The provided erasure target type is not " + |
|---|
| 258 | "supported by this configurer."); |
|---|
| 259 | |
|---|
| 260 | PartitionItem item = (PartitionItem)partitionCmb.SelectedItem; |
|---|
| 261 | |
|---|
| 262 | //Make sure we don't set both Volume and PhysicalDrive |
|---|
| 263 | partition.PhysicalDrive = null; |
|---|
| 264 | |
|---|
| 265 | //Then set the proper values. |
|---|
| 266 | partition.Volume = item.Volume; |
|---|
| 267 | partition.PhysicalDrive = item.PhysicalDrive; |
|---|
| 268 | return true; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | #endregion |
|---|
| 272 | |
|---|
| 273 | private void OnDrawItem(object sender, DrawItemEventArgs e) |
|---|
| 274 | { |
|---|
| 275 | if (e.Index == -1) |
|---|
| 276 | return; |
|---|
| 277 | |
|---|
| 278 | Graphics g = e.Graphics; |
|---|
| 279 | PartitionItem item = (PartitionItem)partitionCmb.Items[e.Index]; |
|---|
| 280 | Color textColour = e.ForeColor; |
|---|
| 281 | PointF textPos = e.Bounds.Location; |
|---|
| 282 | if (item.Icon != null) |
|---|
| 283 | textPos.X += item.Icon.Width + 4; |
|---|
| 284 | textPos.Y += 1; |
|---|
| 285 | |
|---|
| 286 | //Set the text colour and background colour if the control is disabled |
|---|
| 287 | if ((e.State & DrawItemState.Disabled) == 0) |
|---|
| 288 | e.DrawBackground(); |
|---|
| 289 | else |
|---|
| 290 | { |
|---|
| 291 | g.FillRectangle(new SolidBrush(SystemColors.ButtonFace), e.Bounds); |
|---|
| 292 | textColour = SystemColors.GrayText; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | if (item.Icon != null) |
|---|
| 296 | g.DrawIcon(item.Icon, e.Bounds.X + 2, e.Bounds.Y); |
|---|
| 297 | g.DrawString(item.ToString(), e.Font, new SolidBrush(textColour), textPos); |
|---|
| 298 | if ((e.State & DrawItemState.Focus) != 0) |
|---|
| 299 | e.DrawFocusRectangle(); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | } |
|---|