main
cs 74 lines 2.86 KB
Raw
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.Core.ExtensibilityServices
4 {
5 using System;
6 using System.IO;
7 using WixToolset.Data;
8 using WixToolset.Extensibility.Data;
9 using WixToolset.Extensibility.Services;
10
11 internal class LayoutServices : ILayoutServices
12 {
13 private static readonly string[] ReservedFileNames = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
14
15 public LayoutServices(IServiceProvider serviceProvider)
16 {
17 this.Messaging = serviceProvider.GetService<IMessaging>();
18 }
19
20 protected IMessaging Messaging { get; }
21
22 public IFileTransfer CreateFileTransfer(string source, string destination, bool move, SourceLineNumber sourceLineNumbers = null)
23 {
24 var sourceFullPath = this.GetValidatedFullPath(sourceLineNumbers, source);
25
26 var destinationFullPath = this.GetValidatedFullPath(sourceLineNumbers, destination);
27
28 return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer
29 {
30 Source = sourceFullPath,
31 Destination = destinationFullPath,
32 Move = move,
33 SourceLineNumbers = sourceLineNumbers,
34 Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase)
35 };
36 }
37
38 public ITrackedFile TrackFile(string path, TrackedFileType type, SourceLineNumber sourceLineNumbers = null)
39 {
40 return new TrackedFile(path, type, sourceLineNumbers);
41 }
42
43 protected string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path)
44 {
45 try
46 {
47 var result = Path.GetFullPath(path);
48
49 var filename = Path.GetFileName(result);
50
51 foreach (var reservedName in ReservedFileNames)
52 {
53 if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase))
54 {
55 this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
56 return null;
57 }
58 }
59
60 return result;
61 }
62 catch (ArgumentException)
63 {
64 this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path));
65 }
66 catch (PathTooLongException)
67 {
68 this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path));
69 }
70
71 return null;
72 }
73 }
74 }