Merge pull request #3431 from mhutch/fix-targetframeworkattribute
[mono.git] / mcs / class / corlib / System.IO / File.cs
index 6781c451194ee630c553778bc46cb74267e0df3c..de55602cdbe4437a6def908340a01e5156e25b94 100644 (file)
@@ -39,7 +39,7 @@ using System.Security;
 using System.Text;
 using System.Runtime.InteropServices;
 
-#if !NET_2_1
+#if !MOBILE
 using System.Security.AccessControl;
 #endif
 
@@ -113,6 +113,21 @@ namespace System.IO
                        }
                }
 
+               internal static String InternalCopy (String sourceFileName, String destFileName, bool overwrite, bool checkHost)
+               {
+                       String fullSourceFileName = Path.GetFullPathInternal(sourceFileName);
+                       String fullDestFileName = Path.GetFullPathInternal(destFileName);
+
+                       MonoIOError error;
+
+                       if (!MonoIO.CopyFile (fullSourceFileName, fullDestFileName, overwrite, out error)) {
+                               string p = Locale.GetText ("{0}\" or \"{1}", sourceFileName, destFileName);
+                               throw MonoIO.GetException (p, error);
+                       }
+
+                       return fullDestFileName;
+               }
+
                public static FileStream Create (string path)
                {
                        return Create (path, 8192);
@@ -124,14 +139,15 @@ namespace System.IO
                                FileShare.None, bufferSize);
                }
 
-#if !NET_2_1
                [MonoLimitation ("FileOptions are ignored")]
                public static FileStream Create (string path, int bufferSize,
                                                 FileOptions options)
                {
-                       return Create (path, bufferSize, options, null);
+                       return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
+                               FileShare.None, bufferSize, options);
                }
-               
+
+#if !MOBILE
                [MonoLimitation ("FileOptions and FileSecurity are ignored")]
                public static FileStream Create (string path, int bufferSize,
                                                 FileOptions options,
@@ -185,7 +201,7 @@ namespace System.IO
                        return MonoIO.ExistsFile (path, out error);
                }
 
-#if !NET_2_1
+#if !MOBILE
                public static FileSecurity GetAccessControl (string path)
                {
                        // AccessControlSections.Audit requires special permissions.
@@ -406,13 +422,19 @@ namespace System.IO
                                                               "Destination and backup arguments are the same file."));
                        }
 
+                       var attrs = GetAttributes (fullDest);
+
+                       // TODO: Should be done in wapi, win32 api handles this already
+                       if ((attrs & FileAttributes.ReadOnly) != 0)
+                               throw MonoIO.GetException (MonoIOError.ERROR_ACCESS_DENIED);
+
                        if (!MonoIO.ReplaceFile (fullSource, fullDest, fullBackup, 
                                                 ignoreMetadataErrors, out error)) {
                                throw MonoIO.GetException (error);
                        }
                }
 
-#if !NET_2_1
+#if !MOBILE
                public static void SetAccessControl (string path,
                                                     FileSecurity fileSecurity)
                {
@@ -575,7 +597,7 @@ namespace System.IO
 
                public static void WriteAllText (string path, string contents)
                {
-                       WriteAllText (path, contents, Encoding.UTF8Unmarked);
+                       WriteAllText (path, contents, EncodingHelper.UTF8Unmarked);
                }
 
                public static void WriteAllText (string path, string contents, Encoding encoding)
@@ -616,7 +638,6 @@ namespace System.IO
                        throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
                }
 
-#if MOONLIGHT || NET_4_0 || MOBILE
                public static IEnumerable<string> ReadLines (string path)
                {
                        return ReadLines (File.OpenText (path));
@@ -689,6 +710,22 @@ namespace System.IO
                                        w.WriteLine (line);
                        }
                }
-#endif
+
+               internal static int FillAttributeInfo (String path, ref MonoIOStat data, bool tryagain, bool returnErrorOnNotFound)
+               {
+                       if (tryagain)
+                               throw new NotImplementedException ();
+
+                       MonoIOError error;
+                       MonoIO.GetFileStat (path, out data, out error);
+
+                       if (!returnErrorOnNotFound && (error == MonoIOError.ERROR_FILE_NOT_FOUND || error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_NOT_READY)) {
+                               data = default (MonoIOStat);
+                               data.fileAttributes = (FileAttributes) (-1);
+                               return 0;
+                       }
+
+                       return (int) error;
+               }
        }
 }