MoonIsolatedStorage.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.IO.IsolatedStorage.IsolatedQuotaGroup
  3. //
  4. // Authors
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if MOONLIGHT
  29. using System;
  30. using System.IO;
  31. using System.Runtime.InteropServices;
  32. using System.Security;
  33. using System.Security.Cryptography;
  34. using System.Text;
  35. namespace System.IO.IsolatedStorage {
  36. internal static class IsolatedStorage {
  37. // NOTE: both the 'site' and 'application' share the same quota
  38. internal const long DefaultQuota = 1024 * 1024;
  39. // Since we can extend more than AvailableFreeSize we need to substract the "safety" value out of it
  40. private const int SafetyZone = 1024;
  41. static string site_root;
  42. static string site_config;
  43. static long site_quota;
  44. // this is similar to Silverlight hierarchy because differing too much would cause
  45. // problems with the 260 character maximum allowed for paths
  46. // Considering a 10 characters user name the following platform will allow:
  47. // 109 characters path, under Windows Vista (Silverlight 2)
  48. // 77 characters path, under Windows XP (Silverlight 2)
  49. // 100 characters path, under Mac OSX (Silverlight 2)
  50. // 159 characters path, under Linux (Moonlight 2)
  51. // 1234567890123456789012345678901234567890123 = 43 + 28 + 1 + 28 +1 = 101
  52. // 1 2 3 4
  53. // /home/1234567890/.local/share/moonlight/is/{site-hash:28}/{app-hash:28}/
  54. static IsolatedStorage ()
  55. {
  56. string isolated_root = GetIsolatedStorageRoot ();
  57. // enable/disable osilated storage - requires restart
  58. Enabled = !File.Exists (Path.Combine (isolated_root, "disabled"));
  59. // from System.Windows.Application we made "xap_uri" correspond to
  60. // Application.Current.Host.Source.AbsoluteUri
  61. string app = (AppDomain.CurrentDomain.GetData ("xap_uri") as string);
  62. if (app.StartsWith ("file://")) {
  63. // every path is a different site, the XAP is the application
  64. Site = Path.GetDirectoryName (app.Substring (7));
  65. } else {
  66. // for http[s] the "Site Identity" is built using:
  67. // * the protocol (so http and https are different);
  68. // * the host (so beta.moonlight.com is different from www.moonlight.com); and
  69. // * the port (so 8080 is different from 80) but
  70. // ** 80 and none are identical for HTTP
  71. // ** 443 and none are identical for HTTPS
  72. Site = app.Substring (0, app.IndexOf ('/', 8));
  73. }
  74. // the "Site Identity"
  75. string site_hash = Hash (Site);
  76. site_root = TryDirectory (Path.Combine (isolated_root, site_hash));
  77. SetupSite (site_root);
  78. SitePath = TryDirectory (Path.Combine (site_root, site_hash));
  79. // the "Application Identity"
  80. string app_hash = Hash (app);
  81. SetupApplication (app, app_hash, site_root);
  82. ApplicationPath = TryDirectory (Path.Combine (site_root, app_hash));
  83. }
  84. static string GetIsolatedStorageRoot ()
  85. {
  86. // http://freedesktop.org/Standards/basedir-spec/basedir-spec-0.6.html
  87. string xdg_data_home = Environment.GetEnvironmentVariable ("XDG_DATA_HOME");
  88. if (String.IsNullOrEmpty (xdg_data_home)) {
  89. xdg_data_home = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  90. }
  91. string moonlight = TryDirectory (Path.Combine (xdg_data_home, "moonlight"));
  92. return TryDirectory (Path.Combine (moonlight, "is"));
  93. }
  94. static void SetupSite (string dir)
  95. {
  96. site_quota = DefaultQuota;
  97. // read configuration file (e.g. quota) if it exists, otherwise write it
  98. site_config = Path.Combine (dir, "config");
  99. if (File.Exists (site_config)) {
  100. LoadConfiguration ();
  101. } else {
  102. SaveConfiguration ();
  103. }
  104. }
  105. static void LoadConfiguration ()
  106. {
  107. // read quota, the rest is not useful to us
  108. using (StreamReader sr = new StreamReader (site_config)) {
  109. string line = sr.ReadLine ();
  110. while (line != null) {
  111. if (line.StartsWith ("QUOTA = ")) {
  112. if (!Int64.TryParse (line.Substring (8, line.Length - 8), out site_quota))
  113. Quota = DefaultQuota;
  114. }
  115. line = sr.ReadLine ();
  116. }
  117. }
  118. }
  119. static void SaveConfiguration ()
  120. {
  121. using (StreamWriter sw = new StreamWriter (site_config)) {
  122. sw.WriteLine ("URI = {0}", Site);
  123. sw.WriteLine ("QUOTA = {0}", Quota);
  124. }
  125. }
  126. static void SetupApplication (string app, string app_hash, string dir)
  127. {
  128. // save the application information (for the management UI)
  129. string config = Path.Combine (dir, app_hash + ".info");
  130. if (File.Exists (config))
  131. return;
  132. using (StreamWriter sw = new StreamWriter (config)) {
  133. sw.WriteLine ("URI = {0}", app);
  134. }
  135. }
  136. // goal: uniform length directory name
  137. // non-goal: security by obsfucation
  138. static string Hash (string name)
  139. {
  140. string id;
  141. using (SHA1Managed hash = new SHA1Managed ()) {
  142. byte[] digest = hash.ComputeHash (Encoding.UTF8.GetBytes (name));
  143. id = Convert.ToBase64String (digest);
  144. }
  145. return (id.IndexOf ('/') == -1) ? id : id.Replace ('/', '-');
  146. }
  147. static string TryDirectory (string path)
  148. {
  149. try {
  150. Directory.CreateDirectory (path);
  151. return path;
  152. } catch {
  153. return null;
  154. }
  155. }
  156. static internal void Remove (string dir)
  157. {
  158. try {
  159. Directory.Delete (dir, true);
  160. }
  161. finally {
  162. TryDirectory (dir);
  163. }
  164. }
  165. static internal bool CanExtend (long request)
  166. {
  167. return (request <= AvailableFreeSpace + SafetyZone);
  168. }
  169. static public string ApplicationPath {
  170. get; private set;
  171. }
  172. static public string SitePath {
  173. get; private set;
  174. }
  175. static public long AvailableFreeSpace {
  176. get { return Quota - Current - SafetyZone; }
  177. }
  178. [DllImport ("moon")]
  179. extern static long isolated_storage_get_current_usage (string root);
  180. static public long Current {
  181. get { return isolated_storage_get_current_usage (site_root); }
  182. }
  183. static public long Quota {
  184. get { return site_quota; }
  185. set {
  186. site_quota = value;
  187. SaveConfiguration ();
  188. }
  189. }
  190. static public string Site {
  191. get; private set;
  192. }
  193. // it is possible, from the UI, to completely disable IsolatedStorage
  194. static public bool Enabled { get; private set; }
  195. }
  196. }
  197. #endif