From 7e5287447f861a77a28e7388f93580ba3038ce26 Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Tue, 5 Dec 2017 18:11:57 +0100 Subject: [PATCH] Unix: Remove MaxPath, MaxName (dotnet/corefx#25524) 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 --- .../Interop/Unix/System.Native/Interop.GetCwd.cs | 26 +++++-------- .../Interop/Unix/System.Native/Interop.PathConf.cs | 45 ---------------------- .../Common/src/System/IO/PathInternal.Unix.cs | 2 - .../Common/src/System/IO/PathInternal.Windows.cs | 1 - .../Native/Unix/System.Native/pal_process.cpp | 11 ------ .../Native/Unix/System.Native/pal_process.h | 7 ---- 6 files changed, 9 insertions(+), 83 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs index f3c01f5..3717c43 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs @@ -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) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs index 4a1fcf6..7213cb0 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.PathConf.cs @@ -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, } - /// The maximum path length for the system. -1 if it hasn't yet been initialized. - private static int s_maxPath = -1; - - /// The maximum name length for the system. -1 if it hasn't yet been initialized. - 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(); } } diff --git a/src/libraries/Common/src/System/IO/PathInternal.Unix.cs b/src/libraries/Common/src/System/IO/PathInternal.Unix.cs index 5b9951f..27424cb 100644 --- a/src/libraries/Common/src/System/IO/PathInternal.Unix.cs +++ b/src/libraries/Common/src/System/IO/PathInternal.Unix.cs @@ -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 path) diff --git a/src/libraries/Common/src/System/IO/PathInternal.Windows.cs b/src/libraries/Common/src/System/IO/PathInternal.Windows.cs index 165b3f9..1d0dcba 100644 --- a/src/libraries/Common/src/System/IO/PathInternal.Windows.cs +++ b/src/libraries/Common/src/System/IO/PathInternal.Windows.cs @@ -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[] { diff --git a/src/libraries/Native/Unix/System.Native/pal_process.cpp b/src/libraries/Native/Unix/System.Native/pal_process.cpp index 5e0aa7b..c98bad7 100644 --- a/src/libraries/Native/Unix/System.Native/pal_process.cpp +++ b/src/libraries/Native/Unix/System.Native/pal_process.cpp @@ -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 diff --git a/src/libraries/Native/Unix/System.Native/pal_process.h b/src/libraries/Native/Unix/System.Native/pal_process.h index a7a8752..cfa2ff4 100644 --- a/src/libraries/Native/Unix/System.Native/pal_process.h +++ b/src/libraries/Native/Unix/System.Native/pal_process.h @@ -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 -- 2.7.4