server(filesystem): fix archive skipping directories

Fixes https://github.com/pterodactyl/panel/issues/5027

Signed-off-by: Matthew Penner <me@matthewp.io>
This commit is contained in:
Matthew Penner 2024-03-13 15:09:24 -06:00
parent 99b9924a4a
commit 2931430eb8
No known key found for this signature in database
2 changed files with 18 additions and 14 deletions

View File

@ -66,10 +66,9 @@ type Archive struct {
// BaseDirectory . // BaseDirectory .
BaseDirectory string BaseDirectory string
// Files specifies the files to archive, this takes priority over the Ignore option, if // Files specifies the files to archive, this takes priority over the Ignore
// unspecified, all files in the BasePath will be archived unless Ignore is set. // option, if unspecified, all files in the BaseDirectory will be archived
// // unless Ignore is set.
// All items in Files must be absolute within BasePath.
Files []string Files []string
// Progress wraps the writer of the archive to pass through the progress tracker. // Progress wraps the writer of the archive to pass through the progress tracker.
@ -114,11 +113,16 @@ func (a *Archive) Stream(ctx context.Context, w io.Writer) error {
return errors.New("filesystem: archive.Filesystem is unset") return errors.New("filesystem: archive.Filesystem is unset")
} }
if filesLen := len(a.Files); filesLen > 0 {
files := make([]string, filesLen)
for i, f := range a.Files { for i, f := range a.Files {
if !strings.HasPrefix(f, a.Filesystem.Path()) { if !strings.HasPrefix(f, a.Filesystem.Path()) {
files[i] = f
continue continue
} }
a.Files[i] = strings.TrimPrefix(strings.TrimPrefix(f, a.Filesystem.Path()), "/") files[i] = strings.TrimPrefix(strings.TrimPrefix(f, a.Filesystem.Path()), "/")
}
a.Files = files
} }
// Choose which compression level to use based on the compression_level configuration option // Choose which compression level to use based on the compression_level configuration option
@ -128,8 +132,6 @@ func (a *Archive) Stream(ctx context.Context, w io.Writer) error {
compressionLevel = pgzip.NoCompression compressionLevel = pgzip.NoCompression
case "best_compression": case "best_compression":
compressionLevel = pgzip.BestCompression compressionLevel = pgzip.BestCompression
case "best_speed":
fallthrough
default: default:
compressionLevel = pgzip.BestSpeed compressionLevel = pgzip.BestSpeed
} }
@ -196,6 +198,9 @@ func (a *Archive) callback(opts ...walkFunc) walkFunc {
// a non-nil error we will exit immediately. // a non-nil error we will exit immediately.
for _, opt := range opts { for _, opt := range opts {
if err := opt(dirfd, name, relative, d); err != nil { if err := opt(dirfd, name, relative, d); err != nil {
if err == SkipThis {
return nil
}
return err return err
} }
} }
@ -206,6 +211,8 @@ func (a *Archive) callback(opts ...walkFunc) walkFunc {
} }
} }
var SkipThis = errors.New("skip this")
// Pushes only files defined in the Files key to the final archive. // Pushes only files defined in the Files key to the final archive.
func (a *Archive) withFilesCallback() walkFunc { func (a *Archive) withFilesCallback() walkFunc {
return a.callback(func(_ int, _, relative string, _ ufs.DirEntry) error { return a.callback(func(_ int, _, relative string, _ ufs.DirEntry) error {
@ -225,7 +232,7 @@ func (a *Archive) withFilesCallback() walkFunc {
return nil return nil
} }
return ufs.SkipDir return SkipThis
}) })
} }

View File

@ -21,9 +21,6 @@ import (
"github.com/pterodactyl/wings/internal/ufs" "github.com/pterodactyl/wings/internal/ufs"
) )
// TODO: detect and enable
var useOpenat2 = true
type Filesystem struct { type Filesystem struct {
unixFS *ufs.Quota unixFS *ufs.Quota