2 // Copy.cs: Task that can copy files
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Generic;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
35 namespace Microsoft.Build.Tasks {
36 public class Copy : TaskExtension {
38 ITaskItem[] copiedFiles;
39 ITaskItem[] destinationFiles;
40 ITaskItem destinationFolder;
41 bool skipUnchangedFiles;
42 ITaskItem[] sourceFiles;
43 bool overwriteReadOnlyFiles;
49 public override bool Execute ()
51 if (sourceFiles.Length == 0)
56 List <ITaskItem> temporaryCopiedFiles = new List <ITaskItem> ();
58 if (sourceFiles != null && destinationFiles != null &&
59 sourceFiles.Length != destinationFiles.Length) {
60 Log.LogError ("Number of source files is different than number of destination files.");
64 if (destinationFiles != null && destinationFolder != null) {
65 Log.LogError ("You must specify only one attribute from DestinationFiles and DestinationFolder");
69 if (destinationFiles != null && destinationFiles.Length > 0) {
70 for (int i = 0; i < sourceFiles.Length; i ++) {
71 ITaskItem sourceItem = sourceFiles [i];
72 ITaskItem destinationItem = destinationFiles [i];
73 string sourceFile = sourceItem.GetMetadata ("FullPath");
74 string destinationFile = destinationItem.GetMetadata ("FullPath");
76 if (!File.Exists (sourceFile)) {
77 Log.LogError ("Cannot copy {0} to {1}, as the source file doesn't exist.", sourceFile, destinationFile);
81 if (!skipUnchangedFiles || HasFileChanged (sourceFile, destinationFile))
82 CopyFile (sourceFile, destinationFile, true);
84 sourceItem.CopyMetadataTo (destinationItem);
85 temporaryCopiedFiles.Add (destinationItem);
88 } else if (destinationFolder != null) {
89 List<ITaskItem> temporaryDestinationFiles = new List<ITaskItem> ();
90 string destinationDirectory = destinationFolder.GetMetadata ("FullPath");
91 bool directoryCreated = CreateDirectoryIfRequired (destinationDirectory);
93 foreach (ITaskItem sourceItem in sourceFiles) {
94 string sourceFile = sourceItem.GetMetadata ("FullPath");
95 string filename = sourceItem.GetMetadata ("Filename") + sourceItem.GetMetadata ("Extension");
96 string destinationFile = Path.Combine (destinationDirectory,filename);
98 if (!File.Exists (sourceFile)) {
99 Log.LogError ("Cannot copy {0} to {1}, as the source file doesn't exist.", sourceFile, destinationFile);
103 if (!skipUnchangedFiles || directoryCreated ||
104 HasFileChanged (sourceFile, destinationFile))
105 CopyFile (sourceFile, destinationFile, false);
107 temporaryCopiedFiles.Add (new TaskItem (
108 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
109 sourceItem.CloneCustomMetadata ()));
111 temporaryDestinationFiles.Add (new TaskItem (
112 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
113 sourceItem.CloneCustomMetadata ()));
115 destinationFiles = temporaryDestinationFiles.ToArray ();
117 Log.LogError ("You must specify DestinationFolder or DestinationFiles attribute.");
121 copiedFiles = temporaryCopiedFiles.ToArray ();
123 return !Log.HasLoggedErrors;
125 catch (Exception ex) {
126 Log.LogErrorFromException (ex);
132 public ITaskItem[] CopiedFiles {
139 public ITaskItem[] DestinationFiles {
141 return destinationFiles;
144 destinationFiles = value;
148 public ITaskItem DestinationFolder {
150 return destinationFolder;
153 destinationFolder = value;
157 public bool SkipUnchangedFiles {
159 return skipUnchangedFiles;
162 skipUnchangedFiles = value;
167 public bool OverwriteReadOnlyFiles {
169 return overwriteReadOnlyFiles;
172 overwriteReadOnlyFiles = value;
178 public ITaskItem[] SourceFiles {
187 // returns whether directory was created or not
188 bool CreateDirectoryIfRequired (string name)
190 if (Directory.Exists (name))
193 Log.LogMessage ("Creating directory '{0}'", name);
194 Directory.CreateDirectory (name);
198 void CopyFile (string source, string dest, bool create_dir)
201 CreateDirectoryIfRequired (Path.GetDirectoryName (dest));
202 if (overwriteReadOnlyFiles)
203 ClearReadOnlyAttribute (dest);
204 Log.LogMessage ("Copying file from '{0}' to '{1}'", source, dest);
205 if (String.Compare (source, dest) != 0) {
206 // Ensure that we delete the destination file first so that if the file is already
207 // opened via mmap we do not screw up the data for the process which has the file open
208 // Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=9146
209 if (!HasReadOnlyAttribute (dest))
211 File.Copy (source, dest, true);
213 ClearReadOnlyAttribute (dest);
216 void ClearReadOnlyAttribute (string name)
218 if (File.Exists (name) && ((File.GetAttributes (name) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly))
219 File.SetAttributes (name, FileAttributes.Normal);
222 bool HasReadOnlyAttribute (string name)
224 return File.Exists (name) && (File.GetAttributes (name) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
227 bool HasFileChanged (string source, string dest)
229 if (!File.Exists (dest))
232 FileInfo sourceInfo = new FileInfo (source);
233 FileInfo destinationInfo = new FileInfo (dest);
235 return !(sourceInfo.Length == destinationInfo.Length &&
236 File.GetLastWriteTime (source) <= File.GetLastWriteTime (dest));