示例#1
0
        /// <summary>
        /// Resolve symlinks and canonicalize the path (including "/" -> "\" on Windows)
        /// </summary>
        private static string RealPath(string path)
        {
            if (PlatformUtils.IsPosix())
            {
                bool trailingSlash = path.EndsWith("/");

                string resolvedPath;
                byte[] pathBytes = Encoding.UTF8.GetBytes(path);
                unsafe
                {
                    byte *resolvedPtr;
                    fixed(byte *pathPtr = pathBytes)
                    {
                        if ((resolvedPtr = Stdlib.realpath(pathPtr, (byte *)IntPtr.Zero)) == (byte *)IntPtr.Zero)
                        {
                            return(null);
                        }
                    }

                    resolvedPath = U8StringConverter.ToManaged(resolvedPtr);
                }

                // Preserve the trailing slash if there was one present initially
                return(trailingSlash ? $"{resolvedPath}/" : resolvedPath);
            }

            if (PlatformUtils.IsWindows())
            {
                // GetFullPath on Windows already preserves trailing slashes
                return(Path.GetFullPath(path));
            }

            throw new PlatformNotSupportedException();
        }