File size: 9,596 Bytes
8c763fb | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if !UNIX
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Defines the base class from which all catalog commands are derived.
/// </summary>
public abstract class CatalogCommandsBase : PSCmdlet
{
/// <summary>
/// Path of folder/file to generate or validate the catalog file.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByPath")]
public string CatalogFilePath
{
get
{
return catalogFilePath;
}
set
{
catalogFilePath = value;
}
}
private string catalogFilePath;
/// <summary>
/// Path of folder/file to generate or validate the catalog file.
/// </summary>
[Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByPath")]
public string[] Path
{
get
{
return path;
}
set
{
path = value;
}
}
private string[] path;
//
// name of this command
//
private readonly string commandName;
/// <summary>
/// Initializes a new instance of the CatalogCommandsBase class,
/// using the given command name.
/// </summary>
/// <param name="name">
/// The name of the command.
/// </param>
protected CatalogCommandsBase(string name) : base()
{
commandName = name;
}
private CatalogCommandsBase() : base() { }
/// <summary>
/// Processes records from the input pipeline.
/// For each input object, the command either generate the Catalog or
/// Validates the existing Catalog.
/// </summary>
protected override void ProcessRecord()
{
//
// this cannot happen as we have specified the Path
// property to be mandatory parameter
//
Dbg.Assert((CatalogFilePath != null) && (CatalogFilePath.Length > 0),
"CatalogCommands: Param binder did not bind catalogFilePath");
Collection<string> paths = new();
if (Path != null)
{
foreach (string p in Path)
{
foreach (PathInfo tempPath in SessionState.Path.GetResolvedPSPathFromPSPath(p))
{
if (ShouldProcess("Including path " + tempPath.ProviderPath, string.Empty, string.Empty))
{
paths.Add(tempPath.ProviderPath);
}
}
}
}
string drive = null;
// resolve catalog destination Path
if (!SessionState.Path.IsPSAbsolute(catalogFilePath, out drive) && !System.IO.Path.IsPathRooted(catalogFilePath))
{
catalogFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(catalogFilePath);
}
if (ShouldProcess(catalogFilePath))
{
PerformAction(paths, catalogFilePath);
}
}
/// <summary>
/// Performs the action i.e. Generate or Validate the Windows Catalog File.
/// </summary>
/// <param name="path">
/// The name of the Folder or file on which to perform the action.
/// </param>
/// <param name="catalogFilePath">
/// Path to Catalog
/// </param>
protected abstract void PerformAction(Collection<string> path, string catalogFilePath);
}
/// <summary>
/// Defines the implementation of the 'New-FileCatalog' cmdlet.
/// This cmdlet generates the catalog for File or Folder.
/// </summary>
[Cmdlet(VerbsCommon.New, "FileCatalog", SupportsShouldProcess = true, DefaultParameterSetName = "ByPath",
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096596")]
[OutputType(typeof(FileInfo))]
public sealed class NewFileCatalogCommand : CatalogCommandsBase
{
/// <summary>
/// Initializes a new instance of the New-FileCatalog class.
/// </summary>
public NewFileCatalogCommand() : base("New-FileCatalog") { }
/// <summary>
/// Catalog version.
/// </summary>
[Parameter]
public int CatalogVersion
{
get
{
return catalogVersion;
}
set
{
catalogVersion = value;
}
}
// Based on the Catalog version we will decide which hashing Algorithm to use
private int catalogVersion = 2;
/// <summary>
/// Generate the Catalog for the Path.
/// </summary>
/// <param name="path">
/// File or Folder Path
/// </param>
/// <param name="catalogFilePath">
/// Path to Catalog
/// </param>
/// <returns>
/// True if able to Create Catalog or else False
/// </returns>
protected override void PerformAction(Collection<string> path, string catalogFilePath)
{
if (path.Count == 0)
{
// if user has not provided the path use current directory to generate catalog
path.Add(SessionState.Path.CurrentFileSystemLocation.Path);
}
FileInfo catalogFileInfo = new(catalogFilePath);
// If Path points to the expected cat file make sure
// parent Directory exists other wise CryptoAPI fails to create a .cat file
if (catalogFileInfo.Extension.Equals(".cat", StringComparison.Ordinal))
{
System.IO.Directory.CreateDirectory(catalogFileInfo.Directory.FullName);
}
else
{
// This only creates Directory if it does not exists, Append a default name
System.IO.Directory.CreateDirectory(catalogFilePath);
catalogFilePath = System.IO.Path.Combine(catalogFilePath, "catalog.cat");
}
FileInfo catalogFile = CatalogHelper.GenerateCatalog(this, path, catalogFilePath, catalogVersion);
if (catalogFile != null)
{
WriteObject(catalogFile);
}
}
}
/// <summary>
/// Defines the implementation of the 'Test-FileCatalog' cmdlet.
/// This cmdlet validates the Integrity of catalog.
/// </summary>
[Cmdlet(VerbsDiagnostic.Test, "FileCatalog", SupportsShouldProcess = true, DefaultParameterSetName = "ByPath",
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096921")]
[OutputType(typeof(CatalogValidationStatus))]
[OutputType(typeof(CatalogInformation))]
public sealed class TestFileCatalogCommand : CatalogCommandsBase
{
/// <summary>
/// Initializes a new instance of the New-FileCatalog class.
/// </summary>
public TestFileCatalogCommand() : base("Test-FileCatalog") { }
/// <summary>
/// </summary>
[Parameter]
public SwitchParameter Detailed
{
get { return detailed; }
set { detailed = value; }
}
private bool detailed = false;
/// <summary>
/// Patterns used to exclude files from DiskPaths and Catalog.
/// </summary>
[Parameter]
public string[] FilesToSkip
{
get
{
return filesToSkip;
}
set
{
filesToSkip = value;
this.excludedPatterns = new WildcardPattern[filesToSkip.Length];
for (int i = 0; i < filesToSkip.Length; i++)
{
this.excludedPatterns[i] = WildcardPattern.Get(filesToSkip[i], WildcardOptions.IgnoreCase);
}
}
}
private string[] filesToSkip = null;
internal WildcardPattern[] excludedPatterns = null;
/// <summary>
/// Validate the Integrity of given Catalog.
/// </summary>
/// <param name="path">
/// File or Folder Path
/// </param>
/// <param name="catalogFilePath">
/// Path to Catalog
/// </param>
/// <returns>
/// True if able to Validate the Catalog and its not tampered or else False
/// </returns>
protected override void PerformAction(Collection<string> path, string catalogFilePath)
{
if (path.Count == 0)
{
// if user has not provided the path use the path of catalog file itself.
path.Add(new FileInfo(catalogFilePath).Directory.FullName);
}
CatalogInformation catalogInfo = CatalogHelper.ValidateCatalog(this, path, catalogFilePath, excludedPatterns);
if (detailed)
{
WriteObject(catalogInfo);
}
else
{
WriteObject(catalogInfo.Status);
}
}
}
}
#endif
|