MoonIsolatedStorageFile.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 static bool IsEnabled {
  91. get {
  92. Console.WriteLine ("NIEX: System.IO.IsolatedStorage.IsolatedStorageFile:get_IsEnabled");
  93. throw new NotImplementedException ();
  94. }
  95. }
  96. public void CreateDirectory (string dir)
  97. {
  98. PreCheck ();
  99. if (dir == null)
  100. throw new ArgumentNullException ("dir");
  101. // empty dir is ignored
  102. if (dir.Length > 0)
  103. Directory.CreateDirectory (Verify (dir));
  104. }
  105. public IsolatedStorageFileStream CreateFile (string path)
  106. {
  107. PreCheck ();
  108. try {
  109. return new IsolatedStorageFileStream (path, FileMode.Create, this);
  110. }
  111. catch (DirectoryNotFoundException) {
  112. // this can happen if the supplied path includes an unexisting directory
  113. throw new IsolatedStorageException ();
  114. }
  115. }
  116. public void DeleteDirectory (string dir)
  117. {
  118. PreCheck ();
  119. if (dir == null)
  120. throw new ArgumentNullException ("dir");
  121. Directory.Delete (Verify (dir));
  122. }
  123. public void DeleteFile (string file)
  124. {
  125. PreCheck ();
  126. if (file == null)
  127. throw new ArgumentNullException ("file");
  128. string checked_filename = Verify (file);
  129. if (!File.Exists (checked_filename))
  130. throw new IsolatedStorageException ("File does not exists");
  131. File.Delete (checked_filename);
  132. }
  133. public void Dispose ()
  134. {
  135. disposed = true;
  136. }
  137. public bool DirectoryExists (string path)
  138. {
  139. PreCheck ();
  140. return Directory.Exists (Verify (path));
  141. }
  142. public bool FileExists (string path)
  143. {
  144. PreCheck ();
  145. return File.Exists (Verify (path));
  146. }
  147. private string HideAppDir (string path)
  148. {
  149. // remove the "isolated" part of the path (and the extra '/')
  150. return path.Substring (basedir.Length + 1);
  151. }
  152. private string [] HideAppDirs (string[] paths)
  153. {
  154. for (int i=0; i < paths.Length; i++)
  155. paths [i] = HideAppDir (paths [i]);
  156. return paths;
  157. }
  158. private void CheckSearchPattern (string searchPattern)
  159. {
  160. if (searchPattern == null)
  161. throw new ArgumentNullException ("searchPattern");
  162. if (searchPattern.Length == 0)
  163. throw new IsolatedStorageException ("searchPattern");
  164. if (searchPattern.IndexOfAny (Path.GetInvalidPathChars ()) != -1)
  165. throw new ArgumentException ("searchPattern");
  166. }
  167. public string [] GetDirectoryNames ()
  168. {
  169. return HideAppDirs (Directory.GetDirectories (basedir));
  170. }
  171. public string [] GetDirectoryNames (string searchPattern)
  172. {
  173. CheckSearchPattern (searchPattern);
  174. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  175. // so we need to split them to get the right results
  176. string path = Path.GetDirectoryName (searchPattern);
  177. string pattern = Path.GetFileName (searchPattern);
  178. string [] afi = null;
  179. if (path == null || path.Length == 0) {
  180. return HideAppDirs (Directory.GetDirectories (basedir, searchPattern));
  181. } else {
  182. // we're looking for a single result, identical to path (no pattern here)
  183. // we're also looking for something under the current path (not outside isolated storage)
  184. string [] subdirs = Directory.GetDirectories (basedir, path);
  185. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  186. throw new IsolatedStorageException ();
  187. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  188. if (dir.Name != path)
  189. throw new IsolatedStorageException ();
  190. return GetNames (dir.GetDirectories (pattern));
  191. }
  192. }
  193. public string [] GetFileNames ()
  194. {
  195. return HideAppDirs (Directory.GetFiles (basedir));
  196. }
  197. public string [] GetFileNames (string searchPattern)
  198. {
  199. CheckSearchPattern (searchPattern);
  200. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  201. // so we need to split them to get the right results
  202. string path = Path.GetDirectoryName (searchPattern);
  203. string pattern = Path.GetFileName (searchPattern);
  204. string [] afi = null;
  205. if (path == null || path.Length == 0) {
  206. return HideAppDirs (Directory.GetFiles (basedir, searchPattern));
  207. } else {
  208. // we're looking for a single result, identical to path (no pattern here)
  209. // we're also looking for something under the current path (not outside isolated storage)
  210. string [] subdirs = Directory.GetDirectories (basedir, path);
  211. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  212. throw new IsolatedStorageException ();
  213. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  214. if (dir.Name != path)
  215. throw new IsolatedStorageException ();
  216. return GetNames (dir.GetFiles (pattern));
  217. }
  218. }
  219. // Return the file name portion of a full path
  220. private string[] GetNames (FileSystemInfo[] afsi)
  221. {
  222. string[] r = new string[afsi.Length];
  223. for (int i = 0; i != afsi.Length; ++i)
  224. r[i] = afsi[i].Name;
  225. return r;
  226. }
  227. public IsolatedStorageFileStream OpenFile (string path, FileMode mode)
  228. {
  229. return OpenFile (path, mode, FileAccess.ReadWrite, FileShare.None);
  230. }
  231. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access)
  232. {
  233. return OpenFile (path, mode, access, FileShare.None);
  234. }
  235. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access, FileShare share)
  236. {
  237. PreCheck ();
  238. return new IsolatedStorageFileStream (path, mode, access, share, this);
  239. }
  240. public void Remove ()
  241. {
  242. PreCheck ();
  243. IsolatedStorage.Remove (basedir);
  244. removed = true;
  245. }
  246. // note: available free space could be changed from another application (same URL, another ML instance) or
  247. // another application on the same site
  248. public long AvailableFreeSpace {
  249. get {
  250. PreCheck ();
  251. return IsolatedStorage.AvailableFreeSpace;
  252. }
  253. }
  254. // note: quota could be changed from another application (same URL, another ML instance) or
  255. // another application on the same site
  256. public long Quota {
  257. get {
  258. PreCheck ();
  259. return IsolatedStorage.Quota;
  260. }
  261. }
  262. [DllImport ("moon")]
  263. [return: MarshalAs (UnmanagedType.Bool)]
  264. extern static bool isolated_storage_increase_quota_to (string primary_text, string secondary_text);
  265. const long mb = 1024 * 1024;
  266. public bool IncreaseQuotaTo (long newQuotaSize)
  267. {
  268. PreCheck ();
  269. if (newQuotaSize <= Quota)
  270. throw new ArgumentException ("newQuotaSize", "Only increases are possible");
  271. 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>.",
  272. IsolatedStorage.Site, IsolatedStorage.Current / mb, IsolatedStorage.Quota / mb);
  273. string question = String.Format ("Do you want to increase the web site quota to a new maximum of <b>{0:F1} MB</b> ?",
  274. newQuotaSize / mb);
  275. bool result = isolated_storage_increase_quota_to (message, question);
  276. if (result)
  277. IsolatedStorage.Quota = newQuotaSize;
  278. return result;
  279. }
  280. }
  281. }
  282. #endif