-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCdmlets.cs
More file actions
186 lines (160 loc) · 9.08 KB
/
Cdmlets.cs
File metadata and controls
186 lines (160 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//-----------------------------------------------------------------------
// <copyright company="CoApp Project">
// Copyright (c) 2013 Tim Rogers and CoApp Contributors.
// Contributors can be discovered using the 'git log' command.
// All rights reserved.
// </copyright>
// <license>
// The software is licensed under the Apache 2.0 License (the "License")
// You may not use the software except in compliance with the License.
// </license>
//-----------------------------------------------------------------------
using System;
using System.Management.Automation;
using ClrPlus.Powershell.Rest.Commands;
namespace DiffVHD
{
/// <summary>
/// Compares two virtual hard disks (VHD, VHDX, or VMDK) and produces a file-wise diff in the form of a VHD file.
/// </summary>
[Cmdlet(VerbsData.Compare, "VHD")]
public class CompareVHD : RestableCmdlet<CompareVHD>
{
/// <summary>
/// The 'Before' virtual hard disk for the comparison.
/// </summary>
[Parameter(Mandatory = true, Position = 0, HelpMessage = @"The 'Before' virtual hard disk for the comparison.")]
[ValidateNotNullOrEmpty]
public string Base;
/// <summary>
/// The 'After' virtual hard disk for the comparison.
/// </summary>
[Parameter(Mandatory = true, Position = 1, HelpMessage = @"The 'After' virtual hard disk for the comparison.")]
[ValidateNotNullOrEmpty]
public string Child;
/// <summary>
/// The filename on disk to write the output VHD to.
/// </summary>
[Parameter(Mandatory = true, Position = 2, HelpMessage = @"The filename on disk to write the output VHD to.")]
[ValidateNotNullOrEmpty]
public string Output;
/// <summary>
/// [Optional] If provided, only compares this (0-indexed) partition between the two virtual disks. Default behaviour is to compare all partitions on the virtual disks.
/// </summary>
[Parameter(HelpMessage = @"[Optional] If provided, only compares this (0-indexed) partition between the two virtual disks. Default behaviour is to compare all partitions on the virtual disks.")]
[Alias("Partition1")]
public int? Partition = null;
/// <summary>
/// [Optional] If provided along with <seealso cref="Partition"/>, will compare <seealso cref="Partition"/> from <seealso cref="Base"/> against this partition from <seealso cref="Child"/>. Ignored if <seealso cref="Partition"/> is not present.
/// </summary>
[Parameter(HelpMessage = @"[Optional] If provided along with 'Partition', will compare 'Partition' from 'Base' against this partition from 'Child'. Ignored if 'Partition' is not present.")]
public int? Partition2 = null;
/// <summary>
/// [Optional] If set, will overwrite the output file if it exists. If not set, will produce an error if the file already exists.
/// </summary>
[Parameter(HelpMessage = @"[Optional] If set, will overwrite the output file if it exists. If not set, will produce an error if the file already exists.")]
public SwitchParameter Overwrite = false;
/// <summary>
/// [Optional] If set, will populate the diff VHD with a binary copy of any files found to be changed. Normal behaviour is to populate with a textual diff between <seealso cref="Base"/> and <seealso cref="Child"/>.
/// </summary>
[Parameter(HelpMessage = @"[Optional] If set, will populate the diff VHD with a binary copy of any files found to be changed. Normal behaviour is to populate with a textual diff between 'Base' and 'Child'.")]
public SwitchParameter KeepAsBinary = false;
/// <summary>
/// [Optional] The method of comparison to use. Default is <seealso cref="DiffVHD.ComparisonStyle.DateTimeOnly"/>. Valid comparison styles are:
/// <list type="DiffVHD.ComparisonStyle">
/// <item>NameOnly</item>
/// <item>DateTimeOnly</item>
/// <item>Journaled</item>
/// <item>BinaryOnly</item>
/// <item>Full</item>
/// </list>
/// </summary>
[Parameter(
HelpMessage=@"[Optional] The method of comparison to use. Default is DateTimeOnly. Valid comparison styles are:
NameOnly
DateTimeOnly
Journaled
BinaryOnly
Full")]
public DiffVHD.ComparisonStyle Comparison = DiffVHD.ComparisonStyle.DateTimeOnly;
protected override void ProcessRecord()
{
// must use this to support processing record remotely.
if (Remote)
{
ProcessRecordViaRest();
return;
}
if (Partition.HasValue)
if (Partition2.HasValue)
DiffVHD.CreateDiff(Base, Child, Output, Force: Overwrite,
Partition: new Tuple<int, int>(Partition.Value, Partition2.Value), Style: Comparison, AsBinary: KeepAsBinary);
else DiffVHD.CreateDiff(Base, Child, Output, Partition, Force: Overwrite, Style: Comparison, AsBinary: KeepAsBinary);
else DiffVHD.CreateDiff(Base, Child, Output, Force: Overwrite, Style: Comparison, AsBinary: KeepAsBinary);
}
}
/// <summary>
/// Attempts to apply the changes in a diff VHD generated from 'Compare-VHD' to a virtual hard disk.
/// </summary>
[Cmdlet("Apply", "VHDDiff")]
public class ApplyVHDDiff : RestableCmdlet<ApplyVHDDiff>
{
/// <summary>
/// The virtual disk to apply the differences to.
/// </summary>
[Parameter(Mandatory = true, Position = 0, HelpMessage = @"The virtual disk to apply the differences to.")]
[ValidateNotNullOrEmpty]
public string Base;
/// <summary>
/// The diff VHD to apply differences from.
/// </summary>
[Parameter(Mandatory = true, Position = 1, HelpMessage = @"The diff VHD to apply differences from.")]
[ValidateNotNullOrEmpty]
public string Diff;
/// <summary>
/// The new output file (if any) to generate.
/// </summary>
[Parameter(Mandatory = true, Position = 2, HelpMessage = @"The new output file (if any) to generate.")]
public string Output = null;
/// <summary>
/// If set, the <seealso cref="Output"/> file generated will be a differencing disk with <seealso cref="Base"/> set as the parent. Otherwise the differences will be applied directly to <seealso cref="Base"/> (if no <seealso cref="Output"/> is set) or to a copy of <seealso cref="Base"/>.
/// </summary>
[Parameter(HelpMessage = @"If set, the <Output> file generated will be a differencing disk with <Base> set as the parent. Otherwise the differences will be applied directly to <Base> (if no <Output> is set) or to a copy of <Base>.")]
public SwitchParameter MakeDifferencingDisk = false;
/// <summary>
/// [Optional] Only applies differences to this partition of the base disk from this partition of the diff VHD.
/// </summary>
[Parameter(HelpMessage = @"[Optional] Only applies differences to this partition of the base disk from this partition of the diff VHD.")]
[Alias("Partition1")]
public int? Partition = null;
/// <summary>
/// [Optional] Only applies differences from this partition of the diff VHD to <seealso cref="Partition"/> partition of the base disk.
/// </summary>
[Parameter(HelpMessage = @"[Optional] Only applies differences from this partition of the diff VHD to <Partition> partition of the base disk.")]
public int? Partition2 = null;
/// <summary>
/// [Optional] If set, will overwrite the output file if it exists. If not set, will produce an error if the file already exists.
/// </summary>
[Parameter(HelpMessage = @"[Optional] If set, will overwrite the output file if it exists. If not set, will produce an error if the file already exists.")]
public SwitchParameter Overwrite = false;
protected override void ProcessRecord()
{
// must use this to support processing record remotely.
if (Remote)
{
ProcessRecordViaRest();
return;
}
if (!Overwrite && Output == null)
{
WriteWarning(
"Unable to continue. Must provide an output location or specify '-Overwrite' to replace base image.");
return;
}
if (Partition.HasValue)
if (Partition2.HasValue) DiffVHD.ApplyDiff(Base, Diff, Output, MakeDifferencingDisk, new Tuple<int, int>(Partition.Value, Partition2.Value));
else DiffVHD.ApplyDiff(Base, Diff, Output, MakeDifferencingDisk, new Tuple<int,int>(Partition.Value, Partition.Value));
else DiffVHD.ApplyDiff(Base, Diff, Output, MakeDifferencingDisk);
}
}
}