fs: drop Stat() call from fsDeleteFile,deleteFile (#4744)

This commit makes fsDeleteFile() simply call deleteFile() after calling
the relevant path length checking functions. This DRYs the code base.

This commit removes the Stat() call from deleteFile(). This improves
performance and removes any possibility of a race condition.

This additionally adds tests and a benchmark for said function. The
results aren't very consistent, although I'd expect this commit to make
it faster.
This commit is contained in:
Brendan Ashworth
2017-08-03 20:04:28 -07:00
committed by Dee Koder
parent 0f401b67ad
commit bccc386994
3 changed files with 95 additions and 65 deletions

View File

@@ -915,27 +915,23 @@ func (s *posix) StatFile(volume, path string) (file FileInfo, err error) {
}, nil
}
// deleteFile - delete file path if its empty.
// deleteFile deletes a file path if its empty. If it's successfully deleted,
// it will recursively move up the tree, deleting empty parent directories
// until it finds one with files in it. Returns nil for a non-empty directory.
func deleteFile(basePath, deletePath string) error {
if basePath == deletePath {
return nil
}
// Verify if the path exists.
pathSt, err := osStat(preparePath(deletePath))
if err != nil {
if os.IsNotExist(err) {
return errFileNotFound
} else if os.IsPermission(err) {
return errFileAccessDenied
}
return err
}
if pathSt.IsDir() && !isDirEmpty(deletePath) {
// Verify if directory is empty.
return nil
}
// Attempt to remove path.
if err := os.Remove(preparePath(deletePath)); err != nil {
// Ignore errors if the directory is not empty. The server relies on
// this functionality, and sometimes uses recursion that should not
// error on parent directories.
if isSysErrNotEmpty(err) {
return nil
}
if os.IsNotExist(err) {
return errFileNotFound
} else if os.IsPermission(err) {
@@ -943,11 +939,9 @@ func deleteFile(basePath, deletePath string) error {
}
return err
}
// Recursively go down the next path and delete again.
if err := deleteFile(basePath, slashpath.Dir(deletePath)); err != nil {
return err
}
return nil
return deleteFile(basePath, slashpath.Dir(deletePath))
}
// DeleteFile - delete a file at path.