소스 검색

[corlib] FileInfo from referencesource

Marek Safar 9 년 전
부모
커밋
bdd0de7ee5

+ 1 - 1
external/referencesource

@@ -1 +1 @@
-Subproject commit 11356fac43cda5d0db273febfe9a243c421d81e8
+Subproject commit 6b3dd0b5a0f5ebd75d4be8070f711805c0f948f1

+ 4 - 0
mcs/class/corlib/Makefile

@@ -37,6 +37,10 @@ RESOURCE_FILES = \
 
 REFERENCE_SOURCES_FLAGS = -d:FEATURE_PAL,GENERICS_WORK,FEATURE_LIST_PREDICATES,FEATURE_SERIALIZATION,FEATURE_ASCII,FEATURE_LATIN1,FEATURE_UTF7,FEATURE_UTF32,MONO_HYBRID_ENCODING_SUPPORT,FEATURE_ASYNC_IO,NEW_EXPERIMENTAL_ASYNC_IO,FEATURE_UTF32,FEATURE_EXCEPTIONDISPATCHINFO,FEATURE_CORRUPTING_EXCEPTIONS,FEATURE_EXCEPTION_NOTIFICATIONS,FEATURE_STRONGNAME_MIGRATION,FEATURE_USE_LCID,FEATURE_FUSION,FEATURE_CRYPTO,FEATURE_X509_SECURESTRINGS,FEATURE_SYNCHRONIZATIONCONTEXT,FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
 
+ifndef MOBILE_PROFILE
+REFERENCE_SOURCES_FLAGS += -d:FEATURE_MACL
+endif
+
 ifndef MOBILE_STATIC
 REFERENCE_SOURCES_FLAGS += -d:FEATURE_REMOTING,MONO_COM,FEATURE_COMINTEROP,FEATURE_ROLE_BASED_SECURITY
 endif

+ 4 - 0
mcs/class/corlib/ReferenceSources/win32native.cs

@@ -1,3 +1,5 @@
+using System.IO;
+
 namespace Microsoft.Win32
 {
 	static class Win32Native
@@ -53,6 +55,8 @@ namespace Microsoft.Win32
 		internal const int ERROR_NO_SECURITY_ON_OBJECT = 0x546;
 		internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6FD;
 
+		internal const FileAttributes FILE_ATTRIBUTE_DIRECTORY = FileAttributes.Directory;
+
 		public static string GetMessage (int hr)
 		{
 			return "Error " + hr;

+ 22 - 0
mcs/class/corlib/System.IO/File.cs

@@ -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);
@@ -406,6 +421,12 @@ 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);
@@ -699,6 +720,7 @@ namespace System.IO
 
 			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;
 			}
 

+ 0 - 349
mcs/class/corlib/System.IO/FileInfo.cs

@@ -1,349 +0,0 @@
-//------------------------------------------------------------------------------
-// 
-// System.IO.FileInfo.cs 
-//
-// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
-// 
-// Author:         Jim Richardson, [email protected]
-//                 Dan Lewis ([email protected])
-// Created:        Monday, August 13, 2001 
-//
-//------------------------------------------------------------------------------
-
-//
-// Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-// 
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-// 
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System.Runtime.InteropServices;
-using System.Runtime.Serialization;
-using System.Security;
-
-#if !MOBILE
-using System.Security.AccessControl;
-#endif
-
-namespace System.IO {
-
-	[Serializable]
-	[ComVisible (true)]
-	public sealed class FileInfo : FileSystemInfo
-	{
-		private bool exists;
-		private string displayPath;
-
-		public FileInfo (string fileName)
-		{
-			if (fileName == null)
-				throw new ArgumentNullException ("fileName");
-
-			CheckPath (fileName);
-			SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
-
-			OriginalPath = fileName;
-			FullPath = Path.GetFullPath (fileName);
-			
-			displayPath = OriginalPath;
-		}
-
-		private FileInfo (SerializationInfo info, StreamingContext context)
-			: base (info, context)
-		{
-			displayPath = OriginalPath;
-		}
-
-		public override bool Exists {
-			get {
-				if (_dataInitialised == -1) {
-					Refresh ();
-					if (_dataInitialised == 0)
-						exists = File.Exists (FullPath);
-				}
-
-				if (_data.fileAttributes == MonoIO.InvalidFileAttributes)
-					return false;
-
-				if ((_data.fileAttributes & FileAttributes.Directory) != 0)
-					return false;
-
-				return exists;
-			}
-		}
-
-		public override string Name {
-			get {
-				return Path.GetFileName (FullPath);
-			}
-		}
-
-		public bool IsReadOnly {
-			get {
-				if (!Exists)
-					throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
-					
-				return ((_data.fileAttributes & FileAttributes.ReadOnly) != 0);
-			}
-				
-			set {
-				if (!Exists)
-					throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
-					
-				FileAttributes attrs = File.GetAttributes(FullPath);
-
-				if (value) 
-					attrs |= FileAttributes.ReadOnly;
-				else
-					attrs &= ~FileAttributes.ReadOnly;
-
-				File.SetAttributes(FullPath, attrs);
-			}
-		}
-
-		[MonoLimitation ("File encryption isn't supported (even on NTFS).")]
-		[ComVisible (false)]
-		public void Encrypt ()
-		{
-			// MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
-			// otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
-			// we throw the same (instead of a NotImplementedException) because most code should already be
-			// handling this exception to work properly.
-			throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
-		}
-
-		[MonoLimitation ("File encryption isn't supported (even on NTFS).")]
-		[ComVisible (false)]
-		public void Decrypt ()
-		{
-			// MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
-			// otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
-			// we throw the same (instead of a NotImplementedException) because most code should already be
-			// handling this exception to work properly.
-			throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
-		}
-
-		public long Length {
-			get {
-				if (!Exists)
-					throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
-
-				return _data.Length;
-			}
-		}
-
-		public string DirectoryName {
-			get {
-				return Path.GetDirectoryName (FullPath);
-			}
-		}
-
-		public DirectoryInfo Directory {
-			get {
-				return new DirectoryInfo (DirectoryName);
-			}
-		}
-
-		// streamreader methods
-
-		public StreamReader OpenText ()
-		{
-			return new StreamReader (Open (FileMode.Open, FileAccess.Read));
-		}
-
-		public StreamWriter CreateText ()
-		{
-			return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
-		}
-		
-		public StreamWriter AppendText ()
-		{
-			return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
-		}
-
-		// filestream methods
-
-		public FileStream Create ()
-		{
-			return File.Create (FullPath);
-		}
-
-		public FileStream OpenRead ()
-		{
-			return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
-		}
-
-		public FileStream OpenWrite ()
-		{
-			return Open (FileMode.OpenOrCreate, FileAccess.Write);
-		}
-
-		public FileStream Open (FileMode mode)
-		{
-			return Open (mode, FileAccess.ReadWrite);
-		}
-
-		public FileStream Open (FileMode mode, FileAccess access)
-		{
-			return Open (mode, access, FileShare.None);
-		}
-
-		public FileStream Open (FileMode mode, FileAccess access, FileShare share)
-		{
-			return new FileStream (FullPath, mode, access, share);
-		}
-
-		// file methods
-
-		public override void Delete ()
-		{
-			MonoIOError error;
-
-			SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
-
-			if (!MonoIO.Exists (FullPath, out error))
-				// a weird MS.NET behaviour
-				return;
-
-			if (MonoIO.ExistsDirectory (FullPath, out error))
-				throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
-			if (!MonoIO.DeleteFile (FullPath, out error))
-				throw MonoIO.GetException (OriginalPath, error);
-		}
-
-		public void MoveTo (string destFileName)
-		{
-			if (destFileName == null)
-				throw new ArgumentNullException ("destFileName");
-			if (destFileName.Length == 0)
-				throw new ArgumentException ("An empty file name is not valid.", "destFileName");
-
-			var destFullPath = Path.GetFullPath (destFileName);
-			if (destFullPath == FullPath)
-				return;
-			if (!File.Exists (FullPath))
-				throw new FileNotFoundException ();
-
-			File.Move (FullPath, destFullPath);
-			this.FullPath = destFullPath;
-
-			displayPath = destFileName;
-		}
-
-		public FileInfo CopyTo (string destFileName)
-		{
-			return CopyTo (destFileName, false);
-		}
-
-		public FileInfo CopyTo (string destFileName, bool overwrite)
-		{
-			if (destFileName == null)
-				throw new ArgumentNullException ("destFileName");
-			if (destFileName.Length == 0)
-				throw new ArgumentException ("An empty file name is not valid.", "destFileName");
-
-			string dest = Path.GetFullPath (destFileName);
-
-			if (overwrite && File.Exists (dest))
-				File.Delete (dest);
-
-			File.Copy (FullPath, dest);
-		
-			return new FileInfo (dest);
-		}
-
-		public override string ToString ()
-		{
-			return displayPath;
-		}
-
-		void CheckPath (string path)
-		{
-			if (path == null)
-				throw new ArgumentNullException ("path");
-			if (path.Length == 0)
-				throw new ArgumentException ("An empty file name is not valid.");
-			if (path.IndexOfAny (Path.InvalidPathChars) != -1)
-				throw new ArgumentException ("Illegal characters in path.");
-			if (Environment.IsRunningOnWindows) {
-				int idx = path.IndexOf (':');
-				if (idx >= 0 && idx != 1)
-					throw new ArgumentException ("path");
-			}
-		}
-
-#if !MOBILE
-		public FileSecurity GetAccessControl ()
-		{
-			return File.GetAccessControl (FullPath); 
-		}
-		
-		public FileSecurity GetAccessControl (AccessControlSections includeSections)
-		{
-			return File.GetAccessControl (FullPath, includeSections);
-		}
-
-		[ComVisible (false)]
-		public FileInfo Replace (string destinationFileName,
-					 string destinationBackupFileName)
-		{
-			string destinationFullPath = null;
-			if (!Exists)
-                		throw new FileNotFoundException ();
-			if (destinationFileName == null)
-                		throw new ArgumentNullException ("destinationFileName");
-            		if (destinationFileName.Length == 0)
-                		throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
-
-			destinationFullPath = Path.GetFullPath (destinationFileName);
-
-			if (!File.Exists (destinationFullPath))
-				throw new FileNotFoundException ();
-
-			FileAttributes attrs = File.GetAttributes (destinationFullPath);
-
-			if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
-					throw new UnauthorizedAccessException (); 
-
-            		if (destinationBackupFileName != null) {
-                		if (destinationBackupFileName.Length == 0)
-                    			throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
-                		File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
-            		}
-            		File.Copy (FullPath, destinationFullPath,true);
-            		File.Delete (FullPath);
-			return new FileInfo (destinationFullPath);
-		}
-		
-		[ComVisible (false)]
-		[MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
-		public FileInfo Replace (string destinationFileName,
-					 string destinationBackupFileName,
-					 bool ignoreMetadataErrors)
-		{
-			return Replace (destinationFileName, destinationBackupFileName);
-		}
-
-		public void SetAccessControl (FileSecurity fileSecurity)
-		{
-			File.SetAccessControl (FullPath, fileSecurity);
-		}
-#endif
-	}
-}

+ 2 - 2
mcs/class/corlib/Test/System.IO/FileInfoTest.cs

@@ -755,9 +755,9 @@ namespace MonoTests.System.IO
 				try {
 					info.MoveTo (destFile);
 					Assert.Fail ("#1");
-				} catch (DirectoryNotFoundException ex) {
+				} catch (FileNotFoundException ex) {
 					// Could not find a part of the path
-					Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
+					Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
 					Assert.IsNull (ex.InnerException, "#3");
 					Assert.IsNotNull (ex.Message, "#4");
 				}

+ 1 - 1
mcs/class/corlib/corlib.dll.sources

@@ -195,7 +195,6 @@ System.IO/DriveType.cs
 System.IO/File.cs
 System.IO/FileAccess.cs
 System.IO/FileAttributes.cs
-System.IO/FileInfo.cs
 System.IO/FileMode.cs
 System.IO/FileOptions.cs
 System.IO/FileShare.cs
@@ -1196,6 +1195,7 @@ ReferenceSources/SecurityContext.cs
 ../../../external/referencesource/mscorlib/system/io/drivenotfoundexception.cs
 ../../../external/referencesource/mscorlib/system/io/endofstreamexception.cs
 ../../../external/referencesource/mscorlib/system/io/ioexception.cs
+../../../external/referencesource/mscorlib/system/io/fileinfo.cs
 ../../../external/referencesource/mscorlib/system/io/fileloadexception.cs
 ../../../external/referencesource/mscorlib/system/io/filenotfoundexception.cs
 ../../../external/referencesource/mscorlib/system/io/filesysteminfo.cs