[bcl] Remove the ValueAdd and InetAccess NUnit categories (#2212)
[mono.git] / mcs / class / corlib / System.Security.Permissions / FileIOPermission.cs
index 791b8eb03d47d6cee340d3ab1a93ab4ff9b22a6e..c08313b278fd1ee0bd9a4425621eb3014f573494 100644 (file)
@@ -1,11 +1,11 @@
-// \r
-// System.Security.Permissions.FileIOPermission.cs \r
-//\r
+// 
+// System.Security.Permissions.FileIOPermission.cs 
+//
 // Authors:
-//     Nick Drochak, ndrochak@gol.com\r
+//     Nick Drochak, ndrochak@gol.com
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
-// Copyright (C) 2001 Nick Drochak, All Rights Reserved\r
+// Copyright (C) 2001 Nick Drochak, All Rights Reserved
 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-\r
-using System.Collections;\r
-using System.IO;\r
-using System.Text;\r
 
-#if NET_2_0
+using System.Collections;
+using System.IO;
+using System.Text;
+
 using System.Runtime.InteropServices;
 using System.Security.AccessControl;
-#endif
-\r
-namespace System.Security.Permissions {\r
-\r
-#if NET_2_0
+
+namespace System.Security.Permissions {
+
        [ComVisible (true)]
-#endif
-       [Serializable]\r
-       public sealed class FileIOPermission\r
-                : CodeAccessPermission, IBuiltInPermission, IUnrestrictedPermission {\r
+       [Serializable]
+       public sealed class FileIOPermission
+                : CodeAccessPermission, IBuiltInPermission, IUnrestrictedPermission {
 
                private const int version = 1;
-               private static char[] m_badCharacters = {'\"','<', '>', '|', '*', '?'};\r
 
-               private bool m_Unrestricted = false;\r
-               private FileIOPermissionAccess m_AllFilesAccess = FileIOPermissionAccess.NoAccess;\r
-               private FileIOPermissionAccess m_AllLocalFilesAccess = FileIOPermissionAccess.NoAccess;\r
+               private static char[] BadPathNameCharacters;
+               private static char[] BadFileNameCharacters;
+
+               static FileIOPermission ()
+               {
+                       // we keep a local (static) copies to avoid calls/allocations
+                       BadPathNameCharacters = Path.GetInvalidPathChars ();
+                       BadFileNameCharacters = Path.GetInvalidFileNameChars ();
+               }
+
+               private bool m_Unrestricted = false;
+               private FileIOPermissionAccess m_AllFilesAccess = FileIOPermissionAccess.NoAccess;
+               private FileIOPermissionAccess m_AllLocalFilesAccess = FileIOPermissionAccess.NoAccess;
                private ArrayList readList;
                private ArrayList writeList;
                private ArrayList appendList;
                private ArrayList pathList;
 
                public FileIOPermission (PermissionState state)
-               {\r
+               {
                        if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
-                               m_Unrestricted = true;\r
-                               m_AllFilesAccess = FileIOPermissionAccess.AllAccess;\r
-                               m_AllLocalFilesAccess = FileIOPermissionAccess.AllAccess;\r
+                               m_Unrestricted = true;
+                               m_AllFilesAccess = FileIOPermissionAccess.AllAccess;
+                               m_AllLocalFilesAccess = FileIOPermissionAccess.AllAccess;
                        }
-                       CreateLists ();\r
-               }\r
-\r
+                       CreateLists ();
+               }
+
                public FileIOPermission (FileIOPermissionAccess access, string path)
                {
                        if (path == null)
                                throw new ArgumentNullException ("path");
 
                        CreateLists ();
-                       // access and path will be validated in AddPathList\r
-                       AddPathList (access, path);\r
-               }\r
-\r
+                       // access and path will be validated in AddPathList
+                       AddPathList (access, path);
+               }
+
                public FileIOPermission (FileIOPermissionAccess access, string[] pathList)
-               {\r
+               {
                        if (pathList == null)
                                throw new ArgumentNullException ("pathList");
 
-                       CreateLists ();\r
-                       // access and path will be validated in AddPathList\r
-                       AddPathList (access, pathList);\r
+                       CreateLists ();
+                       // access and path will be validated in AddPathList
+                       AddPathList (access, pathList);
                }
 
                internal void CreateLists ()
@@ -95,56 +100,58 @@ namespace System.Security.Permissions {
                        pathList = new ArrayList ();
                }
 
-#if NET_2_0
                [MonoTODO ("(2.0) Access Control isn't implemented")]
                public FileIOPermission (FileIOPermissionAccess access, AccessControlActions control, string path)
                {
                        throw new NotImplementedException ();
-               }\r
-\r
+               }
+
                [MonoTODO ("(2.0) Access Control isn't implemented")]
                public FileIOPermission (FileIOPermissionAccess access, AccessControlActions control, string[] pathList)
-               {\r
+               {
                        throw new NotImplementedException ();
                }
-#endif\r
-\r
-               public FileIOPermissionAccess AllFiles {\r
-                       get { return m_AllFilesAccess; } \r
-                       set {\r
-                               // if we are already set to unrestricted, don't change this property\r
-                               if (!m_Unrestricted){\r
-                                       m_AllFilesAccess = value;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               public FileIOPermissionAccess AllLocalFiles {\r
-                       get { return m_AllLocalFilesAccess; } \r
-                       set {\r
-                               // if we are already set to unrestricted, don't change this property\r
-                               if (!m_Unrestricted){\r
-                                       m_AllLocalFilesAccess = value;\r
-                               }\r
-                       }\r
-               }\r
-\r
+
+               internal FileIOPermission (FileIOPermissionAccess access, string[] pathList, bool checkForDuplicates, bool needFullPath)
+               {
+               }
+
+               public FileIOPermissionAccess AllFiles {
+                       get { return m_AllFilesAccess; } 
+                       set {
+                               // if we are already set to unrestricted, don't change this property
+                               if (!m_Unrestricted){
+                                       m_AllFilesAccess = value;
+                               }
+                       }
+               }
+
+               public FileIOPermissionAccess AllLocalFiles {
+                       get { return m_AllLocalFilesAccess; } 
+                       set {
+                               // if we are already set to unrestricted, don't change this property
+                               if (!m_Unrestricted){
+                                       m_AllLocalFilesAccess = value;
+                               }
+                       }
+               }
+
                public void AddPathList (FileIOPermissionAccess access, string path)
                {
                         if ((FileIOPermissionAccess.AllAccess & access) != access)
                                ThrowInvalidFlag (access, true);
-                       ThrowIfInvalidPath (path);\r
-                       AddPathInternal (access, path);\r
-               }\r
-\r
+                       ThrowIfInvalidPath (path);
+                       AddPathInternal (access, path);
+               }
+
                public void AddPathList (FileIOPermissionAccess access, string[] pathList)
-               {\r
+               {
                         if ((FileIOPermissionAccess.AllAccess & access) != access)
                                ThrowInvalidFlag (access, true);
-                       ThrowIfInvalidPath (pathList);\r
+                       ThrowIfInvalidPath (pathList);
                        foreach (string path in pathList) {
-                               AddPathInternal (access, path);\r
-                       }\r
+                               AddPathInternal (access, path);
+                       }
                }
 
                // internal to avoid duplicate checks
@@ -164,9 +171,9 @@ namespace System.Security.Permissions {
                }
 
                public override IPermission Copy ()
-               {\r
-                       if (m_Unrestricted)\r
-                               return new FileIOPermission (PermissionState.Unrestricted);\r
+               {
+                       if (m_Unrestricted)
+                               return new FileIOPermission (PermissionState.Unrestricted);
 
                        FileIOPermission copy = new FileIOPermission (PermissionState.None);
                        copy.readList = (ArrayList) readList.Clone ();
@@ -175,47 +182,47 @@ namespace System.Security.Permissions {
                        copy.pathList = (ArrayList) pathList.Clone ();
                        copy.m_AllFilesAccess = m_AllFilesAccess;
                        copy.m_AllLocalFilesAccess = m_AllLocalFilesAccess;
-                       return copy;\r
-               }\r
-\r
+                       return copy;
+               }
+
                public override void FromXml (SecurityElement esd)
-               {\r
+               {
                        // General validation in CodeAccessPermission
                        CheckSecurityElement (esd, "esd", version, version);
                        // Note: we do not (yet) care about the return value 
                        // as we only accept version 1 (min/max values)
 
-                       if (IsUnrestricted (esd)) {\r
-                               m_Unrestricted = true;\r
-                       }\r
-                       else{\r
-                               m_Unrestricted = false;\r
-                               string fileList = esd.Attribute ("Read");\r
-                               string[] files;\r
-                               if (fileList != null){\r
-                                       files = fileList.Split (';');\r
-                                       AddPathList (FileIOPermissionAccess.Read, files);\r
-                               }\r
-                               fileList = esd.Attribute ("Write");\r
-                               if (fileList != null){\r
-                                       files = fileList.Split (';');\r
-                                       AddPathList (FileIOPermissionAccess.Write, files);\r
-                               }\r
-                               fileList = esd.Attribute ("Append");\r
-                               if (fileList != null){\r
-                                       files = fileList.Split (';');\r
-                                       AddPathList (FileIOPermissionAccess.Append, files);\r
-                               }\r
-                               fileList = esd.Attribute ("PathDiscovery");\r
-                               if (fileList != null){\r
-                                       files = fileList.Split (';');\r
-                                       AddPathList (FileIOPermissionAccess.PathDiscovery, files);\r
-                               }\r
-                       }\r
-               }\r
-\r
+                       if (IsUnrestricted (esd)) {
+                               m_Unrestricted = true;
+                       }
+                       else{
+                               m_Unrestricted = false;
+                               string fileList = esd.Attribute ("Read");
+                               string[] files;
+                               if (fileList != null){
+                                       files = fileList.Split (';');
+                                       AddPathList (FileIOPermissionAccess.Read, files);
+                               }
+                               fileList = esd.Attribute ("Write");
+                               if (fileList != null){
+                                       files = fileList.Split (';');
+                                       AddPathList (FileIOPermissionAccess.Write, files);
+                               }
+                               fileList = esd.Attribute ("Append");
+                               if (fileList != null){
+                                       files = fileList.Split (';');
+                                       AddPathList (FileIOPermissionAccess.Append, files);
+                               }
+                               fileList = esd.Attribute ("PathDiscovery");
+                               if (fileList != null){
+                                       files = fileList.Split (';');
+                                       AddPathList (FileIOPermissionAccess.PathDiscovery, files);
+                               }
+                       }
+               }
+
                public string[] GetPathList (FileIOPermissionAccess access)
-               {\r
+               {
                         if ((FileIOPermissionAccess.AllAccess & access) != access)
                                ThrowInvalidFlag (access, true);
 
@@ -239,9 +246,9 @@ namespace System.Security.Permissions {
                                        ThrowInvalidFlag (access, false);
                                        break;
                        }
-                       return (result.Count > 0) ? (string[]) result.ToArray (typeof (string)) : null;\r
-               }\r
-\r
+                       return (result.Count > 0) ? (string[]) result.ToArray (typeof (string)) : null;
+               }
+
                public override IPermission Intersect (IPermission target)
                {
                        FileIOPermission fiop = Cast (target);
@@ -263,8 +270,8 @@ namespace System.Security.Permissions {
                        IntersectKeys (pathList, fiop.pathList, result.pathList);
 
                        return (result.IsEmpty () ? null : result);
-               }\r
-\r
+               }
+
                public override bool IsSubsetOf (IPermission target)
                {
                        FileIOPermission fiop = Cast (target);
@@ -293,63 +300,63 @@ namespace System.Security.Permissions {
                                return false;
 
                        return true;
-               }\r
+               }
 
                public bool IsUnrestricted ()
-               {\r
-                       return m_Unrestricted;\r
-               }\r
-\r
+               {
+                       return m_Unrestricted;
+               }
+
                public void SetPathList (FileIOPermissionAccess access, string path)
                {
                         if ((FileIOPermissionAccess.AllAccess & access) != access)
                                ThrowInvalidFlag (access, true);
-                       ThrowIfInvalidPath (path);\r
+                       ThrowIfInvalidPath (path);
                        // note: throw before clearing the actual list
-                       Clear (access);\r
-                       AddPathInternal (access, path);\r
-               }\r
-               \r
+                       Clear (access);
+                       AddPathInternal (access, path);
+               }
+               
                public void SetPathList (FileIOPermissionAccess access, string[] pathList)
-               {\r
+               {
                         if ((FileIOPermissionAccess.AllAccess & access) != access)
                                ThrowInvalidFlag (access, true);
-                       ThrowIfInvalidPath (pathList);\r
+                       ThrowIfInvalidPath (pathList);
                        // note: throw before clearing the actual list
                        Clear (access);
-                       foreach (string path in pathList)\r
-                               AddPathInternal (access, path);\r
+                       foreach (string path in pathList)
+                               AddPathInternal (access, path);
                }
 
                public override SecurityElement ToXml ()
-               {\r
+               {
                        SecurityElement se = Element (1);
-                       if (m_Unrestricted) {\r
-                               se.AddAttribute ("Unrestricted", "true");\r
-                       }\r
-                       else {\r
-                               string[] paths = GetPathList (FileIOPermissionAccess.Append);\r
-                               if (null != paths && paths.Length > 0) {\r
-                                       se.AddAttribute ("Append", String.Join (";", paths));\r
-                               }\r
-                               paths = GetPathList (FileIOPermissionAccess.Read);\r
-                               if (null != paths && paths.Length > 0) {\r
-                                       se.AddAttribute ("Read", String.Join (";", paths));\r
-                               }\r
-                               paths = GetPathList (FileIOPermissionAccess.Write);\r
-                               if (null != paths && paths.Length > 0) {\r
-                                       se.AddAttribute ("Write", String.Join  (";", paths));\r
-                               }\r
-                               paths = GetPathList (FileIOPermissionAccess.PathDiscovery);\r
-                               if (null != paths && paths.Length > 0) {\r
-                                       se.AddAttribute ("PathDiscovery", String.Join  (";", paths));\r
-                               }\r
-                       }\r
-                       return se;\r
-               }\r
-\r
+                       if (m_Unrestricted) {
+                               se.AddAttribute ("Unrestricted", "true");
+                       }
+                       else {
+                               string[] paths = GetPathList (FileIOPermissionAccess.Append);
+                               if (null != paths && paths.Length > 0) {
+                                       se.AddAttribute ("Append", String.Join (";", paths));
+                               }
+                               paths = GetPathList (FileIOPermissionAccess.Read);
+                               if (null != paths && paths.Length > 0) {
+                                       se.AddAttribute ("Read", String.Join (";", paths));
+                               }
+                               paths = GetPathList (FileIOPermissionAccess.Write);
+                               if (null != paths && paths.Length > 0) {
+                                       se.AddAttribute ("Write", String.Join  (";", paths));
+                               }
+                               paths = GetPathList (FileIOPermissionAccess.PathDiscovery);
+                               if (null != paths && paths.Length > 0) {
+                                       se.AddAttribute ("PathDiscovery", String.Join  (";", paths));
+                               }
+                       }
+                       return se;
+               }
+
                public override IPermission Union (IPermission other)
-               {\r
+               {
                        FileIOPermission fiop = Cast (other);
                        if (fiop == null)
                                return Copy ();
@@ -381,9 +388,8 @@ namespace System.Security.Permissions {
                                UnionKeys (result.pathList, paths);
                        
                        return result;
-               }\r
-\r
-#if NET_2_0
+               }
+
                [MonoTODO ("(2.0)")]
                [ComVisible (false)]
                public override bool Equals (object obj)
@@ -397,11 +403,10 @@ namespace System.Security.Permissions {
                {
                        return base.GetHashCode ();
                }
-#endif
 
-               // IBuiltInPermission\r
-               int IBuiltInPermission.GetTokenIndex ()\r
-               {\r
+               // IBuiltInPermission
+               int IBuiltInPermission.GetTokenIndex ()
+               {
                        return (int) BuiltInToken.FileIO;
                }
 
@@ -413,7 +418,7 @@ namespace System.Security.Permissions {
                                && (writeList.Count == 0) && (pathList.Count == 0));
                }
 
-               private FileIOPermission Cast (IPermission target)
+               private static FileIOPermission Cast (IPermission target)
                {
                        if (target == null)
                                return null;
@@ -426,7 +431,7 @@ namespace System.Security.Permissions {
                        return fiop;
                }
 
-               internal void ThrowInvalidFlag (FileIOPermissionAccess access, bool context) 
+               internal static void ThrowInvalidFlag (FileIOPermissionAccess access, bool context) 
                {
                        string msg = null;
                        if (context)
@@ -436,20 +441,27 @@ namespace System.Security.Permissions {
                        throw new ArgumentException (String.Format (msg, access), "access");
                }
 
-               internal void ThrowIfInvalidPath (string path)
+               internal static void ThrowIfInvalidPath (string path)
                {
-                       if (path.LastIndexOfAny (m_badCharacters) >= 0) {\r
-                               string msg = String.Format (Locale.GetText ("Invalid characters in path: '{0}'"), path);\r
-                               throw new ArgumentException (msg, "path");\r
-                       }\r
-                       // LAMESPEC: docs don't say it must be a rooted path, but the MS implementation enforces it, so we will too.\r
+                       string dir = Path.GetDirectoryName (path);
+                       if ((dir != null) && (dir.LastIndexOfAny (BadPathNameCharacters) >= 0)) {
+                               string msg = String.Format (Locale.GetText ("Invalid path characters in path: '{0}'"), path);
+                               throw new ArgumentException (msg, "path");
+                       }
+
+                       string fname = Path.GetFileName (path);
+                       if ((fname != null) && (fname.LastIndexOfAny (BadFileNameCharacters) >= 0)) {
+                               string msg = String.Format (Locale.GetText ("Invalid filename characters in path: '{0}'"), path);
+                               throw new ArgumentException (msg, "path");
+                       }
+                       // LAMESPEC: docs don't say it must be a rooted path, but the MS implementation enforces it, so we will too.
                        if (!Path.IsPathRooted (path)) {
                                string msg = Locale.GetText ("Absolute path information is required.");
-                               throw new ArgumentException (msg, "path");\r
+                               throw new ArgumentException (msg, "path");
                        }
                }
-\r
-               internal void ThrowIfInvalidPath (string[] paths)
+
+               internal static void ThrowIfInvalidPath (string[] paths)
                {
                        foreach (string path in paths)
                                ThrowIfInvalidPath (path);
@@ -466,15 +478,15 @@ namespace System.Security.Permissions {
                                appendList.Clear ();
                        if ((access & FileIOPermissionAccess.PathDiscovery) == FileIOPermissionAccess.PathDiscovery)
                                pathList.Clear ();
-               }\r
+               }
 
                // note: all path in IList are already "full paths"
-               internal bool KeyIsSubsetOf (IList local, IList target)
+               internal static bool KeyIsSubsetOf (IList local, IList target)
                {
                        bool result = false;
                        foreach (string l in local) {
                                foreach (string t in target) {
-                                       if (l.StartsWith (t)) {
+                                       if (Path.IsPathSubsetOf (t, l)) {
                                                result = true;
                                                break;
                                        }
@@ -485,7 +497,7 @@ namespace System.Security.Permissions {
                        return true;
                }
 
-               internal void UnionKeys (IList list, string[] paths)
+               internal static void UnionKeys (IList list, string[] paths)
                {
                        foreach (string path in paths) {
                                int len = list.Count;
@@ -493,40 +505,40 @@ namespace System.Security.Permissions {
                                        list.Add (path);
                                }
                                else {
-                                       for (int i=0; i < len; i++) {
+                                       int i;
+                                       for (i=0; i < len; i++) {
                                                string s = (string) list [i];
-                                               if (s.StartsWith (path)) {
+                                               if (Path.IsPathSubsetOf (path, s)) {
                                                        // replace (with reduced version)
                                                        list [i] = path;
                                                        break;
                                                }
-                                               else if (path.StartsWith (s)) {
+                                               else if (Path.IsPathSubsetOf (s, path)) {
                                                        // no need to add
                                                        break;
                                                }
-                                               else {
-                                                       list.Add (path);
-                                                       break;
-                                               }
+                                       }
+                                       if (i == len) {
+                                               list.Add (path);
                                        }
                                }
                        }
                }
 
-               internal void IntersectKeys (IList local, IList target, IList result)
+               internal static void IntersectKeys (IList local, IList target, IList result)
                {
                        foreach (string l in local) {
                                foreach (string t in target) {
                                        if (t.Length > l.Length) {
-                                               if (t.StartsWith (l))
+                                               if (Path.IsPathSubsetOf (l ,t))
                                                        result.Add (t);
                                        }
                                        else {
-                                               if (l.StartsWith (t))
+                                               if (Path.IsPathSubsetOf (t, l))
                                                        result.Add (l);
                                        }
                                }
                        }
                }
-       }\r
-}\r
+       }
+}