From 854e616eb3516c7405691b679617aa08c1dd1cdd Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 26 Jul 2018 23:52:12 -0700 Subject: Support change of FileTransfer to interface --- src/WixToolset.Core/Bind/TransferFilesCommand.cs | 4 +- .../ExtensibilityServices/BackendHelper.cs | 70 ++++++++++++++++++++++ src/WixToolset.Core/FileTransfer.cs | 22 +++++++ src/WixToolset.Core/Layout.cs | 12 ++-- src/WixToolset.Core/LayoutContext.cs | 2 +- src/WixToolset.Core/WixToolsetServiceProvider.cs | 1 + 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 src/WixToolset.Core/ExtensibilityServices/BackendHelper.cs create mode 100644 src/WixToolset.Core/FileTransfer.cs (limited to 'src/WixToolset.Core') diff --git a/src/WixToolset.Core/Bind/TransferFilesCommand.cs b/src/WixToolset.Core/Bind/TransferFilesCommand.cs index 79f0cd81..b89d3d03 100644 --- a/src/WixToolset.Core/Bind/TransferFilesCommand.cs +++ b/src/WixToolset.Core/Bind/TransferFilesCommand.cs @@ -13,7 +13,7 @@ namespace WixToolset.Core.Bind internal class TransferFilesCommand { - public TransferFilesCommand(IMessaging messaging, IEnumerable extensions, IEnumerable fileTransfers, bool suppressAclReset) + public TransferFilesCommand(IMessaging messaging, IEnumerable extensions, IEnumerable fileTransfers, bool suppressAclReset) { this.FileSystem = new FileSystem(extensions); this.Messaging = messaging; @@ -25,7 +25,7 @@ namespace WixToolset.Core.Bind private IMessaging Messaging { get; } - private IEnumerable FileTransfers { get; } + private IEnumerable FileTransfers { get; } private bool SuppressAclReset { get; } 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 @@ +// 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. + +namespace WixToolset.Core.ExtensibilityServices +{ + using System; + using System.IO; + using WixToolset.Data; + using WixToolset.Extensibility.Data; + using WixToolset.Extensibility.Services; + + internal class BackendHelper : IBackendHelper + { + 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" }; + + public BackendHelper(IServiceProvider serviceProvider) + { + this.Messaging = serviceProvider.GetService(); + } + + private IMessaging Messaging { get; } + + public IFileTransfer CreateFileTransfer(string source, string destination, bool move, FileTransferType type, SourceLineNumber sourceLineNumbers) + { + var sourceFullPath = GetValidatedFullPath(sourceLineNumbers, source); + + var destinationFullPath = GetValidatedFullPath(sourceLineNumbers, destination); + + return (String.IsNullOrEmpty(sourceFullPath) || String.IsNullOrEmpty(destinationFullPath)) ? null : new FileTransfer + { + Source = sourceFullPath, + Destination = destinationFullPath, + Move = move, + Type = type, + SourceLineNumbers = sourceLineNumbers, + Redundant = String.Equals(sourceFullPath, destinationFullPath, StringComparison.OrdinalIgnoreCase) + }; + } + + private string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path) + { + try + { + var result = Path.GetFullPath(path); + + var filename = Path.GetFileName(result); + + foreach (var reservedName in ReservedFileNames) + { + if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase)) + { + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); + return null; + } + } + + return result; + } + catch (ArgumentException) + { + this.Messaging.Write(ErrorMessages.InvalidFileName(sourceLineNumbers, path)); + } + catch (PathTooLongException) + { + this.Messaging.Write(ErrorMessages.PathTooLong(sourceLineNumbers, path)); + } + + return null; + } + } +} diff --git a/src/WixToolset.Core/FileTransfer.cs b/src/WixToolset.Core/FileTransfer.cs new file mode 100644 index 00000000..9da406eb --- /dev/null +++ b/src/WixToolset.Core/FileTransfer.cs @@ -0,0 +1,22 @@ +// 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. + +namespace WixToolset.Core +{ + using WixToolset.Data; + using WixToolset.Extensibility.Data; + + internal class FileTransfer : IFileTransfer + { + public string Source { get; set; } + + public string Destination { get; set; } + + public bool Move { get; set; } + + public SourceLineNumber SourceLineNumbers { get; set; } + + public FileTransferType Type { get; set; } + + public bool Redundant { get; set; } + } +} diff --git a/src/WixToolset.Core/Layout.cs b/src/WixToolset.Core/Layout.cs index a5e885e5..28c9d5fc 100644 --- a/src/WixToolset.Core/Layout.cs +++ b/src/WixToolset.Core/Layout.cs @@ -28,7 +28,7 @@ namespace WixToolset.Core private IMessaging Messaging { get; } - public IEnumerable FileTransfers { get; set; } + public IEnumerable FileTransfers { get; set; } public IEnumerable ContentFilePaths { get; set; } @@ -124,14 +124,14 @@ namespace WixToolset.Core /// /// Path to write file. /// Collection of files that were transferred to the output directory. - private void CreateOutputsFile(string path, IEnumerable fileTransfers) + private void CreateOutputsFile(string path, IEnumerable fileTransfers) { var directory = Path.GetDirectoryName(path); Directory.CreateDirectory(directory); using (var outputs = new StreamWriter(path, false)) { - foreach (FileTransfer fileTransfer in fileTransfers) + foreach (var fileTransfer in fileTransfers) { // Don't list files where the source is the same as the destination since // that might be the only place the file exists. The outputs file is often @@ -149,18 +149,18 @@ namespace WixToolset.Core /// /// Path to write file. /// Collection of files that were transferred to the output directory. - private void CreateBuiltOutputsFile(string path, IEnumerable fileTransfers) + private void CreateBuiltOutputsFile(string path, IEnumerable fileTransfers) { var directory = Path.GetDirectoryName(path); Directory.CreateDirectory(directory); using (var outputs = new StreamWriter(path, false)) { - foreach (FileTransfer fileTransfer in fileTransfers) + foreach (var fileTransfer in fileTransfers) { // Only write the built file transfers. Also, skip redundant // files for the same reason spelled out in this.CreateOutputsFile(). - if (fileTransfer.Built && !fileTransfer.Redundant) + if (fileTransfer.Type == FileTransferType.Built && !fileTransfer.Redundant) { outputs.WriteLine(fileTransfer.Destination); } diff --git a/src/WixToolset.Core/LayoutContext.cs b/src/WixToolset.Core/LayoutContext.cs index af0df518..172cea65 100644 --- a/src/WixToolset.Core/LayoutContext.cs +++ b/src/WixToolset.Core/LayoutContext.cs @@ -20,7 +20,7 @@ namespace WixToolset.Core public IEnumerable FileSystemExtensions { get; set; } - public IEnumerable FileTransfers { get; set; } + public IEnumerable FileTransfers { get; set; } public IEnumerable ContentFilePaths { get; set; } diff --git a/src/WixToolset.Core/WixToolsetServiceProvider.cs b/src/WixToolset.Core/WixToolsetServiceProvider.cs index dd6da8c8..ffcdbdd1 100644 --- a/src/WixToolset.Core/WixToolsetServiceProvider.cs +++ b/src/WixToolset.Core/WixToolsetServiceProvider.cs @@ -22,6 +22,7 @@ namespace WixToolset.Core { typeof(ITupleDefinitionCreator), (provider, singletons) => AddSingleton(singletons, typeof(ITupleDefinitionCreator), new TupleDefinitionCreator(provider)) }, { typeof(IParseHelper), (provider, singletons) => AddSingleton(singletons, typeof(IParseHelper), new ParseHelper(provider)) }, { typeof(IPreprocessHelper), (provider, singletons) => AddSingleton(singletons, typeof(IPreprocessHelper), new PreprocessHelper(provider)) }, + { typeof(IBackendHelper), (provider, singletons) => AddSingleton(singletons, typeof(IBackendHelper), new BackendHelper(provider)) }, { typeof(IWindowsInstallerBackendHelper), (provider, singletons) => AddSingleton(singletons, typeof(IWindowsInstallerBackendHelper), new WindowsInstallerBackendHelper(provider)) }, // Transients. -- cgit v1.2.3-55-g6feb