server(filesystem): handle writing empty files

Fixes https://github.com/pterodactyl/panel/issues/5038

Signed-off-by: Matthew Penner <me@matthewp.io>
This commit is contained in:
Matthew Penner 2024-03-17 14:46:05 -06:00
parent a877305202
commit 1c5ddcd20c
No known key found for this signature in database
2 changed files with 14 additions and 7 deletions

View File

@ -239,7 +239,8 @@ func postServerWriteFile(c *gin.Context) {
return return
} }
if c.Request.ContentLength < 1 { // A content length of -1 means the actual length is unknown.
if c.Request.ContentLength == -1 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "Missing Content-Length", "error": "Missing Content-Length",
}) })

View File

@ -166,17 +166,23 @@ func (fs *Filesystem) Write(p string, r io.Reader, newSize int64, mode ufs.FileM
} }
defer file.Close() defer file.Close()
// Do not use CopyBuffer here, it is wasteful as the file implements if newSize == 0 {
// io.ReaderFrom, which causes it to not use the buffer anyways. // Subtract the previous size of the file if the new size is 0.
n, err := io.Copy(file, io.LimitReader(r, newSize)) fs.unixFS.Add(-currentSize)
} else {
// Do not use CopyBuffer here, it is wasteful as the file implements
// io.ReaderFrom, which causes it to not use the buffer anyways.
var n int64
n, err = io.Copy(file, io.LimitReader(r, newSize))
// Adjust the disk usage to account for the old size and the new size of the file. // Adjust the disk usage to account for the old size and the new size of the file.
fs.unixFS.Add(n - currentSize) fs.unixFS.Add(n - currentSize)
}
if err := fs.chownFile(p); err != nil { if err := fs.chownFile(p); err != nil {
return err return err
} }
// Return the error from io.Copy. // Return any remaining error.
return err return err
} }