From d8cd8ae36a0866d9ba3919d69d62e3154538521f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 22 Sep 2020 20:41:14 -0700 Subject: [PATCH] Don't treat certain "expected" errors from the filesystem as 500 errors; closes pterodactyl/panel#2376 --- router/router_server_files.go | 14 ++++++++++++++ server/filesystem.go | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/router/router_server_files.go b/router/router_server_files.go index ffe5f35..a880a9f 100644 --- a/router/router_server_files.go +++ b/router/router_server_files.go @@ -247,6 +247,13 @@ func postServerWriteFile(c *gin.Context) { f = "/" + strings.TrimLeft(f, "/") if err := s.Filesystem.Writefile(f, c.Request.Body); err != nil { + if errors.Is(err, server.ErrIsDirectory) { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "error": "Cannot write file, name conflicts with an existing directory by the same name.", + }) + return + } + TrackedServerError(err, s).AbortWithServerError(c) return } @@ -268,6 +275,13 @@ func postServerCreateDirectory(c *gin.Context) { } if err := s.Filesystem.CreateDirectory(data.Name, data.Path); err != nil { + if err.Error() == "not a directory" { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "error": "Part of the path being created is not a directory (ENOTDIR).", + }) + return + } + TrackedServerError(err, s).AbortWithServerError(c) return } diff --git a/server/filesystem.go b/server/filesystem.go index 0642970..89283ce 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -29,6 +29,7 @@ import ( // Error returned when there is a bad path provided to one of the FS calls. type PathResolutionError struct{} +var ErrIsDirectory = errors.New("is a directory") var ErrNotEnoughDiskSpace = errors.New("not enough disk space is available to perform this operation") // Returns the error response in a string form that can be more easily consumed. @@ -396,7 +397,7 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error { } } else { if stat.IsDir() { - return errors.New("cannot write file contents to a directory") + return ErrIsDirectory } currentSize = stat.Size()