Beispiel #1
0
        public void VerifyMinimatcherSearchPatternMatch()
        {
            var matcher = new Minimatcher("/test/*", new Minimatch.Options {
                IgnoreCase = true
            });


            Assert.IsTrue(matcher.IsMatch("/test/dummy.csv"));
            Assert.IsTrue(matcher.IsMatch("/TEST/dummy.csv"));
            Assert.IsFalse(matcher.IsMatch("/second/dummy.csv"));
        }
        public string GetSchemaFor(string fileLocation)
        {
            foreach (string url in _schemas.Keys)
            {
                try
                {
                    if (_schemas[url] == null)
                    {
                        continue;
                    }
                    foreach (string file in _schemas[url])
                    {
                        var pattern = "**/" + file.TrimStart('*', '/');

                        var matche = new Minimatcher(pattern, _options);
                        if (matche.IsMatch(fileLocation))
                        {
                            return(url);
                        }
                    }
                }
                catch
                {
                    Logger.Log("JSON Schema: Couldn't parse " + _schemas[url] + " as a valid regex.");
                }
            }

            return(null);
        }
Beispiel #3
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            if (!environment.ContainsKey("owin.RequestPath"))
            {
                throw new ApplicationException("Invalid OWIN request. Expected owin.RequestPath, but not present.");
            }

            var path = Uri.UnescapeDataString((string)environment["owin.RequestPath"]);

            if (statelessAuthOptions != null)
            {
                foreach (var ignorePath in statelessAuthOptions.IgnorePaths)
                {
                    var mm = new Minimatcher(ignorePath, new Options()
                    {
                        IgnoreCase = true
                    });

                    if (mm.IsMatch(path))
                    {
                        return(nextFunc(environment));
                    }
                }
            }

            var requestHeaders = (IDictionary <string, string[]>)environment["owin.RequestHeaders"];

            if (!requestHeaders.ContainsKey("Authorization"))
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            var token = requestHeaders["Authorization"].FirstOrDefault();

            if (string.IsNullOrWhiteSpace(token))
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            var validatedUser = tokenValidator.ValidateUser(token);

            if (validatedUser == null)
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            if (environment.ContainsKey(ServerUser))
            {
                environment[ServerUser] = validatedUser;
            }
            else
            {
                environment.Add(ServerUser, validatedUser);
            }

            return(nextFunc(environment));
        }
Beispiel #4
0
        public Vehicle[] Search(SearchCtx ctx)
        {
            Minimatcher mm        = GetMinimatcher(ctx);
            var         vehicles  = JoinVehicles();
            var         typesById = TypesById();
            var         found     = vehicles
                                    .Where(a => ctx.SelectedType == "1" ||
                                           a.Typename == typesById[ctx.SelectedType])
                                    .Where(a => !ctx.OnlyToday ||
                                           DateTime.Now.Day == a.checkInDate.Day)
                                    .Where(a => mm == null ||
                                           (a.RegNr != null && mm.IsMatch(a.RegNr)) ||
                                           (a.OwnerName != null && mm.IsMatch(a.OwnerName)));

            return(found.ToArray());
        }
        public bool IsMatch(string path)
        {
            if (path != "/")
            {
                return(_matcher.IsMatch(path.TrimStart('/')));
            }

            return(false);
        }
        private IEnumerable <DbvItemBase> FilterDeleted(IEnumerable <DbvItemBase> source, string itemMask)
        {
            var matcher = new Minimatcher(string.IsNullOrEmpty(itemMask) ? "**" : itemMask, new Options {
                IgnoreCase = true, MatchBase = true
            });

            // code duplication with the above
            return(source.Where(item => matcher.IsMatch(item.Name))
                   .Where(item => !IsDeleted(item)));
        }
        public IEnumerable <DbvItemBase> History(string itemMask)
        {
            var matcher = new Minimatcher(string.IsNullOrEmpty(itemMask) ? "**" : itemMask, new Options {
                IgnoreCase = true, MatchBase = true
            });

            return(_revisionStore.List(0)
                   .Where(item => matcher.IsMatch(item.Name))
                   .OrderBy(k => k.Revision));
        }
        public void TestIssue3()
        {
            var filterOptions = new Options
            {
                AllowWindowsPaths = true,
                IgnoreCase = true
            };

            var minimatcher = new Minimatcher("**/Thumbs.db", filterOptions);

            Assert.IsFalse(minimatcher.IsMatch(@"NewFolder\"));
        }
Beispiel #9
0
        public void IncrementAndUpdateAll()
        {
            b.Verbose.Log("IncrementAndUpdateAll called");
            ValidateForUpdate();
            LoadVersioningComponent();
            b.Verbose.Log("Versioning Loaded ");
            ver.Increment();
            b.Verbose.Log("Saving");
            SaveVersioningComponent();
            b.Verbose.Log($"Searching {BaseSearchDir} there are {pendingUpdates.Count} pends.");

            var  enumer         = Directory.EnumerateFiles(BaseSearchDir, "*.*", SearchOption.AllDirectories).GetEnumerator();
            bool shouldContinue = true;

            while (shouldContinue)
            {
                try {
                    shouldContinue = enumer.MoveNext();
                    if (shouldContinue)
                    {
                        var v = enumer.Current;

                        // Check every file that we have returned.
                        foreach (var chk in pendingUpdates.Keys)
                        {
                            var mm = new Minimatcher(chk, new Options {
                                AllowWindowsPaths = true, IgnoreCase = true
                            });
                            b.Verbose.Log($"Checking {chk} against {v}");
                            if (mm.IsMatch(v))
                            {
                                b.Info.Log("Match...");
                                // TODO Cache this and make it less loopey
                                VersionFileUpdater sut = new VersionFileUpdater(ver);
                                foreach (var updateType in pendingUpdates[chk])
                                {
                                    b.Verbose.Log($"Perform update {v}");
                                    sut.PerformUpdate(v, updateType);
                                }
                            }
                        }
                    }
                } catch (System.UnauthorizedAccessException) {
                    // If you run through all the filles in a directory you can hit areas of the filesystem
                    // that you dont have access to - this skips those files and then continues.
                    b.Verbose.Log("Unauthorised area of the filesystem, skipping");
                }
            }

            VersionString = ver.GetVersionString();
        }
        private static void CheckTheseMatches(List <Tuple <string, bool> > mtchs, string againstThis)
        {
            var mm = new Minimatcher(againstThis, new Options {
                AllowWindowsPaths = true, IgnoreCase = true
            });
            int i = 0;

            foreach (var v in mtchs)
            {
                i++;
                bool isMatch = mm.IsMatch(v.Item1);
                Assert.Equal(v.Item2, isMatch); //, "Mismatch " + i.ToString());
            }
        }
        public void MiniMatchSyntax_FindAssemblyInfo()
        {
            var mtchs = new List <Tuple <string, bool> >();

            mtchs.Add(new Tuple <string, bool>(@"C:\temp\te st\properties\assemblyinfo.cs", true));
            mtchs.Add(new Tuple <string, bool>(@"C:\te mp\test\assemblyinfo.cs", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\te mp\t e s t\properties\notassemblyinfo.cs", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\temp\test\properties\assemblyinfo.cs.txt", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\a\1\s\PliskyLibrary\PliskyLib\Properties\AssemblyInfo.cs", true));
            string againstThis = @"**\properties\assemblyinfo.cs";

            CheckTheseMatches(mtchs, againstThis);

            var mm2 = new Minimatcher(@"C:\temp\test\testfile.tst", new Options {
                AllowWindowsPaths = true
            });

            Assert.True(mm2.IsMatch(@"C:\temp\test\testfile.tst"), "Cant match on full filename");
        }
Beispiel #12
0
        private bool CheckAndUpdate(string fl, Minimatcher assemblyMM, string versionValue, Action <string, string> p)
        {
            b.Assert.True(p != null, "The action used cant be null");

            b.Verbose.Log("Checking file :" + fl);

            bool result = assemblyMM.IsMatch(fl);

            if ((result) && (File.Exists(fl)))
            {
                b.Info.Log($"Updating VersioningFile File ({fl}) to ({versionValue})");

                hook?.PreUpdateFileAction(fl); // PreUpdateAllAction?.Invoke(fl);
                //PreUpdateAction?.Invoke(fl);

                p(fl, versionValue);

                hook?.PostUpdateFileAction(fl);
            }
            return(result);
        }
        public void IncrementAndUpdateAll()
        {
            Bilge.VerboseLog("IncrementAndUpdateAll called");
            ValidateForUpdate();
            LoadVersioningComponent();

            Bilge.VerboseLog("Versioning Loaded Ver at " + ver.ToString());
            ver.PerformIncrement();
            Bilge.VerboseLog("Saving Ver At " + ver.ToString());
            SaveVersioningComponent();
            Bilge.VerboseLog($"Searching {BaseSearchDir} there are {pendingUpdates.Count} pends.");
            foreach (var v in Directory.EnumerateFiles(BaseSearchDir, "*.*", SearchOption.AllDirectories))
            {
                // Check every file that we have returned.
                foreach (var chk in pendingUpdates.Keys)
                {
                    var mm = new Minimatcher(chk, new Options {
                        AllowWindowsPaths = true, IgnoreCase = true
                    });

                    // Dump out every file considered. Bilge.VerboseLog($"Checking {chk} against {v}");

                    if (mm.IsMatch(v))
                    {
                        Bilge.Log($"Match...{chk}", v);
                        // TODO Cache this and make it less loopey
                        VersionFileUpdater vfu = new VersionFileUpdater(ver);
                        foreach (var updateType in pendingUpdates[chk])
                        {
                            Bilge.VerboseLog($"Perform update {v} using {updateType.ToString()} as {(int)updateType}");
                            vfu.PerformUpdate(v, updateType);
                        }
                    }
                }
            }
        }
Beispiel #14
0
        public void Start()
        {
            var filesMinimatched = new List <string>();
            var baseFolderLength = Arguments.BaseFolder.Length;
            var relativeFiles    = Directory.EnumerateFiles(Arguments.BaseFolder, "*.*", SearchOption.AllDirectories)
                                   .Select(f => f.Substring(baseFolderLength));

            // Search files matching the Pattern
            foreach (string file in relativeFiles)
            {
                if (Minimatcher.IsMatch(file))
                {
                    filesMinimatched.Add(file);
                }
            }

            // Read and replace
            foreach (var relativeFile in filesMinimatched)
            {
                var filename = Arguments.BaseFolder + relativeFile;
                var file     = new File(filename);

                string newContent = Regex.Replace(file.Content, Arguments.Find, Arguments.Replace, RegexOptions.IgnoreCase);

                if (newContent != file.Content)
                {
                    if (!Arguments.IsDemoMode)
                    {
                        file.WriteAllText(newContent);
                    }

                    this.FilesMatched.Add(relativeFile);
                    Logger?.Invoke(relativeFile, newContent);
                }
            }
        }
        /// <inheritdoc/>
        public override async Task<FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(command.Argument))
            {
                var taskStates = Server.GetBackgroundTaskStates();
                var statusMessage = new StringBuilder();
                statusMessage.AppendFormat("Server functional, {0} open connections", Server.Statistics.ActiveConnections);
                if (taskStates.Count != 0)
                    statusMessage.AppendFormat(", {0} active background transfers", taskStates.Count);
                return new FtpResponse(211, statusMessage.ToString());
            }

            var mask = command.Argument;
            if (!mask.EndsWith("*"))
                mask += "*";

            var mmOptions = new Options()
            {
                IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"),
                NoGlobStar = true,
                Dot = true,
            };

            var mm = new Minimatcher(mask, mmOptions);

            var formatter = new LongListFormatter();
            await Connection.WriteAsync($"211-STAT {command.Argument}", cancellationToken);

            foreach (var entry in (await Data.FileSystem.GetEntriesAsync(Data.CurrentDirectory, cancellationToken)).Where(x => mm.IsMatch(x.Name)))
            {
                var line = formatter.Format(entry, entry.Name);
                Connection.Log?.Debug(line);
                await Connection.WriteAsync($" {line}", cancellationToken);
            }

            return new FtpResponse(211, "STAT");
        }
 /// <inheritdoc/>
 public Task <bool> Accept(HttpContext context) =>
 Task.FromResult(
     !String.IsNullOrEmpty(_pattern) &&
     _matcher.IsMatch(context.Request.Path)
     );
Beispiel #17
0
        /// <inheritdoc/>
        public override async Task <FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
        {
            await Connection.WriteAsync(new FtpResponse(150, "Opening data connection."), cancellationToken);

            ITcpSocketClient responseSocket;

            try
            {
                responseSocket = await Connection.CreateResponseSocket();
            }
            catch (Exception)
            {
                return(new FtpResponse(425, "Can't open data connection."));
            }
            try
            {
                // Parse arguments in a way that's compatible with broken FTP clients
                var argument   = new ListArguments(command.Argument);
                var showHidden = argument.All;

                // Instantiate the formatter
                IListFormatter formatter;
                if (string.Equals(command.Name, "NLST", StringComparison.OrdinalIgnoreCase))
                {
                    formatter = new ShortListFormatter();
                }
                else if (string.Equals(command.Name, "LS", StringComparison.OrdinalIgnoreCase))
                {
                    formatter = new LongListFormatter();
                }
                else
                {
                    formatter = new LongListFormatter();
                }

                // Parse the given path to determine the mask (e.g. when information about a file was requested)
                var directoriesToProcess = new Queue <DirectoryQueueItem>();

                // Use braces to avoid the definition of mask and path in the following parts
                // of this function.
                {
                    var mask = "*";
                    var path = Data.Path.Clone();

                    if (!string.IsNullOrEmpty(argument.Path))
                    {
                        var foundEntry = await Data.FileSystem.SearchEntryAsync(path, argument.Path, cancellationToken);

                        if (foundEntry?.Directory == null)
                        {
                            return(new FtpResponse(550, "File system entry not found."));
                        }
                        var dirEntry = foundEntry.Entry as IUnixDirectoryEntry;
                        if (dirEntry == null)
                        {
                            mask = foundEntry.FileName;
                        }
                        else if (!dirEntry.IsRoot)
                        {
                            path.Push(dirEntry);
                        }
                    }
                    directoriesToProcess.Enqueue(new DirectoryQueueItem(path, mask));
                }

                var encoding = Data.NlstEncoding ?? Connection.Encoding;

                using (var stream = await Connection.CreateEncryptedStream(responseSocket.WriteStream))
                {
                    using (var writer = new StreamWriter(stream, encoding, 4096, true)
                    {
                        NewLine = "\r\n",
                    })
                    {
                        while (directoriesToProcess.Count != 0)
                        {
                            var queueItem = directoriesToProcess.Dequeue();

                            var currentPath     = queueItem.Path;
                            var mask            = queueItem.Mask;
                            var currentDirEntry = currentPath.Count != 0 ? currentPath.Peek() : Data.FileSystem.Root;

                            if (argument.Recursive)
                            {
                                var line = currentPath.ToDisplayString() + ":";
                                Connection.Log?.Debug(line);
                                await writer.WriteLineAsync(line);
                            }

                            var mmOptions = new Options()
                            {
                                IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"),
                                NoGlobStar = true,
                                Dot        = true,
                            };

                            var mm = new Minimatcher(mask, mmOptions);

                            var entries = await Data.FileSystem.GetEntriesAsync(currentDirEntry, cancellationToken);

                            var enumerator = new DirectoryListingEnumerator(entries, Data.FileSystem, currentPath, true);
                            while (enumerator.MoveNext())
                            {
                                var name = enumerator.Name;
                                if (!enumerator.IsDotEntry)
                                {
                                    if (!mm.IsMatch(name))
                                    {
                                        continue;
                                    }
                                    if (name.StartsWith(".") && !showHidden)
                                    {
                                        continue;
                                    }
                                }

                                var entry = enumerator.Entry;

                                if (argument.Recursive && !enumerator.IsDotEntry)
                                {
                                    var dirEntry = entry as IUnixDirectoryEntry;
                                    if (dirEntry != null)
                                    {
                                        var subDirPath = currentPath.Clone();
                                        subDirPath.Push(dirEntry);
                                        directoriesToProcess.Enqueue(new DirectoryQueueItem(subDirPath, "*"));
                                    }
                                }

                                var line = formatter.Format(entry, name);
                                Connection.Log?.Debug(line);
                                await writer.WriteLineAsync(line);
                            }
                        }
                    }
                }
            }
            finally
            {
                responseSocket.Dispose();
            }

            // Use 250 when the connection stays open.
            return(new FtpResponse(250, "Closing data connection."));
        }
Beispiel #18
0
        public static List <string> GetProjectFiles(this ICollection <string> patterns, bool recursive = false)
        {
            var result = new List <string>();

            foreach (var p in patterns)
            {
                var pattern = p ?? "";

                if (recursive)
                {
                    pattern = pattern.NativeToUnix().Trailing('/') + "**";
                }

                if (pattern.IsValidPath() && (
                        File.Exists(pattern) ||
                        Directory.Exists(pattern)
                        ))
                {
                    result.Add(GetProjectFile(pattern));
                    continue;
                }

                if (pattern.IndexOf('*') == -1)
                {
                    throw new FileNotFoundException("No such file or directory: " + pattern);
                }

                if (pattern.IndexOf("..", StringComparison.Ordinal) != -1)
                {
                    throw new Exception("'..' is not supported in glob pattern: " + p);
                }

                while (pattern.StartsWith("./") || pattern.StartsWith(".\\"))
                {
                    pattern = pattern.Substring(2);
                }

                var glob = new Minimatcher(pattern, new Options {
                    IgnoreCase = true, AllowWindowsPaths = true
                });
                var root = Directory.GetCurrentDirectory();

                foreach (var e in Directory.EnumerateFiles(
                             root, "*.unoproj",
                             pattern.Contains("**")
                                ? SearchOption.AllDirectories
                                : SearchOption.TopDirectoryOnly))
                {
                    if (glob.IsMatch(e.Substring(root.Length + 1)))
                    {
                        result.Add(e);
                    }
                }
            }

            if (result.Count == 0)
            {
                throw new FileNotFoundException("No project".Plural(patterns) + " matching: " + string.Join(", ", patterns));
            }

            return(result);
        }
        public void VerifyMinimatcherSearchPatternMatch()
        {
            var matcher = new Minimatcher("/test/*", new Minimatch.Options {IgnoreCase = true});

            Assert.IsTrue(matcher.IsMatch("/test/dummy.csv"));
            Assert.IsTrue(matcher.IsMatch("/TEST/dummy.csv"));
            Assert.IsFalse(matcher.IsMatch("/second/dummy.csv"));
        }