diff options
Diffstat (limited to 'src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs')
-rw-r--r-- | src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs b/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs new file mode 100644 index 0000000..af2d1d0 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/HardLinkCopy.cs @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------- +// <copyright file="HardLinkCopy.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + using System.IO; + + public class HardLinkCopy : Task { + [Required] + public ITaskItem[] SourceFiles { get; set; } + + [Required] + public ITaskItem[] DestinationFiles { get; set; } + + /// <summary> + /// Executes this instance. + /// </summary> + public override bool Execute() { + if (this.SourceFiles.Length != this.DestinationFiles.Length) { + this.Log.LogError("SourceFiles has {0} elements and DestinationFiles has {1} elements.", this.SourceFiles.Length, this.DestinationFiles.Length); + return false; + } + + for (int i = 0; i < this.SourceFiles.Length; i++) { + bool hardLink; + bool.TryParse(this.DestinationFiles[i].GetMetadata("HardLink"), out hardLink); + string sourceFile = this.SourceFiles[i].ItemSpec; + string destinationFile = this.DestinationFiles[i].ItemSpec; + this.Log.LogMessage( + MessageImportance.Low, + "Copying {0} -> {1}{2}.", + sourceFile, + destinationFile, + hardLink ? " as hard link" : string.Empty); + + if (!Directory.Exists(Path.GetDirectoryName(destinationFile))) { + Directory.CreateDirectory(Path.GetDirectoryName(destinationFile)); + } + + if (hardLink) { + if (File.Exists(destinationFile)) { + File.Delete(destinationFile); + } + NativeMethods.CreateHardLink(sourceFile, destinationFile); + } else { + File.Copy(sourceFile, destinationFile, true); + } + } + + return !this.Log.HasLoggedErrors; + } + } +} |