IsolatedStorageFileStream.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if !MOONLIGHT
  31. using System.Diagnostics;
  32. using System.Globalization;
  33. using System.Reflection;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Runtime.InteropServices;
  37. using Microsoft.Win32.SafeHandles;
  38. namespace System.IO.IsolatedStorage {
  39. [ComVisible (true)]
  40. public class IsolatedStorageFileStream : FileStream {
  41. [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
  42. private static string CreateIsolatedPath (IsolatedStorageFile isf, string path, FileMode mode)
  43. {
  44. if (path == null)
  45. throw new ArgumentNullException ("path");
  46. if (!Enum.IsDefined (typeof (FileMode), mode))
  47. throw new ArgumentException ("mode");
  48. if (isf == null) {
  49. // we can't call GetUserStoreForDomain here because it depends on
  50. // Assembly.GetCallingAssembly (), which would be our constructor,
  51. // i.e. the result would always be mscorlib.dll. So we need to do
  52. // a small stack walk to find who's calling the constructor
  53. StackFrame sf = new StackFrame (3); // skip self and constructor
  54. isf = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly,
  55. IsolatedStorageFile.GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence),
  56. IsolatedStorageFile.GetAssemblyIdentityFromEvidence (sf.GetMethod ().ReflectedType.Assembly.UnprotectedGetEvidence ()));
  57. }
  58. #if NET_4_0
  59. if (isf.IsDisposed)
  60. throw new ObjectDisposedException ("IsolatedStorageFile");
  61. if (isf.IsClosed)
  62. throw new InvalidOperationException ("Storage needs to be open for this operation.");
  63. #endif
  64. // ensure that the _root_ isolated storage can be (and is) created.
  65. FileInfo fi = new FileInfo (isf.Root);
  66. if (!fi.Directory.Exists)
  67. fi.Directory.Create ();
  68. // remove the root path character(s) if they exist
  69. if (Path.IsPathRooted (path)) {
  70. string root = Path.GetPathRoot (path);
  71. path = path.Remove (0, root.Length);
  72. }
  73. // other directories (provided by 'path') must already exists
  74. string file = Path.Combine (isf.Root, path);
  75. string full = Path.GetFullPath (file);
  76. full = Path.GetFullPath (file);
  77. if (!full.StartsWith (isf.Root))
  78. throw new IsolatedStorageException ();
  79. fi = new FileInfo (file);
  80. if (!fi.Directory.Exists) {
  81. // don't leak the path information for isolated storage
  82. string msg = Locale.GetText ("Could not find a part of the path \"{0}\".");
  83. throw new DirectoryNotFoundException (String.Format (msg, path));
  84. }
  85. // FIXME: this is probably a good place to Assert our security
  86. // needs (once Mono supports imperative security stack modifiers)
  87. return file;
  88. }
  89. public IsolatedStorageFileStream (string path, FileMode mode)
  90. : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.Read, DefaultBufferSize, null)
  91. {
  92. }
  93. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access)
  94. : this (path, mode, access, access == FileAccess.Write ? FileShare.None : FileShare.Read, DefaultBufferSize, null)
  95. {
  96. }
  97. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share)
  98. : this (path, mode, access, share, DefaultBufferSize, null)
  99. {
  100. }
  101. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  102. : this (path, mode, access, share, bufferSize, null)
  103. {
  104. }
  105. // FIXME: Further limit the assertion when imperative Assert is implemented
  106. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  107. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, IsolatedStorageFile isf)
  108. : base (CreateIsolatedPath (isf, path, mode), mode, access, share, bufferSize, false, true)
  109. {
  110. }
  111. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
  112. : this (path, mode, access, share, DefaultBufferSize, isf)
  113. {
  114. }
  115. public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
  116. : this (path, mode, access, access == FileAccess.Write ? FileShare.None : FileShare.Read, DefaultBufferSize, isf)
  117. {
  118. }
  119. public IsolatedStorageFileStream (string path, FileMode mode, IsolatedStorageFile isf)
  120. : this (path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.Read, DefaultBufferSize, isf)
  121. {
  122. }
  123. public override bool CanRead {
  124. get {return base.CanRead;}
  125. }
  126. public override bool CanSeek {
  127. get {return base.CanSeek;}
  128. }
  129. public override bool CanWrite {
  130. get {return base.CanWrite;}
  131. }
  132. public override SafeFileHandle SafeFileHandle {
  133. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  134. get {
  135. throw new IsolatedStorageException (
  136. Locale.GetText ("Information is restricted"));
  137. }
  138. }
  139. [Obsolete ("Use SafeFileHandle - once available")]
  140. public override IntPtr Handle {
  141. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  142. get {
  143. throw new IsolatedStorageException (
  144. Locale.GetText ("Information is restricted"));
  145. }
  146. }
  147. public override bool IsAsync {
  148. get {return base.IsAsync;}
  149. }
  150. public override long Length {
  151. get {return base.Length;}
  152. }
  153. public override long Position {
  154. get {return base.Position;}
  155. set {base.Position = value;}
  156. }
  157. public override IAsyncResult BeginRead (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
  158. {
  159. return base.BeginRead (buffer, offset, numBytes, userCallback, stateObject);
  160. }
  161. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
  162. {
  163. return base.BeginWrite (buffer, offset, numBytes, userCallback, stateObject);
  164. }
  165. public override int EndRead (IAsyncResult asyncResult)
  166. {
  167. return base.EndRead (asyncResult);
  168. }
  169. public override void EndWrite (IAsyncResult asyncResult)
  170. {
  171. base.EndWrite (asyncResult);
  172. }
  173. public override void Flush ()
  174. {
  175. base.Flush ();
  176. }
  177. #if NET_4_0
  178. public override void Flush (bool flushToDisk)
  179. {
  180. base.Flush (flushToDisk);
  181. }
  182. #endif
  183. public override int Read (byte[] buffer, int offset, int count)
  184. {
  185. return base.Read (buffer, offset, count);
  186. }
  187. public override int ReadByte ()
  188. {
  189. return base.ReadByte ();
  190. }
  191. public override long Seek (long offset, SeekOrigin origin)
  192. {
  193. return base.Seek (offset, origin);
  194. }
  195. public override void SetLength (long value)
  196. {
  197. base.SetLength (value);
  198. }
  199. public override void Write (byte[] buffer, int offset, int count)
  200. {
  201. base.Write (buffer, offset, count);
  202. }
  203. public override void WriteByte (byte value)
  204. {
  205. base.WriteByte (value);
  206. }
  207. protected override void Dispose (bool disposing)
  208. {
  209. base.Dispose (disposing);
  210. }
  211. }
  212. }
  213. #endif