mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Merge pull request #1474 from PowerShell/vors/remove-cimcmdlets-binary
Remove *BinaryMiLog cmdlets
This commit is contained in:
@@ -1,201 +0,0 @@
|
||||
/*============================================================================
|
||||
* Copyright (C) Microsoft Corporation, All rights reserved.
|
||||
*============================================================================
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace Microsoft.Management.Infrastructure.CimCmdlets
|
||||
{
|
||||
public abstract class BinaryMiLogBase : PSCmdlet
|
||||
{
|
||||
internal const string Noun = "BinaryMiLog";
|
||||
|
||||
internal BinaryMiLogBase()
|
||||
{
|
||||
}
|
||||
|
||||
[Parameter(Mandatory = true, Position = 0)]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string Path { get; set; }
|
||||
|
||||
#region Path utilities copied from internal admin/monad/src/utils/PathUtils.cs
|
||||
|
||||
/* command.Context.ProviderNames.FileSystem is internal */
|
||||
private const string FileSystemProviderName = "FileSystem";
|
||||
|
||||
private static string ResolveFilePath(string filePath, PSCmdlet command, bool isLiteralPath)
|
||||
{
|
||||
string path = null;
|
||||
|
||||
try
|
||||
{
|
||||
ProviderInfo provider = null;
|
||||
PSDriveInfo drive = null;
|
||||
List<string> filePaths = new List<string>();
|
||||
|
||||
if (isLiteralPath)
|
||||
{
|
||||
filePaths.Add(command.SessionState.Path.GetUnresolvedProviderPathFromPSPath(filePath, out provider, out drive));
|
||||
}
|
||||
else
|
||||
{
|
||||
filePaths.AddRange(command.SessionState.Path.GetResolvedProviderPathFromPSPath(filePath, out provider));
|
||||
}
|
||||
|
||||
/* command.Context.ProviderNames.FileSystem is internal */
|
||||
if (!provider.Name.Equals(FileSystemProviderName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
ReportWrongProviderType(command, provider.Name);
|
||||
}
|
||||
|
||||
if (filePaths.Count > 1)
|
||||
{
|
||||
ReportMultipleFilesNotSupported(command);
|
||||
}
|
||||
if (filePaths.Count == 0)
|
||||
{
|
||||
ReportWildcardingFailure(command, filePath);
|
||||
}
|
||||
|
||||
path = filePaths[0];
|
||||
}
|
||||
catch (ItemNotFoundException)
|
||||
{
|
||||
if (!isLiteralPath)
|
||||
{
|
||||
return ResolveFilePath(filePath, command, isLiteralPath: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
internal string GetVerifiedFilePath(FileAccess fileAccess)
|
||||
{
|
||||
string resolvedPath = ResolveFilePath(this.Path, this, isLiteralPath: false);
|
||||
|
||||
try
|
||||
{
|
||||
FileMode fileMode;
|
||||
FileShare fileShare;
|
||||
switch (fileAccess)
|
||||
{
|
||||
case FileAccess.Read:
|
||||
fileMode = FileMode.Open;
|
||||
fileShare = FileShare.Read;
|
||||
break;
|
||||
case FileAccess.Write:
|
||||
fileMode = FileMode.Create;
|
||||
fileShare = FileShare.None;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("fileAccess");
|
||||
}
|
||||
|
||||
using (new FileStream(resolvedPath, fileMode, fileAccess, fileShare))
|
||||
{
|
||||
/* open and immediately close the file - to get exception in PowerShell style;
|
||||
* obviously there is a race here - we can still get error from mibincodec.dll later on */
|
||||
}
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
// NOTE: this call will throw
|
||||
ReportFileOpenFailure(this, resolvedPath, e);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// NOTE: this call will throw
|
||||
ReportFileOpenFailure(this, resolvedPath, e);
|
||||
}
|
||||
catch (UnauthorizedAccessException e)
|
||||
{
|
||||
// NOTE: this call will throw
|
||||
ReportFileOpenFailure(this, resolvedPath, e);
|
||||
}
|
||||
catch (NotSupportedException e)
|
||||
{
|
||||
// NOTE: this call will throw
|
||||
ReportFileOpenFailure(this, resolvedPath, e);
|
||||
}
|
||||
catch (System.Security.SecurityException e)
|
||||
{
|
||||
// NOTE: this call will throw
|
||||
ReportFileOpenFailure(this, resolvedPath, e);
|
||||
}
|
||||
|
||||
return resolvedPath;
|
||||
}
|
||||
|
||||
private static void ReportFileOpenFailure(Cmdlet cmdlet, string filePath, Exception e)
|
||||
{
|
||||
ErrorRecord errorRecord = new ErrorRecord(
|
||||
e,
|
||||
"FileOpenFailure",
|
||||
ErrorCategory.OpenError,
|
||||
null);
|
||||
|
||||
cmdlet.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
|
||||
internal static void ReportWrongProviderType(Cmdlet cmdlet, string providerId)
|
||||
{
|
||||
string msg = string.Format(
|
||||
CultureInfo.InvariantCulture, /* providerId is culture invariant */
|
||||
Strings.ReadWriteFileNotFileSystemProvider,
|
||||
providerId);
|
||||
|
||||
var errorRecord = new ErrorRecord (
|
||||
new InvalidOperationException(msg),
|
||||
"ReadWriteFileNotFileSystemProvider",
|
||||
ErrorCategory.InvalidArgument,
|
||||
null);
|
||||
|
||||
errorRecord.ErrorDetails = new ErrorDetails(msg);
|
||||
cmdlet.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
internal static void ReportMultipleFilesNotSupported(Cmdlet cmdlet)
|
||||
{
|
||||
string msg = Strings.MultipleFilesNotSupported;
|
||||
|
||||
var errorRecord = new ErrorRecord (
|
||||
new InvalidOperationException(msg),
|
||||
"ReadWriteMultipleFilesNotSupported",
|
||||
ErrorCategory.InvalidArgument,
|
||||
null);
|
||||
|
||||
errorRecord.ErrorDetails = new ErrorDetails(msg);
|
||||
cmdlet.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
internal static void ReportWildcardingFailure(Cmdlet cmdlet, string filePath)
|
||||
{
|
||||
string msg = string.Format(
|
||||
CultureInfo.InvariantCulture, /* filePath is culture invariant */
|
||||
Strings.DidNotResolveFile,
|
||||
filePath);
|
||||
|
||||
var errorRecord = new ErrorRecord (
|
||||
new FileNotFoundException(msg),
|
||||
"FileOpenFailure",
|
||||
ErrorCategory.OpenError,
|
||||
filePath);
|
||||
|
||||
errorRecord.ErrorDetails = new ErrorDetails(msg);
|
||||
cmdlet.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
/*============================================================================
|
||||
* Copyright (C) Microsoft Corporation, All rights reserved.
|
||||
*============================================================================
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Management.Automation;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
#if CORECLR
|
||||
// Use stubs for SafeHandleZeroOrMinusOneIsInvalid and ReliabilityContractAttribute
|
||||
using Microsoft.PowerShell.CoreClr.Stubs;
|
||||
#else
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
#endif
|
||||
|
||||
namespace Microsoft.Management.Infrastructure.CimCmdlets
|
||||
{
|
||||
[Cmdlet(VerbsData.Export, BinaryMiLogBase.Noun, HelpUri = "http://go.microsoft.com/fwlink/?LinkId=301310")]
|
||||
public sealed class ExportBinaryMiLogCommand : BinaryMiLogBase, IDisposable
|
||||
{
|
||||
[Parameter(ValueFromPipeline = true)]
|
||||
[ValidateNotNull]
|
||||
public CimInstance InputObject { get; set; }
|
||||
|
||||
private static class NativeMethods
|
||||
{
|
||||
internal class WriterHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private WriterHandle()
|
||||
: base(true)
|
||||
{
|
||||
}
|
||||
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
Native.InstanceHandle errorHandle = null;
|
||||
Native.MiResult result = SyncBmilWriter_Delete(this.handle, out errorHandle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern Native.MiResult SyncBmilWriter_Create(
|
||||
string filePath,
|
||||
out WriterHandle writerHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern Native.MiResult SyncBmilWriter_WriteInstance(
|
||||
WriterHandle writerHandle,
|
||||
Native.InstanceHandle instanceHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern Native.MiResult SyncBmilWriter_Delete(
|
||||
IntPtr writerHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
}
|
||||
|
||||
private sealed class Writer : IDisposable
|
||||
{
|
||||
private readonly NativeMethods.WriterHandle _writerHandle = null;
|
||||
|
||||
internal Writer(string file)
|
||||
{
|
||||
Native.InstanceHandle errorHandle;
|
||||
Native.MiResult miResult;
|
||||
miResult = NativeMethods.SyncBmilWriter_Create(file, out _writerHandle, out errorHandle);
|
||||
CimException.ThrowIfMiResultFailure(miResult, errorHandle);
|
||||
}
|
||||
|
||||
internal void WriteInstance(CimInstance cimInstance)
|
||||
{
|
||||
if (cimInstance == null)
|
||||
{
|
||||
throw new ArgumentNullException("cimInstance");
|
||||
}
|
||||
|
||||
Native.InstanceHandle errorHandle;
|
||||
Native.MiResult miResult;
|
||||
miResult = NativeMethods.SyncBmilWriter_WriteInstance(_writerHandle, cimInstance.InstanceHandle, out errorHandle);
|
||||
CimException.ThrowIfMiResultFailure(miResult, errorHandle);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_writerHandle.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private Writer _writer;
|
||||
|
||||
protected override void BeginProcessing()
|
||||
{
|
||||
_writer = new Writer(this.GetVerifiedFilePath(FileAccess.Write));
|
||||
}
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
_writer.WriteInstance(InputObject);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_writer != null)
|
||||
{
|
||||
_writer.Dispose();
|
||||
_writer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
/*============================================================================
|
||||
* Copyright (C) Microsoft Corporation, All rights reserved.
|
||||
*============================================================================
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Management.Automation;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
#if CORECLR
|
||||
// Use stubs for SafeHandleZeroOrMinusOneIsInvalid and ReliabilityContractAttribute
|
||||
using Microsoft.PowerShell.CoreClr.Stubs;
|
||||
#else
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
#endif
|
||||
|
||||
namespace Microsoft.Management.Infrastructure.CimCmdlets
|
||||
{
|
||||
[Cmdlet(VerbsData.Import, BinaryMiLogBase.Noun, HelpUri = "http://go.microsoft.com/fwlink/?LinkId=301309")]
|
||||
public sealed class ImportBinaryMiLogCommand : BinaryMiLogBase
|
||||
{
|
||||
private static class NativeMethods
|
||||
{
|
||||
internal class ReaderHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private ReaderHandle()
|
||||
: base(true)
|
||||
{
|
||||
}
|
||||
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
Native.InstanceHandle errorHandle = null;
|
||||
Native.MiResult result = SyncBmilReader_Delete(this.handle, out errorHandle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern Native.MiResult SyncBmilReader_Create(
|
||||
string filePath,
|
||||
out ReaderHandle readerHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern Native.MiResult SyncBmilReader_ReadInstance(
|
||||
ReaderHandle readerHandle,
|
||||
out Native.InstanceHandle instanceHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
|
||||
[DllImport("mpunits.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern Native.MiResult SyncBmilReader_Delete(
|
||||
IntPtr readerHandle,
|
||||
out Native.InstanceHandle errorHandle);
|
||||
}
|
||||
|
||||
private sealed class Reader : IDisposable
|
||||
{
|
||||
private readonly NativeMethods.ReaderHandle _readerHandle;
|
||||
|
||||
internal Reader(string file)
|
||||
{
|
||||
Native.InstanceHandle errorHandle;
|
||||
Native.MiResult miResult;
|
||||
miResult = NativeMethods.SyncBmilReader_Create(file, out _readerHandle, out errorHandle);
|
||||
CimException.ThrowIfMiResultFailure(miResult, errorHandle);
|
||||
}
|
||||
|
||||
internal CimInstance ReadInstance()
|
||||
{
|
||||
Native.InstanceHandle errorHandle;
|
||||
Native.InstanceHandle instanceHandle;
|
||||
Native.MiResult miResult;
|
||||
miResult = NativeMethods.SyncBmilReader_ReadInstance(_readerHandle, out instanceHandle, out errorHandle);
|
||||
CimException.ThrowIfMiResultFailure(miResult, errorHandle);
|
||||
|
||||
if (instanceHandle.IsInvalid)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var cimInstance = new CimInstance(instanceHandle, null);
|
||||
return cimInstance;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_readerHandle.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void BeginProcessing()
|
||||
{
|
||||
base.BeginProcessing();
|
||||
|
||||
this.GetVerifiedFilePath(FileAccess.Read);
|
||||
using (var reader = new Reader(this.GetVerifiedFilePath(FileAccess.Read)))
|
||||
{
|
||||
CimInstance cimInstance;
|
||||
do
|
||||
{
|
||||
cimInstance = reader.ReadInstance();
|
||||
if (cimInstance != null)
|
||||
{
|
||||
this.WriteObject(cimInstance);
|
||||
}
|
||||
} while (cimInstance != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user