| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Compression.Cab |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Resources; |
| 8 | using System.Globalization; |
| 9 | using System.Runtime.Serialization; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Exception class for cabinet operations. |
| 13 | /// </summary> |
| 14 | [Serializable] |
| 15 | public class CabException : ArchiveException |
| 16 | { |
| 17 | private static ResourceManager errorResources; |
| 18 | private int error; |
| 19 | private int errorCode; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Creates a new CabException with a specified error message and a reference to the |
| 23 | /// inner exception that is the cause of this exception. |
| 24 | /// </summary> |
| 25 | /// <param name="message">The message that describes the error.</param> |
| 26 | /// <param name="innerException">The exception that is the cause of the current exception. If the |
| 27 | /// innerException parameter is not a null reference (Nothing in Visual Basic), the current exception |
| 28 | /// is raised in a catch block that handles the inner exception.</param> |
| 29 | public CabException(string message, Exception innerException) |
| 30 | : this(0, 0, message, innerException) { } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Creates a new CabException with a specified error message. |
| 34 | /// </summary> |
| 35 | /// <param name="message">The message that describes the error.</param> |
| 36 | public CabException(string message) |
| 37 | : this(0, 0, message, null) { } |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Creates a new CabException. |
| 41 | /// </summary> |
| 42 | public CabException() |
| 43 | : this(0, 0, null, null) { } |
| 44 | |
| 45 | internal CabException(int error, int errorCode, string message, Exception innerException) |
| 46 | : base(message, innerException) |
| 47 | { |
| 48 | this.error = error; |
| 49 | this.errorCode = errorCode; |
| 50 | } |
| 51 | |
| 52 | internal CabException(int error, int errorCode, string message) |
| 53 | : this(error, errorCode, message, null) { } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Initializes a new instance of the CabException class with serialized data. |
| 57 | /// </summary> |
| 58 | /// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param> |
| 59 | /// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param> |
| 60 | protected CabException(SerializationInfo info, StreamingContext context) : base(info, context) |
| 61 | { |
| 62 | if (info == null) |
| 63 | { |
| 64 | throw new ArgumentNullException("info"); |
| 65 | } |
| 66 | |
| 67 | this.error = info.GetInt32("cabError"); |
| 68 | this.errorCode = info.GetInt32("cabErrorCode"); |
| 69 | } |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Gets the FCI or FDI cabinet engine error number. |
| 73 | /// </summary> |
| 74 | /// <value>A cabinet engine error number, or 0 if the exception was |
| 75 | /// not related to a cabinet engine error number.</value> |
| 76 | public int Error |
| 77 | { |
| 78 | get |
| 79 | { |
| 80 | return this.error; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// <summary> |
| 85 | /// Gets the Win32 error code. |
| 86 | /// </summary> |
| 87 | /// <value>A Win32 error code, or 0 if the exception was |
| 88 | /// not related to a Win32 error.</value> |
| 89 | public int ErrorCode |
| 90 | { |
| 91 | get |
| 92 | { |
| 93 | return this.errorCode; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | internal static ResourceManager ErrorResources |
| 98 | { |
| 99 | get |
| 100 | { |
| 101 | if (errorResources == null) |
| 102 | { |
| 103 | errorResources = new ResourceManager( |
| 104 | typeof(CabException).Namespace + ".Errors", |
| 105 | typeof(CabException).Assembly); |
| 106 | } |
| 107 | return errorResources; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// <summary> |
| 112 | /// Sets the SerializationInfo with information about the exception. |
| 113 | /// </summary> |
| 114 | /// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param> |
| 115 | /// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param> |
| 116 | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| 117 | { |
| 118 | if (info == null) |
| 119 | { |
| 120 | throw new ArgumentNullException("info"); |
| 121 | } |
| 122 | |
| 123 | info.AddValue("cabError", this.error); |
| 124 | info.AddValue("cabErrorCode", this.errorCode); |
| 125 | base.GetObjectData(info, context); |
| 126 | } |
| 127 | |
| 128 | internal static string GetErrorMessage(int error, int errorCode, bool extracting) |
| 129 | { |
| 130 | const int FCI_ERROR_RESOURCE_OFFSET = 1000; |
| 131 | const int FDI_ERROR_RESOURCE_OFFSET = 2000; |
| 132 | int resourceOffset = (extracting ? FDI_ERROR_RESOURCE_OFFSET : FCI_ERROR_RESOURCE_OFFSET); |
| 133 | |
| 134 | string msg = CabException.ErrorResources.GetString( |
| 135 | (resourceOffset + error).ToString(CultureInfo.InvariantCulture.NumberFormat), |
| 136 | CultureInfo.CurrentCulture); |
| 137 | |
| 138 | if (msg == null) |
| 139 | { |
| 140 | msg = CabException.ErrorResources.GetString( |
| 141 | resourceOffset.ToString(CultureInfo.InvariantCulture.NumberFormat), |
| 142 | CultureInfo.CurrentCulture); |
| 143 | } |
| 144 | |
| 145 | if (errorCode != 0) |
| 146 | { |
| 147 | const string GENERIC_ERROR_RESOURCE = "1"; |
| 148 | string msg2 = CabException.ErrorResources.GetString(GENERIC_ERROR_RESOURCE, CultureInfo.CurrentCulture); |
| 149 | msg = String.Format(CultureInfo.InvariantCulture, "{0} " + msg2, msg, errorCode); |
| 150 | } |
| 151 | return msg; |
| 152 | } |
| 153 | } |
| 154 | } |