// Dietmar Maurer (dietmar@ximian.com)
// Dan Lewis (dihlewis@yahoo.co.uk)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
+// Marek Safar (marek.safar@gmail.com)
//
// (C) 2001-2003 Ximian, Inc. http://www.ximian.com
-// Copyright (C) 2004-2005, 2008 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005, 2008, 2010 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
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
+using System.Security;
using System.Security.Permissions;
using System.Threading;
using Microsoft.Win32.SafeHandles;
#if NET_2_1
using System.IO.IsolatedStorage;
-using System.Security;
#else
using System.Security.AccessControl;
#endif
: this (handle, access, ownsHandle, bufferSize, isAsync, false) {}
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
- internal FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync, bool noBuffering)
+ internal FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync, bool isZeroSize)
{
this.handle = MonoIO.InvalidHandle;
if (handle == this.handle)
this.access = access;
this.owner = ownsHandle;
this.async = isAsync;
-#if NET_2_1 && !MONOTOUCH
+#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
+ if (isZeroSize)
+ bufferSize = 1;
- InitBuffer (bufferSize, noBuffering);
+ InitBuffer (bufferSize);
if (canseek) {
buf_start = MonoIO.Seek (handle, 0, SeekOrigin.Current, out error);
this.safeHandle = handle;
}
+ [MonoLimitation ("This ignores the rights parameter")]
public FileStream (string path, FileMode mode,
FileSystemRights rights, FileShare share,
int bufferSize, FileOptions options)
+ : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), share, bufferSize, false, options)
{
- throw new NotImplementedException ();
}
+ [MonoLimitation ("This ignores the rights and fileSecurity parameters")]
public FileStream (string path, FileMode mode,
FileSystemRights rights, FileShare share,
int bufferSize, FileOptions options,
FileSecurity fileSecurity)
+ : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), share, bufferSize, false, options)
{
- throw new NotImplementedException ();
}
#endif
throw new ArgumentException (string.Format (msg, access, mode));
}
+ SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
+
string dname = Path.GetDirectoryName (path);
if (dname.Length > 0) {
string fp = Path.GetFullPath (dname);
}
}
- InitBuffer (bufferSize, false);
+ InitBuffer (bufferSize);
if (mode==FileMode.Append) {
this.Seek (0, SeekOrigin.End);
public string Name {
get {
- return name;
+#if MOONLIGHT
+ return SecurityManager.CheckElevatedPermissions () ? name : "[Unknown]";
+#else
+ return name;
+#endif
}
}
MonoIOError error;
FlushBuffer ();
-
- MonoIO.Write (handle, src, offset, count, out error);
- if (error != MonoIOError.ERROR_SUCCESS) {
- // don't leak the path information for isolated storage
- throw MonoIO.GetException (GetSecureFileName (name), error);
- }
+ int wcount = count;
+ while (wcount > 0){
+ int n = MonoIO.Write (handle, src, offset, wcount, out error);
+ if (error != MonoIOError.ERROR_SUCCESS)
+ throw MonoIO.GetException (GetSecureFileName (name), error);
+
+ wcount -= n;
+ offset += n;
+ }
buf_start += count;
} else {
throw new ObjectDisposedException ("Stream has been closed");
FlushBuffer ();
-
- // The flushing is not actually required, in
- //the mono runtime we were mapping flush to
- //`fsync' which is not the same.
- //
- //MonoIO.Flush (handle);
}
+#if NET_4_0
+ public virtual void Flush (bool flushToDisk)
+ {
+ FlushBuffer ();
+
+ // This does the fsync
+ if (flushToDisk){
+ MonoIOError error;
+ MonoIO.Flush (handle, out error);
+ }
+ }
+#endif
+
public virtual void Lock (long position, long length)
{
if (handle == MonoIO.InvalidHandle)
protected override void Dispose (bool disposing)
{
+ Exception exc = null;
if (handle != MonoIO.InvalidHandle) {
- FlushBuffer ();
+ try {
+ FlushBuffer ();
+ } catch (Exception e) {
+ exc = e;
+ }
if (owner) {
MonoIOError error;
canseek = false;
access = 0;
- if (disposing) {
+
+ if (disposing && buf != null) {
+ if (buf.Length == DefaultBufferSize && buf_recycle == null) {
+ lock (buf_recycle_lock) {
+ if (buf_recycle == null) {
+ buf_recycle = buf;
+ }
+ }
+ }
+
buf = null;
- }
- if (disposing)
GC.SuppressFinalize (this);
+ }
+ if (exc != null)
+ throw exc;
}
#if !NET_2_1
}
}
if (st == null) {
- MonoIO.Write (handle, buf, 0,
- buf_length, out error);
-
- if (error != MonoIOError.ERROR_SUCCESS) {
- // don't leak the path information for isolated storage
- throw MonoIO.GetException (GetSecureFileName (name), error);
+ int wcount = buf_length;
+ int offset = 0;
+ while (wcount > 0){
+ int n = MonoIO.Write (handle, buf, 0, buf_length, out error);
+ if (error != MonoIOError.ERROR_SUCCESS) {
+ // don't leak the path information for isolated storage
+ throw MonoIO.GetException (GetSecureFileName (name), error);
+ }
+ wcount -= n;
+ offset += n;
}
} else {
st.Write (buf, 0, buf_length);
return(amount);
}
- private void InitBuffer (int size, bool noBuffering)
+ void InitBuffer (int size)
{
- if (noBuffering) {
- size = 0;
- // We need a buffer for the ReadByte method. This buffer won't
- // be used for anything else since buf_size==0.
- buf = new byte [1];
+ if (size <= 0)
+ throw new ArgumentOutOfRangeException ("bufferSize", "Positive number required.");
+
+ size = Math.Max (size, 8);
+
+ //
+ // Instead of allocating a new default buffer use the
+ // last one if there is any available
+ //
+ if (size <= DefaultBufferSize && buf_recycle != null) {
+ lock (buf_recycle_lock) {
+ if (buf_recycle != null) {
+ buf = buf_recycle;
+ buf_recycle = null;
+ }
+ }
}
- else {
- if (size <= 0)
- throw new ArgumentOutOfRangeException ("bufferSize", "Positive number required.");
- if (size < 8)
- size = 8;
+
+ if (buf == null)
buf = new byte [size];
- }
+ else
+ Array.Clear (buf, 0, size);
buf_size = size;
- buf_start = 0;
- buf_offset = buf_length = 0;
- buf_dirty = false;
+// buf_start = 0;
+// buf_offset = buf_length = 0;
+// buf_dirty = false;
}
private string GetSecureFileName (string filename)
internal const int DefaultBufferSize = 8192;
+ // Input buffer ready for recycling
+ static byte[] buf_recycle;
+ static readonly object buf_recycle_lock = new object ();
+
private FileAccess access;
private bool owner;
private bool async;