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 System.Threading;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
36 namespace Microsoft.Build.Tasks {
37 public class Copy : TaskExtension {
39 ITaskItem[] copiedFiles;
40 ITaskItem[] destinationFiles;
41 ITaskItem destinationFolder;
42 bool skipUnchangedFiles;
43 ITaskItem[] sourceFiles;
44 bool overwriteReadOnlyFiles;
46 int retryDelayMilliseconds;
47 bool useHardlinksIfPossible;
53 public override bool Execute ()
55 if (sourceFiles.Length == 0)
60 List <ITaskItem> temporaryCopiedFiles = new List <ITaskItem> ();
62 if (sourceFiles != null && destinationFiles != null &&
63 sourceFiles.Length != destinationFiles.Length) {
64 Log.LogError ("Number of source files is different than number of destination files.");
68 if (destinationFiles != null && destinationFolder != null) {
69 Log.LogError ("You must specify only one attribute from DestinationFiles and DestinationFolder");
73 if (destinationFiles != null && destinationFiles.Length > 0) {
74 for (int i = 0; i < sourceFiles.Length; i ++) {
75 ITaskItem sourceItem = sourceFiles [i];
76 ITaskItem destinationItem = destinationFiles [i];
77 string sourceFile = sourceItem.GetMetadata ("FullPath");
78 string destinationFile = destinationItem.GetMetadata ("FullPath");
80 if (!File.Exists (sourceFile)) {
81 Log.LogError ("Cannot copy {0} to {1}, as the source file doesn't exist.", sourceFile, destinationFile);
85 if (!skipUnchangedFiles || HasFileChanged (sourceFile, destinationFile))
86 CopyFileWithRetries (sourceFile, destinationFile, true);
88 sourceItem.CopyMetadataTo (destinationItem);
89 temporaryCopiedFiles.Add (destinationItem);
92 } else if (destinationFolder != null) {
93 List<ITaskItem> temporaryDestinationFiles = new List<ITaskItem> ();
94 string destinationDirectory = destinationFolder.GetMetadata ("FullPath");
95 bool directoryCreated = CreateDirectoryIfRequired (destinationDirectory);
97 foreach (ITaskItem sourceItem in sourceFiles) {
98 string sourceFile = sourceItem.GetMetadata ("FullPath");
99 string filename = sourceItem.GetMetadata ("Filename") + sourceItem.GetMetadata ("Extension");
100 string destinationFile = Path.Combine (destinationDirectory,filename);
102 if (!File.Exists (sourceFile)) {
103 Log.LogError ("Cannot copy {0} to {1}, as the source file doesn't exist.", sourceFile, destinationFile);
107 if (!skipUnchangedFiles || directoryCreated ||
108 HasFileChanged (sourceFile, destinationFile))
109 CopyFileWithRetries (sourceFile, destinationFile, false);
111 temporaryCopiedFiles.Add (new TaskItem (
112 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
113 sourceItem.CloneCustomMetadata ()));
115 temporaryDestinationFiles.Add (new TaskItem (
116 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
117 sourceItem.CloneCustomMetadata ()));
119 destinationFiles = temporaryDestinationFiles.ToArray ();
121 Log.LogError ("You must specify DestinationFolder or DestinationFiles attribute.");
125 copiedFiles = temporaryCopiedFiles.ToArray ();
127 return !Log.HasLoggedErrors;
129 catch (Exception ex) {
130 Log.LogErrorFromException (ex);
136 public ITaskItem[] CopiedFiles {
143 public ITaskItem[] DestinationFiles {
145 return destinationFiles;
148 destinationFiles = value;
152 public ITaskItem DestinationFolder {
154 return destinationFolder;
157 destinationFolder = value;
161 public bool SkipUnchangedFiles {
163 return skipUnchangedFiles;
166 skipUnchangedFiles = value;
170 public bool OverwriteReadOnlyFiles {
172 return overwriteReadOnlyFiles;
175 overwriteReadOnlyFiles = value;
188 public int RetryDelayMilliseconds {
190 return retryDelayMilliseconds;
193 retryDelayMilliseconds = value;
197 [MonoTODO ("Not implemented yet.")]
198 public bool UseHardlinksIfPossible {
200 return useHardlinksIfPossible;
203 useHardlinksIfPossible = value;
208 public ITaskItem[] SourceFiles {
217 // returns whether directory was created or not
218 bool CreateDirectoryIfRequired (string name)
220 if (Directory.Exists (name))
223 Log.LogMessage ("Creating directory '{0}'", name);
224 Directory.CreateDirectory (name);
228 void CopyFileWithRetries (string source, string dest, bool create_dir)
230 for (int i = retries; i >= 0; i--) {
232 CopyFile (source, dest, create_dir);
234 catch (Exception ex) {
235 Log.LogMessage ("Copying failed. Retries left: {0}.", i);
240 Thread.Sleep (retryDelayMilliseconds);
245 void CopyFile (string source, string dest, bool create_dir)
248 CreateDirectoryIfRequired (Path.GetDirectoryName (dest));
249 if (overwriteReadOnlyFiles)
250 ClearReadOnlyAttribute (dest);
251 Log.LogMessage ("Copying file from '{0}' to '{1}'", source, dest);
252 if (String.Compare (source, dest) != 0) {
253 // Ensure that we delete the destination file first so that if the file is already
254 // opened via mmap we do not screw up the data for the process which has the file open
255 // Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=9146
256 if (!HasReadOnlyAttribute (dest))
258 File.Copy (source, dest, true);
260 ClearReadOnlyAttribute (dest);
263 void ClearReadOnlyAttribute (string name)
265 if (File.Exists (name) && ((File.GetAttributes (name) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly))
266 File.SetAttributes (name, FileAttributes.Normal);
269 bool HasReadOnlyAttribute (string name)
271 return File.Exists (name) && (File.GetAttributes (name) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
274 bool HasFileChanged (string source, string dest)
276 if (!File.Exists (dest))
279 FileInfo sourceInfo = new FileInfo (source);
280 FileInfo destinationInfo = new FileInfo (dest);
282 return !(sourceInfo.Length == destinationInfo.Length &&
283 File.GetLastWriteTime (source) <= File.GetLastWriteTime (dest));