[xbuild] Add 'OverrideReadOnlyFiles' property to Copy task.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Copy.cs
index 81113ef1999b4a417fcff1d81504d3bcb8ae606e..8c28c2ae27b5a9c8a8dcfec93da8975974c9b151 100644 (file)
@@ -42,6 +42,7 @@ namespace Microsoft.Build.Tasks {
                ITaskItem       destinationFolder;
                bool            skipUnchangedFiles;
                ITaskItem[]     sourceFiles;
+               bool            overwriteReadOnlyFiles;
                
                public Copy ()
                {
@@ -57,10 +58,16 @@ namespace Microsoft.Build.Tasks {
                                List <ITaskItem> temporaryCopiedFiles = new List <ITaskItem> ();
                        
                                if (sourceFiles != null && destinationFiles != null &&
-                                       sourceFiles.Length != destinationFiles.Length)
-                                       throw new Exception ("Number of source files is different than number of destination files.");
-                               if (destinationFiles != null && destinationFolder != null)
-                                       throw new Exception ("You must specify only one attribute from DestinationFiles and DestinationFolder");
+                                       sourceFiles.Length != destinationFiles.Length) {
+                                       Log.LogError ("Number of source files is different than number of destination files.");
+                                       return false;
+                               }
+
+                               if (destinationFiles != null && destinationFolder != null) {
+                                       Log.LogError ("You must specify only one attribute from DestinationFiles and DestinationFolder");
+                                       return false;
+                               }
+
                                if (destinationFiles != null && destinationFiles.Length > 0) {
                                        for (int i = 0; i < sourceFiles.Length; i ++) {
                                                ITaskItem sourceItem = sourceFiles [i];
@@ -109,7 +116,8 @@ namespace Microsoft.Build.Tasks {
                                        }
                                        destinationFiles = temporaryDestinationFiles.ToArray ();
                                } else {
-                                       throw new Exception ("You must specify DestinationFolder or DestinationFiles attribute.");
+                                       Log.LogError ("You must specify DestinationFolder or DestinationFiles attribute.");
+                                       return false;
                                }
                                
                                copiedFiles = temporaryCopiedFiles.ToArray ();
@@ -157,6 +165,17 @@ namespace Microsoft.Build.Tasks {
                        }
                }
 
+#if NET_3_5 || NET_4_0
+               public bool OverwriteReadOnlyFiles {
+                       get {
+                               return overwriteReadOnlyFiles;
+                       }
+                       set {
+                               overwriteReadOnlyFiles = value;
+                       }
+               }
+#endif
+
                [Required]
                public ITaskItem[] SourceFiles {
                        get {
@@ -182,9 +201,18 @@ namespace Microsoft.Build.Tasks {
                {
                        if (create_dir)
                                CreateDirectoryIfRequired (Path.GetDirectoryName (dest));
+                       if (overwriteReadOnlyFiles)
+                               ClearReadOnlyAttribute (dest);
                        Log.LogMessage ("Copying file from '{0}' to '{1}'", source, dest);
                        if (String.Compare (source, dest) != 0)
                                File.Copy (source, dest, true);
+                       ClearReadOnlyAttribute (dest);
+               }
+
+               void ClearReadOnlyAttribute (string name)
+               {
+                       if (File.Exists (name) && ((File.GetAttributes (name) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly))
+                               File.SetAttributes (name, FileAttributes.Normal);
                }
 
                bool HasFileChanged (string source, string dest)
@@ -196,7 +224,7 @@ namespace Microsoft.Build.Tasks {
                        FileInfo destinationInfo = new FileInfo (dest);
 
                        return !(sourceInfo.Length == destinationInfo.Length &&
-                                       File.GetLastWriteTime(source) <= File.GetLastWriteTime (dest));
+                                       File.GetLastWriteTime (source) <= File.GetLastWriteTime (dest));
                }
 
        }