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:
parent
a877305202
commit
1c5ddcd20c
|
@ -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",
|
||||||
})
|
})
|
||||||
|
|
|
@ -166,17 +166,23 @@ func (fs *Filesystem) Write(p string, r io.Reader, newSize int64, mode ufs.FileM
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
if newSize == 0 {
|
||||||
|
// Subtract the previous size of the file if the new size is 0.
|
||||||
|
fs.unixFS.Add(-currentSize)
|
||||||
|
} else {
|
||||||
// Do not use CopyBuffer here, it is wasteful as the file implements
|
// Do not use CopyBuffer here, it is wasteful as the file implements
|
||||||
// io.ReaderFrom, which causes it to not use the buffer anyways.
|
// io.ReaderFrom, which causes it to not use the buffer anyways.
|
||||||
n, err := io.Copy(file, io.LimitReader(r, newSize))
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user