diff options
| author | Rob Mensching <rob@firegiant.com> | 2018-07-26 23:52:12 -0700 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2018-07-27 11:31:18 -0700 |
| commit | 854e616eb3516c7405691b679617aa08c1dd1cdd (patch) | |
| tree | ce7694749bf88bc796825b84fbc4f9d8acae331d /src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs | |
| parent | c64faa7f6cb789a0756dff725146728889df8311 (diff) | |
| download | wix-854e616eb3516c7405691b679617aa08c1dd1cdd.tar.gz wix-854e616eb3516c7405691b679617aa08c1dd1cdd.tar.bz2 wix-854e616eb3516c7405691b679617aa08c1dd1cdd.zip | |
Support change of FileTransfer to interface
Diffstat (limited to 'src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs')
| -rw-r--r-- | src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs b/src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs new file mode 100644 index 00000000..0e5c4b5b --- /dev/null +++ b/src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs | |||
| @@ -0,0 +1,70 @@ | |||
| 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 BackendHelper : IBackendHelper | ||
| 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 BackendHelper(IServiceProvider serviceProvider) | ||
| 16 | { | ||
| 17 | this.Messaging = serviceProvider.GetService<IMessaging>(); | ||
| 18 | } | ||
| 19 | |||
| 20 | private IMessaging Messaging { get; } | ||
| 21 | |||
| 22 | public IFileTransfer CreateFileTransfer(string source, string destination, bool move, FileTransferType type, SourceLineNumber sourceLineNumbers) | ||
| 23 | { | ||
| 24 | var sourceFullPath = GetValidatedFullPath(sourceLineNumbers, source); | ||
| 25 | |||
| 26 | var destinationFullPath = 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 | Type = type, | ||
| 34 | SourceLineNumbers = sourceLineNumbers, | ||
| 35 | Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase) | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | |||
| 39 | private string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path) | ||
| 40 | { | ||
| 41 | try | ||
| 42 | { | ||
| 43 | var result = Path.GetFullPath(path); | ||
| 44 | |||
| 45 | var filename = Path.GetFileName(result); | ||
| 46 | |||
| 47 | foreach (var reservedName in ReservedFileNames) | ||
| 48 | { | ||
| 49 | if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase)) | ||
| 50 | { | ||
| 51 | this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); | ||
| 52 | return null; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return result; | ||
| 57 | } | ||
| 58 | catch (ArgumentException) | ||
| 59 | { | ||
| 60 | this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); | ||
| 61 | } | ||
| 62 | catch (PathTooLongException) | ||
| 63 | { | ||
| 64 | this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path)); | ||
| 65 | } | ||
| 66 | |||
| 67 | return null; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
