IsolatedStorageFileStream.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // System.IO.IsolatedStorage.IsolatedStorageFileStream
  3. //
  4. // Authors
  5. // Sean MacIsaac ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2001 Ximian, Inc.
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Diagnostics;
  31. using System.Globalization;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. #if NET_2_0
  36. using Microsoft.Win32.SafeHandles;
  37. #endif
  38. namespace System.IO.IsolatedStorage {
  39. public class IsolatedStorageFileStream : FileStream {
  40. [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
  41. private static string CreateIsolatedPath (IsolatedStorageFile isf, string path)
  42. {
  43. if (path == null)
  44. throw new ArgumentNullException ("path");
  45. if (isf == null) {
  46. // we can't call GetUserStoreForDomain here because it depends on
  47. // Assembly.GetCallingAssembly (), which would be our constructor,
  48. // i.e. the result would always be mscorlib.dll. So we need to do
  49. // a small stack walk to find who's calling the constructor
  50. StackFrame sf = new StackFrame (3); // skip self and constructor
  51. isf = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly,
  52. IsolatedStorageFile.GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence),
  53. IsolatedStorageFile.GetAssemblyIdentityFromEvidence (sf.GetMethod ().ReflectedType.Assembly.Evidence));
  54. }
  55. // ensure that the _root_ isolated storage can be (and is) created.
  56. FileInfo fi = new FileInfo (isf.Root);
  57. if (!fi.Directory.Exists)
  58. fi.Directory.Create ();
  59. // other directories (provided by 'path') must already exists
  60. string file = Path.Combine (isf.Root, path);
  61. fi = new FileInfo (file);
  62. if (!fi.Directory.Exists) {
  63. // don't leak the path information for isolated storage
  64. string msg = Locale.GetText ("Could not find a part of the path \"{0}\".");
  65. throw new DirectoryNotFoundException (String.Format (msg, path));
  66. }
  67. // FIXME: this is probably a good place to Assert our security
  68. // needs (once Mono supports imperative security stack modifiers)
  69. return file;
  70. }
  71. public IsolatedStorageFileStream (string path, FileMode mode)
  72. : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.Read, DefaultBufferSize, null)
  73. {
  74. }
  75. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access)
  76. : this (path, mode, access, access == FileAccess.Write ? FileShare.None : FileShare.Read, DefaultBufferSize, null)
  77. {
  78. }
  79. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share)
  80. : this (path, mode, access, share, DefaultBufferSize, null)
  81. {
  82. }
  83. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  84. : this (path, mode, access, share, bufferSize, null)
  85. {
  86. }
  87. // FIXME: Further limit the assertion when imperative Assert is implemented
  88. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  89. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, IsolatedStorageFile isf)
  90. : base (CreateIsolatedPath (isf, path), mode, access, share, bufferSize, false, true)
  91. {
  92. }
  93. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
  94. : this (path, mode, access, share, DefaultBufferSize, isf)
  95. {
  96. }
  97. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
  98. : this (path, mode, access, access == FileAccess.Write ? FileShare.None : FileShare.Read, DefaultBufferSize, isf)
  99. {
  100. }
  101. public IsolatedStorageFileStream (string path, FileMode mode, IsolatedStorageFile isf)
  102. : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.Read, DefaultBufferSize, isf)
  103. {
  104. }
  105. public override bool CanRead {
  106. get {return base.CanRead;}
  107. }
  108. public override bool CanSeek {
  109. get {return base.CanSeek;}
  110. }
  111. public override bool CanWrite {
  112. get {return base.CanWrite;}
  113. }
  114. #if NET_2_0
  115. public override SafeFileHandle SafeFileHandle {
  116. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  117. get {
  118. throw new IsolatedStorageException (
  119. Locale.GetText ("Information is restricted"));
  120. }
  121. }
  122. [Obsolete ("Use SafeFileHandle - once available")]
  123. #endif
  124. public override IntPtr Handle {
  125. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  126. get {
  127. throw new IsolatedStorageException (
  128. Locale.GetText ("Information is restricted"));
  129. }
  130. }
  131. public override bool IsAsync {
  132. get {return base.IsAsync;}
  133. }
  134. public override long Length {
  135. get {return base.Length;}
  136. }
  137. public override long Position {
  138. get {return base.Position;}
  139. set {base.Position = value;}
  140. }
  141. public override IAsyncResult BeginRead (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
  142. {
  143. return base.BeginRead (buffer, offset, numBytes, userCallback, stateObject);
  144. }
  145. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
  146. {
  147. return base.BeginWrite (buffer, offset, numBytes, userCallback, stateObject);
  148. }
  149. public override void Close ()
  150. {
  151. base.Close ();
  152. }
  153. public override int EndRead (IAsyncResult asyncResult)
  154. {
  155. return base.EndRead (asyncResult);
  156. }
  157. public override void EndWrite (IAsyncResult asyncResult)
  158. {
  159. base.EndWrite (asyncResult);
  160. }
  161. public override void Flush ()
  162. {
  163. base.Flush ();
  164. }
  165. public override int Read (byte[] buffer, int offset, int count)
  166. {
  167. return base.Read (buffer, offset, count);
  168. }
  169. public override int ReadByte ()
  170. {
  171. return base.ReadByte ();
  172. }
  173. public override long Seek (long offset, SeekOrigin origin)
  174. {
  175. return base.Seek (offset, origin);
  176. }
  177. public override void SetLength (long value)
  178. {
  179. base.SetLength (value);
  180. }
  181. public override void Write (byte[] buffer, int offset, int count)
  182. {
  183. base.Write (buffer, offset, count);
  184. }
  185. public override void WriteByte (byte value)
  186. {
  187. base.WriteByte (value);
  188. }
  189. protected override void Dispose (bool disposing)
  190. {
  191. base.Dispose (disposing);
  192. }
  193. }
  194. }