IsolatedStorageFileStream.cs 7.7 KB

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