Unix: Remove MaxPath, MaxName (dotnet/corefx#25524)
authorTom Deseyn <tom.deseyn@gmail.com>
Tue, 5 Dec 2017 17:11:57 +0000 (18:11 +0100)
committerStephen Toub <stoub@microsoft.com>
Tue, 5 Dec 2017 17:11:57 +0000 (12:11 -0500)
PATH_MAX is not an upper bound on path lengths on Unix. system function return
values should be used to determine behavior, instead of limiting the path length.

MaxName is not used. When used, it should take into account the file system at
that path.

Commit migrated from https://github.com/dotnet/corefx/commit/385225e78205d9558924e05fa854326e6ee7116f

src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs
src/libraries/Common/src/System/IO/PathInternal.Unix.cs
src/libraries/Common/src/System/IO/PathInternal.Windows.cs
src/libraries/Native/Unix/System.Native/pal_process.cpp
src/libraries/Native/Unix/System.Native/pal_process.h

index f3c01f5..3717c43 100644 (file)
@@ -25,29 +25,21 @@ internal static partial class Interop
             }
 
             // If that was too small, try increasing large buffer sizes
-            // until we get one that works or until we hit MaxPath.
-            int maxPath = Interop.Sys.MaxPath;
-            if (StackLimit < maxPath)
+            int bufferSize = StackLimit;
+            do
             {
-                int bufferSize = StackLimit;
-                do
+                checked { bufferSize *= 2; }
+                var buf = new byte[bufferSize];
+                fixed (byte* ptr = &buf[0])
                 {
-                    checked { bufferSize *= 2; }
-                    var buf = new byte[Math.Min(bufferSize, maxPath)];
-                    fixed (byte* ptr = &buf[0])
+                    result = GetCwdHelper(ptr, buf.Length);
+                    if (result != null)
                     {
-                        result = GetCwdHelper(ptr, buf.Length);
-                        if (result != null)
-                        {
-                            return result;
-                        }
+                        return result;
                     }
                 }
-                while (bufferSize < maxPath);
             }
-
-            // If we couldn't get the cwd with a MaxPath-sized buffer, something's wrong.
-            throw Interop.GetExceptionForIoErrno(new ErrorInfo(Interop.Error.ENAMETOOLONG));
+            while (true);
         }
 
         private static unsafe string GetCwdHelper(byte* ptr, int bufferSize)
index 4a1fcf6..7213cb0 100644 (file)
@@ -9,8 +9,6 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        internal static int DEFAULT_PC_NAME_MAX = 255;
-
         internal enum PathConfName : int
         {
             PC_LINK_MAX         = 1,
@@ -24,50 +22,7 @@ internal static partial class Interop
             PC_VDISABLE         = 9,
         }
 
-        /// <summary>The maximum path length for the system.  -1 if it hasn't yet been initialized.</summary>
-        private static int s_maxPath = -1;
-
-        /// <summary>The maximum name length for the system.  -1 if it hasn't yet been initialized.</summary>
-        private static int s_maxName = -1;
-
-        internal static int MaxPath
-        {
-            get
-            {
-                // Benign race condition on cached value
-                if (s_maxPath < 0) 
-                {
-                    // GetMaximumPath returns a long from PathConf
-                    // but our callers expect an int so we need to convert.
-                    long temp = GetMaximumPath();
-                    if (temp > int.MaxValue)
-                        s_maxPath = int.MaxValue;
-                    else
-                        s_maxPath = Convert.ToInt32(temp);
-                }
-                return s_maxPath; 
-            }
-        }
-
-        internal static int MaxName
-        {
-            get
-            {
-                // Benign race condition on cached value
-                if (s_maxName < 0)
-                {
-                    int result = PathConf("/", PathConfName.PC_NAME_MAX);
-                    s_maxName = result >= 0 ? result : DEFAULT_PC_NAME_MAX;
-                }
-                
-                return s_maxName;
-            }
-        }
-
         [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PathConf", SetLastError = true)]
         private static extern int PathConf(string path, PathConfName name);
-
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetMaximumPath")]
-        private static extern long GetMaximumPath();
     }
 }
index 5b9951f..27424cb 100644 (file)
@@ -15,8 +15,6 @@ namespace System.IO
         private const char InvalidPathChar = '\0';
         internal static char[] GetInvalidPathChars() => new char[] { InvalidPathChar };
 
-        internal static readonly int MaxComponentLength = Interop.Sys.MaxName;
-
         internal const string ParentDirectoryPrefix = @"../";
 
         internal static int GetRootLength(ReadOnlySpan<char> path)
index 165b3f9..1d0dcba 100644 (file)
@@ -56,7 +56,6 @@ namespace System.IO
         internal const int UncPrefixLength = 2;
         // \\?\UNC\, \\.\UNC\
         internal const int UncExtendedPrefixLength = 8;
-        internal static readonly int MaxComponentLength = 255;
 
         internal static char[] GetInvalidPathChars() => new char[]
         {
index 5e0aa7b..c98bad7 100644 (file)
@@ -504,17 +504,6 @@ extern "C" int64_t SystemNative_PathConf(const char* path, PathConfName name)
     return pathconf(path, confValue);
 }
 
-extern "C" int64_t SystemNative_GetMaximumPath()
-{
-    int64_t result = pathconf("/", _PC_PATH_MAX);
-    if (result == -1)
-    {
-        result = PATH_MAX;
-    }
-
-    return result;
-}
-
 extern "C" int32_t SystemNative_GetPriority(PriorityWhich which, int32_t who)
 {
     // GetPriority uses errno 0 to show success to make sure we don't have a stale value
index a7a8752..cfa2ff4 100644 (file)
@@ -242,13 +242,6 @@ extern "C" int32_t SystemNative_WTermSig(int32_t status);
 extern "C" int64_t SystemNative_PathConf(const char* path, PathConfName name);
 
 /**
- * Gets the current (or default, on failure) Maximum Path allowed by the system.
- *
- * This is called out explicitly, rather than using PathConf, since the default value changes depending on the platform.
- */
-extern "C" int64_t SystemNative_GetMaximumPath();
-
-/**
  * Gets the priority (nice value) of a certain execution group.
  *
  * Returns the nice value (from -20 to 20) of the group on success; otherwise, returns -1. Unfortunately, -1 is also a