From 98ff91b4842d7c837f5e3aaf26bb3b3997953a6a Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 19 Mar 2021 23:42:01 +0100 Subject: [PATCH] xl: Reduce usage of isDirEmpty() (#11838) When an object is removed, its parent directory is inspected to check if it is empty to remove if that is the case. However, we can use os.Remove() directly since it is only able to remove a file or an empty directory. --- cmd/xl-storage.go | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/cmd/xl-storage.go b/cmd/xl-storage.go index 991f34c03..a13e46ff7 100644 --- a/cmd/xl-storage.go +++ b/cmd/xl-storage.go @@ -750,15 +750,6 @@ func (s *xlStorage) isLeaf(volume string, leafPath string) bool { return false } -func (s *xlStorage) isLeafDir(volume, leafPath string) bool { - volumeDir, err := s.getVolDir(volume) - if err != nil { - return false - } - - return isDirEmpty(pathJoin(volumeDir, leafPath)) -} - // ListDir - return all the entries at the given directory path. // If an entry is a directory it will be returned with a trailing SlashSeparator. func (s *xlStorage) ListDir(ctx context.Context, volume, dirPath string, count int) (entries []string, err error) { @@ -1973,10 +1964,8 @@ func (s *xlStorage) RenameData(ctx context.Context, srcVolume, srcPath, dataDir, } // Remove parent dir of the source file if empty - if parentDir := pathutil.Dir(srcFilePath); isDirEmpty(parentDir) { - s.deleteFile(srcVolumeDir, parentDir, false) - } - + parentDir := pathutil.Dir(srcFilePath) + s.deleteFile(srcVolumeDir, parentDir, false) return nil } @@ -2028,18 +2017,18 @@ func (s *xlStorage) RenameFile(ctx context.Context, srcVolume, srcPath, dstVolum // If source is a directory, we expect the destination to be non-existent but we // we still need to allow overwriting an empty directory since it represents // an object empty directory. - _, err = os.Lstat(dstFilePath) + dirInfo, err := os.Lstat(dstFilePath) if isSysErrIO(err) { return errFaultyDisk } - if err == nil && !isDirEmpty(dstFilePath) { - return errFileAccessDenied - } - if err != nil && !osIsNotExist(err) { - return err - } - // Empty destination remove it before rename. - if isDirEmpty(dstFilePath) { + if err != nil { + if !osIsNotExist(err) { + return err + } + } else { + if !dirInfo.IsDir() { + return errFileAccessDenied + } if err = os.Remove(dstFilePath); err != nil { if isSysErrNotEmpty(err) { return errFileAccessDenied @@ -2054,9 +2043,8 @@ func (s *xlStorage) RenameFile(ctx context.Context, srcVolume, srcPath, dstVolum } // Remove parent dir of the source file if empty - if parentDir := pathutil.Dir(srcFilePath); isDirEmpty(parentDir) { - s.deleteFile(srcVolumeDir, parentDir, false) - } + parentDir := pathutil.Dir(srcFilePath) + s.deleteFile(srcVolumeDir, parentDir, false) return nil }