File size: 4,714 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
namespace System.Management.Automation.Internal
{
/// <summary>
/// CabinetExtractor interface, implemented by CabinetExtractor
/// in native code to handle the extraction of cabinet files.
/// </summary>
internal abstract class ICabinetExtractor : IDisposable
{
/// <summary>
/// Extracts a cabinet file.
/// </summary>
/// <param name="cabinetName">Cabinet file name.</param>
/// <param name="srcPath">Cabinet directory name, must be back slash terminated.</param>
/// <param name="destPath">Destination directory name, must be back slash terminated.</param>
internal abstract bool Extract(string cabinetName, string srcPath, string destPath);
#region IDisposable Interface
//
// This is a special case of the IDisposable pattern because the resource
// to be disposed is managed by the derived class. The implementation here
// enables derived classes to handle it cleanly.
//
/// <summary>
/// Flag: Has Dispose already been called?
/// </summary>
private bool _disposed = false;
/// <summary>
/// Disposes the instance, callable by consumers.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
// Nothing to do since the resource has already been disposed.
return;
}
// If this class had to free objects:
// Free managed objects if disposing == true;
// Free unmanaged objects regardless.
_disposed = true;
}
~ICabinetExtractor()
{
Dispose(false);
}
#endregion
}
/// <summary>
/// Abstract class which defines a CabinetExtractor loader. An implementation
/// of this class will be instantiated onetime from the C++/CLI
/// assembly using reflection.
/// </summary>
/// <remarks>The C++/CLI implementation of this class needs to be
/// static</remarks>
internal abstract class ICabinetExtractorLoader
{
internal virtual ICabinetExtractor GetCabinetExtractor() { return null; }
}
/// <summary>
/// Used to create a CabinetExtractor class.
/// </summary>
internal static class CabinetExtractorFactory
{
private static readonly ICabinetExtractorLoader s_cabinetLoader;
internal static readonly ICabinetExtractor EmptyExtractor = new EmptyCabinetExtractor();
/// <summary>
/// Static constructor.
/// </summary>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
static CabinetExtractorFactory()
{
s_cabinetLoader = CabinetExtractorLoader.GetInstance();
}
/// <summary>
/// Provider a CabinetExtractor instance.
/// </summary>
/// <returns>Tracer instance.</returns>
internal static ICabinetExtractor GetCabinetExtractor()
{
if (s_cabinetLoader != null)
{
return s_cabinetLoader.GetCabinetExtractor();
}
else
{
return EmptyExtractor;
}
}
}
/// <summary>
/// Dummy cabinet extractor implementation.
/// </summary>
internal sealed class EmptyCabinetExtractor : ICabinetExtractor
{
/// <summary>
/// Extracts a cabinet file.
/// </summary>
/// <param name="cabinetName">Cabinet file name.</param>
/// <param name="srcPath">Cabinet directory name, must be back slash terminated.</param>
/// <param name="destPath">Destination directory name, must be back slash terminated.</param>
internal override bool Extract(string cabinetName, string srcPath, string destPath)
{
// its intentional that this method has no definition
return false;
}
/// <summary>
/// Disposes the instance.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
// it's intentional that this method has no definition since there is nothing to dispose.
// If a resource is added to this class, it should implement IDisposable for derived classes.
}
}
}
|