Merge pull request #946 from akoeplinger/fix-mono-parallel
[mono.git] / mcs / class / corlib / System.IO / FileStream.cs
index 2db2f0c8a3f8256b0e82a8dbbac4310dbe3c8393..62be3c834f4b45562475d9c863997efec66fbf76 100644 (file)
@@ -100,18 +100,11 @@ namespace System.IO
                        }
 
                        this.handle = handle;
+                       ExposeHandle ();
                        this.access = access;
                        this.owner = ownsHandle;
                        this.async = isAsync;
-#if MOONLIGHT
-                       // 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, isZeroSize);
-
                        if (canseek) {
                                buf_start = MonoIO.Seek (handle, 0, SeekOrigin.Current, out error);
                                if (error != MonoIOError.ERROR_SUCCESS) {
@@ -225,20 +218,10 @@ namespace System.IO
                        }
 
                        if (access < FileAccess.Read || access > FileAccess.ReadWrite) {
-#if MOONLIGHT
-                               if (anonymous)
-                                       throw new IsolatedStorageException ("Enum value for FileAccess was out of legal range.");
-                               else
-#endif
                                throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
                        }
 
                        if (share < FileShare.None || share > (FileShare.ReadWrite | FileShare.Delete)) {
-#if MOONLIGHT
-                               if (anonymous)
-                                       throw new IsolatedStorageException ("Enum value for FileShare was out of legal range.");
-                               else
-#endif
                                throw new ArgumentOutOfRangeException ("share", "Enum value was out of legal range.");
                        }
 
@@ -280,12 +263,7 @@ namespace System.IO
                                        // don't leak the path information for isolated storage
                                        string msg = Locale.GetText ("Could not find a part of the path \"{0}\".");
                                        string fname = (anonymous) ? dname : Path.GetFullPath (path);
-#if MOONLIGHT
-                                       // don't use GetSecureFileName for the directory name
-                                       throw new IsolatedStorageException (String.Format (msg, fname));
-#else
                                        throw new DirectoryNotFoundException (String.Format (msg, fname));
-#endif
                                }
                        }
 
@@ -294,11 +272,7 @@ namespace System.IO
                                // don't leak the path information for isolated storage
                                string msg = Locale.GetText ("Could not find file \"{0}\".");
                                string fname = GetSecureFileName (path);
-#if MOONLIGHT
-                               throw new IsolatedStorageException (String.Format (msg, fname));
-#else
                                throw new FileNotFoundException (String.Format (msg, fname), fname);
-#endif
                        }
 
                        // IsolatedStorage needs to keep the Name property to the default "[Unknown]"
@@ -377,11 +351,7 @@ namespace System.IO
 
                public string Name {
                        get {
-#if MOONLIGHT
-                               return SecurityManager.CheckElevatedPermissions () ? name : "[Unknown]";
-#else
                                return name;
-#endif
                        }
                }
 
@@ -414,19 +384,26 @@ namespace System.IO
                                if (handle == MonoIO.InvalidHandle)
                                        throw new ObjectDisposedException ("Stream has been closed");
 
-                               if(CanSeek == false)
+                               if (CanSeek == false)
                                        throw new NotSupportedException("The stream does not support seeking");
-                               
+
+                               if (safeHandle != null) {
+                                       // If the handle was leaked outside we always ask the real handle
+                                       MonoIOError error;
+
+                                       long ret = MonoIO.Seek (handle, 0,SeekOrigin.Current,out error);
+
+                                       if (error != MonoIOError.ERROR_SUCCESS) {
+                                               // don't leak the path information for isolated storage
+                                               throw MonoIO.GetException (GetSecureFileName (name), error);
+                                       }
+
+                                       return ret;
+                               }
+       
                                return(buf_start + buf_offset);
                        }
                        set {
-                               if (handle == MonoIO.InvalidHandle)
-                                       throw new ObjectDisposedException ("Stream has been closed");
-
-                               if(CanSeek == false) {
-                                       throw new NotSupportedException("The stream does not support seeking");
-                               }
-
                                if(value < 0) {
                                        throw new ArgumentOutOfRangeException("Attempt to set the position to a negative value");
                                }
@@ -440,6 +417,9 @@ namespace System.IO
                        [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                        [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
                        get {
+                               if (safeHandle == null) {
+                                       ExposeHandle ();
+                               }
                                return handle;
                        }
                }
@@ -448,20 +428,22 @@ namespace System.IO
                        [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                        [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
                        get {
-                               SafeFileHandle ret;
-
-                               if (safeHandle != null)
-                                       ret = safeHandle;
-                               else
-                                       ret = new SafeFileHandle (handle, owner);
-
-                               FlushBuffer ();
-                               return ret;
+                               if (safeHandle == null) {
+                                       ExposeHandle ();
+                               }
+                               return safeHandle;
                        }
                }
 
                // methods
 
+         void ExposeHandle ()
+               {
+                       safeHandle = new SafeFileHandle (handle, false);
+                       FlushBuffer ();
+                       InitBuffer (0, true);
+               }
+
                public override int ReadByte ()
                {
                        if (handle == MonoIO.InvalidHandle)
@@ -717,11 +699,14 @@ namespace System.IO
                                MemoryStream ms = new MemoryStream ();
                                FlushBuffer (ms);
                                ms.Write (array, offset, numBytes);
+
+                               // Set arguments to new compounded buffer 
                                offset = 0;
-                               numBytes = (int) ms.Length;
+                               array = ms.ToArray ();
+                               numBytes = array.Length;
                        }
 
-                       WriteDelegate w = new WriteDelegate (WriteInternal);
+                       WriteDelegate w = WriteInternal;
                        return w.BeginInvoke (array, offset, numBytes, userCallback, stateObject);                      
                }
                
@@ -841,7 +826,7 @@ namespace System.IO
                        FlushBuffer ();
                }
 
-#if NET_4_0 || MOONLIGHT || MOBILE
+#if NET_4_0
                public virtual void Flush (bool flushToDisk)
                {
                        FlushBuffer ();
@@ -909,6 +894,9 @@ namespace System.IO
                        Exception exc = null;
                        if (handle != MonoIO.InvalidHandle) {
                                try {
+                                       // If the FileStream is in "exposed" status
+                                       // it means that we do not have a buffer(we write the data without buffering)
+                                       // therefor we don't and can't flush the buffer becouse we don't have one.
                                        FlushBuffer ();
                                } catch (Exception e) {
                                        exc = e;
@@ -949,12 +937,18 @@ namespace System.IO
 #if !NET_2_1
                public FileSecurity GetAccessControl ()
                {
-                       throw new NotImplementedException ();
+                       return new FileSecurity (SafeFileHandle,
+                                                AccessControlSections.Owner |
+                                                AccessControlSections.Group |
+                                                AccessControlSections.Access);
                }
                
                public void SetAccessControl (FileSecurity fileSecurity)
                {
-                       throw new NotImplementedException ();
+                       if (null == fileSecurity)
+                               throw new ArgumentNullException ("fileSecurity");
+                               
+                       fileSecurity.PersistModifications (SafeFileHandle);
                }
 #endif
 
@@ -1022,7 +1016,7 @@ namespace System.IO
                        if (buf_dirty) {
                                MonoIOError error;
 
-                               if (CanSeek == true) {
+                               if (CanSeek == true && safeHandle == null) {
                                        MonoIO.Seek (handle, buf_start,
                                                     SeekOrigin.Begin,
                                                     out error);
@@ -1145,7 +1139,7 @@ namespace System.IO
 
                // fields
 
-               internal const int DefaultBufferSize = 8192;
+               internal const int DefaultBufferSize = 4096;
 
                // Input buffer ready for recycling                             
                static byte[] buf_recycle;