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
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-01-11 00:33:39 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/apex/log"
|
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"
|
2020-12-16 04:51:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
code ErrorCode
|
2020-11-08 21:52:20 +00:00
|
|
|
path string
|
|
|
|
resolved string
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
return "filesystem: is a directory"
|
|
|
|
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)
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
2020-12-16 04:51:13 +00:00
|
|
|
return "filesystem: unhandled error type"
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
// 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
|
|
|
// Checks if the given error is one of the Filesystem errors.
|
|
|
|
func IsFilesystemError(err error) (*Error, bool) {
|
2021-01-10 22:25:39 +00:00
|
|
|
var fserr *Error
|
|
|
|
if errors.As(err, &fserr) {
|
2020-12-16 04:51:13 +00:00
|
|
|
return fserr, true
|
2020-11-08 21:52:20 +00:00
|
|
|
}
|
2020-12-16 04:51:13 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
2020-11-08 23:15:39 +00:00
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
// 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 {
|
|
|
|
if e, ok := IsFilesystemError(err); ok {
|
|
|
|
return e.code == code
|
|
|
|
}
|
2020-11-08 21:52:20 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-09-27 19:24:08 +00:00
|
|
|
|
2020-12-16 04:51:13 +00:00
|
|
|
// Returns a new BadPathResolution error.
|
|
|
|
func NewBadPathResolution(path string, resolved string) *Error {
|
|
|
|
return &Error{code: ErrCodePathResolution, path: path, resolved: resolved}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return log.WithField("subsystem", "filesystem").WithField("root", fs.root).WithField("error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// for the remainder of the directory. This is assuming an os.FileInfo struct was even returned.
|
|
|
|
func (fs *Filesystem) handleWalkerError(err error, f os.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
|
|
|
}
|