From cff747711178e0682ea942b36e3367593d4882e0 Mon Sep 17 00:00:00 2001 From: Ferks-FK Date: Wed, 19 Nov 2025 14:13:20 +0000 Subject: [PATCH] Add delete files patterns. --- parser/parser.go | 50 +++++++++++++++++++++++++++++++++++ router/router_server_files.go | 34 +++++++++++++++--------- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index e7c98b3b2..0ddafa2a1 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/json" "io" + "path/filepath" "strconv" "strings" @@ -19,6 +20,7 @@ import ( "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/internal/ufs" + "github.com/pterodactyl/wings/server/filesystem" ) // The file parsing options that are available for a server configuration file. @@ -591,3 +593,51 @@ func (f *ConfigurationFile) parsePropertiesFile(file ufs.File) error { } return nil } + +// MatchPattern takes a filesystem, root path, and pattern to match files against, +// returning a list of matched file paths. It supports wildcard patterns using +// standard filepath matching rules. +func MatchPattern(fs *filesystem.Filesystem, rootPath, pattern string) ([]string, error) { + var results []string + + // Separate the pattern into parts + parts := strings.Split(pattern, string(filepath.Separator)) + + // If the pattern has multiple parts (e.g., logs/*.txt), process recursively + if len(parts) > 1 { + currentDir := rootPath + for i, part := range parts { + if i == len(parts)-1 { + // Last part is the file pattern + entries, err := fs.ListDirectory(currentDir) + // If directory don't exist, just ignore. + if err != nil { + return nil, nil + } + + for _, entry := range entries { + if matched, _ := filepath.Match(part, entry.Name()); matched { + results = append(results, filepath.Join(currentDir, entry.Name())) + } + } + } else { + // Intermediate parts: enter the directory if it exists + currentDir = filepath.Join(currentDir, part) + } + } + } else { + // Simple pattern (e.g., *.txt) - match in the rootPath + entries, err := fs.ListDirectory(rootPath) + if err != nil { + return nil, err + } + + for _, entry := range entries { + if matched, _ := filepath.Match(pattern, entry.Name()); matched { + results = append(results, filepath.Join(rootPath, entry.Name())) + } + } + } + + return results, nil +} diff --git a/router/router_server_files.go b/router/router_server_files.go index 09ad8cd1f..f5d564080 100644 --- a/router/router_server_files.go +++ b/router/router_server_files.go @@ -20,6 +20,7 @@ import ( "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/internal/models" + "github.com/pterodactyl/wings/parser" "github.com/pterodactyl/wings/router/downloader" "github.com/pterodactyl/wings/router/middleware" "github.com/pterodactyl/wings/router/tokens" @@ -202,21 +203,30 @@ func postServerDeleteFiles(c *gin.Context) { return } - g, ctx := errgroup.WithContext(context.Background()) + // Expand any patterns provided in the files array. + var expandedFiles []string + for _, pattern := range data.Files { + matches, err := parser.MatchPattern(s.Filesystem(), data.Root, pattern) + if err != nil { + middleware.CaptureAndAbort(c, err) + return + } + expandedFiles = append(expandedFiles, matches...) + } - // Loop over the array of files passed in and delete them. If any of the file deletions - // fail just abort the process entirely. - for _, p := range data.Files { - pi := path.Join(data.Root, p) + g, ctx := errgroup.WithContext(context.Background()) - g.Go(func() error { - select { - case <-ctx.Done(): - return ctx.Err() - default: - return s.Filesystem().Delete(pi) + for _, filePath := range expandedFiles { + g.Go(func(fp string) func() error { + return func() error { + select { + case <-ctx.Done(): + return ctx.Err() + default: + return s.Filesystem().Delete(fp) + } } - }) + }(filePath)) } if err := g.Wait(); err != nil {