Only create the directory structure if it is a file being renamed; closes pterodactyl/panel#2292

This commit is contained in:
Dane Everitt 2020-08-31 21:02:06 -07:00
parent cbf914e7a1
commit 481df3d543
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 21 additions and 9 deletions

View File

@ -149,6 +149,13 @@ func putServerRenameFiles(c *gin.Context) {
}
if err := g.Wait(); err != nil {
if errors.Is(err, os.ErrExist) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "Cannot move or rename file, destination already exists.",
})
return
}
TrackedServerError(err, s).AbortWithServerError(c)
return
}

View File

@ -496,17 +496,22 @@ func (fs *Filesystem) Rename(from string, to string) error {
return errors.WithStack(err)
}
if f, err := os.Stat(cleanedFrom); err != nil {
return errors.WithStack(err)
} else {
d := cleanedTo
if !f.IsDir() {
d = strings.TrimSuffix(d, path.Base(cleanedTo))
// If the target file or directory already exists the rename function will fail, so just
// bail out now.
if _, err := os.Stat(cleanedTo); err == nil {
return os.ErrExist
}
// Ensure that the directory we're moving into exists correctly on the system.
if cleanedTo == fs.Path() {
return errors.New("attempting to rename into an invalid directory space")
}
d := strings.TrimSuffix(cleanedTo, path.Base(cleanedTo))
// Ensure that the directory we're moving into exists correctly on the system. Only do this if
// we're not at the root directory level.
if d != fs.Path() {
if mkerr := os.MkdirAll(d, 0644); mkerr != nil {
return errors.WithStack(mkerr)
return errors.Wrap(mkerr, "failed to create directory structure for file rename")
}
}