From b64f1897fbe9cd544fb44a62815065318fac7ae0 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 15 Jul 2020 12:28:45 -0600 Subject: [PATCH 1/2] Add endpoint for decompressing archives --- router/router.go | 1 + router/router_server_files.go | 27 +++++++++++++++++++++++++++ server/filesystem.go | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/router/router.go b/router/router.go index 12d270e..fbc6d8a 100644 --- a/router/router.go +++ b/router/router.go @@ -84,6 +84,7 @@ func Configure() *gin.Engine { files.POST("/create-directory", postServerCreateDirectory) files.POST("/delete", postServerDeleteFiles) files.POST("/compress", postServerCompressFiles) + files.POST("/decompress", postServerDecompressFiles) } backup := server.Group("/backup") diff --git a/router/router_server_files.go b/router/router_server_files.go index d9635e7..5072ef2 100644 --- a/router/router_server_files.go +++ b/router/router_server_files.go @@ -277,3 +277,30 @@ func postServerCompressFiles(c *gin.Context) { Mimetype: "application/tar+gzip", }) } + +func postServerDecompressFiles(c *gin.Context) { + s := GetServer(c.Param("server")) + + var data struct { + RootPath string `json:"root"` + File string `json:"file"` + } + + if err := c.BindJSON(&data); err != nil { + return + } + + if !s.Filesystem.HasSpaceAvailable() { + c.AbortWithStatusJSON(http.StatusConflict, gin.H{ + "error": "This server does not have enough available disk space to decompress an archive.", + }) + return + } + + if err := s.Filesystem.DecompressFile(data.RootPath, data.File); err != nil { + TrackedServerError(err, s).AbortWithServerError(c) + return + } + + c.Status(http.StatusNoContent) +} diff --git a/server/filesystem.go b/server/filesystem.go index 22a4ff7..7db9b07 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "github.com/gabriel-vasile/mimetype" + "github.com/mholt/archiver/v3" "github.com/pkg/errors" "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/server/backup" @@ -742,3 +743,25 @@ func (fs *Filesystem) CompressFiles(dir string, paths []string) (os.FileInfo, er return a.Create(d, context.Background()) } + +// DecompressFile decompresses an archive. +func (fs *Filesystem) DecompressFile(dir, file string) error { + // Clean the directory path where the contents of archive will be extracted to. + safeBasePath, err := fs.SafePath(dir) + if err != nil { + return err + } + + // Clean the full path to the archive which will be unarchived. + safeArchivePath, err := fs.SafePath(filepath.Join(safeBasePath, file)) + if err != nil { + return err + } + + // Decompress the archive using it's extension to determine the algorithm. + if err := archiver.Unarchive(safeArchivePath, safeBasePath); err != nil { + return err + } + + return nil +} From f4c10e5a23dbb190d96f587d0515c98833986023 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 15 Jul 2020 13:11:12 -0600 Subject: [PATCH 2/2] Add some missing error handling, fix a few typos --- server/backup/archiver.go | 17 ++++++++++------- server/filesystem.go | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/server/backup/archiver.go b/server/backup/archiver.go index e2c1ec3..5330e6f 100644 --- a/server/backup/archiver.go +++ b/server/backup/archiver.go @@ -20,9 +20,9 @@ type Archive struct { Files *IncludedFiles } -// Creates an archive at dest with all of the files definied in the included files struct. -func (a *Archive) Create(dest string, ctx context.Context) (os.FileInfo, error) { - f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) +// Creates an archive at dst with all of the files defined in the included files struct. +func (a *Archive) Create(dst string, ctx context.Context) (os.FileInfo, error) { + f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return nil, err } @@ -66,14 +66,17 @@ func (a *Archive) Create(dest string, ctx context.Context) (os.FileInfo, error) // Attempt to remove the archive if there is an error, report that error to // the logger if it fails. - if rerr := os.Remove(dest); rerr != nil && !os.IsNotExist(rerr) { - log.WithField("location", dest).Warn("failed to delete corrupted backup archive") + if rerr := os.Remove(dst); rerr != nil && !os.IsNotExist(rerr) { + log.WithField("location", dst).Warn("failed to delete corrupted backup archive") } return nil, err } - st, _ := f.Stat() + st, err := f.Stat() + if err != nil { + return nil, err + } return st, nil } @@ -101,7 +104,7 @@ func (a *Archive) addToArchive(p string, s *os.FileInfo, w *tar.Writer) error { a.Lock() defer a.Unlock() - if err = w.WriteHeader(header); err != nil { + if err := w.WriteHeader(header); err != nil { return err } diff --git a/server/filesystem.go b/server/filesystem.go index 7db9b07..50285cc 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -677,7 +677,7 @@ func (fs *Filesystem) GetIncludedFiles(dir string, ignored []string) (*backup.In return inc, nil } -// Compresses all of the files matching the given paths in the specififed directory. This function +// Compresses all of the files matching the given paths in the specified directory. This function // also supports passing nested paths to only compress certain files and folders when working in // a larger directory. This effectively creates a local backup, but rather than ignoring specific // files and folders, it takes an allow-list of files and folders.