Ejemplo n.º 1
0
        internal static FileSystemFinderResult?MatchFile(
            string path,
            Filter nameFilter,
            Filter extensionFilter,
            FileSystemFinderOptions options,
            IProgress <FileSystemFinderProgress> progress,
            NamePartKind namePartKind)
        {
            if (options.Attributes != 0 &&
                (File.GetAttributes(path) & options.Attributes) != options.Attributes)
            {
                return(null);
            }

            progress?.Report(new FileSystemFinderProgress(path, ProgressKind.File));

            if (extensionFilter?.IsMatch(NamePart.FromFile(path, extensionFilter.NamePart)) == false)
            {
                return(null);
            }

            NamePart namePart = NamePart.FromFile(path, namePartKind);
            Match    match    = null;

            if (nameFilter != null)
            {
                match = (options.PartOnly)
                    ? nameFilter.Regex.Match(namePart.ToString())
                    : nameFilter.Regex.Match(path, namePart.Index, namePart.Length);

                if (!nameFilter.IsMatch(match))
                {
                    return(null);
                }
            }

            if (options.Empty != null)
            {
                try
                {
                    if ((options.Empty.Value)
                        ? !FileSystemHelpers.IsEmptyFile(path)
                        : FileSystemHelpers.IsEmptyFile(path))
                    {
                        return(null);
                    }
                }
                catch (Exception ex) when(ex is IOException ||
                                          ex is UnauthorizedAccessException)
                {
                    progress?.Report(new FileSystemFinderProgress(path, ProgressKind.File, ex));
                    return(null);
                }
            }

            return(new FileSystemFinderResult(namePart, match));
        }
Ejemplo n.º 2
0
        internal static FileSystemFinderResult MatchDirectory(
            string path,
            Filter nameFilter,
            FileSystemFinderOptions options,
            IProgress <FileSystemFinderProgress> progress,
            NamePartKind namePartKind)
        {
            if (options.Attributes != 0 &&
                (File.GetAttributes(path) & options.Attributes) != options.Attributes)
            {
                return(null);
            }

            progress?.Report(new FileSystemFinderProgress(path, ProgressKind.Directory));

            NamePart namePart = NamePart.FromDirectory(path, namePartKind);
            Match    match    = null;

            if (nameFilter != null)
            {
                match = (options.PartOnly)
                    ? nameFilter.Regex.Match(namePart.ToString())
                    : nameFilter.Regex.Match(path, namePart.Index, namePart.Length);

                if (!nameFilter.IsMatch(match))
                {
                    return(null);
                }
            }

            if (options.Empty != null)
            {
                try
                {
                    if ((options.Empty.Value)
                        ? !IsEmptyDirectory(path)
                        : IsEmptyDirectory(path))
                    {
                        return(null);
                    }
                }
                catch (Exception ex) when(ex is IOException ||
                                          ex is UnauthorizedAccessException)
                {
                    progress?.Report(new FileSystemFinderProgress(path, ProgressKind.Directory, ex));
                    return(null);
                }
            }

            return(new FileSystemFinderResult(namePart, match, isDirectory: true));
        }