Ejemplo n.º 1
0
        /// <summary>
        /// Trims the path by removing unecessary '..' and '.' path items.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="currentPath">The current path.</param>
        /// <param name="paths">The paths.</param>
        /// <param name="hasDrivePath">Does path has a drive letter in it?</param>
        /// <param name="isLastTrim">if set to <c>true</c> is last trim to occur.</param>
        private static unsafe void TrimParentAndSelfPath(StringBuilder builder, ref int currentPath, StringSpan *paths, bool hasDrivePath, bool isLastTrim)
        {
            if (currentPath < 0)
            {
                return;
            }

            var path = paths[currentPath];

            if (IsParentComponentPath(builder, path))
            {
                // If we have 2 or more components we can remove them but only if the
                // previous path is not already a relative path.
                if ((currentPath > 0) && !IsParentComponentPath(builder, paths[currentPath - 1]))
                {
                    if (currentPath == 1)
                    {
                        // Case of just 'a/../' or '/../'
                        if (paths[0].Length == 1)
                        {
                            currentPath     = 0;
                            paths[0].Length = 1;
                        }
                        else
                        {
                            // We are back to an empty path.
                            currentPath    = -1;
                            builder.Length = 0;
                            return;
                        }
                    }
                    else if ((currentPath == 2) && hasDrivePath)
                    {
                        // Case of just 'c:/..' which becomes 'c:/'.
                        currentPath--;
                    }
                    else
                    {
                        // Case of something like '.../a/b/../' => '.../a/'
                        currentPath = currentPath - 2;
                    }
                    // The new length is where the last removed component started
                    builder.Length = paths[currentPath + 1].Start;
                }
            }
            else if (IsRelativeCurrentComponentPath(builder, path) && ((isLastTrim && currentPath > 0) || !isLastTrim))
            {
                // We do not need the current component, we starts from the parent if any (or no parent if !isLastTrim)
                currentPath--;
                // The new length is where the last removed component started
                builder.Length = paths[currentPath + 1].Start;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Trims the path by removing unecessary '..' and '.' path items.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="currentPath">The current path.</param>
        /// <param name="paths">The paths.</param>
        /// <param name="isLastTrim">if set to <c>true</c> is last trim to occur.</param>
        /// <returns><c>true</c> if trim has been done, <c>false</c> otherwise.</returns>
        private unsafe static bool TrimParentAndSelfPath(StringBuilder builder, ref int currentPath, StringSpan *paths, bool isLastTrim)
        {
            var path = paths[currentPath];

            if (currentPath > 0 && IsParentPath(builder, path))
            {
                // If the root path is a drive, and we are going to back slash, just don't
                if (IsInvalidRelativeBacktrackOnDrive(currentPath, paths))
                {
                    return(false);
                }

                // If previous path is already a relative path, then we probably can popup
                var previousPath = paths[currentPath - 1];
                if (IsParentPath(builder, previousPath))
                {
                    return(false);
                }

                // We can popup the previous path
                paths[currentPath] = new StringSpan();
                currentPath--;
                paths[currentPath].Length = 0;
                builder.Length            = paths[currentPath].Start;
                return(true);
            }

            var isRelativeCurrentPath = IsRelativeCurrentPath(builder, path);

            if (!(isLastTrim && currentPath == 0 && isRelativeCurrentPath) && isRelativeCurrentPath)
            {
                // We can popup the previous path
                paths[currentPath].Length = 0;
                builder.Length            = paths[currentPath].Start;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 3
0
 private unsafe static bool IsInvalidBacktrackOnDrive(StringBuilder builder, int currentPath, StringSpan *paths)
 {
     // If the root path is a drive, and we are going to back slash, just don't
     return(currentPath > 0 && IsParentPath(builder, paths[currentPath]) && IsInvalidRelativeBacktrackOnDrive(currentPath, paths));
 }
Ejemplo n.º 4
0
 private unsafe static bool IsInvalidRelativeBacktrackOnDrive(int currentPath, StringSpan *paths)
 {
     // If the root path is a drive, and we are going to back slash, just don't
     return(IsDriveSpan(paths[0]) && (currentPath == 1 || (currentPath == 2 && paths[1].Length == 0)));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Trims the path by removing unecessary '..' and '.' path items.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="currentPath">The current path.</param>
        /// <param name="paths">The paths.</param>
        /// <param name="isLastTrim">if set to <c>true</c> is last trim to occur.</param>
        /// <returns><c>true</c> if trim has been done, <c>false</c> otherwise.</returns>
        private static unsafe bool TrimParentAndSelfPath(StringBuilder builder, ref int currentPath, StringSpan *paths, bool isLastTrim)
        {
            var path = paths[currentPath];

            if (currentPath > 0 && IsParentPath(builder, path))
            {
                // If previous path is already a relative path, then we probably can popup
                var previousPath = paths[currentPath - 1];
                if (IsParentPath(builder, previousPath))
                {
                    return(false);
                }

                // Note: the drive path has a negative Length at that moment so it will also be considered invalid (which is what we want)
                if (!previousPath.IsValid)
                {
                    // Swallow the parent path if we reached some root level
                    paths[currentPath].Length = 0;
                    builder.Length            = paths[currentPath].Start;
                    return(true);
                }

                // We can popup the previous path
                paths[currentPath] = new StringSpan();
                currentPath--;
                paths[currentPath].Length = 0;
                builder.Length            = paths[currentPath].Start;
                return(true);
            }

            var isRelativeCurrentPath = IsRelativeCurrentPath(builder, path);

            if (!(isLastTrim && currentPath == 0 && isRelativeCurrentPath) && isRelativeCurrentPath)
            {
                // We can popup the previous path
                paths[currentPath].Length = 0;
                builder.Length            = paths[currentPath].Start;
                return(true);
            }
            return(false);
        }