2020-09-27 19:24:08 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
2020-11-08 21:52:20 +00:00
|
|
|
"fmt"
|
2020-09-27 19:24:08 +00:00
|
|
|
"path/filepath"
|
2021-01-11 00:33:39 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/apex/log"
|
2024-03-13 03:44:55 +00:00
|
|
|
|
|
|
|
"github.com/pterodactyl/wings/internal/ufs"
|
2020-09-27 19:24:08 +00:00
|
|
|
)
|
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
type ErrorCode string
|
2020-11-08 21:52:20 +00:00
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
const (
|
|
|
|
ErrCodeIsDirectory ErrorCode = "E_ISDIR"
|
|
|
|
ErrCodeDiskSpace ErrorCode = "E_NODISK"
|
|
|
|
ErrCodeUnknownArchive ErrorCode = "E_UNKNFMT"
|
|
|
|
ErrCodePathResolution ErrorCode = "E_BADPATH"
|
2021-01-11 00:33:39 +00:00
|
|
|
ErrCodeDenylistFile ErrorCode = "E_DENYLIST"
|
2021-01-16 19:48:30 +00:00
|
|
|
ErrCodeUnknownError ErrorCode = "E_UNKNOWN"
|
2022-11-22 18:18:27 +00:00
|
|
|
ErrNotExist ErrorCode = "E_NOTEXIST"
|
2020-12-16 04:51:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Error struct {
|
2021-01-16 19:48:30 +00:00
|
|
|
code ErrorCode
|
|
|
|
// Contains the underlying error leading to this. This value may or may not be
|
|
|
|
// present, it is entirely dependent on how this error was triggered.
|
|
|
|
err error
|
|
|
|
// This contains the value of the final destination that triggered this specific
|
|
|
|
// error event.
|
2020-11-08 21:52:20 +00:00
|
|
|
resolved string
|
2021-01-16 19:48:30 +00:00
|
|
|
// This value is generally only present on errors stemming from a path resolution
|
|
|
|
// error. For everything else you should be setting and reading the resolved path
|
|
|
|
// value which will be far more useful.
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2021-04-17 20:29:18 +00:00
|
|
|
// newFilesystemError returns a new error instance with a stack trace associated.
|
|
|
|
func newFilesystemError(code ErrorCode, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStackDepth(&Error{code: code, err: err}, 1)
|
|
|
|
}
|
|
|
|
return errors.WithStackDepth(&Error{code: code}, 1)
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:48:30 +00:00
|
|
|
// Code returns the ErrorCode for this specific error instance.
|
|
|
|
func (e *Error) Code() ErrorCode {
|
|
|
|
return e.code
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
// Returns a human-readable error string to identify the Error by.
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
switch e.code {
|
|
|
|
case ErrCodeIsDirectory:
|
2021-01-16 19:48:30 +00:00
|
|
|
return fmt.Sprintf("filesystem: cannot perform action: [%s] is a directory", e.resolved)
|
2020-12-16 04:51:13 +00:00
|
|
|
case ErrCodeDiskSpace:
|
|
|
|
return "filesystem: not enough disk space"
|
|
|
|
case ErrCodeUnknownArchive:
|
|
|
|
return "filesystem: unknown archive format"
|
2021-01-11 00:33:39 +00:00
|
|
|
case ErrCodeDenylistFile:
|
2021-01-11 00:43:33 +00:00
|
|
|
r := e.resolved
|
|
|
|
if r == "" {
|
|
|
|
r = "<empty>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("filesystem: file access prohibited: [%s] is on the denylist", r)
|
2020-12-16 04:51:13 +00:00
|
|
|
case ErrCodePathResolution:
|
|
|
|
r := e.resolved
|
|
|
|
if r == "" {
|
|
|
|
r = "<empty>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("filesystem: server path [%s] resolves to a location outside the server root: %s", e.path, r)
|
2022-11-22 18:18:27 +00:00
|
|
|
case ErrNotExist:
|
|
|
|
return "filesystem: does not exist"
|
2021-01-16 19:48:30 +00:00
|
|
|
case ErrCodeUnknownError:
|
|
|
|
fallthrough
|
|
|
|
default:
|
2021-04-17 20:29:18 +00:00
|
|
|
return fmt.Sprintf("filesystem: an error occurred: %s", e.Unwrap())
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 19:24:08 +00:00
|
|
|
|
2021-04-17 20:29:18 +00:00
|
|
|
// Unwrap returns the underlying cause of this filesystem error. In some causes
|
2021-01-16 19:48:30 +00:00
|
|
|
// there may not be a cause present, in which case nil will be returned.
|
2021-04-17 20:29:18 +00:00
|
|
|
func (e *Error) Unwrap() error {
|
2021-01-16 19:48:30 +00:00
|
|
|
return e.err
|
2020-12-16 04:51:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 19:24:08 +00:00
|
|
|
// Generates an error logger instance with some basic information.
|
|
|
|
func (fs *Filesystem) error(err error) *log.Entry {
|
2024-03-13 03:44:55 +00:00
|
|
|
return log.WithField("subsystem", "filesystem").WithField("root", fs.Path()).WithField("error", err)
|
2020-09-27 19:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle errors encountered when walking through directories.
|
|
|
|
//
|
|
|
|
// If there is a path resolution error just skip the item entirely. Only return this for a
|
|
|
|
// directory, otherwise return nil. Returning this error for a file will stop the walking
|
2024-03-13 03:44:55 +00:00
|
|
|
// for the remainder of the directory. This is assuming an FileInfo struct was even returned.
|
|
|
|
func (fs *Filesystem) handleWalkerError(err error, f ufs.FileInfo) error {
|
2020-12-16 04:51:13 +00:00
|
|
|
if !IsErrorCode(err, ErrCodePathResolution) {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-09-27 19:24:08 +00:00
|
|
|
}
|
|
|
|
if f != nil && f.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
2021-01-16 19:48:30 +00:00
|
|
|
|
|
|
|
// IsFilesystemError checks if the given error is one of the Filesystem errors.
|
|
|
|
func IsFilesystemError(err error) bool {
|
|
|
|
var fserr *Error
|
|
|
|
if err != nil && errors.As(err, &fserr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrorCode checks if "err" is a filesystem Error type. If so, it will then
|
|
|
|
// drop in and check that the error code is the same as the provided ErrorCode
|
|
|
|
// passed in "code".
|
|
|
|
func IsErrorCode(err error, code ErrorCode) bool {
|
|
|
|
var fserr *Error
|
|
|
|
if err != nil && errors.As(err, &fserr) {
|
|
|
|
return fserr.code == code
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBadPathResolution returns a new BadPathResolution error.
|
2021-04-17 20:29:18 +00:00
|
|
|
func NewBadPathResolution(path string, resolved string) error {
|
|
|
|
return errors.WithStackDepth(&Error{code: ErrCodePathResolution, path: path, resolved: resolved}, 1)
|
2021-01-16 19:48:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 20:29:18 +00:00
|
|
|
// wrapError wraps the provided error as a Filesystem error and attaches the
|
2021-01-16 19:48:30 +00:00
|
|
|
// provided resolved source to it. If the error is already a Filesystem error
|
|
|
|
// no action is taken.
|
2021-04-17 20:29:18 +00:00
|
|
|
func wrapError(err error, resolved string) error {
|
|
|
|
if err == nil || IsFilesystemError(err) {
|
|
|
|
return err
|
2021-01-16 19:48:30 +00:00
|
|
|
}
|
2021-04-17 20:29:18 +00:00
|
|
|
return errors.WithStackDepth(&Error{code: ErrCodeUnknownError, err: err, resolved: resolved}, 1)
|
2021-08-02 21:07:00 +00:00
|
|
|
}
|