2 // System.IO.IsolatedStorage.MoonIsolatedStorageFile
4 // Moonlight's implementation for the IsolatedStorageFile
7 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2007, 2008 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Security;
35 namespace System.IO.IsolatedStorage {
37 public sealed class IsolatedStorageFile : IDisposable {
40 static string TryDirectory (string path)
43 Directory.CreateDirectory (path);
50 static IsolatedStorageFile ()
52 string xdg_data_home = Environment.GetEnvironmentVariable ("XDG_DATA_HOME");
53 if (String.IsNullOrEmpty (xdg_data_home)) {
54 xdg_data_home = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
58 basedir = TryDirectory (Path.Combine (xdg_data_home, "moonlight"));
61 // Maybe try a few others?
66 // FIXME: Use the actual url from the plugin for this.
67 appdir = Path.Combine (basedir, "url");
69 Directory.CreateDirectory (appdir);
76 private bool removed = false;
77 private bool disposed = false;
79 internal IsolatedStorageFile (string basedir)
83 internal void PreCheck ()
86 throw new ObjectDisposedException ("Storage was disposed");
88 throw new IsolatedStorageException ("Storage was removed");
91 public static IsolatedStorageFile GetUserStoreForApplication ()
94 throw new SecurityException ();
96 return new IsolatedStorageFile (appdir);
99 public static IsolatedStorageFile GetUserStoreForSite ()
102 throw new SecurityException ();
104 return new IsolatedStorageFile (appdir);
107 internal static string Verify (string path)
110 string full = Path.GetFullPath (Path.Combine (appdir, path));
111 if (full.StartsWith (appdir + Path.DirectorySeparatorChar))
114 throw new IsolatedStorageException ();
116 throw new IsolatedStorageException ();
119 public void CreateDirectory (string dir)
122 Directory.CreateDirectory (dir);
125 public IsolatedStorageFileStream CreateFile (string path)
129 throw new ArgumentNullException ("path");
131 return new IsolatedStorageFileStream (path, FileMode.Create, this);
134 public void DeleteDirectory (string dir)
138 Directory.Delete (dir);
141 public bool DirectoryExists (string path)
145 return Directory.Exists (path);
148 public bool FileExists (string path)
152 return File.Exists (path);
155 public string [] GetDirectoryNames ()
157 return Directory.GetFiles (appdir);
160 public string [] GetDirectoryNames (string searchPattern)
162 if (searchPattern.IndexOf ('/') != -1)
163 throw new IsolatedStorageException ();
165 return Directory.GetDirectories (appdir, searchPattern);
168 public string [] GetFileNames ()
170 return Directory.GetFiles (appdir);
173 public string [] GetFileNames (string searchPattern)
175 if (searchPattern.IndexOf ('/') != -1)
176 throw new IsolatedStorageException ();
178 return Directory.GetFiles (appdir, searchPattern);
181 public void DeleteFile (string file)
187 public void Dispose ()
192 public IsolatedStorageFileStream OpenFile (string path, FileMode mode)
194 return OpenFile (path, mode, FileAccess.ReadWrite, FileShare.None);
197 public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access)
199 return OpenFile (path, mode, access, FileShare.None);
202 public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access, FileShare share)
206 throw new ArgumentNullException ("path");
208 return new IsolatedStorageFileStream (path, mode, access, share, this);
211 public void Remove ()
215 // TODO - try to clean out everything
222 public long AvailableFreeSpace {
236 public bool IncreaseQuotaTo (long newQuotaSize)
239 if (newQuotaSize <= Quota)
240 throw new ArgumentException ("newQuotaSize", "Only increase is possible");