Exemple #1
0
    public static bool IsContainedInSymLinkHelper(IDictionary<DirectoryName, DirectoryData> directories, FileSystemName name) {
      var directoryName = (name as DirectoryName) ?? name.Parent;
      if (directoryName == null)
        return false;

      DirectoryData directoryData;
      if (!directories.TryGetValue(directoryName, out directoryData))
        return false;

      if (directoryData.IsSymLink)
        return true;

      var parent = directoryName.Parent;
      if (parent == null)
        return false;

      return IsContainedInSymLinkHelper(directories, parent);
    }
Exemple #2
0
        /// <summary>
        /// Validates the directory and expression strings to check that they have no invalid characters, any special DOS wildcard characters in Win32 in the expression get replaced with their proper escaped representation, and if the expression string begins with a directory name, the directory name is moved and appended at the end of the directory string.
        /// </summary>
        /// <param name="directory">A reference to a directory string that we will be checking for normalization.</param>
        /// <param name="expression">A reference to a expression string that we will be checking for normalization.</param>
        /// <param name="matchType">The kind of matching we want to check in the expression. If the value is Win32, we will replace special DOS wild characters to their safely escaped representation. This replacement does not affect the normalization status of the expression.</param>
        /// <returns><cref langword="false" /> if the directory reference string get modified inside this function due to the expression beginning with a directory name. <cref langword="true" /> if the directory reference string was not modified.</returns>
        /// <exception cref="ArgumentException">
        /// The expression is a rooted path.
        /// -or-
        /// The directory or the expression reference strings contain a null character.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The match type is out of the range of the valid MatchType enum values.
        /// </exception>
        internal static bool NormalizeInputs(ref string directory, ref string expression, MatchType matchType)
        {
            if (Path.IsPathRooted(expression))
            {
                throw new ArgumentException(SR.Arg_Path2IsRooted, nameof(expression));
            }

            if (expression.Contains('\0'))
            {
                throw new ArgumentException(SR.Argument_InvalidPathChars, expression);
            }

            if (directory.Contains('\0'))
            {
                throw new ArgumentException(SR.Argument_InvalidPathChars, directory);
            }

            // We always allowed breaking the passed ref directory and filter to be separated
            // any way the user wanted. Looking for "C:\foo\*.cs" could be passed as "C:\" and
            // "foo\*.cs" or "C:\foo" and "*.cs", for example. As such we need to combine and
            // split the inputs if the expression contains a directory separator.
            //
            // We also allowed for expression to be "foo\" which would translate to "foo\*".

            ReadOnlySpan <char> directoryName = Path.GetDirectoryName(expression.AsSpan());

            bool isDirectoryModified = true;

            if (directoryName.Length != 0)
            {
                // Need to fix up the input paths
                directory  = Path.Join(directory.AsSpan(), directoryName);
                expression = expression.Substring(directoryName.Length + 1);

                isDirectoryModified = false;
            }

            switch (matchType)
            {
            case MatchType.Win32:
                if (expression == "*")
                {
                    // Most common case
                    break;
                }
                else if (string.IsNullOrEmpty(expression) || expression == "." || expression == "*.*")
                {
                    // Historically we always treated "." as "*"
                    expression = "*";
                }
                else
                {
                    if (Path.DirectorySeparatorChar != '\\' && expression.IndexOfAny(s_unixEscapeChars) != -1)
                    {
                        // Backslash isn't the default separator, need to escape (e.g. Unix)
                        expression = expression.Replace("\\", "\\\\");

                        // Also need to escape the other special wild characters ('"', '<', and '>')
                        expression = expression.Replace("\"", "\\\"");
                        expression = expression.Replace(">", "\\>");
                        expression = expression.Replace("<", "\\<");
                    }

                    // Need to convert the expression to match Win32 behavior
                    expression = FileSystemName.TranslateWin32Expression(expression);
                }
                break;

            case MatchType.Simple:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(matchType));
            }

            return(isDirectoryModified);
        }
Exemple #3
0
        /// <inheritdoc />
        public FileSystemEnumerable <(string Path, long Length)> EnumerateFiles()
        {
            var options = new EnumerationOptions()
            {
                RecurseSubdirectories = this.recurse,
                AttributesToSkip      = 0,
            };

            FileSystemEnumerable <(string Path, long Length)> files;

            if (this.excludeDir == null)
            {
                files = new FileSystemEnumerable <(string Path, long Length)>(this.SearchPath, (ref FileSystemEntry entry) => (entry.ToFullPath(), entry.Length), options)
                {
                    ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory && FileSystemName.MatchesSimpleExpression(this.searchFile, entry.FileName),
                };
            }
            else
            {
                files = new FileSystemEnumerable <(string Path, long Length)>(this.SearchPath, (ref FileSystemEntry entry) => (entry.ToFullPath(), entry.Length), options)
                {
                    ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory && FileSystemName.MatchesSimpleExpression(this.searchFile, entry.FileName) && !this.excludeDir.IsMatch(entry.Directory.ToString()),
                };
            }

            return(files);
        }
Exemple #4
0
 protected internal override bool Matches(string?input, string?pattern)
 => FileSystemName.MatchesSimpleExpression(pattern, input);
Exemple #5
0
 public static bool MatchesPattern(string searchPattern, string name, bool ignoreCase)
 {
     return(FileSystemName.MatchesSimpleExpression(searchPattern.AsSpan(),
                                                   name.AsSpan(), ignoreCase));
 }
 public static void DosMatch(string expression, string name, bool ignoreCase, bool expected)
 {
     Assert.Equal(expected, FileSystemName.MatchesDosExpression(expression, name.AsReadOnlySpan(), ignoreCase));
 }
Exemple #7
0
 public static void Win32Match(string expression, string name, bool ignoreCase, bool expected)
 {
     Assert.Equal(expected, FileSystemName.MatchesWin32Expression(expression, name.AsSpan(), ignoreCase));
 }
Exemple #8
0
 /// <inheritdoc />
 public bool MatchesSimpleExpression(ReadOnlySpan <char> expression, ReadOnlySpan <char> name, bool ignoreCase = true)
 {
     return(FileSystemName.MatchesSimpleExpression(expression, name, ignoreCase));
 }
Exemple #9
0
 /// <inheritdoc />
 public string TranslateWin32Expression(string?expression)
 {
     return(FileSystemName.TranslateWin32Expression(expression));
 }
Exemple #10
0
 public bool IsContainedInSymLink(FileSystemName name) {
   return IsContainedInSymLinkHelper(_directories, name);
 }
Exemple #11
0
        public static bool IsContainedInSymLinkHelper(IDictionary <DirectoryName, DirectoryData> directories, FileSystemName name)
        {
            var directoryName = (name as DirectoryName) ?? name.Parent;

            if (directoryName == null)
            {
                return(false);
            }

            DirectoryData directoryData;

            if (!directories.TryGetValue(directoryName, out directoryData))
            {
                return(false);
            }

            if (directoryData.IsSymLink)
            {
                return(true);
            }

            var parent = directoryName.Parent;

            if (parent == null)
            {
                return(false);
            }

            return(IsContainedInSymLinkHelper(directories, parent));
        }
Exemple #12
0
 public bool IsContainedInSymLink(FileSystemName name)
 {
     return(IsContainedInSymLinkHelper(_directories, name));
 }