posix.RenameFile(): Allow overwriting an empty directory (#5551)

Overwriting files is allowed, but since the introduction of
the object directory, we will aslo need to allow overwriting
an empty directory. Putting twice the same object directory
won't fail with 403 error anymore.
master
Anis Elleuch 7 years ago committed by kannappanr
parent b2b5056163
commit 926e480156
  1. 17
      cmd/posix.go

@ -932,14 +932,14 @@ func (s *posix) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err e
return err return err
} }
// Stat a volume entry. // Stat a volume entry.
_, err = os.Stat((srcVolumeDir)) _, err = os.Stat(srcVolumeDir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return errVolumeNotFound return errVolumeNotFound
} }
return err return err
} }
_, err = os.Stat((dstVolumeDir)) _, err = os.Stat(dstVolumeDir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return errVolumeNotFound return errVolumeNotFound
@ -953,23 +953,24 @@ func (s *posix) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err e
return errFileAccessDenied return errFileAccessDenied
} }
srcFilePath := slashpath.Join(srcVolumeDir, srcPath) srcFilePath := slashpath.Join(srcVolumeDir, srcPath)
if err = checkPathLength((srcFilePath)); err != nil { if err = checkPathLength(srcFilePath); err != nil {
return err return err
} }
dstFilePath := slashpath.Join(dstVolumeDir, dstPath) dstFilePath := slashpath.Join(dstVolumeDir, dstPath)
if err = checkPathLength((dstFilePath)); err != nil { if err = checkPathLength(dstFilePath); err != nil {
return err return err
} }
if srcIsDir { if srcIsDir {
// If source is a directory we expect the destination to be non-existent always. // If source is a directory, we expect the destination to be non-existent but we
_, err = os.Stat((dstFilePath)) // we still need to allow overwriting an empty directory since it represents
if err == nil { // an object empty directory.
_, err = os.Stat(dstFilePath)
if err == nil && !isDirEmpty(dstFilePath) {
return errFileAccessDenied return errFileAccessDenied
} }
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return err return err
} }
// Destination does not exist, hence proceed with the rename.
} }
if err = renameAll(srcFilePath, dstFilePath); err != nil { if err = renameAll(srcFilePath, dstFilePath); err != nil {

Loading…
Cancel
Save