Skip to content

Commit

Permalink
Only create the directory structure if it is a file being renamed; cl…
Browse files Browse the repository at this point in the history
  • Loading branch information
DaneEveritt committed Sep 1, 2020
1 parent cbf914e commit 481df3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
7 changes: 7 additions & 0 deletions router/router_server_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 14 additions & 9 deletions server/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

if cleanedTo == fs.Path() {
return errors.New("attempting to rename into an invalid directory space")
}

// Ensure that the directory we're moving into exists correctly on the system.
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")
}
}

Expand Down

0 comments on commit 481df3d

Please sign in to comment.