| 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.WindowsInstaller |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Text; |
| 8 | using System.Globalization; |
| 9 | using System.Diagnostics.CodeAnalysis; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// A list of source media for an installed product or patch. |
| 13 | /// </summary> |
| 14 | [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] |
| 15 | public class SourceMediaList : ICollection<MediaDisk> |
| 16 | { |
| 17 | private Installation installation; |
| 18 | |
| 19 | internal SourceMediaList(Installation installation) |
| 20 | { |
| 21 | this.installation = installation; |
| 22 | } |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Gets the number of source media in the list. |
| 26 | /// </summary> |
| 27 | public int Count |
| 28 | { |
| 29 | get |
| 30 | { |
| 31 | int count = 0; |
| 32 | IEnumerator<MediaDisk> e = this.GetEnumerator(); |
| 33 | while (e.MoveNext()) |
| 34 | { |
| 35 | count++; |
| 36 | } |
| 37 | return count; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Gets a boolean value indicating whether the list is read-only. |
| 43 | /// A SourceMediaList is never read-only. |
| 44 | /// </summary> |
| 45 | /// <value>read-only status of the list</value> |
| 46 | public bool IsReadOnly |
| 47 | { |
| 48 | get |
| 49 | { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Adds or updates a disk of the media source for the product or patch. |
| 56 | /// </summary> |
| 57 | /// <remarks><p> |
| 58 | /// Win32 MSI API: |
| 59 | /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msisourcelistaddmediadisk.asp">MsiSourceListAddMediaDisk</a> |
| 60 | /// </p></remarks> |
| 61 | public void Add(MediaDisk item) |
| 62 | { |
| 63 | uint ret = NativeMethods.MsiSourceListAddMediaDisk( |
| 64 | this.installation.InstallationCode, |
| 65 | this.installation.UserSid, |
| 66 | this.installation.Context, |
| 67 | (uint) this.installation.InstallationType, |
| 68 | (uint) item.DiskId, |
| 69 | item.VolumeLabel, |
| 70 | item.DiskPrompt); |
| 71 | |
| 72 | if (ret != 0) |
| 73 | { |
| 74 | throw InstallerException.ExceptionFromReturnCode(ret); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Removes all source media from the list. |
| 80 | /// </summary> |
| 81 | /// <remarks><p> |
| 82 | /// Win32 MSI API: |
| 83 | /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msisourcelistclearallex.asp">MsiSourceListClearAllEx</a> |
| 84 | /// </p></remarks> |
| 85 | public void Clear() |
| 86 | { |
| 87 | uint ret = NativeMethods.MsiSourceListClearAllEx( |
| 88 | this.installation.InstallationCode, |
| 89 | this.installation.UserSid, |
| 90 | this.installation.Context, |
| 91 | (uint) NativeMethods.SourceType.Media | (uint) this.installation.InstallationType); |
| 92 | |
| 93 | if (ret != 0) |
| 94 | { |
| 95 | throw InstallerException.ExceptionFromReturnCode(ret); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// <summary> |
| 100 | /// Checks if the specified media disk id exists in the list. |
| 101 | /// </summary> |
| 102 | /// <param name="diskId">disk id of the media to look for</param> |
| 103 | /// <returns>true if the media exists in the list, false otherwise</returns> |
| 104 | public bool Contains(int diskId) |
| 105 | { |
| 106 | foreach (MediaDisk mediaDisk in this) |
| 107 | { |
| 108 | if (mediaDisk.DiskId == diskId) |
| 109 | { |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool ICollection<MediaDisk>.Contains(MediaDisk mediaDisk) |
| 117 | { |
| 118 | return this.Contains(mediaDisk.DiskId); |
| 119 | } |
| 120 | |
| 121 | /// <summary> |
| 122 | /// Copies the source media info from this list into an array. |
| 123 | /// </summary> |
| 124 | /// <param name="array">destination array to be filed</param> |
| 125 | /// <param name="arrayIndex">offset into the destination array where copying begins</param> |
| 126 | public void CopyTo(MediaDisk[] array, int arrayIndex) |
| 127 | { |
| 128 | foreach (MediaDisk mediaDisk in this) |
| 129 | { |
| 130 | array[arrayIndex++] = mediaDisk; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// <summary> |
| 135 | /// Removes a specified disk from the set of registered disks. |
| 136 | /// </summary> |
| 137 | /// <param name="diskId">ID of the disk to remove</param> |
| 138 | /// <remarks><p> |
| 139 | /// Win32 MSI API: |
| 140 | /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msisourcelistclearmediadisk.asp">MsiSourceListClearMediaDisk</a> |
| 141 | /// </p></remarks> |
| 142 | public bool Remove(int diskId) |
| 143 | { |
| 144 | uint ret = NativeMethods.MsiSourceListClearMediaDisk( |
| 145 | this.installation.InstallationCode, |
| 146 | this.installation.UserSid, |
| 147 | this.installation.Context, |
| 148 | (uint) this.installation.InstallationType, |
| 149 | (uint) diskId); |
| 150 | |
| 151 | if (ret != 0) |
| 152 | { |
| 153 | // TODO: Figure out when to return false. |
| 154 | throw InstallerException.ExceptionFromReturnCode(ret); |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool ICollection<MediaDisk>.Remove(MediaDisk mediaDisk) |
| 160 | { |
| 161 | return this.Remove(mediaDisk.DiskId); |
| 162 | } |
| 163 | |
| 164 | /// <summary> |
| 165 | /// Enumerates the source media in the source list of the patch or product installation. |
| 166 | /// </summary> |
| 167 | /// <remarks><p> |
| 168 | /// Win32 MSI API: |
| 169 | /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msisourcelistenummediadisks.asp">MsiSourceListEnumMediaDisks</a> |
| 170 | /// </p></remarks> |
| 171 | public IEnumerator<MediaDisk> GetEnumerator() |
| 172 | { |
| 173 | uint diskId; |
| 174 | StringBuilder volumeBuf = new StringBuilder(40); |
| 175 | uint volumeBufSize = (uint) volumeBuf.Capacity; |
| 176 | StringBuilder promptBuf = new StringBuilder(80); |
| 177 | uint promptBufSize = (uint) promptBuf.Capacity; |
| 178 | for (uint i = 0; true; i++) |
| 179 | { |
| 180 | uint ret = NativeMethods.MsiSourceListEnumMediaDisks( |
| 181 | this.installation.InstallationCode, |
| 182 | this.installation.UserSid, |
| 183 | this.installation.Context, |
| 184 | (uint) this.installation.InstallationType, |
| 185 | i, |
| 186 | out diskId, |
| 187 | volumeBuf, |
| 188 | ref volumeBufSize, |
| 189 | promptBuf, |
| 190 | ref promptBufSize); |
| 191 | |
| 192 | if (ret == (uint) NativeMethods.Error.MORE_DATA) |
| 193 | { |
| 194 | volumeBuf.Capacity = (int) ++volumeBufSize; |
| 195 | promptBuf.Capacity = (int) ++promptBufSize; |
| 196 | |
| 197 | ret = NativeMethods.MsiSourceListEnumMediaDisks( |
| 198 | this.installation.InstallationCode, |
| 199 | this.installation.UserSid, |
| 200 | this.installation.Context, |
| 201 | (uint) this.installation.InstallationType, |
| 202 | i, |
| 203 | out diskId, |
| 204 | volumeBuf, |
| 205 | ref volumeBufSize, |
| 206 | promptBuf, |
| 207 | ref promptBufSize); |
| 208 | } |
| 209 | |
| 210 | if (ret == (uint) NativeMethods.Error.NO_MORE_ITEMS) |
| 211 | { |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | if (ret != 0) |
| 216 | { |
| 217 | throw InstallerException.ExceptionFromReturnCode(ret); |
| 218 | } |
| 219 | |
| 220 | yield return new MediaDisk((int) diskId, volumeBuf.ToString(), promptBuf.ToString()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| 225 | { |
| 226 | return this.GetEnumerator(); |
| 227 | } |
| 228 | } |
| 229 | } |