MoonIsolatedStorageFile.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. [MonoTODO ("always return true since this was the only behavior in Silverlight 3")]
  91. public static bool IsEnabled {
  92. get {
  93. return true;
  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. public DateTimeOffset GetCreationTime (string path)
  148. {
  149. throw new NotImplementedException ();
  150. }
  151. public DateTimeOffset GetLastAccessTime (string path)
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. public DateTimeOffset GetLastWriteTime (string path)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. private string HideAppDir (string path)
  160. {
  161. // remove the "isolated" part of the path (and the extra '/')
  162. return path.Substring (basedir.Length + 1);
  163. }
  164. private string [] HideAppDirs (string[] paths)
  165. {
  166. for (int i=0; i < paths.Length; i++)
  167. paths [i] = HideAppDir (paths [i]);
  168. return paths;
  169. }
  170. private void CheckSearchPattern (string searchPattern)
  171. {
  172. if (searchPattern == null)
  173. throw new ArgumentNullException ("searchPattern");
  174. if (searchPattern.Length == 0)
  175. throw new IsolatedStorageException ("searchPattern");
  176. if (searchPattern.IndexOfAny (Path.GetInvalidPathChars ()) != -1)
  177. throw new ArgumentException ("searchPattern");
  178. }
  179. public string [] GetDirectoryNames ()
  180. {
  181. return HideAppDirs (Directory.GetDirectories (basedir));
  182. }
  183. public string [] GetDirectoryNames (string searchPattern)
  184. {
  185. CheckSearchPattern (searchPattern);
  186. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  187. // so we need to split them to get the right results
  188. string path = Path.GetDirectoryName (searchPattern);
  189. string pattern = Path.GetFileName (searchPattern);
  190. string [] afi = null;
  191. if (path == null || path.Length == 0) {
  192. return HideAppDirs (Directory.GetDirectories (basedir, searchPattern));
  193. } else {
  194. // we're looking for a single result, identical to path (no pattern here)
  195. // we're also looking for something under the current path (not outside isolated storage)
  196. string [] subdirs = Directory.GetDirectories (basedir, path);
  197. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  198. throw new IsolatedStorageException ();
  199. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  200. if (dir.Name != path)
  201. throw new IsolatedStorageException ();
  202. return GetNames (dir.GetDirectories (pattern));
  203. }
  204. }
  205. public string [] GetFileNames ()
  206. {
  207. return HideAppDirs (Directory.GetFiles (basedir));
  208. }
  209. public string [] GetFileNames (string searchPattern)
  210. {
  211. CheckSearchPattern (searchPattern);
  212. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  213. // so we need to split them to get the right results
  214. string path = Path.GetDirectoryName (searchPattern);
  215. string pattern = Path.GetFileName (searchPattern);
  216. string [] afi = null;
  217. if (path == null || path.Length == 0) {
  218. return HideAppDirs (Directory.GetFiles (basedir, searchPattern));
  219. } else {
  220. // we're looking for a single result, identical to path (no pattern here)
  221. // we're also looking for something under the current path (not outside isolated storage)
  222. string [] subdirs = Directory.GetDirectories (basedir, path);
  223. if (subdirs.Length != 1 || subdirs [0].IndexOf (basedir) < 0)
  224. throw new IsolatedStorageException ();
  225. DirectoryInfo dir = new DirectoryInfo (subdirs [0]);
  226. if (dir.Name != path)
  227. throw new IsolatedStorageException ();
  228. return GetNames (dir.GetFiles (pattern));
  229. }
  230. }
  231. // Return the file name portion of a full path
  232. private string[] GetNames (FileSystemInfo[] afsi)
  233. {
  234. string[] r = new string[afsi.Length];
  235. for (int i = 0; i != afsi.Length; ++i)
  236. r[i] = afsi[i].Name;
  237. return r;
  238. }
  239. public IsolatedStorageFileStream OpenFile (string path, FileMode mode)
  240. {
  241. return OpenFile (path, mode, FileAccess.ReadWrite, FileShare.None);
  242. }
  243. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access)
  244. {
  245. return OpenFile (path, mode, access, FileShare.None);
  246. }
  247. public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access, FileShare share)
  248. {
  249. PreCheck ();
  250. return new IsolatedStorageFileStream (path, mode, access, share, this);
  251. }
  252. public void Remove ()
  253. {
  254. PreCheck ();
  255. IsolatedStorage.Remove (basedir);
  256. removed = true;
  257. }
  258. // note: available free space could be changed from another application (same URL, another ML instance) or
  259. // another application on the same site
  260. public long AvailableFreeSpace {
  261. get {
  262. PreCheck ();
  263. return IsolatedStorage.AvailableFreeSpace;
  264. }
  265. }
  266. // note: quota could be changed from another application (same URL, another ML instance) or
  267. // another application on the same site
  268. public long Quota {
  269. get {
  270. PreCheck ();
  271. return IsolatedStorage.Quota;
  272. }
  273. }
  274. [DllImport ("moon")]
  275. [return: MarshalAs (UnmanagedType.Bool)]
  276. extern static bool isolated_storage_increase_quota_to (string primary_text, string secondary_text);
  277. const long mb = 1024 * 1024;
  278. public bool IncreaseQuotaTo (long newQuotaSize)
  279. {
  280. PreCheck ();
  281. if (newQuotaSize <= Quota)
  282. throw new ArgumentException ("newQuotaSize", "Only increases are possible");
  283. 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>.",
  284. IsolatedStorage.Site, IsolatedStorage.Current / mb, IsolatedStorage.Quota / mb);
  285. string question = String.Format ("Do you want to increase the web site quota to a new maximum of <b>{0:F1} MB</b> ?",
  286. newQuotaSize / mb);
  287. bool result = isolated_storage_increase_quota_to (message, question);
  288. if (result)
  289. IsolatedStorage.Quota = newQuotaSize;
  290. return result;
  291. }
  292. public void CopyFile (string sourceFileName, string destinationFileName)
  293. {
  294. throw new NotImplementedException ();
  295. }
  296. public void CopyFile (string sourceFileName, string destinationFileName, bool overwrite)
  297. {
  298. throw new NotImplementedException ();
  299. }
  300. public void MoveDirectory (string sourceDirectoryName, string destinationDirectoryName)
  301. {
  302. throw new NotImplementedException ();
  303. }
  304. public void MoveFile (string sourceFileName, string destinationFileName)
  305. {
  306. throw new NotImplementedException ();
  307. }
  308. public long UsedSize {
  309. get {
  310. throw new NotImplementedException ();
  311. }
  312. }
  313. }
  314. }
  315. #endif