dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/IO/IsolatedStorage Iso


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/IO/IsolatedStorage IsolatedStorage.cs,1.1,1.2 IsolatedStorageFile.cs,1.2,1.3
Date: Thu, 24 Apr 2003 19:36:55 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO/IsolatedStorage
In directory subversions:/tmp/cvs-serv9694/runtime/System/IO/IsolatedStorage

Modified Files:
        IsolatedStorage.cs IsolatedStorageFile.cs 
Log Message:


Fill out the implementation of "System.IO.IsolatedStorage".


Index: IsolatedStorage.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO/IsolatedStorage/IsolatedStorage.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** IsolatedStorage.cs  19 Apr 2003 04:03:04 -0000      1.1
--- IsolatedStorage.cs  24 Apr 2003 23:36:53 -0000      1.2
***************
*** 28,41 ****
  using System.Security.Permissions;
  
  public abstract class IsolatedStorage : MarshalByRefObject
  {
        // Internal state.
        private IsolatedStorageScope scope;
  
        // Constructor.
        protected IsolatedStorage() {}
  
!       // Get the current amount of space that has be used.
!       [TODO]
        [CLSCompliant(false)]
        public virtual ulong CurrentSize
--- 28,55 ----
  using System.Security.Permissions;
  
+ // Isolated storage is used in the .NET Framework SDK to create "compartments"
+ // that are physically separated from the rest of the file system and from
+ // "less trusted" applications, on a per user or per domain basis.
+ //
+ // Physical separation isn't really possible on most platforms, and arguably
+ // it is a bad idea as it may prevent the user from accessing data created by
+ // applications running on their system.
+ //
+ // In this implementation, we use the regular filesystem operations with no
+ // physical separation, and let the underlying runtime engine and operating
+ // system worry about the security issues.  Per user and per domain profiles
+ // are handled by basing all operations in a store on a "base" directory.
+ 
  public abstract class IsolatedStorage : MarshalByRefObject
  {
        // Internal state.
        private IsolatedStorageScope scope;
+       private Object assemblyIdentity;
+       private Object domainIdentity;
  
        // Constructor.
        protected IsolatedStorage() {}
  
!       // Get the current amount of space that has been used.
        [CLSCompliant(false)]
        public virtual ulong CurrentSize
***************
*** 43,75 ****
                                get
                                {
!                                       // TODO
!                                       return 0;
                                }
                        }
  
        // Get the assembly identity for this isolated storage object.
-       [TODO]
        public Object AssemblyIdentity
                        {
                                get
                                {
!                                       // TODO
!                                       return null;
                                }
                        }
  
        // Get the domain identity for this isolated storage object.
-       [TODO]
        public Object DomainIdentity
                        {
                                get
                                {
!                                       // TODO
!                                       return null;
                                }
                        }
  
        // Get the maximum amount of space that can be used.
-       [TODO]
        [CLSCompliant(false)]
        public virtual ulong MaximumSize
--- 57,84 ----
                                get
                                {
!                                       throw new InvalidOperationException
!                                               
(_("Invalid_IsolatedCurrentSize"));
                                }
                        }
  
        // Get the assembly identity for this isolated storage object.
        public Object AssemblyIdentity
                        {
                                get
                                {
!                                       return assemblyIdentity;
                                }
                        }
  
        // Get the domain identity for this isolated storage object.
        public Object DomainIdentity
                        {
                                get
                                {
!                                       return domainIdentity;
                                }
                        }
  
        // Get the maximum amount of space that can be used.
        [CLSCompliant(false)]
        public virtual ulong MaximumSize
***************
*** 77,82 ****
                                get
                                {
!                                       // TODO
!                                       return 0;
                                }
                        }
--- 86,91 ----
                                get
                                {
!                                       throw new InvalidOperationException
!                                               (_("Invalid_IsolatedQuota"));
                                }
                        }
***************
*** 96,100 ****
                                get
                                {
!                                       return Path.DirectorySeparatorChar;
                                }
                        }
--- 105,109 ----
                                get
                                {
!                                       return '\\';
                                }
                        }
***************
*** 113,124 ****
                        GetPermission(PermissionSet ps);
  
!       // Initialise this storage object.
!       [TODO]
        protected void InitStore(IsolatedStorageScope scope,
                                                         Type 
domainEvidenceType,
                                                         Type 
assemblyEvidenceType)
                        {
-                               // TODO
                                this.scope = scope;
                        }
  
--- 122,134 ----
                        GetPermission(PermissionSet ps);
  
!       // Initialise this storage object.  We don't use the evidence 
information,
!       // because we let the underlying filesystem enforce security 
constraints.
        protected void InitStore(IsolatedStorageScope scope,
                                                         Type 
domainEvidenceType,
                                                         Type 
assemblyEvidenceType)
                        {
                                this.scope = scope;
+                               this.assemblyIdentity = null;
+                               this.domainIdentity = null;
                        }
  

Index: IsolatedStorageFile.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO/IsolatedStorage/IsolatedStorageFile.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** IsolatedStorageFile.cs      24 Apr 2003 09:20:35 -0000      1.2
--- IsolatedStorageFile.cs      24 Apr 2003 23:36:53 -0000      1.3
***************
*** 29,37 ****
  using System.Security.Permissions;
  using System.Security.Policy;
  
  public sealed class IsolatedStorageFile : IsolatedStorage, IDisposable
  {
        // Constructor.
!       internal IsolatedStorageFile() {}
  
        // Destructor.
--- 29,54 ----
  using System.Security.Permissions;
  using System.Security.Policy;
+ using System.Text;
  
+ // Note: see the general comments in "IsolatedStorage.cs".
+ 
+ [TODO]
  public sealed class IsolatedStorageFile : IsolatedStorage, IDisposable
  {
+       // Internal state.
+       private String baseDirectory;
+       private int refCount;
+       private bool closed;
+       private static Hashtable stores = new Hashtable();
+ 
        // Constructor.
!       internal IsolatedStorageFile(IsolatedStorageScope scope,
!                                                                String 
baseDirectory)
!                       {
!                               InitStore(scope, null, null);
!                               this.baseDirectory = baseDirectory;
!                               this.refCount = 1;
!                               this.closed = false;
!                       }
  
        // Destructor.
***************
*** 41,46 ****
                        }
  
!       // Get the current amount of space that has be used.
!       [TODO]
        [CLSCompliant(false)]
        public override ulong CurrentSize
--- 58,62 ----
                        }
  
!       // Get the current amount of space that has been used.
        [CLSCompliant(false)]
        public override ulong CurrentSize
***************
*** 48,58 ****
                                get
                                {
!                                       // TODO
!                                       return 0;
                                }
                        }
  
        // Get the maximum amount of space that can be used.
-       [TODO]
        [CLSCompliant(false)]
        public override ulong MaximumSize
--- 64,72 ----
                                get
                                {
!                                       return base.CurrentSize;
                                }
                        }
  
        // Get the maximum amount of space that can be used.
        [CLSCompliant(false)]
        public override ulong MaximumSize
***************
*** 60,64 ****
                                get
                                {
-                                       // TODO
                                        return base.MaximumSize;
                                }
--- 74,77 ----
***************
*** 66,94 ****
  
        // Close this storage area.
-       [TODO]
        public void Close()
                        {
!                               // TODO
                        }
  
        // Create a directory within this storage area.
-       [TODO]
        public void CreateDirectory(String dir)
                        {
!                               // TODO
                        }
  
        // Delete a file from this storage area.
-       [TODO]
        public void DeleteFile(String file)
                        {
!                               // TODO
                        }
  
        // Delete a directory from this storage area.
-       [TODO]
        public void DeleteDirectory(String dir)
                        {
!                               // TODO
                        }
  
--- 79,200 ----
  
        // Close this storage area.
        public void Close()
                        {
!                               lock(typeof(IsolatedStorageFile))
!                               {
!                                       if(!closed)
!                                       {
!                                               if(--refCount == 0)
!                                               {
!                                                       // Remove the store 
from the global list because
!                                                       // nothing is 
referencing it at the moment.
!                                                       stores.Remove(Scope);
!                                               }
!                                               closed = true;
!                                       }
!                               }
!                       }
! 
!       // Get the full path of an item in this storage area.
!       private String GetFullPath(String name, String path)
!                       {
!                               // Perform simple validation on the pathname.
!                               if(path == null)
!                               {
!                                       throw new ArgumentNullException(name);
!                               }
! 
!                               // Validate the pathname components and build
!                               // the full pathname.
!                               StringBuilder builder = new StringBuilder();
!                               int posn = 0;
!                               int posn2;
!                               builder.Append(baseDirectory);
!                               builder.Append(Path.DirectorySeparatorChar);
!                               while(posn < path.Length)
!                               {
!                                       posn2 = posn;
!                                       while(posn2 < path.Length &&
!                                                 path[posn2] != '\\' && 
path[posn2] != '/' &&
!                                                 path[posn2] != 
Path.DirectorySeparatorChar &&
!                                                 path[posn2] != 
Path.AltDirectorySeparatorChar)
!                                       {
!                                               ++posn2;
!                                       }
!                                       if(posn2 == posn)
!                                       {
!                                               // Empty pathname component.
!                                               throw new 
IsolatedStorageException
!                                                       
(_("IO_InvalidPathname"));
!                                       }
!                                       else if(posn2 == (posn + 1) && 
path[posn] == '.')
!                                       {
!                                               // Reference to the "current" 
directory.
!                                               throw new 
IsolatedStorageException
!                                                       
(_("IO_InvalidPathname"));
!                                       }
!                                       else if(posn2 == (posn + 2) && 
path[posn] == '.' &&
!                                                       path[posn + 1] == '.')
!                                       {
!                                               // Reference to the "parent" 
directory.
!                                               throw new 
IsolatedStorageException
!                                                       
(_("IO_InvalidPathname"));
!                                       }
!                                       builder.Append(path, posn, posn2 - 
posn);
!                                       if(posn2 < path.Length)
!                                       {
!                                               posn = posn2 + 1;
!                                               
builder.Append(Path.DirectorySeparatorChar);
!                                       }
!                                       else
!                                       {
!                                               posn = posn2;
!                                       }
!                               }
! 
!                               // Return the final pathname to the caller.
!                               return builder.ToString();
                        }
  
        // Create a directory within this storage area.
        public void CreateDirectory(String dir)
                        {
!                               try
!                               {
!                                       
Directory.CreateDirectory(GetFullPath("dir", dir));
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
        // Delete a file from this storage area.
        public void DeleteFile(String file)
                        {
!                               try
!                               {
!                                       File.Delete(GetFullPath("file", file));
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
        // Delete a directory from this storage area.
        public void DeleteDirectory(String dir)
                        {
!                               try
!                               {
!                                       Directory.Delete(GetFullPath("dir", 
dir), false);
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
***************
*** 97,116 ****
                        {
                                Close();
                        }
  
        // Get a list of directories from a storage area directory.
-       [TODO]
        public String[] GetDirectoryNames(String searchPattern)
                        {
!                               // TODO
!                               return null;
                        }
  
        // Get a list of files from a storage area directory.
-       [TODO]
        public String[] GetFileNames(String searchPattern)
                        {
!                               // TODO
!                               return null;
                        }
  
--- 203,249 ----
                        {
                                Close();
+                               GC.SuppressFinalize(this);
                        }
  
        // Get a list of directories from a storage area directory.
        public String[] GetDirectoryNames(String searchPattern)
                        {
!                               // Split the pattern into directory and 
wildcards.
!                               String fullPath = GetFullPath("searchPattern", 
searchPattern);
!                               int index = 
fullPath.LastIndexOf(Path.DirectorySeparatorChar);
!                               searchPattern = fullPath.Substring(index);
!                               fullPath = fullPath.Substring(0, index - 1);
! 
!                               // Scan the directory and return the names.
!                               try
!                               {
!                                       return 
Directory.GetDirectories(fullPath, searchPattern);
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
        // Get a list of files from a storage area directory.
        public String[] GetFileNames(String searchPattern)
                        {
!                               // Split the pattern into directory and 
wildcards.
!                               String fullPath = GetFullPath("searchPattern", 
searchPattern);
!                               int index = 
fullPath.LastIndexOf(Path.DirectorySeparatorChar);
!                               searchPattern = fullPath.Substring(index);
!                               fullPath = fullPath.Substring(0, index - 1);
! 
!                               // Scan the directory and return the names.
!                               try
!                               {
!                                       return Directory.GetFiles(fullPath, 
searchPattern);
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
***************
*** 135,156 ****
                        }
  
!       // Get an isolated storage area.
        [TODO]
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Object 
domainIdentity,
                                 Object assemblyIdentity)
                        {
!                               // TODO
!                               return null;
                        }
-       [TODO]
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Type 
domainEvidenceType,
                                 Type assemblyEvidenceType)
                        {
!                               // TODO
!                               return null;
                        }
-       [TODO]
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Evidence 
domainEvidence,
--- 268,336 ----
                        }
  
!       // Get the base directory for an isolated storage scope.
        [TODO]
+       private static String GetBaseDirectory(IsolatedStorageScope scope)
+                       {
+                               // TODO
+                               throw new 
SecurityException(_("IO_IsolatedPermissions"));
+                       }
+ 
+       // Get an isolated storage area.  We don't use evidence information
+       // in this implementation because the underlying filesystem classes
+       // take care of the security issues.
+       private static IsolatedStorageFile GetStore(IsolatedStorageScope scope)
+                       {
+                               lock(typeof(IsolatedStorageFile))
+                               {
+                                       // Search for an existing open 
reference to the scope.
+                                       IsolatedStorageFile file;
+                                       file = 
(IsolatedStorageFile)(stores[scope]);
+                                       if(file != null)
+                                       {
+                                               ++(file.refCount);
+                                               return file;
+                                       }
+ 
+                                       // Get the base directory for the 
scope, which will also
+                                       // check that the caller has sufficient 
permissions.
+                                       String baseDirectory = 
GetBaseDirectory(scope);
+ 
+                                       // Make sure that the directory exists, 
because
+                                       // it may have been removed previously.
+                                       if(!Directory.Exists(baseDirectory))
+                                       {
+                                               try
+                                               {
+                                                       
Directory.CreateDirectory(baseDirectory);
+                                               }
+                                               catch(IOException e)
+                                               {
+                                                       throw new 
IsolatedStorageException
+                                                               
(_("IO_IsolatedStorage"), e);
+                                               }
+                                       }
+ 
+                                       // Create a new isolated storage area.
+                                       file = new IsolatedStorageFile(scope, 
baseDirectory);
+ 
+                                       // Add the storage area to the global 
list.
+                                       stores[scope] = file;
+ 
+                                       // Return the store to the caller.
+                                       return file;
+                               }
+                       }
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Object 
domainIdentity,
                                 Object assemblyIdentity)
                        {
!                               return GetStore(scope);
                        }
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Type 
domainEvidenceType,
                                 Type assemblyEvidenceType)
                        {
!                               return GetStore(scope);
                        }
        public static IsolatedStorageFile GetStore
                                (IsolatedStorageScope scope, Evidence 
domainEvidence,
***************
*** 158,201 ****
                                 Type assemblyEvidenceType)
                        {
!                               // TODO
!                               return null;
                        }
  
        // Get the user storage area for the current assembly.
-       [TODO]
        public static IsolatedStorageFile GetUserStoreForAssembly()
                        {
!                               // TODO
!                               return null;
                        }
  
        // Get the user storage area for the current domain.
-       [TODO]
        public static IsolatedStorageFile GetUserStoreForDomain()
                        {
!                               // TODO
!                               return null;
                        }
  
        // Remove this isolated storage object.
-       [TODO]
        public override void Remove()
                        {
!                               // TODO
                        }
  
        // Remove all stores of a particular type.
-       [TODO]
        public static void Remove(IsolatedStorageScope scope)
                        {
!                               // TODO
                        }
  
        // Enumerate all storage areas of a particular type.
-       [TODO]
        public static IEnumerator GetEnumerator(IsolatedStorageScope scope)
                        {
!                               // TODO
!                               return null;
                        }
  
--- 338,412 ----
                                 Type assemblyEvidenceType)
                        {
!                               return GetStore(scope);
                        }
  
        // Get the user storage area for the current assembly.
        public static IsolatedStorageFile GetUserStoreForAssembly()
                        {
!                               return GetStore(IsolatedStorageScope.User |
!                                                               
IsolatedStorageScope.Assembly);
                        }
  
        // Get the user storage area for the current domain.
        public static IsolatedStorageFile GetUserStoreForDomain()
                        {
!                               return GetStore(IsolatedStorageScope.User |
!                                                               
IsolatedStorageScope.Domain);
                        }
  
        // Remove this isolated storage object.
        public override void Remove()
                        {
!                               try
!                               {
!                                       Directory.Delete(baseDirectory, true);
!                               }
!                               catch(IOException e)
!                               {
!                                       throw new IsolatedStorageException
!                                               (_("IO_IsolatedStorage"), e);
!                               }
                        }
  
        // Remove all stores of a particular type.
        public static void Remove(IsolatedStorageScope scope)
                        {
!                               IsolatedStorageFile file = GetStore(scope);
!                               try
!                               {
!                                       file.Remove();
!                               }
!                               finally
!                               {
!                                       file.Close();
!                               }
                        }
  
        // Enumerate all storage areas of a particular type.
        public static IEnumerator GetEnumerator(IsolatedStorageScope scope)
                        {
!                               IsolatedStorageFile store;
! 
!                               // We can only do this for particular storage 
scopes.
!                               if(scope == IsolatedStorageScope.User ||
!                                  scope == (IsolatedStorageScope.User |
!                                                    
IsolatedStorageScope.Roaming))
!                               {
!                                       try
!                                       {
!                                               store = GetStore(scope);
!                                       }
!                                       catch(Exception)
!                                       {
!                                               store = null;
!                                       }
!                               }
!                               else
!                               {
!                                       store = null;
!                               }
! 
!                               // Build and return an enumerator for the scope 
that we found.
!                               return new ScopeEnumerator(store);
                        }
  
***************
*** 205,212 ****
                                get
                                {
!                                       // TODO
!                                       return null;
                                }
                        }
  
  }; // class IsolatedStorageFile
--- 416,482 ----
                                get
                                {
!                                       return baseDirectory;
                                }
                        }
+ 
+       // Enumerator class for isolated storage areas.
+       private sealed class ScopeEnumerator : IEnumerator
+       {
+               // Internal state.
+               private IsolatedStorageFile store;
+               private IsolatedStorageFile current;
+               private bool done;
+               private bool reset;
+ 
+               // Constructor.
+               public ScopeEnumerator(IsolatedStorageFile store)
+                               {
+                                       this.store = store;
+                                       this.current = null;
+                                       this.done = (store == null);
+                                       this.reset = false;
+                               }
+ 
+               // Implement the IEnumerator interface.
+               public bool MoveNext()
+                               {
+                                       if(done)
+                                       {
+                                               current = null;
+                                               return false;
+                                       }
+                                       else if(reset)
+                                       {
+                                               current = store;
+                                               reset = false;
+                                               return true;
+                                       }
+                                       else
+                                       {
+                                               current = null;
+                                               done = true;
+                                               return false;
+                                       }
+                               }
+               public void Reset()
+                               {
+                                       done = (store == null);
+                                       current = null;
+                                       reset = true;
+                               }
+               public Object Current
+                               {
+                                       get
+                                       {
+                                               if(current == null)
+                                               {
+                                                       throw new 
InvalidOperationException
+                                                               
(_("Invalid_BadEnumeratorPosition"));
+                                               }
+                                               return current;
+                                       }
+                               }
+ 
+       }; // class ScopeEnumerator
  
  }; // class IsolatedStorageFile





reply via email to

[Prev in Thread] Current Thread [Next in Thread]