Check disk before even trying to run the download

This commit is contained in:
Dane Everitt 2020-12-20 11:08:01 -08:00
parent 17daa2071f
commit 9c53436470
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 18 additions and 8 deletions

View File

@ -60,7 +60,6 @@ func (dl *Download) Execute() error {
dl.cancelFunc = &cancel
defer dl.Cancel()
fnameparts := strings.Split(dl.req.URL.Path, "/")
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, dl.req.URL.String(), nil)
res, err := client.Do(req)
if err != nil {
@ -70,6 +69,8 @@ func (dl *Download) Execute() error {
if res.StatusCode >= 300 || res.StatusCode < 200 {
return errors.New("downloader: got bad response status from endpoint: " + res.Status)
}
fnameparts := strings.Split(dl.req.URL.Path, "/")
p := filepath.Join(dl.req.Directory, fnameparts[len(fnameparts)-1])
dl.server.Log().WithField("path", p).Debug("writing remote file to disk")
if err := dl.server.Filesystem().Writefile(p, res.Body); err != nil {

View File

@ -256,6 +256,11 @@ func postServerPullRemoteFile(c *gin.Context) {
return
}
if err := s.Filesystem().HasSpaceErr(true); err != nil {
WithError(c, err)
return
}
dl := downloader.New(s, downloader.DownloadRequest{
URL: u,
Directory: data.Directory,

View File

@ -48,6 +48,15 @@ func (fs *Filesystem) SetDiskLimit(i int64) {
fs.mu.Unlock()
}
// The same concept as HasSpaceAvailable however this will return an error if there is
// no space, rather than a boolean value.
func (fs *Filesystem) HasSpaceErr(allowStaleValue bool) error {
if !fs.HasSpaceAvailable(allowStaleValue) {
return &Error{code: ErrCodeDiskSpace}
}
return nil
}
// Determines if the directory a file is trying to be added to has enough space available
// for the file to be written to.
//

View File

@ -67,10 +67,6 @@ func IsErrorCode(err error, code ErrorCode) bool {
return false
}
func NewDiskSpaceError() *Error {
return &Error{code: ErrCodeDiskSpace}
}
// Returns a new BadPathResolution error.
func NewBadPathResolution(path string, resolved string) *Error {
return &Error{code: ErrCodePathResolution, path: path, resolved: resolved}

View File

@ -5,7 +5,6 @@ import (
"emperror.dev/errors"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/server/filesystem"
"golang.org/x/sync/semaphore"
"os"
"time"
@ -168,8 +167,8 @@ func (s *Server) onBeforeStart() error {
s.Filesystem().HasSpaceAvailable(true)
} else {
s.PublishConsoleOutputFromDaemon("Checking server disk space usage, this could take a few seconds...")
if !s.Filesystem().HasSpaceAvailable(false) {
return filesystem.NewDiskSpaceError()
if err := s.Filesystem().HasSpaceErr(false); err != nil {
return err
}
}