فهرست منبع

2009-10-28 Sebastien Pouliot <[email protected]>

	* FileStream.cs: Move code to deal with 'anonymous' filenames
	into two methods (returning the path, fullpath or only the 
	filename). Default Moonlight to anonymous (not only for isolated
	storage) unless the coreclr is disable (e.g. smcs)


svn path=/trunk/mcs/; revision=144945
Sebastien Pouliot 16 سال پیش
والد
کامیت
01721e7fa2
2فایلهای تغییر یافته به همراه40 افزوده شده و 27 حذف شده
  1. 7 0
      mcs/class/corlib/System.IO/ChangeLog
  2. 33 27
      mcs/class/corlib/System.IO/FileStream.cs

+ 7 - 0
mcs/class/corlib/System.IO/ChangeLog

@@ -1,3 +1,10 @@
+2009-10-28  Sebastien Pouliot  <[email protected]>
+
+	* FileStream.cs: Move code to deal with 'anonymous' filenames
+	into two methods (returning the path, fullpath or only the 
+	filename). Default Moonlight to anonymous (not only for isolated
+	storage) unless the coreclr is disable (e.g. smcs)
+
 2009-10-21  Miguel de Icaza  <[email protected]>
 
 	* BinaryWriter.cs, BinaryReader.cs: 4.0 signature update.

+ 33 - 27
mcs/class/corlib/System.IO/FileStream.cs

@@ -40,6 +40,7 @@ using System.Threading;
 using Microsoft.Win32.SafeHandles;
 #if NET_2_1
 using System.IO.IsolatedStorage;
+using System.Security;
 #else
 using System.Security.AccessControl;
 #endif
@@ -96,7 +97,13 @@ namespace System.IO
 			this.access = access;
 			this.owner = ownsHandle;
 			this.async = isAsync;
+#if NET_2_1 && !MONOTOUCH
+			// default the browser to 'all' anonymous files and let other usage (like smcs) with 'normal'
+			// (i.e. non-anonymous except for isolated storage) files and paths
+			this.anonymous = SecurityManager.SecurityEnabled;
+#else
 			this.anonymous = false;
+#endif
 
 			InitBuffer (bufferSize, noBuffering);
 
@@ -234,8 +241,7 @@ namespace System.IO
 			if (Directory.Exists (path)) {
 				// don't leak the path information for isolated storage
 				string msg = Locale.GetText ("Access to the path '{0}' is denied.");
-				string fname = (anonymous) ? Path.GetFileName (path) : Path.GetFullPath (path);
-				throw new UnauthorizedAccessException (String.Format (msg, fname));
+				throw new UnauthorizedAccessException (String.Format (msg, GetSecureFileName (path, false)));
 			}
 
 			/* Append streams can't be read (see FileMode
@@ -261,6 +267,7 @@ namespace System.IO
 					string msg = Locale.GetText ("Could not find a part of the path \"{0}\".");
 					string fname = (anonymous) ? dname : Path.GetFullPath (path);
 #if NET_2_1
+					// don't use GetSecureFileName for the directory name
 					throw new IsolatedStorageException (String.Format (msg, fname));
 #else
 					throw new DirectoryNotFoundException (String.Format (msg, fname));
@@ -272,7 +279,7 @@ namespace System.IO
 					mode != FileMode.CreateNew && !File.Exists (path)) {
 				// don't leak the path information for isolated storage
 				string msg = Locale.GetText ("Could not find file \"{0}\".");
-				string fname = (anonymous) ? Path.GetFileName (path) : Path.GetFullPath (path);
+				string fname = GetSecureFileName (path);
 #if NET_2_1
 				throw new IsolatedStorageException (String.Format (msg, fname));
 #else
@@ -291,8 +298,7 @@ namespace System.IO
 			this.handle = MonoIO.Open (path, mode, access, share, options, out error);
 			if (handle == MonoIO.InvalidHandle) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (path) : Path.GetFullPath (path);
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (path), error);
 			}
 
 			this.access = access;
@@ -379,8 +385,7 @@ namespace System.IO
 				length = MonoIO.GetLength (handle, out error);
 				if (error != MonoIOError.ERROR_SUCCESS) {
 					// don't leak the path information for isolated storage
-					string fname = (anonymous) ? Path.GetFileName (name) : name;
-					throw MonoIO.GetException (fname, error);
+					throw MonoIO.GetException (GetSecureFileName (name), error);
 				}
 
 				return(length);
@@ -643,8 +648,7 @@ namespace System.IO
 				MonoIO.Write (handle, src, offset, count, out error);
 				if (error != MonoIOError.ERROR_SUCCESS) {
 					// don't leak the path information for isolated storage
-					string fname = (anonymous) ? Path.GetFileName (name) : name;
-					throw MonoIO.GetException (fname, error);
+					throw MonoIO.GetException (GetSecureFileName (name), error);
 				}
 				
 				buf_start += count;
@@ -784,8 +788,7 @@ namespace System.IO
 
 			if (error != MonoIOError.ERROR_SUCCESS) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (name) : name;
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (name), error);
 			}
 			
 			return(buf_start);
@@ -812,8 +815,7 @@ namespace System.IO
 			MonoIO.SetLength (handle, value, out error);
 			if (error != MonoIOError.ERROR_SUCCESS) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (name) : name;
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (name), error);
 			}
 
 			if (Position > value)
@@ -853,8 +855,7 @@ namespace System.IO
 			MonoIO.Lock (handle, position, length, out error);
 			if (error != MonoIOError.ERROR_SUCCESS) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (name) : name;
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (name), error);
 			}
 		}
 
@@ -874,8 +875,7 @@ namespace System.IO
 			MonoIO.Unlock (handle, position, length, out error);
 			if (error != MonoIOError.ERROR_SUCCESS) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (name) : name;
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (name), error);
 			}
 		}
 
@@ -897,8 +897,7 @@ namespace System.IO
 					MonoIO.Close (handle, out error);
 					if (error != MonoIOError.ERROR_SUCCESS) {
 						// don't leak the path information for isolated storage
-						string fname = (anonymous) ? Path.GetFileName (name) : name;
-						throw MonoIO.GetException (fname, error);
+						throw MonoIO.GetException (GetSecureFileName (name), error);
 					}
 
 					handle = MonoIO.InvalidHandle;
@@ -981,8 +980,7 @@ namespace System.IO
 						     out error);
 					if (error != MonoIOError.ERROR_SUCCESS) {
 						// don't leak the path information for isolated storage
-						string fname = (anonymous) ? Path.GetFileName (name) : name;
-						throw MonoIO.GetException (fname, error);
+						throw MonoIO.GetException (GetSecureFileName (name), error);
 					}
 				}
 				st.Write (buf, 0, buf_length);
@@ -1004,8 +1002,7 @@ namespace System.IO
 						     out error);
 					if (error != MonoIOError.ERROR_SUCCESS) {
 						// don't leak the path information for isolated storage
-						string fname = (anonymous) ? Path.GetFileName (name) : name;
-						throw MonoIO.GetException (fname, error);
+						throw MonoIO.GetException (GetSecureFileName (name), error);
 					}
 				}
 				MonoIO.Write (handle, buf, 0,
@@ -1013,8 +1010,7 @@ namespace System.IO
 
 				if (error != MonoIOError.ERROR_SUCCESS) {
 					// don't leak the path information for isolated storage
-					string fname = (anonymous) ? Path.GetFileName (name) : name;
-					throw MonoIO.GetException (fname, error);
+					throw MonoIO.GetException (GetSecureFileName (name), error);
 				}
 			}
 
@@ -1050,8 +1046,7 @@ namespace System.IO
 				amount = 0; // might not be needed, but well...
 			} else if (error != MonoIOError.ERROR_SUCCESS) {
 				// don't leak the path information for isolated storage
-				string fname = (anonymous) ? Path.GetFileName (name) : name;
-				throw MonoIO.GetException (fname, error);
+				throw MonoIO.GetException (GetSecureFileName (name), error);
 			}
 			
 			/* Check for read error */
@@ -1084,6 +1079,17 @@ namespace System.IO
 			buf_dirty = false;
 		}
 
+		private string GetSecureFileName (string filename)
+		{
+			return (anonymous) ? Path.GetFileName (filename) : Path.GetFullPath (filename);
+		}
+
+		private string GetSecureFileName (string filename, bool full)
+		{
+			return (anonymous) ? Path.GetFileName (filename) : 
+				(full) ? Path.GetFullPath (filename) : filename;
+		}
+
 		// fields
 
 		internal const int DefaultBufferSize = 8192;