MoonIsolatedStorageFile.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // System.IO.IsolatedStorage.MoonIsolatedStorageFile
  3. //
  4. // Moonlight's implementation for the IsolatedStorageFile
  5. //
  6. // Authors
  7. // Miguel de Icaza ([email protected])
  8. // Sebastien Pouliot <[email protected]>
  9. //
  10. // Copyright (C) 2007, 2008, 2009 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if MOONLIGHT
  32. using System;
  33. using System.IO;
  34. using System.Runtime.InteropServices;
  35. using System.Security;
  36. namespace System.IO.IsolatedStorage {
  37. // Most of the time there will only be a single instance of both
  38. // * Application Store (GetUserStoreForApplication)
  39. // * Site Store (GetUserStoreForSite)
  40. // However both can have multiple concurrent uses, e.g.
  41. // * another instance of the same application (same URL) running in another Moonlight instance
  42. // * another application on the same site (i.e. host) for a site store
  43. // and share the some quota, i.e. a site and all applications on the sites share the same space
  44. // notes:
  45. // * quota seems computed in (disk) blocks, i.e. a small file will have a (non-small) size
  46. // e.g. every files and directories entries takes 1KB
  47. public sealed class IsolatedStorageFile : IDisposable {
  48. static object locker = new object ();
  49. private string basedir;
  50. private long used;
  51. private bool removed = false;
  52. private bool disposed = false;
  53. internal IsolatedStorageFile (string root)
  54. {
  55. basedir = root;
  56. }
  57. internal void PreCheck ()
  58. {
  59. if (disposed)
  60. throw new ObjectDisposedException ("Storage was disposed");
  61. if (removed)
  62. throw new IsolatedStorageException ("Storage was removed");
  63. }
  64. public static IsolatedStorageFile GetUserStoreForApplication ()
  65. {
  66. return new IsolatedStorageFile (IsolatedStorage.ApplicationPath);
  67. }
  68. public static IsolatedStorageFile GetUserStoreForSite ()
  69. {
  70. return new IsolatedStorageFile (IsolatedStorage.SitePath);
  71. }
  72. internal string Verify (string path)
  73. {
  74. // special case: 'path' would be returned (instead of combined)
  75. if ((path.Length > 0) && (path [0] == '/'))
  76. path = path.Substring (1, path.Length - 1);
  77. // outside of try/catch since we want to get things like
  78. // ArgumentException for invalid characters
  79. string combined = Path.Combine (basedir, path);
  80. try {
  81. string full = Path.GetFullPath (combined);
  82. if (full.StartsWith (basedir))
  83. return full;
  84. } catch {
  85. // we do not supply an inner exception since it could contains details about the path
  86. throw new IsolatedStorageException ();
  87. }
  88. throw new IsolatedStorageException ();
  89. }
  90. public void CreateDirectory (string dir)
  91. {
  92. PreCheck ();
  93. if (dir == null)
  94. throw new ArgumentNullException ("dir");
  95. // empty dir is ignored
  96. if (dir.Length > 0)
  97. Directory.CreateDirectory (Verify (dir));
  98. }
  99. public IsolatedStorageFileStream CreateFile (string path)
  100. {
  101. PreCheck ();
  102. try {
  103. return new IsolatedStorageFileStream (path, FileMode.Create, this);
  104. }
  105. catch (DirectoryNotFoundException) {
  106. // this can happen if the supplied path includes an unexisting directory
  107. throw new IsolatedStorageException ();
  108. }
  109. }
  110. public void DeleteDirectory (string dir)
  111. {
  112. PreCheck ();
  113. if (dir == null)
  114. throw new ArgumentNullException ("dir");
  115. Directory.Delete (Verify (dir));
  116. }
  117. public void DeleteFile (string file)
  118. {
  119. PreCheck ();
  120. if (file == null)
  121. throw new ArgumentNullException ("file");
  122. string checked_filename = Verify (file);
  123. if (!File.Exists (checked_filename))
  124. throw new IsolatedStorageException ("File does not exists");
  125. File.Delete (checked_filename);
  126. }
  127. public void Dispose ()
  128. {
  129. disposed = true;
  130. }
  131. public bool DirectoryExists (string path)
  132. {
  133. PreCheck ();
  134. return Directory.Exists (Verify (path));
  135. }
  136. public bool FileExists (string path)
  137. {
  138. PreCheck ();
  139. return File.Exists (Verify (path));
  140. }
  141. private string HideAppDir (string path)
  142. {
  143. // remove the "isolated" part of the path (and the extra '/')
  144. return path.Substring (basedir.Length + 1);
  145. }
  146. private string [] HideAppDirs (string[] paths)
  147. {
  148. for (int i=0; i < paths.Length; i++)
  149. paths [i] = HideAppDir (paths [i]);
  150. return paths;
  151. }
  152. private void CheckSearchPattern (string searchPattern)
  153. {
  154. if (searchPattern == null)
  155. throw new ArgumentNullException ("searchPattern");
  156. if (searchPattern.Length == 0)
  157. throw new IsolatedStorageException ("searchPattern");
  158. if (searchPattern.IndexOfAny (Path.GetInvalidPathChars ()) != -1)
  159. throw new ArgumentException ("searchPattern");
  160. }
  161. public string [] GetDirectoryNames ()
  162. {
  163. return HideAppDirs (Directory.GetDirectories (basedir));
  164. }
  165. public string [] GetDirectoryNames (string searchPattern)
  166. {
  167. CheckSearchPattern (searchPattern);
  168. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  169. // so we need to split them to get the right results
  170. string path = Path.GetDirectoryName (searchPattern);
  171. string pattern = Path.GetFileName (searchPattern);
  172. string [] afi = null;
  173. if (path == null || path.Length == 0) {
  174. return HideAppDirs (Directory.GetDirectories (basedir, searchPattern));
  175. } else {
  176. // we're looking for a single result, identical to path (no pattern here)
  177. // we're also looking for something under the current path (not outside isolated storage)
  178. string [] subdirs = Directory.GetDirectories (basedir, path);
  179. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  180. throw new IsolatedStorageException ();
  181. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  182. if (dir.Name != path)
  183. throw new IsolatedStorageException ();
  184. return GetNames (dir.GetDirectories (pattern));
  185. }
  186. }
  187. public string [] GetFileNames ()
  188. {
  189. return HideAppDirs (Directory.GetFiles (basedir));
  190. }
  191. public string [] GetFileNames (string searchPattern)
  192. {
  193. CheckSearchPattern (searchPattern);
  194. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  195. // so we need to split them to get the right results
  196. string path = Path.GetDirectoryName (searchPattern);
  197. string pattern = Path.GetFileName (searchPattern);
  198. string [] afi = null;
  199. if (path == null || path.Length == 0) {
  200. return HideAppDirs (Directory.GetFiles (basedir, searchPattern));
  201. } else {
  202. // we're looking for a single result, identical to path (no pattern here)
  203. // we're also looking for something under the current path (not outside isolated storage)
  204. string [] subdirs = Directory.GetDirectories (basedir, path);
  205. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  206. throw new IsolatedStorageException ();
  207. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  208. if (dir.Name != path)
  209. throw new IsolatedStorageException ();
  210. return GetNames (dir.GetFiles (pattern));
  211. }
  212. }
  213. // Return the file name portion of a full path
  214. private string[] GetNames (FileSystemInfo[] afsi)
  215. {
  216. string[] r = new string[afsi.Length];
  217. for (int i = 0; i != afsi.Length; ++i)
  218. r[i] = afsi[i].Name;
  219. return r;
  220. }
  221. public IsolatedStorageFileStream OpenFile (string path, FileMode mode)
  222. {
  223. return OpenFile (path, mode, FileAccess.ReadWrite, FileShare.None);
  224. }
  225. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access)
  226. {
  227. return OpenFile (path, mode, access, FileShare.None);
  228. }
  229. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access, FileShare share)
  230. {
  231. PreCheck ();
  232. return new IsolatedStorageFileStream (path, mode, access, share, this);
  233. }
  234. public void Remove ()
  235. {
  236. PreCheck ();
  237. IsolatedStorage.Remove (basedir);
  238. removed = true;
  239. }
  240. // note: available free space could be changed from another application (same URL, another ML instance) or
  241. // another application on the same site
  242. public long AvailableFreeSpace {
  243. get {
  244. PreCheck ();
  245. return IsolatedStorage.AvailableFreeSpace;
  246. }
  247. }
  248. // note: quota could be changed from another application (same URL, another ML instance) or
  249. // another application on the same site
  250. public long Quota {
  251. get {
  252. PreCheck ();
  253. return IsolatedStorage.Quota;
  254. }
  255. }
  256. [DllImport ("moon")]
  257. [return: MarshalAs (UnmanagedType.Bool)]
  258. extern static bool isolated_storage_increase_quota_to (string primary_text, string secondary_text);
  259. const long mb = 1024 * 1024;
  260. public bool IncreaseQuotaTo (long newQuotaSize)
  261. {
  262. PreCheck ();
  263. if (newQuotaSize <= Quota)
  264. throw new ArgumentException ("newQuotaSize", "Only increases are possible");
  265. string message = String.Format ("This web site, <u>{0}</u>, is requesting an increase of its local storage capacity on your computer. It is currently using <b>{1:F1} MB</b> out of a maximum of <b>{2:F1} MB</b>.",
  266. IsolatedStorage.Site, IsolatedStorage.Current / mb, IsolatedStorage.Quota / mb);
  267. string question = String.Format ("Do you want to increase the web site quota to a new maximum of <b>{0:F1} MB</b> ?",
  268. newQuotaSize / mb);
  269. bool result = isolated_storage_increase_quota_to (message, question);
  270. if (result)
  271. IsolatedStorage.Quota = newQuotaSize;
  272. return result;
  273. }
  274. }
  275. }
  276. #endif