forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftwareIdentity.cs
More file actions
360 lines (313 loc) · 14.4 KB
/
Copy pathSoftwareIdentity.cs
File metadata and controls
360 lines (313 loc) · 14.4 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PackageManagement.Packaging {
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using Implementation;
using Internal.Api;
using Internal.Packaging;
using Internal.Utility.Collections;
using Internal.Utility.Extensions;
/// <summary>
/// This class represents a package (retrieved from Find-SoftwareIdentity or Get-SoftwareIdentity)
/// Will eventually also represent a swidtag.
/// </summary>
public class SoftwareIdentity : Swidtag {
public SoftwareIdentity(XDocument document)
: base(document) {
}
public SoftwareIdentity() {
}
public string FastPackageReference {get; set;}
internal PackageProvider Provider {get; set;}
public string ProviderName {
get {
return Provider != null ? Provider.ProviderName : null;
}
}
public string Source {get; internal set;}
public string Status {get; internal set;}
public string SearchKey {get; internal set;}
public string FullPath {get; internal set;}
public string PackageFilename {get; internal set;}
public bool FromTrustedSource {get; set;}
public string Summary {
get {
return Element.Elements(Iso19770_2.Elements.Meta).Select(each => {
var summary = each.GetAttribute(Iso19770_2.Attributes.Summary);
if (string.IsNullOrWhiteSpace(summary)) {
summary = each.GetAttribute(Iso19770_2.Attributes.Description);
}
return summary;
}).WhereNotNull().FirstOrDefault();
}
internal set {
(Meta.FirstOrDefault() ?? AddMeta()).AddAttribute(Iso19770_2.Attributes.Summary, value);
}
}
public IEnumerable<Swidtag> SwidTags {
get {
// todo: in the not-too-distant future, the SoftwareIdentity will support a collection of swidtags in addition to 'itself'
// todo: at that time, we'll add the remaining logic to support collections of swidtags for a given SoftwareIdentity.
yield return this;
}
}
public string CanonicalId {
get {
return CreateCanonicalId(ProviderName, Name, Version, Source);
}
}
public MetadataIndexer Metadata {
get {
return new MetadataIndexer(this);
}
}
internal string InstallationPath
{
get
{
// no payload or directories, just returns empty string
if (Payload == null || Payload.Directories == null)
{
return string.Empty;
}
// find the installation directories based on the payload.
var installationDirectories = Payload.Directories
// Use the directory if it is key, if its location is not null or white space and its name is not null or whitespace
.Where(directory => directory.IsKey.HasValue
&& (bool)directory.IsKey
&& !string.IsNullOrWhiteSpace(directory.Location)
&& !string.IsNullOrWhiteSpace(directory.Name))
// combine location and name to get the directory
.Select(directory => System.IO.Path.Combine(directory.Location, directory.Name));
// if there is installation directories, concat them with comma
return installationDirectories.JoinWithComma();
}
}
internal static string CreateCanonicalId(string provider, string name, string version, string source) {
if (provider == null || name == null) {
return null;
}
if (string.IsNullOrWhiteSpace(version) && string.IsNullOrWhiteSpace(source)) {
return "{0}:{1}".format(CultureInfo.CurrentCulture.TextInfo.ToLower(provider), name);
}
if (string.IsNullOrWhiteSpace(source)) {
return "{0}:{1}/{2}".format(CultureInfo.CurrentCulture.TextInfo.ToLower(provider), name, version);
}
if (string.IsNullOrWhiteSpace(version)) {
"{0}:{1}#{2}".format(CultureInfo.CurrentCulture.TextInfo.ToLower(provider), name, source);
}
return "{0}:{1}/{2}#{3}".format(CultureInfo.CurrentCulture.TextInfo.ToLower(provider), name, version, source);
}
public void FetchPackageDetails(IHostApi api) {
Provider.GetPackageDetails(this, api);
}
/// <summary>
/// Sets a SoftwareMetadata value
/// </summary>
/// <param name="metaKey"></param>
/// <param name="value"></param>
internal void AddMetadataAttribute(string metaKey, string value) {
if (string.IsNullOrWhiteSpace(value)) {
// we don't store empty values.
return;
}
var metaElements = Element.Elements(Iso19770_2.Elements.Meta).ReEnumerable();
var currentValue = metaElements.Select(each => each.GetAttribute(metaKey)).WhereNotNull().FirstOrDefault();
// if the current value is already the value, then don't worry about it.
if (currentValue == value) {
return;
}
if (metaElements.Any() && currentValue == null) {
// value has not been set (and we have at least one metadata element)
// let's set it in the first meta element.
Element.Elements(Iso19770_2.Elements.Meta).FirstOrDefault().AddAttribute(metaKey, value);
} else {
// add a new metadata object at the end and set the value in that element.
AddElement(new Meta())
.AddAttribute(metaKey, value);
}
}
protected override XElement FindElementWithUniqueId(string elementId) {
if (elementId == FastPackageReference) {
return Element;
}
return base.FindElementWithUniqueId(elementId);
}
internal string AddMeta(string elementPath) {
var element = FindElementWithUniqueId(elementPath);
if (element != null) {
switch (element.Name.LocalName) {
case "SoftwareIdentity":
// adds a SoftwareMeta to the swidtag
return AddElement(new SoftwareMetadata()).ElementUniqueId;
case "Entity":
// adds a Meta to the entity
return new Entity(element).AddMeta().ElementUniqueId;
}
}
return null;
}
internal string AddLink(Uri referenceUri, string relationship, string mediaType, string ownership, string use, string appliesToMedia, string artifact) {
var link = AddLink(referenceUri, relationship);
if (!string.IsNullOrWhiteSpace(mediaType)) {
link.MediaType = mediaType;
}
if (!string.IsNullOrWhiteSpace(ownership)) {
link.Ownership = ownership;
}
if (!string.IsNullOrWhiteSpace(use)) {
link.Use = use;
}
if (!string.IsNullOrWhiteSpace(appliesToMedia)) {
link.Media = appliesToMedia;
}
if (!string.IsNullOrWhiteSpace(artifact)) {
link.Artifact = artifact;
}
return link.ElementUniqueId;
}
internal string AddEntity(string name, string regid, string role, string thumbprint) {
var entity = AddEntity(name, regid, role);
if (!string.IsNullOrWhiteSpace(thumbprint) && entity.Thumbprint == null) {
entity.Thumbprint = thumbprint;
}
return entity.ElementUniqueId;
}
public string AddMetadataValue(string elementPath, Uri @namespace, string name, string value) {
if (@namespace == null) {
return null;
}
var element = FindElementWithUniqueId(elementPath);
if (element != null) {
element.AddAttribute(XNamespace.Get(@namespace.ToString()) + name, value);
return PathToElement(element);
}
return null;
}
internal string AddMetadataValue(string elementPath, string name, string value) {
var element = FindElementWithUniqueId(elementPath);
if (element == null || string.IsNullOrWhiteSpace(name)) {
return null;
}
if (element == Element) {
// special case:
if (name.EqualsIgnoreCase("FromTrustedSource")) {
FromTrustedSource = (value ?? string.Empty).IsTrue();
return FastPackageReference;
}
// metadata values on the swidtag go to the first Meta
var meta = (Meta.FirstOrDefault() ?? AddMeta());
meta.AddAttribute(name, value);
return meta.ElementUniqueId;
}
if (element.Name == Iso19770_2.Elements.Entity) {
// metadata values on entities go to the first Meta in the entity
var entity = new Entity(element);
var meta = (entity.Meta.FirstOrDefault() ?? entity.AddMeta());
meta.AddAttribute(name, value);
return meta.ElementUniqueId;
}
// for non-namespaced metadata values, the target element must be one that inherits from Meta
if (IsMetaElement(element)) {
var meta = new Meta(element);
meta.AddAttribute(name, value);
return meta.ElementUniqueId;
}
return null;
}
internal string AddResource(string elementPath, string type) {
var element = FindElementWithUniqueId(elementPath);
if (element == null || string.IsNullOrWhiteSpace(type)) {
return null;
}
if (element.Name == Iso19770_2.Elements.Payload || element.Name == Iso19770_2.Elements.Evidence) {
return new ResourceCollection(element).AddResource(type).ElementUniqueId;
}
return null;
}
internal string AddProcess(string elementPath, string processName, int pid) {
var element = FindElementWithUniqueId(elementPath);
if (element == null || string.IsNullOrWhiteSpace(processName)) {
return null;
}
if (element.Name == Iso19770_2.Elements.Payload || element.Name == Iso19770_2.Elements.Evidence) {
var process = new ResourceCollection(element).AddProcess(processName);
if (pid != 0) {
process.Pid = pid;
}
return process.ElementUniqueId;
}
return null;
}
internal string AddFile(string elementPath, string fileName, string location, string root, bool isKey, long size, string version) {
var element = FindElementWithUniqueId(elementPath);
if (element == null || string.IsNullOrWhiteSpace(fileName)) {
return null;
}
if (element.Name == Iso19770_2.Elements.Payload || element.Name == Iso19770_2.Elements.Evidence || element.Name == Iso19770_2.Elements.Directory) {
var file = new ResourceCollection(element).AddFile(fileName);
if (!string.IsNullOrWhiteSpace(location)) {
file.Location = location;
}
if (!string.IsNullOrWhiteSpace(root)) {
file.Root = root;
}
if (isKey) {
file.IsKey = true;
}
if (size > 0) {
file.Size = size;
}
if (!string.IsNullOrWhiteSpace(version)) {
file.Version = version;
}
return file.ElementUniqueId;
}
return null;
}
internal string AddDirectory(string elementPath, string directoryName, string location, string root, bool isKey) {
var element = FindElementWithUniqueId(elementPath);
if (element == null || string.IsNullOrWhiteSpace(directoryName)) {
return null;
}
if (element.Name == Iso19770_2.Elements.Payload || element.Name == Iso19770_2.Elements.Evidence || element.Name == Iso19770_2.Elements.Directory) {
var file = new ResourceCollection(element).AddDirectory(directoryName);
if (!string.IsNullOrWhiteSpace(location)) {
file.Location = location;
}
if (!string.IsNullOrWhiteSpace(root)) {
file.Root = root;
}
if (isKey) {
file.IsKey = true;
}
return file.ElementUniqueId;
}
return null;
}
internal Evidence AddEvidence(DateTime date, string deviceId) {
var evidence = AddEvidence();
evidence.Date = date;
evidence.DeviceId = deviceId;
return evidence;
}
public string AddDependency(string providerName, string packageName, string version, string source, string appliesTo) {
return AddLink(new Uri(CreateCanonicalId(providerName, packageName, version, source)), Iso19770_2.Relationship.Requires, null, null, null, appliesTo, null);
}
}
}