* BinaryReader.cs: Fixed line endings.
[mono.git] / mcs / class / corlib / System.IO / Path.cs
index e91c89e079b5701fdaef0888c41bce44ed03fb64..ca1a18ff9028ae629c08483739158eff292e83d3 100644 (file)
@@ -52,7 +52,7 @@ namespace System.IO {
        [ComVisible (true)]
        public static class Path {
 
-               [Obsolete ("see GetInvalidPathChars and GetInvalidfileNameChars methods.")]
+               [Obsolete ("see GetInvalidPathChars and GetInvalidFileNameChars methods.")]
                public static readonly char[] InvalidPathChars;
 #else
        public sealed class Path {
@@ -323,11 +323,16 @@ namespace System.IO {
                                        // UNC: \\server or \\server\share
                                        // Get server
                                        while (len < path.Length && !IsDsc (path [len])) len++;
+
                                        // Get share
-                                       while (len < path.Length && !IsDsc (path [len])) len++;
+                                       if (len < path.Length) {
+                                               len++;
+                                               while (len < path.Length && !IsDsc (path [len])) len++;
+                                       }
+
                                        return DirectorySeparatorStr +
                                                DirectorySeparatorStr +
-                                               path.Substring (2).Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
+                                               path.Substring (2, len - 2).Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
                                } else if (IsDsc (path [0])) {
                                        // path starts with '\' or '/'
                                        return DirectorySeparatorStr;
@@ -356,7 +361,8 @@ namespace System.IO {
                                path = Path.Combine (GetTempPath(), "tmp" + num.ToString("x") + ".tmp");
 
                                try {
-                                       f = new FileStream (path, FileMode.CreateNew);
+                                       f = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read,
+                                                           8192, false, (FileOptions) 1);
                                }
                                catch (SecurityException) {
                                        // avoid an endless loop
@@ -424,10 +430,10 @@ namespace System.IO {
                {
                        // return a new array as we do not want anyone to be able to change the values
                        if (Environment.IsRunningOnWindows) {
-                               return new char [36] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
+                               return new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
                                        '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12', 
                                        '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', 
-                                       '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C' };
+                                       '\x1E', '\x1F' };
                        } else {
                                return new char [1] { '\x00' };
                        }
@@ -445,7 +451,7 @@ namespace System.IO {
                                int i = 0;
                                rng.GetNonZeroBytes (buffer);
                                while ((i < buffer.Length) && (sb.Length < 12)) {
-                                       char c = (char)i;
+                                       char c = (char) buffer [i];
                                        if (Array.IndexOf (invalid, c) == -1)
                                                sb.Append (c);
                                        i++;
@@ -504,15 +510,20 @@ namespace System.IO {
                
                static bool SameRoot (string root, string path)
                {
-                       // compare path's root with the specified root
-                       int sep = root.Length - 1;
-                       // but don't compare the directory separator
-                       if (String.Compare (root, 0, path, 0, sep, true, CultureInfo.InvariantCulture) == 0) {
-                               // as they can be different for each one
-                               if (IsDsc (root[sep]) && IsDsc (path[sep]))
-                                       return true;
+                       // compare root - if enough details are available
+                       if ((root.Length < 2) || (path.Length < 2))
+                               return false;
+                       // same volume/drive
+                       if (!root [0].Equals (path [0]))
+                               return false;
+                       // presence if the separator
+                       if (path[1] != Path.VolumeSeparatorChar)
+                               return false;
+                       if ((root.Length > 2) && (path.Length > 2)) {
+                               // but don't directory compare the directory separator
+                               return (IsDsc (root[2]) && IsDsc (path[2]));
                        }
-                       return false;
+                       return true;
                }
 
                static string CanonicalizePath (string path)
@@ -576,5 +587,29 @@ namespace System.IO {
                                return ret;
                        }
                }
+
+               // required for FileIOPermission (and most proibably reusable elsewhere too)
+               // both path MUST be "full paths"
+               static internal bool IsPathSubsetOf (string subset, string path)
+               {
+                       if (subset.Length > path.Length)
+                               return false;
+
+                       // check that everything up to the last separator match
+                       int slast = subset.LastIndexOfAny (PathSeparatorChars);
+                       if (String.Compare (subset, 0, path, 0, slast) != 0)
+                               return false;
+
+                       slast++;
+                       // then check if the last segment is identical
+                       int plast = path.IndexOfAny (PathSeparatorChars, slast);
+                       if (plast >= slast) {
+                               return String.Compare (subset, slast, path, slast, path.Length - plast) == 0;
+                       }
+                       if (subset.Length != path.Length)
+                               return false;
+
+                       return String.Compare (subset, slast, path, slast, subset.Length - slast) == 0;
+               }
        }
 }