Skip to content

Commit

Permalink
server(filesystem): fix archive skipping directories
Browse files Browse the repository at this point in the history
Fixes pterodactyl/panel#5027

Signed-off-by: Matthew Penner <[email protected]>
  • Loading branch information
matthewpi committed Mar 13, 2024
1 parent 99b9924 commit 2931430
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
29 changes: 18 additions & 11 deletions server/filesystem/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ type Archive struct {
// BaseDirectory .
BaseDirectory string

// Files specifies the files to archive, this takes priority over the Ignore option, if
// unspecified, all files in the BasePath will be archived unless Ignore is set.
//
// All items in Files must be absolute within BasePath.
// Files specifies the files to archive, this takes priority over the Ignore
// option, if unspecified, all files in the BaseDirectory will be archived
// unless Ignore is set.
Files []string

// Progress wraps the writer of the archive to pass through the progress tracker.
Expand Down Expand Up @@ -114,11 +113,16 @@ func (a *Archive) Stream(ctx context.Context, w io.Writer) error {
return errors.New("filesystem: archive.Filesystem is unset")
}

for i, f := range a.Files {
if !strings.HasPrefix(f, a.Filesystem.Path()) {
continue
if filesLen := len(a.Files); filesLen > 0 {
files := make([]string, filesLen)
for i, f := range a.Files {
if !strings.HasPrefix(f, a.Filesystem.Path()) {
files[i] = f
continue
}
files[i] = strings.TrimPrefix(strings.TrimPrefix(f, a.Filesystem.Path()), "/")
}
a.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
Expand All @@ -128,8 +132,6 @@ func (a *Archive) Stream(ctx context.Context, w io.Writer) error {
compressionLevel = pgzip.NoCompression
case "best_compression":
compressionLevel = pgzip.BestCompression
case "best_speed":
fallthrough
default:
compressionLevel = pgzip.BestSpeed
}
Expand Down Expand Up @@ -196,6 +198,9 @@ func (a *Archive) callback(opts ...walkFunc) walkFunc {
// a non-nil error we will exit immediately.
for _, opt := range opts {
if err := opt(dirfd, name, relative, d); err != nil {
if err == SkipThis {
return nil
}
return err
}
}
Expand All @@ -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.
func (a *Archive) withFilesCallback() walkFunc {
return a.callback(func(_ int, _, relative string, _ ufs.DirEntry) error {
Expand All @@ -225,7 +232,7 @@ func (a *Archive) withFilesCallback() walkFunc {
return nil
}

return ufs.SkipDir
return SkipThis
})
}

Expand Down
3 changes: 0 additions & 3 deletions server/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"github.com/pterodactyl/wings/internal/ufs"
)

// TODO: detect and enable
var useOpenat2 = true

type Filesystem struct {
unixFS *ufs.Quota

Expand Down

0 comments on commit 2931430

Please sign in to comment.