public void TestFind()
 {
     FindFileOptions o = new FindFileOptions(@"testfiles\files1", new string[] {}, new string[] {});
     var f = new FileMatcher(o);
     var r = new List<string>(f.Filter());
     Assert.Equal(3, r.Count);
 }
Exemple #2
0
        public void Find()
        {
            var files = new FileMatcher(fileOptions).Filter().AsCounted();
            var matches = new LineMatcher(lineOptions).Filter(files).AsCounted();

            IOutputMatches h = new HtmlOutputter(lineOptions.Pattern, fileOptions.Directory, files, matches);
            h.OutputHeader();
            foreach (var match in matches)
                h.OutputMatch(match);
            h.OutputFooter();
        }
Exemple #3
0
        private static (ITaskItem[] Element, FileMatcher.SearchAction Action, string FileSpec) ExpandWildcards(ITaskItem[] expand)
        {
            // Used to detect and log drive enumerating wildcard patterns.
            string[] files;
            FileMatcher.SearchAction action = FileMatcher.SearchAction.None;
            string itemSpec = string.Empty;

            if (expand == null)
            {
                return(null, action, itemSpec);
            }
            else
            {
                var expanded = new List <ITaskItem>();
                foreach (ITaskItem i in expand)
                {
                    if (FileMatcher.HasWildcards(i.ItemSpec))
                    {
                        (files, action, _) = FileMatcher.Default.GetFiles(null /* use current directory */, i.ItemSpec);
                        itemSpec           = i.ItemSpec;
                        if (action == FileMatcher.SearchAction.FailOnDriveEnumeratingWildcard)
                        {
                            return(expanded.ToArray(), action, itemSpec);
                        }

                        foreach (string file in files)
                        {
                            TaskItem newItem = new TaskItem(i)
                            {
                                ItemSpec = file
                            };

                            // Compute the RecursiveDir portion.
                            FileMatcher.Result match = FileMatcher.Default.FileMatch(i.ItemSpec, file);
                            if (match.isLegalFileSpec && match.isMatch)
                            {
                                if (!string.IsNullOrEmpty(match.wildcardDirectoryPart))
                                {
                                    newItem.SetMetadata(FileUtilities.ItemSpecModifiers.RecursiveDir, match.wildcardDirectoryPart);
                                }
                            }

                            expanded.Add(newItem);
                        }
                    }
                    else
                    {
                        expanded.Add(i);
                    }
                }
                return(expanded.ToArray(), action, itemSpec);
            }
        }
        /// <summary>
        /// Used for the purposes of evaluating an item specification. Given a filespec that may include wildcard characters * and
        /// ?, we translate it into an actual list of files. If the input filespec doesn't contain any wildcard characters, and it
        /// doesn't appear to point to an actual file on disk, then we just give back the input string as an array of length one,
        /// assuming that it wasn't really intended to be a filename (as items are not required to necessarily represent files).
        /// Any wildcards passed in that are unescaped will be treated as real wildcards.
        /// The "include" of items passed back from the filesystem will be returned canonically escaped.
        /// The ordering of the list returned is deterministic (it is sorted).
        /// Will never throw IO exceptions: if there is no match, returns the input verbatim.
        /// </summary>
        /// <param name="directoryEscaped">The directory to evaluate, escaped.</param>
        /// <param name="filespecEscaped">The filespec to evaluate, escaped.</param>
        /// <returns>Array of file paths.</returns>
        private static string[] GetFileList
        (
            string directoryEscaped,
            string filespecEscaped,
            bool returnEscaped,
            IEnumerable <string> excludeSpecsUnescaped = null
        )
        {
            ErrorUtilities.VerifyThrowInternalLength(filespecEscaped, "filespecEscaped");

            string[] fileList;

            if (FilespecHasWildcards(filespecEscaped))
            {
                // Unescape before handing it to the filesystem.
                string directoryUnescaped = EscapingUtilities.UnescapeAll(directoryEscaped);
                string filespecUnescaped  = EscapingUtilities.UnescapeAll(filespecEscaped);

                // Get the list of actual files which match the filespec.  Put
                // the list into a string array.  If the filespec started out
                // as a relative path, we will get back a bunch of relative paths.
                // If the filespec started out as an absolute path, we will get
                // back a bunch of absolute paths.
                fileList = FileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped);

                ErrorUtilities.VerifyThrow(fileList != null, "We must have a list of files here, even if it's empty.");

                // Before actually returning the file list, we sort them alphabetically.  This
                // provides a certain amount of extra determinism and reproducability.  That is,
                // we're sure that the build will behave in exactly the same way every time,
                // and on every machine.
                Array.Sort(fileList, StringComparer.OrdinalIgnoreCase);

                if (returnEscaped)
                {
                    // We must now go back and make sure all special characters are escaped because we always
                    // store data in the engine in escaped form so it doesn't interfere with our parsing.
                    // Note that this means that characters that were not escaped in the original filespec
                    // may now be escaped, but that's not easy to avoid.
                    for (int i = 0; i < fileList.Length; i++)
                    {
                        fileList[i] = EscapingUtilities.Escape(fileList[i]);
                    }
                }
            }
            else
            {
                // Just return the original string.
                fileList = new string[] { returnEscaped?filespecEscaped : EscapingUtilities.UnescapeAll(filespecEscaped) };
            }

            return(fileList);
        }
Exemple #5
0
        private static bool IsValidExclude(string exclude)
        {
            // TODO: assumption on legal path characters: https://github.com/Microsoft/msbuild/issues/781
            // Excludes that have both wildcards and non escaped wildcards will never be matched on Windows, because
            // wildcard characters are invalid in Windows paths.
            // Filtering these excludes early keeps the glob expander simpler. Otherwise unescaping logic would reach all the way down to
            // filespec parsing (parse escaped string (to correctly ignore escaped wildcards) and then
            // unescape the path fragments to unfold potentially escaped wildcard chars)
            var hasBothWildcardsAndEscapedWildcards = FileMatcher.HasWildcards(exclude) && EscapingUtilities.ContainsEscapedWildcards(exclude);

            return(!hasBothWildcardsAndEscapedWildcards);
        }
        public void Execute()
        {
            VaultInventory vaultInventory = VaultInventoryParser.Parse(_vaultInventoryFile);

            NasFiles nasFiles = NasBackup.GetFiles(_nasPath);

            IEnumerable <GlacierFile> glacierFiles = vaultInventory.ArchiveList;

            IEnumerable <File> localFiles = nasFiles.Files;

            IEnumerable <GlacierFile> glacierExtraAgeFiles = FileMatcher.GetGlacierExtraFiles(glacierFiles,
                                                                                              localFiles,
                                                                                              _glacierOlderThan);

            var deleteFileResults = new List <GlacierDeleteFileResult>();

            using (var mgr = new ArchiveTransferManager(RegionEndpoint.USWest2))
            {
                foreach (GlacierFile g in glacierExtraAgeFiles)
                {
                    string deleteFailure = null;

                    bool isOldEnough = g.AgeDays > 90;

                    if (isOldEnough)
                    {
                        try
                        {
                            mgr.DeleteArchive(vaultName: "backup-home",
                                              archiveId: g.ArchiveId);
                        }
                        catch (Exception ex)
                        {
                            deleteFailure = ex.Message;
                        }
                    }
                    else
                    {
                        deleteFailure = string.Format("File is not old enough to be deleted. Age: [{0}]", g.AgeDays);
                    }

                    var deleteFileResult = new GlacierDeleteFileResult(g.NormalizedFilePath,
                                                                       deleteFailure,
                                                                       g.SizeBytes);

                    deleteFileResults.Add(deleteFileResult);
                }
            }

            var result = new DeleteGlacierFilesResult(deleteFileResults);

            ConsoleView.Show(result);
        }
Exemple #7
0
        internal static bool FilespecHasWildcards(string filespecEscaped)
        {
            if (!FileMatcher.HasWildcards(filespecEscaped))
            {
                return(false);
            }

            // If the item's Include has both escaped wildcards and real wildcards, then it's
            // not clear what they are asking us to do.  Go to the file system and find
            // files that literally have '*' in their filename?  Well, that's not going to
            // happen because '*' is an illegal character to have in a filename.
            return(!EscapingUtilities.ContainsEscapedWildcards(filespecEscaped));
        }
Exemple #8
0
        public void read_matcher_from_file()
        {
            new FileSystem().WriteStringToFile(FileMatcher.File, @"
*.spark=Application
*.css=Content
");


            var matcher = FileMatcher.ReadFromFile(FileMatcher.File);

            matcher.MatchersFor(FileChangeCategory.Content).ShouldContain(new ExtensionMatch(FileChangeCategory.Content, "*.css"));
            matcher.MatchersFor(FileChangeCategory.Application).ShouldContain(new ExtensionMatch(FileChangeCategory.Application, "*.spark"));
        }
 public Metadata(
     ILogger <Metadata> logger,
     IFileSystem fileSystem,
     FileMatcher fileMatcher,
     OutputRecordWriter writer,
     MetadataRegister register)
 {
     this.logger            = logger;
     this.fileSystem        = fileSystem;
     this.fileMatcher       = fileMatcher;
     this.writer            = writer;
     this.extractorRegister = register;
 }
        /// <summary>
        ///     Parse the given <paramref name="fileSpec" /> into a <see cref="MSBuildGlob" /> using a given
        ///     <paramref name="globRoot" />.
        /// </summary>
        /// <param name="globRoot">
        ///     The root of the glob.
        ///     The fixed directory part of the glob and the match arguments (<see cref="IsMatch" /> and <see cref="MatchInfo" />)
        ///     will get normalized against this root.
        ///     If empty, the current working directory is used.
        ///     Cannot be null, and cannot contain invalid path arguments.
        /// </param>
        /// <param name="fileSpec">The string to parse</param>
        /// <returns></returns>
        public static MSBuildGlob Parse(string globRoot, string fileSpec)
        {
            ErrorUtilities.VerifyThrowArgumentNull(globRoot, nameof(globRoot));
            ErrorUtilities.VerifyThrowArgumentNull(fileSpec, nameof(fileSpec));
            ErrorUtilities.VerifyThrowArgumentInvalidPath(globRoot, nameof(globRoot));

            if (globRoot == string.Empty)
            {
                globRoot = Directory.GetCurrentDirectory();
            }

            globRoot = FileUtilities.NormalizePath(globRoot).WithTrailingSlash();

            var lazyState = new Lazy <GlobState>(() =>
            {
                string fixedDirectoryPart    = null;
                string wildcardDirectoryPart = null;
                string filenamePart          = null;

                string matchFileExpression;
                bool needsRecursion;
                bool isLegalFileSpec;

                FileMatcher.GetFileSpecInfo(
                    fileSpec,
                    out fixedDirectoryPart,
                    out wildcardDirectoryPart,
                    out filenamePart,
                    out matchFileExpression,
                    out needsRecursion,
                    out isLegalFileSpec,
                    FileMatcher.s_defaultGetFileSystemEntries,
                    (fixedDirPart, wildcardDirPart, filePart) =>
                {
                    var normalizedFixedPart = NormalizeTheFixedDirectoryPartAgainstTheGlobRoot(fixedDirPart, globRoot);

                    return(Tuple.Create(normalizedFixedPart, wildcardDirPart, filePart));
                });

                // compile the regex since it's expected to be used multiple times
                var regex = isLegalFileSpec
                    ? new Regex(matchFileExpression, FileMatcher.DefaultRegexOptions | RegexOptions.Compiled)
                    : null;

                return(new GlobState(globRoot, fileSpec, isLegalFileSpec, fixedDirectoryPart, wildcardDirectoryPart, filenamePart, matchFileExpression, needsRecursion, regex));
            },
                                                 true);

            return(new MSBuildGlob(lazyState));
        }
Exemple #11
0
        private bool ShouldSkipEntry(ZipArchiveEntry zipArchiveEntry)
        {
            bool result = false;

            if (_includePatterns.Length > 0)
            {
                result = _includePatterns.All(pattern => !FileMatcher.IsMatch(FileMatcher.Normalize(zipArchiveEntry.FullName), pattern));
            }

            if (_excludePatterns.Length > 0)
            {
                result |= _excludePatterns.Any(pattern => FileMatcher.IsMatch(FileMatcher.Normalize(zipArchiveEntry.FullName), pattern));
            }

            return(result);
        }
Exemple #12
0
        public static IContentMatcher ParseContentMatch(XElement matcherElement)
        {
            switch (matcherElement.Name.LocalName.ToLower())
            {
            case "showmatcher":
                ShowMatcher matcher = GetShowMatcher(matcherElement);
                return(matcher);

            case "filematcher":
                FileMatcher filematcher = GetFileMatcher(matcherElement);
                return(filematcher);

            default:
                throw new NotImplementedException();
            }
        }
Exemple #13
0
        /// <summary>
        ///     Parse the given <paramref name="fileSpec" /> into a <see cref="MSBuildGlob" /> using a given
        ///     <paramref name="globRoot" />.
        /// </summary>
        /// <param name="globRoot">
        ///     The root of the glob.
        ///     The fixed directory part of the glob and the match arguments (<see cref="IsMatch" /> and <see cref="MatchInfo" />)
        ///     will get normalized against this root.
        ///     If empty, the current working directory is used.
        ///     Cannot be null, and cannot contain invalid path arguments.
        /// </param>
        /// <param name="fileSpec">The string to parse</param>
        /// <returns></returns>
        public static MSBuildGlob Parse(string globRoot, string fileSpec)
        {
            ErrorUtilities.VerifyThrowArgumentNull(globRoot, nameof(globRoot));
            ErrorUtilities.VerifyThrowArgumentNull(fileSpec, nameof(fileSpec));
            ErrorUtilities.VerifyThrowArgumentInvalidPath(globRoot, nameof(globRoot));

            if (globRoot == string.Empty)
            {
                globRoot = Directory.GetCurrentDirectory();
            }

            globRoot = FileUtilities.NormalizePath(globRoot).WithTrailingSlash();

            string fixedDirectoryPart    = null;
            string wildcardDirectoryPart = null;
            string filenamePart          = null;

            string matchFileExpression;
            bool   needsRecursion;
            bool   isLegalFileSpec;

            FileMatcher.GetFileSpecInfo(
                fileSpec,
                out fixedDirectoryPart,
                out wildcardDirectoryPart,
                out filenamePart,
                out matchFileExpression,
                out needsRecursion,
                out isLegalFileSpec,
                FileMatcher.s_defaultGetFileSystemEntries,
                (fixedDirPart, wildcardDirPart, filePart) =>
            {
                var normalizedFixedPart = NormalizeTheFixedDirectoryPartAgainstTheGlobRoot(fixedDirPart, globRoot);

                return(Tuple.Create(normalizedFixedPart, wildcardDirPart, filePart));
            });

            return(new MSBuildGlob(
                       globRoot,
                       fileSpec,
                       fixedDirectoryPart,
                       wildcardDirectoryPart,
                       filenamePart,
                       matchFileExpression,
                       needsRecursion,
                       isLegalFileSpec));
        }
Exemple #14
0
        private static Func <string, string, string[]> GetGetFilesFunction()
        {
            var func14 = FileMatcher.GetMethod("GetFiles", BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(string), typeof(string) }, new ParameterModifier[0]);

            if (func14 != null)
            {
                return((Func <string, string, string[]>)func14.CreateDelegate(typeof(Func <string, string, string[]>)));
            }
            var func15 = FileMatcher.GetMethod("GetFiles", BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(string), typeof(string), typeof(IEnumerable <string>) }, new ParameterModifier[0]);

            if (func15 != null)
            {
                var f = (Func <string, string, IEnumerable <string>, string[]>)func15.CreateDelegate(typeof(Func <string, string, IEnumerable <string>, string[]>));
                return((a, b) => f(a, b, Enumerable.Empty <string>()));
            }
            throw new MissingMethodException("Could not find FileMatcher.GetFiles");
        }
Exemple #15
0
        public void Run(string[] args)
        {
            UserVariables userVars      = new ArgumentsParser(args).ParseArguments();
            UserVariables validatedVars = new UserVariablesValidator(userVars).Validate();

            validatedVars.Print();

            var fileNamePool    = new WikiAnalyzer(validatedVars).Analyze();
            var fileSystemFiles = new FolderAnalyzer(validatedVars.Folder).Analyze();

            Dictionary <string, string> renaming = new FileMatcher(fileNamePool, fileSystemFiles).Match();

            new Renamer(renaming).Rename();

            Console.WriteLine("Pres Any Key to Exit...");
            Console.ReadKey();
        }
        private IEnumerable <string> EnumerateFullFileSystemPaths(string path, string searchPattern, bool includeFiles, bool includeDirectories)
        {
            FindPredicate predicate = (ref ReadOnlySpan <char> fileName) =>
            {
                return(FileMatcher.IsAllFilesWildcard(searchPattern) || FileMatcher.IsMatch(fileName, searchPattern));
            };
            FindTransform <string> transform = (ref ReadOnlySpan <char> fileName) => Path.Join(path.AsSpan(), fileName);

            IEnumerable <string> directories = includeDirectories
                ? _directoryCache.EnumerateDirectories(path, searchPattern, predicate, transform)
                : Enumerable.Empty <string>();
            IEnumerable <string> files = includeFiles
                ? _directoryCache.EnumerateFiles(path, searchPattern, predicate, transform)
                : Enumerable.Empty <string>();

            return(Enumerable.Concat(directories, files));
        }
Exemple #17
0
        /// <summary>
        /// Expand wildcards in the item list.
        /// </summary>
        /// <param name="expand"></param>
        /// <returns>Array of items expanded</returns>
        public static ITaskItem[] ExpandWildcards(ITaskItem[] expand)
        {
            if (expand == null)
            {
                return(null);
            }

            var expanded = new List <ITaskItem>(expand.Length);

            foreach (ITaskItem item in expand)
            {
                if (FileMatcher.HasWildcards(item.ItemSpec))
                {
                    string[] files;
                    string   directoryName = Path.GetDirectoryName(item.ItemSpec);
                    string   searchPattern = Path.GetFileName(item.ItemSpec);

                    // Very often with TLog files we're talking about
                    // a directory and a simply wildcarded filename
                    // Optimize for that case here.
                    if (!FileMatcher.HasWildcards(directoryName) && Directory.Exists(directoryName))
                    {
                        files = Directory.GetFiles(directoryName, searchPattern);
                    }
                    else
                    {
                        files = FileMatcher.Default.GetFiles(null, item.ItemSpec);
                    }

                    foreach (string file in files)
                    {
                        expanded.Add(new TaskItem(item)
                        {
                            ItemSpec = file
                        });
                    }
                }
                else
                {
                    expanded.Add(item);
                }
            }
            return(expanded.ToArray());
        }
Exemple #18
0
        /// <summary>
        /// Expand wildcards in the item list.
        /// </summary>
        private static ITaskItem[] ExpandWildcards(ITaskItem[] expand)
        {
            if (expand == null)
            {
                return(null);
            }
            else
            {
                var expanded = new List <ITaskItem>();
                foreach (ITaskItem i in expand)
                {
                    if (FileMatcher.HasWildcards(i.ItemSpec))
                    {
                        string[] files = FileMatcher.Default.GetFiles(null /* use current directory */, i.ItemSpec);
                        foreach (string file in files)
                        {
                            TaskItem newItem = new TaskItem(i)
                            {
                                ItemSpec = file
                            };

                            // Compute the RecursiveDir portion.
                            FileMatcher.Result match = FileMatcher.Default.FileMatch(i.ItemSpec, file);
                            if (match.isLegalFileSpec && match.isMatch)
                            {
                                if (!string.IsNullOrEmpty(match.wildcardDirectoryPart))
                                {
                                    newItem.SetMetadata(FileUtilities.ItemSpecModifiers.RecursiveDir, match.wildcardDirectoryPart);
                                }
                            }

                            expanded.Add(newItem);
                        }
                    }
                    else
                    {
                        expanded.Add(i);
                    }
                }
                return(expanded.ToArray());
            }
        }
Exemple #19
0
        // this method parses the glob and extracts the fixed directory part in order to normalize it and make it absolute
        // without this normalization step, strings pointing outside the globbing cone would still match when they shouldn't
        // for example, we dont want "**/*.cs" to match "../Shared/Foo.cs"
        // todo: glob rooting knowledge partially duplicated with MSBuildGlob.Parse and FileMatcher.ComputeFileEnumerationCacheKey
        private static void CreateRegexOrFilenamePattern(string unescapedFileSpec, string currentDirectory, out string filenamePattern, out Regex regex)
        {
            FileMatcher.Default.SplitFileSpec(
                unescapedFileSpec,
                out string fixedDirPart,
                out string wildcardDirectoryPart,
                out string filenamePart);

            if (FileUtilities.PathIsInvalid(fixedDirPart))
            {
                filenamePattern = null;
                regex           = null;
                return;
            }

            // Most file specs have "**" as their directory specification so we special case these and make matching faster.
            if (string.IsNullOrEmpty(fixedDirPart) && FileMatcher.IsRecursiveDirectoryMatch(wildcardDirectoryPart))
            {
                filenamePattern = filenamePart;
                regex           = null;
                return;
            }

            var absoluteFixedDirPart   = Path.Combine(currentDirectory, fixedDirPart);
            var normalizedFixedDirPart = string.IsNullOrEmpty(absoluteFixedDirPart)
                                         // currentDirectory is empty for some in-memory projects
                ? Directory.GetCurrentDirectory()
                : FileUtilities.GetFullPathNoThrow(absoluteFixedDirPart);

            normalizedFixedDirPart = FileUtilities.EnsureTrailingSlash(normalizedFixedDirPart);

            var recombinedFileSpec = string.Concat(normalizedFixedDirPart, wildcardDirectoryPart, filenamePart);

            FileMatcher.Default.GetFileSpecInfoWithRegexObject(
                recombinedFileSpec,
                out Regex regexObject,
                out bool _,
                out bool isLegal);

            filenamePattern = null;
            regex           = isLegal ? regexObject : null;
        }
Exemple #20
0
        // this method parses the glob and extracts the fixed directory part in order to normalize it and make it absolute
        // without this normalization step, strings pointing outside the globbing cone would still match when they shouldn't
        // for example, we dont want "**/*.cs" to match "../Shared/Foo.cs"
        // todo: glob rooting partially duplicated with MSBuildGlob.Parse
        private static Regex CreateRegex(string unescapedFileSpec, string currentDirectory)
        {
            Regex  regex                 = null;
            string fixedDirPart          = null;
            string wildcardDirectoryPart = null;
            string filenamePart          = null;

            FileMatcher.SplitFileSpec(
                unescapedFileSpec,
                out fixedDirPart,
                out wildcardDirectoryPart,
                out filenamePart,
                FileMatcher.s_defaultGetFileSystemEntries);

            if (FileUtilities.PathIsInvalid(fixedDirPart))
            {
                return(null);
            }

            var absoluteFixedDirPart   = Path.Combine(currentDirectory, fixedDirPart);
            var normalizedFixedDirPart = string.IsNullOrEmpty(absoluteFixedDirPart)
                                         // currentDirectory is empty for some in-memory projects
                ? Directory.GetCurrentDirectory()
                : FileUtilities.GetFullPathNoThrow(absoluteFixedDirPart);

            normalizedFixedDirPart = FileUtilities.EnsureTrailingSlash(normalizedFixedDirPart);

            var recombinedFileSpec = string.Join("", normalizedFixedDirPart, wildcardDirectoryPart, filenamePart);

            bool isRecursive;
            bool isLegal;

            FileMatcher.GetFileSpecInfoWithRegexObject(
                recombinedFileSpec,
                out regex,
                out isRecursive,
                out isLegal,
                FileMatcher.s_defaultGetFileSystemEntries);

            return(isLegal ? regex : null);
        }
Exemple #21
0
        /// <summary>
        /// Extract the value for "RecursiveDir", if any, from the Include.
        /// If there is none, returns an empty string.
        /// </summary>
        /// <remarks>
        /// Inputs to and outputs of this function are all escaped.
        /// </remarks>
        private static string GetRecursiveDirValue(string evaluatedIncludeBeforeWildcardExpansionEscaped, string evaluatedIncludeEscaped)
        {
            // If there were no wildcards, the two strings will be the same, and there is no recursivedir part.
            if (String.Equals(evaluatedIncludeBeforeWildcardExpansionEscaped, evaluatedIncludeEscaped, StringComparison.OrdinalIgnoreCase))
            {
                return(String.Empty);
            }

            // we're going to the file system, so unescape the include value here:
            string evaluatedIncludeBeforeWildcardExpansion = EscapingUtilities.UnescapeAll(evaluatedIncludeBeforeWildcardExpansionEscaped);
            string evaluatedInclude = EscapingUtilities.UnescapeAll(evaluatedIncludeEscaped);

            FileMatcher.Result match = FileMatcher.FileMatch(evaluatedIncludeBeforeWildcardExpansion, evaluatedInclude);

            if (match.isLegalFileSpec && match.isMatch)
            {
                return(EscapingUtilities.Escape(match.wildcardDirectoryPart));
            }

            return(String.Empty);
        }
Exemple #22
0
        /// <summary>
        ///     Similar to <see cref="IsMatch" /> but also provides the match groups for the glob parts
        /// </summary>
        /// <param name="stringToMatch"></param>
        /// <returns></returns>
        public MatchInfoResult MatchInfo(string stringToMatch)
        {
            ErrorUtilities.VerifyThrowArgumentNull(stringToMatch, nameof(stringToMatch));

            if (FileUtilities.PathIsInvalid(stringToMatch) || !IsLegal)
            {
                return(MatchInfoResult.Empty);
            }

            string normalizedInput = NormalizeMatchInput(stringToMatch);

            FileMatcher.GetRegexMatchInfo(
                normalizedInput,
                _state.Value.Regex,
                out bool isMatch,
                out string fixedDirectoryPart,
                out string wildcardDirectoryPart,
                out string filenamePart);

            return(new MatchInfoResult(isMatch, fixedDirectoryPart, wildcardDirectoryPart, filenamePart));
        }
        public void Execute()
        {
            VaultInventory vaultInventory = VaultInventoryParser.Parse(_vaultInventoryFile);

            NasFiles nasFiles = NasBackup.GetFiles(_nasPath);

            IEnumerable <GlacierFile> glacierFiles = vaultInventory.ArchiveList;

            IEnumerable <File> localFiles = nasFiles.Files;

            GlacierFile[] glacierExtraAgeFiles = FileMatcher.GetGlacierExtraFiles(glacierFiles,
                                                                                  localFiles,
                                                                                  _glacierOlderThan)
                                                 .ToArray();

            int glacierExtraFilesCount = glacierExtraAgeFiles.Count();

            var result = new GlacierExtraAgeResult(glacierExtraFilesCount, glacierExtraAgeFiles);

            ConsoleView.Show(result);
        }
Exemple #24
0
        /// <summary>
        /// Expand wildcards in the item list.
        /// </summary>
        /// <param name="expand"></param>
        /// <returns></returns>
        private static ITaskItem[] ExpandWildcards(ITaskItem[] expand)
        {
            if (expand == null)
            {
                return(null);
            }
            else
            {
                ArrayList expanded = new ArrayList();
                foreach (ITaskItem i in expand)
                {
                    if (FileMatcher.HasWildcards(i.ItemSpec))
                    {
                        string[] files = FileMatcher.GetFiles(null /* use current directory */, i.ItemSpec);
                        foreach (string file in files)
                        {
                            TaskItem newItem = new TaskItem((ITaskItem)i);
                            newItem.ItemSpec = file;

                            // Compute the RecursiveDir portion.
                            FileMatcher.Result match = FileMatcher.FileMatch(i.ItemSpec, file);
                            if (match.isLegalFileSpec && match.isMatch)
                            {
                                if (match.wildcardDirectoryPart != null && match.wildcardDirectoryPart.Length > 0)
                                {
                                    newItem.SetMetadata(FileUtilities.ItemSpecModifiers.RecursiveDir, match.wildcardDirectoryPart);
                                }
                            }

                            expanded.Add(newItem);
                        }
                    }
                    else
                    {
                        expanded.Add(i);
                    }
                }
                return((ITaskItem[])expanded.ToArray(typeof(ITaskItem)));
            }
        }
        public static ITaskItem[] ExpandWildcards(ITaskItem[] expand)
        {
            if (expand == null)
            {
                return(null);
            }
            List <ITaskItem> list = new List <ITaskItem>(expand.Length);

            foreach (ITaskItem item in expand)
            {
                if (FileMatcher.HasWildcards(item.ItemSpec))
                {
                    string[] files;
                    string   directoryName = Path.GetDirectoryName(item.ItemSpec);
                    string   fileName      = Path.GetFileName(item.ItemSpec);
                    if (!FileMatcher.HasWildcards(directoryName) && Directory.Exists(directoryName))
                    {
                        files = Directory.GetFiles(directoryName, fileName);
                    }
                    else
                    {
                        files = FileMatcher.GetFiles(null, item.ItemSpec);
                    }
                    foreach (string str3 in files)
                    {
                        TaskItem item2 = new TaskItem(item)
                        {
                            ItemSpec = str3
                        };
                        list.Add(item2);
                    }
                }
                else
                {
                    list.Add(item);
                }
            }
            return(list.ToArray());
        }
Exemple #26
0
        public Main(DirectoryInfo _dir)
        {
            while (!DirIsProjectRoot(_dir))
            {
                _dir = _dir.Parent;
                if (_dir == null)
                {
                    // TODO
                    // Create an exception class to throw here.
                    // It'll get caught somewhere and result in a special interaction with the user
                    // to determine which directory to create the project file in.
                    throw new InvalidOperationException();
                }
            }
            Root = _dir;
            Ini.IniParser.Parse(Path.Combine(Root.FullName, ConfigFileName), ref config);
            var m = new FileMatcher();
            m.ExcludeExecutableFiles = config[""].GetBoolWithDefault("exclude-exec", true);
            if (config.ContainsKey("include"))
            {
                foreach (var i in config["include"].Keys)
                {
                    m.IncludeGlob(i);
                }
            }
            if (config.ContainsKey("exclude"))
            {
                foreach (var e in config["exclude"].Keys)
                {
                    m.ExcludeGlob(e);
                }
            }
            files = m.MatchAll(Root).Select(f => new File(this, f)).ToList();
            Files = new ReadOnlyCollection<File>(files);

            buffers = new BindList<Buffer>();
            buffers.Add(new Buffer());
            Buffers = new ReadOnlyCollection<Buffer>(buffers);
        }
Exemple #27
0
 private void ParsePattern(string pattern, out string[] patterns)
 {
     patterns = Array.Empty <string>();
     if (!string.IsNullOrWhiteSpace(pattern))
     {
         if (FileMatcher.HasPropertyOrItemReferences(pattern))
         {
             // Supporting property references would require access to Expander which is unavailable in Microsoft.Build.Tasks
             Log.LogErrorWithCodeFromResources("Unzip.ErrorParsingPatternPropertyReferences", pattern);
         }
         else if (pattern.IndexOfAny(FileUtilities.InvalidPathChars) != -1)
         {
             Log.LogErrorWithCodeFromResources("Unzip.ErrorParsingPatternInvalidPath", pattern);
         }
         else
         {
             patterns = pattern.Contains(';')
                            ? pattern.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(FileMatcher.Normalize).ToArray()
                            : new[] { pattern };
         }
     }
 }
        internal static Func <string, bool> GetMatchTester(string filespec, string currentDirectory)
        {
            var   unescapedSpec = EscapingUtilities.UnescapeAll(filespec);
            Regex regex         = null;

            // TODO: assumption on file system case sensitivity: https://github.com/Microsoft/msbuild/issues/781

            if (FilespecHasWildcards(filespec))
            {
                Regex regexFileMatch;
                bool  isRecursive;
                bool  isLegal;

                //  TODO: If creating Regex's here ends up being expensive perf-wise, consider how to avoid it in common cases
                FileMatcher.GetFileSpecInfo(
                    unescapedSpec,
                    out regexFileMatch,
                    out isRecursive,
                    out isLegal,
                    FileMatcher.s_defaultGetFileSystemEntries);

                // If the spec is not legal, it doesn't match anything
                regex = isLegal ? regexFileMatch : null;
            }

            return(file =>
            {
                var unescapedFile = EscapingUtilities.UnescapeAll(file);

                // check if there is a regex matching the file
                if (regex != null)
                {
                    return regex.IsMatch(unescapedFile);
                }

                return FileUtilities.ComparePathsNoThrow(unescapedSpec, unescapedFile, currentDirectory);
            });
        }
Exemple #29
0
        private static bool FilespecHasWildcards(string filespecEscaped)
        {
            bool containsEscapedWildcards = EscapingUtilities.ContainsEscapedWildcards(filespecEscaped);
            bool containsRealWildcards    = FileMatcher.HasWildcards(filespecEscaped);

            if (containsEscapedWildcards && containsRealWildcards)
            {
                // Umm, this makes no sense.  The item's Include has both escaped wildcards and
                // real wildcards.  What does he want us to do?  Go to the file system and find
                // files that literally have '*' in their filename?  Well, that's not going to
                // happen because '*' is an illegal character to have in a filename.

                return(false);
            }
            else if (!containsEscapedWildcards && containsRealWildcards)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #30
0
        /// <summary>
        ///     Similar to <see cref="IsMatch" /> but also provides the match groups for the glob parts
        /// </summary>
        /// <param name="stringToMatch"></param>
        /// <returns></returns>
        public MatchInfoResult MatchInfo(string stringToMatch)
        {
            ErrorUtilities.VerifyThrowArgumentNull(stringToMatch, nameof(stringToMatch));

            if (FileUtilities.PathIsInvalid(stringToMatch) || !IsLegal)
            {
                return(MatchInfoResult.Empty);
            }

            string normalizedInput = NormalizeMatchInput(stringToMatch);

            FileMatcher.GetRegexMatchInfo(
                normalizedInput,
                _state.Value.Regex,
                out bool isMatch,
                out string wildcardDirectoryPart,
                out string filenamePart);

            // We don't capture the fixed directory part in the regex but we can infer it from the other two.
            int    fixedDirectoryPartLength = normalizedInput.Length - wildcardDirectoryPart.Length - filenamePart.Length;
            string fixedDirectoryPart       = normalizedInput.Substring(0, fixedDirectoryPartLength);

            return(new MatchInfoResult(isMatch, fixedDirectoryPart, wildcardDirectoryPart, filenamePart));
        }
        public DeploymentService(
            DeployerConfiguration deployerConfiguration,
            ILogger logger,
            [NotNull] IKeyValueConfiguration keyValueConfiguration,
            IWebDeployHelper webDeployHelper,
            Func <DeploymentExecutionDefinition, IIisManager> iisManager,
            NuGetPackageInstaller nugetPackageInstaller,
            IFtpHandlerFactory ftpHandlerFactor)
        {
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (keyValueConfiguration is null)
            {
                throw new ArgumentNullException(nameof(keyValueConfiguration));
            }

            DeployerConfiguration =
                deployerConfiguration ?? throw new ArgumentNullException(nameof(deployerConfiguration));

            _directoryCleaner = new DirectoryCleaner(logger);

            _packageInstaller = new PackageInstaller(logger, deployerConfiguration, keyValueConfiguration);

            _fileMatcher = new FileMatcher(logger);

            _xmlTransformer = new XmlTransformer(logger, _fileMatcher);

            _logger                = logger;
            _webDeployHelper       = webDeployHelper;
            _iisManager            = iisManager;
            _nugetPackageInstaller = nugetPackageInstaller;
            _ftpHandlerFactory     = ftpHandlerFactor;
        }
Exemple #32
0
        /// <summary>
        /// Same as <see cref="IsMatch" /> but the argument is expected to be a normalized path.
        /// </summary>
        public bool IsMatchNormalized(string normalizedFileToMatch)
        {
            Debug.Assert(!string.IsNullOrEmpty(normalizedFileToMatch));

            // We do the matching using one of three code paths, depending on the value of _filenamePattern and _regex.
            if (_regex != null)
            {
                return(_regex.IsMatch(normalizedFileToMatch));
            }

            if (_filenamePattern != null)
            {
                // Check file name first as it's more likely to not match.
                string filename = Path.GetFileName(normalizedFileToMatch);
                if (!FileMatcher.IsMatch(filename, _filenamePattern))
                {
                    return(false);
                }

                return(normalizedFileToMatch.StartsWith(_currentDirectory, StringComparison.OrdinalIgnoreCase));
            }

            return(string.Equals(_unescapedFileSpec, normalizedFileToMatch, StringComparison.OrdinalIgnoreCase));
        }
Exemple #33
0
        /// <summary>
        ///     Parse the given <paramref name="fileSpec" /> into a <see cref="MSBuildGlob" /> using a given
        ///     <paramref name="globRoot" />.
        /// </summary>
        /// <param name="globRoot">
        ///     The root of the glob.
        ///     The fixed directory part of the glob and the match arguments (<see cref="IsMatch" /> and <see cref="MatchInfo" />)
        ///     will get normalized against this root.
        ///     If empty, the current working directory is used.
        ///     Cannot be null, and cannot contain invalid path arguments.
        /// </param>
        /// <param name="fileSpec">The string to parse</param>
        /// <returns></returns>
        public static MSBuildGlob Parse(string globRoot, string fileSpec)
        {
            ErrorUtilities.VerifyThrowArgumentNull(globRoot, nameof(globRoot));
            ErrorUtilities.VerifyThrowArgumentNull(fileSpec, nameof(fileSpec));
            ErrorUtilities.VerifyThrowArgumentInvalidPath(globRoot, nameof(globRoot));

            if (string.IsNullOrEmpty(globRoot))
            {
                globRoot = Directory.GetCurrentDirectory();
            }

            globRoot = Strings.WeakIntern(FileUtilities.NormalizePath(globRoot).WithTrailingSlash());

            var lazyState = new Lazy <GlobState>(() =>
            {
                FileMatcher.Default.GetFileSpecInfo(
                    fileSpec,
                    out string fixedDirectoryPart,
                    out string wildcardDirectoryPart,
                    out string filenamePart,
                    out bool needsRecursion,
                    out bool isLegalFileSpec,
                    (fixedDirPart, wildcardDirPart, filePart) =>
                {
                    var normalizedFixedPart = NormalizeTheFixedDirectoryPartAgainstTheGlobRoot(fixedDirPart, globRoot);

                    return(normalizedFixedPart, wildcardDirPart, filePart);
                });

                Regex regex = null;
                if (isLegalFileSpec)
                {
                    string matchFileExpression = FileMatcher.RegularExpressionFromFileSpec(fixedDirectoryPart, wildcardDirectoryPart, filenamePart);

                    lock (s_regexCache)
                    {
                        s_regexCache.TryGetValue(matchFileExpression, out regex);
                    }

                    if (regex == null)
                    {
                        RegexOptions regexOptions = FileMatcher.DefaultRegexOptions;
                        // compile the regex since it's expected to be used multiple times
                        // For the kind of regexes used here, compilation on .NET Framework tends to be expensive and not worth the small
                        // run-time boost so it's enabled only on .NET Core by default.
#if RUNTIME_TYPE_NETCORE
                        bool compileRegex = true;
#else
                        bool compileRegex = !ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_0);
#endif
                        if (compileRegex)
                        {
                            regexOptions |= RegexOptions.Compiled;
                        }
                        Regex newRegex = new Regex(matchFileExpression, regexOptions);
                        lock (s_regexCache)
                        {
                            if (!s_regexCache.TryGetValue(matchFileExpression, out regex))
                            {
                                s_regexCache[matchFileExpression] = newRegex;
                            }
                        }
                        regex ??= newRegex;
                    }
                }
                return(new GlobState(globRoot, fileSpec, isLegalFileSpec, fixedDirectoryPart, wildcardDirectoryPart, filenamePart, needsRecursion, regex));
            },
Exemple #34
0
        /// <summary>
        /// Processes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public object Process(dynamic args)
        {
            Logger.Info(string.Format("Processing {0} ", args.ToString()));
            logType = args[0].ToString();
            filters = (ICollection)args[1];
            matcher = defaultMatcher;
            aggregator = new FileAggregator();
            aggregator.Matcher = matcher;

            if (matcher == null)
            {
                BaseMessage.ErrorHandler(String.Format(CultureInfo.InvariantCulture, "matcher for {0} logs not found", logType));
            }
            if (aggregator == null)
            {
                BaseMessage.ErrorHandler(String.Format(CultureInfo.InvariantCulture, "aggregator for {0} logs not found", logType));
            }

            if (filters != null && filters.Count > 0)
            {
                matcher.Globs = filterGlobs;
            }

            string tarballPath = aggregator.GenerateTarball();

            string blobstoreId = UploadTarball(tarballPath);

            return new Dictionary<string, string>() { { "blobstore_id", blobstoreId } };
        }