MoonIsolatedStorageFile.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // System.IO.IsolatedStorage.MoonIsolatedStorageFile
  3. //
  4. // Moonlight's implementation for the IsolatedStorageFile
  5. //
  6. // Authors
  7. // Miguel de Icaza ([email protected])
  8. //
  9. // Copyright (C) 2007 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;
  32. using System.IO;
  33. using System.Security;
  34. namespace System.IO.IsolatedStorage {
  35. public sealed class IsolatedStorageFile : IDisposable {
  36. static string appdir;
  37. static string TryDirectory (string path)
  38. {
  39. try {
  40. Directory.CreateDirectory (path);
  41. return path;
  42. } catch {
  43. return null;
  44. }
  45. }
  46. static IsolatedStorageFile ()
  47. {
  48. string xdg_data_home = Environment.GetEnvironmentVariable ("XDG_DATA_HOME");
  49. if (xdg_data_home == null || xdg_data_home == String.Empty){
  50. xdg_data_home = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  51. }
  52. string basedir;
  53. basedir = TryDirectory (Path.Combine (xdg_data_home, "moonlight"));
  54. if (basedir == null){
  55. //
  56. // Maybe try a few others?
  57. //
  58. return;
  59. }
  60. // FIXME: Use the actual url from the plugin for this.
  61. appdir = Path.Combine (basedir, "url");
  62. try {
  63. Directory.CreateDirectory (appdir);
  64. } catch {
  65. appdir = null;
  66. }
  67. }
  68. internal IsolatedStorageFile (string basedir)
  69. {
  70. }
  71. [SecuritySafeCritical]
  72. public static IsolatedStorageFile GetUserStoreForApplication ()
  73. {
  74. if (appdir == null)
  75. throw new SecurityException ();
  76. return new IsolatedStorageFile (appdir);
  77. }
  78. internal static string Verify (string path)
  79. {
  80. try {
  81. string full = Path.GetFullPath (Path.Combine (appdir, path));
  82. if (full.StartsWith (appdir + Path.DirectorySeparatorChar))
  83. return full;
  84. } catch {
  85. throw new IsolatedStorageException ();
  86. }
  87. throw new IsolatedStorageException ();
  88. }
  89. [SecuritySafeCritical]
  90. public void CreateDirectory (string dir)
  91. {
  92. Verify (dir);
  93. Directory.CreateDirectory (dir);
  94. }
  95. [SecuritySafeCritical]
  96. public void DeleteDirectory (string dir)
  97. {
  98. Verify (dir);
  99. Directory.Delete (dir);
  100. }
  101. [SecuritySafeCritical]
  102. public string [] GetDirectoryNames (string searchPattern)
  103. {
  104. if (searchPattern.IndexOf ('/') != -1)
  105. throw new IsolatedStorageException ();
  106. return Directory.GetDirectories (appdir, searchPattern);
  107. }
  108. [SecuritySafeCritical]
  109. public string [] GetFileNames (string searchPattern)
  110. {
  111. if (searchPattern.IndexOf ('/') != -1)
  112. throw new IsolatedStorageException ();
  113. return Directory.GetDirectories (appdir, searchPattern);
  114. }
  115. [SecuritySafeCritical]
  116. public void DeleteFile (string file)
  117. {
  118. Verify (file);
  119. }
  120. public void Dispose ()
  121. {
  122. }
  123. [SecuritySafeCritical]
  124. public void Close ()
  125. {
  126. }
  127. [CLSCompliant(false)]
  128. public ulong CurrentSize {
  129. get {
  130. return 0;
  131. }
  132. }
  133. [CLSCompliant(false)]
  134. public ulong MaximumSize {
  135. get {
  136. return 1024*1024;
  137. }
  138. }
  139. }
  140. }
  141. #endif