예제 #1
0
 public Glob(string pattern, GlobOptions options = GlobOptions.None)
 {
     this.Pattern = pattern;
     if(options.HasFlag(GlobOptions.Compiled))
     {
         this.Compile();
     }
 }
        /// <summary>
        /// Find path names matching a pattern.
        /// </summary>
        //[return: CastToFalse]
        public static PhpArray glob(Context ctx, string pattern, GlobOptions flags = GlobOptions.None)
        {
            var result = new PhpArray();

            if (!string.IsNullOrEmpty(pattern))
            {
                foreach (var fileName in GetMatches(ctx, pattern, flags))
                {
                    result.Add(fileName);
                }

                if (result.Count == 0 && (flags & GlobOptions.NoCheck) != 0)
                {
                    result.Add(pattern);
                }
            }

            return(result);
        }
예제 #3
0
        public static string[] GetIncludeFiles(string projectPath, string itemType)
        {
            var result        = new List <string>();
            var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");

            var globOptions = new GlobOptions();

            globOptions.Evaluation.CaseInsensitive = false;

            var root = ProjectRootElement.Open(projectPath);

            foreach (var itemGroup in root.ItemGroups)
            {
                if (itemGroup.Condition.Length != 0)
                {
                    continue;
                }

                foreach (var item in itemGroup.Items)
                {
                    if (item.ItemType != itemType)
                    {
                        continue;
                    }

                    string normalizedInclude = item.Include.NormalizePath();

                    var glob = Glob.Parse(normalizedInclude, globOptions);

                    // TODO Check somehow if path has no blob to avoid the following loop...

                    foreach (var existingFile in existingFiles)
                    {
                        if (glob.IsMatch(existingFile))
                        {
                            result.Add(existingFile);
                        }
                    }
                }
            }

            return(result.ToArray());
        }
예제 #4
0
        public void GlobCollection1(GlobOptions options)
        {
            using var directory = TemporaryDirectory.Create();
            directory.CreateEmptyFile("d1/d2/f1.txt");
            directory.CreateEmptyFile("d1/d2/f2.txt");
            directory.CreateEmptyFile("d1/f3.txt");
            directory.CreateEmptyFile("d3/f4.txt");

            var glob = new GlobCollection(
                Glob.Parse("**/*.txt", options),
                Glob.Parse("!d1/*.txt", options));

            TestEvaluate(directory, glob, new[]
            {
                "d1/d2/f1.txt",
                "d1/d2/f2.txt",
                "d3/f4.txt",
            });
        }
예제 #5
0
        private static void Command_KillAll(bool makePersistent = false, string pattern = "*")
        {
            var globOptions = new GlobOptions(); globOptions.Evaluation.CaseInsensitive = true;

            foreach (var ped in World.GetAllPeds())
            {
                if (!ped.Exists() || ped.IsExplosionProof || ped.IsPlayer)
                {
                    continue;
                }
                if (!Glob.Parse(pattern, globOptions).IsMatch(ped.Model.Name))
                {
                    continue;
                }
                ped.Kill();
                if (makePersistent)
                {
                    ped.MakePersistent();
                }
            }
        }
예제 #6
0
        public static PhpArray Glob(string pattern, GlobOptions flags)
        {
            if (pattern == null)
            {
                return(new PhpArray(0, 0));
            }

            PhpArray result = new PhpArray();

            foreach (var fileName in GetMatches(pattern, flags))
            {
                result.Add(fileName);
            }

            if (result.Count == 0 && (flags & GlobOptions.NoCheck) != 0)
            {
                result.Add(pattern);
            }

            return(result);
        }
예제 #7
0
        public Matcher(StringComparison stringComparison)
        {
            _excludePatterns = new Collection <string>();
            _includePatterns = new Collection <string>();

            _globOptions = new GlobOptions();
            switch (stringComparison)
            {
            case StringComparison.CurrentCultureIgnoreCase:
            case StringComparison.InvariantCultureIgnoreCase:
            case StringComparison.OrdinalIgnoreCase:
                _globOptions.Evaluation.CaseInsensitive = true;
                break;

            default:
            case StringComparison.CurrentCulture:
            case StringComparison.InvariantCulture:
            case StringComparison.Ordinal:
                _globOptions.Evaluation.CaseInsensitive = false;
                break;
            }
        }
예제 #8
0
        public static PhpArray glob(Context ctx, string pattern, GlobOptions flags = GlobOptions.None)
        {
            if ((flags & ~GlobOptions.SupportedMask) != 0)
            {
                PhpException.InvalidArgument(nameof(flags), string.Format(Resources.Resources.glob_invalid_flags, (flags & ~GlobOptions.SupportedMask).ToString()));
                return(null); // FALSE
            }

            if (string.IsNullOrEmpty(pattern))
            {
                return(PhpArray.NewEmpty());
            }

            var result = new PhpArray();

            result.AddRange(GetMatches(ctx, pattern, flags));

            if (result.Count == 0 && (flags & GlobOptions.NoCheck) != 0)
            {
                result.Add(pattern);
            }

            return(result);
        }
예제 #9
0
파일: Glob.cs 프로젝트: piksel/nuke
 public static IEnumerable <DirectoryInfo> Directories(DirectoryInfo directory, string pattern, GlobOptions options)
 {
     throw new NotSupportedException();
 }
예제 #10
0
 public static IEnumerable <DirectoryInfo> GlobDirectories(this DirectoryInfo di, string pattern, GlobOptions options)
 {
     return(Glob.Directories(di, pattern, options));
 }
예제 #11
0
 public static IEnumerable <FileSystemInfo> GlobFileSystemInfos(this DirectoryInfo di, string pattern, GlobOptions options)
 {
     return(Glob.FilesAndDirectories(di, pattern, options));
 }
예제 #12
0
        /// <summary>
        /// Gets all files matching glob pattern.
        /// See: https://github.com/kthompson/glob for supported pattern expressions and use cases.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="directory"></param>
        /// <param name="globPattern"></param>
        /// <returns></returns>
        public List <FileFullPath> GetFiles(GlobOptions globOptions = GlobOptions.None, params string[] globPattern)
        {
            var directoryInfo = new DirectoryInfo(_fullPath);

            return(globPattern.SelectMany(pattern => Glob.Files(directoryInfo, pattern, globOptions)).Select(x => new FileFullPath(x.FullName)).ToList());
        }
예제 #13
0
        internal static IEnumerable <string> /*!*/ GetMatches(Context ctx, string /*!*/ pattern, GlobOptions flags)
        {
            if (string.IsNullOrEmpty(pattern))
            {
                yield break;
            }

            var noEscape = (flags & GlobOptions.NoEscape) != 0;
            var brace    = (flags & GlobOptions.Brace) != 0;

            var groups = UngroupGlobs(pattern, noEscape, brace);

            foreach (string group in groups)
            {
                var matcher = new GlobMatcher(ctx, group, flags);

                foreach (string filename in matcher.DoGlob())
                {
                    yield return(CurrentPlatform.NormalizeSlashes(filename));
                }
            }
        }
예제 #14
0
        public static IEnumerable <string> FilesAndDirectories(string workingDirectory, string pattern, GlobOptions options)
        {
            var directoryInfo  = new DirectoryInfo(workingDirectory);
            var truncateLength = GetTruncateLength(directoryInfo);

            return(FilesAndDirectories(directoryInfo, pattern, options).Select(info => info.FullName.Remove(0, truncateLength)));
        }
예제 #15
0
        /// <summary>Returns an enumerable collection of <see cref="UsnEntry"/> entries that meet specified criteria.</summary>
        /// <param name="filter">The filter.</param>
        /// <param name="onlyFiles">If gets only the file entries.</param>
        public IEnumerable <UsnEntry> EnumerateUsnEntries(string filter, bool?onlyFiles)
        {
            var usnState = new USN_JOURNAL_DATA_V0();

            if (QueryUsnJournal(ref usnState) != (int)UsnJournalReturnCode.USN_JOURNAL_SUCCESS)
            {
                throw new Win32Exception("Failed to query the USN journal on the volume.");
            }

            // Set up MFT_ENUM_DATA_V0 structure.
            var mftData = new MFT_ENUM_DATA_V0
            {
                StartFileReferenceNumber = 0,
                LowUsn  = 0,
                HighUsn = usnState.NextUsn
            };

            var mftDataSize   = Marshal.SizeOf(mftData);
            var mftDataBuffer = Marshal.AllocHGlobal(mftDataSize);

            Win32Api.ZeroMemory(mftDataBuffer, mftDataSize);
            Marshal.StructureToPtr(mftData, mftDataBuffer, true);


            // Set up the data buffer which receives the USN_RECORD data.
            const int pDataSize = sizeof(ulong) + 10000;
            var       pData     = Marshal.AllocHGlobal(pDataSize);

            Win32Api.ZeroMemory(pData, pDataSize);


            // Gather up volume's directories.
            while (Win32Api.DeviceIoControl(
                       _usnJournalRootHandle,
                       Win32Api.FSCTL_ENUM_USN_DATA,
                       mftDataBuffer,
                       mftDataSize,
                       pData,
                       pDataSize,
                       out var outBytesReturned,
                       IntPtr.Zero))
            {
                var pUsnRecord = new IntPtr(pData.ToInt64() + sizeof(long));

                // While there is at least one entry in the USN journal.
                while (outBytesReturned > 60)
                {
                    var usnEntry = new UsnEntry(pUsnRecord);

                    switch (onlyFiles)
                    {
                    case true when usnEntry.IsFolder:
                    case false when !usnEntry.IsFolder:
                    {
                        pUsnRecord        = new IntPtr(pUsnRecord.ToInt64() + usnEntry.RecordLength);
                        outBytesReturned -= usnEntry.RecordLength;
                        continue;
                    }
                    }

                    if (string.IsNullOrWhiteSpace(filter))
                    {
                        yield return(usnEntry);
                    }
                    else
                    {
                        var options = new GlobOptions {
                            Evaluation = { CaseInsensitive = true }
                        };
                        var glob = Glob.Parse(filter, options);
                        if (glob.IsMatch(usnEntry.Name.AsSpan()))
                        {
                            yield return(usnEntry);
                        }
                    }

                    pUsnRecord        = new IntPtr(pUsnRecord.ToInt64() + usnEntry.RecordLength);
                    outBytesReturned -= usnEntry.RecordLength;
                }

                Marshal.WriteInt64(mftDataBuffer, Marshal.ReadInt64(pData, 0));
            }

            Marshal.FreeHGlobal(pData);
        }
예제 #16
0
        public static IEnumerable <string> FilesAndDirectories(string workingDirectory, string pattern, GlobOptions options)
        {
            var directoryInfo  = new DirectoryInfo(workingDirectory);
            var truncateLength = GetTruncateLength(directoryInfo);

            return(FilesAndDirectories(directoryInfo, pattern, options).Select(RemovePrefix <FileSystemInfo>(truncateLength)));
        }
예제 #17
0
        public IEnumerable <UsnEntry> ReadUsnEntries(USN_JOURNAL_DATA_V0 previousUsnState, uint reasonMask, string filter, bool?onlyFiles)
        {
            var newUsnState = new USN_JOURNAL_DATA_V0();
            var lastError   = (int)UsnJournalReturnCode.VOLUME_NOT_NTFS;

            if (_isNtfsVolume)
            {
                if (_usnJournalRootHandle.ToInt64() != Win32Api.INVALID_HANDLE_VALUE)
                {
                    // Get current USN journal state.
                    lastError = QueryUsnJournal(ref newUsnState);
                    if (lastError == (int)UsnJournalReturnCode.USN_JOURNAL_SUCCESS)
                    {
                        var bReadMore = true;

                        // Sequentially process the USN journal looking for image file entries.
                        const int pbDataSize = sizeof(ulong) * 16384;
                        var       pbData     = Marshal.AllocHGlobal(pbDataSize);
                        Win32Api.ZeroMemory(pbData, pbDataSize);

                        var rujd = new READ_USN_JOURNAL_DATA_V0
                        {
                            StartUsn          = (ulong)previousUsnState.NextUsn,
                            ReasonMask        = reasonMask,
                            ReturnOnlyOnClose = 0,
                            Timeout           = 0,
                            BytesToWaitFor    = 0,
                            UsnJournalId      = previousUsnState.UsnJournalID
                        };

                        var sizeRujd   = Marshal.SizeOf(rujd);
                        var rujdBuffer = Marshal.AllocHGlobal(sizeRujd);
                        Win32Api.ZeroMemory(rujdBuffer, sizeRujd);
                        Marshal.StructureToPtr(rujd, rujdBuffer, true);

                        // Read USN journal entries.
                        while (bReadMore)
                        {
                            var bRtn = Win32Api.DeviceIoControl(_usnJournalRootHandle, Win32Api.FSCTL_READ_USN_JOURNAL, rujdBuffer, sizeRujd, pbData, pbDataSize, out var outBytesReturned, IntPtr.Zero);
                            if (bRtn)
                            {
                                var pUsnRecord = new IntPtr(pbData.ToInt64() + sizeof(ulong));

                                // While there is at least one entry in the USN journal.
                                while (outBytesReturned > 60)
                                {
                                    var usnEntry = new UsnEntry(pUsnRecord);

                                    // Only read until the current usn points beyond the current state's USN.
                                    if (usnEntry.USN >= newUsnState.NextUsn)
                                    {
                                        bReadMore = false;
                                        break;
                                    }

                                    switch (onlyFiles)
                                    {
                                    case true when usnEntry.IsFolder:
                                    case false when !usnEntry.IsFolder:
                                    {
                                        pUsnRecord        = new IntPtr(pUsnRecord.ToInt64() + usnEntry.RecordLength);
                                        outBytesReturned -= usnEntry.RecordLength;
                                        continue;
                                    }
                                    }

                                    if (string.IsNullOrWhiteSpace(filter))
                                    {
                                        yield return(usnEntry);
                                    }
                                    else
                                    {
                                        var options = new GlobOptions {
                                            Evaluation = { CaseInsensitive = true }
                                        };
                                        var glob = Glob.Parse(filter, options);
                                        if (glob.IsMatch(usnEntry.Name.AsSpan()))
                                        {
                                            yield return(usnEntry);
                                        }
                                    }

                                    pUsnRecord        = new IntPtr(pUsnRecord.ToInt64() + usnEntry.RecordLength);
                                    outBytesReturned -= usnEntry.RecordLength;
                                }
                            }

                            else
                            {
                                var lastWin32Error = Marshal.GetLastWin32Error();
                                if (lastWin32Error == (int)Win32Errors.ERROR_HANDLE_EOF)
                                {
                                    lastError = (int)UsnJournalReturnCode.USN_JOURNAL_SUCCESS;
                                }

                                break;
                            }

                            var nextUsn = Marshal.ReadInt64(pbData, 0);
                            if (nextUsn >= newUsnState.NextUsn)
                            {
                                break;
                            }

                            Marshal.WriteInt64(rujdBuffer, nextUsn);
                        }

                        Marshal.FreeHGlobal(rujdBuffer);
                        Marshal.FreeHGlobal(pbData);
                    }
                }
                else
                {
                    lastError = (int)UsnJournalReturnCode.INVALID_HANDLE_VALUE;
                }
            }
        }
예제 #18
0
        /// <summary>
        /// Gets all directories matching glob pattern.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="directory"></param>
        /// <param name="globPattern"></param>
        /// <returns></returns>
        public static List <FullPath> GetDirectories(string directory, GlobOptions globOptions = GlobOptions.None, params string[] globPattern)
        {
            var directoryInfo = new DirectoryInfo(directory);

            return(globPattern.SelectMany(pattern => Glob.Directories(directoryInfo, pattern, globOptions)).Select(x => new FullPath(x.FullName)).ToList());
        }
예제 #19
0
 public static bool IsMatch(string input, string pattern, GlobOptions options = GlobOptions.None) =>
 new Glob(pattern, options).IsMatch(input);
예제 #20
0
 private static bool TryParse(ReadOnlySpan <char> pattern, GlobOptions options, [NotNullWhen(true)] out Glob?result, [NotNullWhen(false)] out string?errorMessage)
 {
     return(GlobParser.TryParse(pattern, options, out result, out errorMessage));
 }
예제 #21
0
 public static bool TryParse(ReadOnlySpan <char> pattern, GlobOptions options, [NotNullWhen(true)] out Glob?result)
 {
     return(TryParse(pattern, options, out result, out _));
 }
예제 #22
0
 /// <summary>
 /// Gets all directories matching glob pattern.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="directory"></param>
 /// <param name="globPattern"></param>
 /// <returns></returns>
 public static List <FullPath> GetDirectories(this IBuildPropertiesContext context, string directory, GlobOptions globOptions = GlobOptions.None, params string[] globPattern)
 {
     return(ContextBaseExtensions.GetDirectories(directory, globOptions, globPattern));
 }
예제 #23
0
        internal static IEnumerable <string> /*!*/ GetMatches(Context ctx, string /*!*/ pattern, GlobOptions flags)
        {
            if (pattern.Length == 0)
            {
                yield break;
            }

            bool noEscape = ((flags & GlobOptions.NoEscape) != 0);
            bool brace    = ((flags & GlobOptions.Brace) != 0);

            string[] groups = UngroupGlobs(pattern, noEscape, brace);
            if (groups.Length == 0)
            {
                yield break;
            }

            foreach (string group in groups)
            {
                GlobMatcher matcher = new GlobMatcher(ctx, group, flags);
                foreach (string filename in matcher.DoGlob())
                {
                    yield return(filename.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
                }
            }
        }
예제 #24
0
        public static PhpArray Glob(string pattern, GlobOptions flags)
        {
            if (pattern == null)
				return new PhpArray(0, 0);


            PhpArray result = new PhpArray();
            foreach (var fileName in GetMatches(pattern, flags))
            {
                result.Add(fileName);
            }

			if (result.Count == 0 && (flags & GlobOptions.NoCheck) != 0) 
                result.Add(pattern);

            return result;
        }
예제 #25
0
 public static Glob Parse(string pattern, GlobOptions options)
 {
     return(Parse(pattern.AsSpan(), options));
 }
예제 #26
0
        public static void AddDirectoryFilterGlob(this FilterOptions options, string globPattern, GlobOptions globOptions = GlobOptions.None, bool negateFilter = false)
        {
            var filter = new GlobFilter(globPattern, globOptions);

            if (negateFilter)
            {
                filter.NegateFilter();
            }

            options.AddDirectoryFilters(filter);
        }
예제 #27
0
 public GlobFilter(string pattern, GlobOptions globOptions = GlobOptions.None)
 {
     _pattern     = pattern;
     _globOptions = globOptions;
 }
예제 #28
0
 public static bool TryParse(string pattern, GlobOptions options, [NotNullWhen(true)] out Glob?result)
 {
     return(TryParse(pattern.AsSpan(), options, out result));
 }
예제 #29
0
 public static IEnumerable <DirectoryInfo> Directories(DirectoryInfo workingDirectory, string pattern, GlobOptions options) =>
 workingDirectory
 .Traverse(pattern, !options.HasFlag(GlobOptions.CaseInsensitive), false, true)
 .OfType <DirectoryInfo>();
예제 #30
0
            public GlobMatcher(string/*!*/ pattern, GlobOptions flags)
            {
                _pattern = FileSystemUtils.CanonicalizePath(pattern);
                _flags = flags;
                _result = new List<string>();
                _dirOnly = _pattern.LastCharacter() == '/' || (flags & GlobOptions.OnlyDir) != 0;

                _fnMatchFlags = NoEscapes ? FnMatchOptions.NoEscape : FnMatchOptions.None;
            }
예제 #31
0
 public static IEnumerable <FileSystemInfo> FilesAndDirectories(DirectoryInfo workingDirectory, string pattern, GlobOptions options) =>
 workingDirectory.Traverse(pattern, !options.HasFlag(GlobOptions.CaseInsensitive), true, true);
예제 #32
0
        internal static IEnumerable<string>/*!*/ GetMatches(string/*!*/ pattern, GlobOptions flags) 
        {
            if (pattern.Length == 0) {
                yield break;
            }

            bool noEscape = ((flags & GlobOptions.NoEscape) != 0);
            bool brace = ((flags & GlobOptions.Brace) != 0);

            string[] groups = UngroupGlobs(pattern, noEscape, brace);
            if (groups.Length == 0) {
                yield break;
            }

            foreach (string group in groups) {
                GlobMatcher matcher = new GlobMatcher(group, flags);
                foreach (string filename in matcher.DoGlob()) {                 
                    yield return filename;
                }
            }            
        }