posix: filepath shouldn't be used anymore use path.Join (#1486)

This commit is contained in:
Harshavardhana 2016-05-05 01:39:26 -07:00 committed by Anand Babu (AB) Periasamy
parent 82fbe908a3
commit ad40036cba
6 changed files with 24 additions and 31 deletions

View File

@ -108,10 +108,9 @@ func treeWalk(bucketDir, prefixDir, entryPrefixMatch, marker string, recursive b
fileInfo.Mode = dirent.mode
}
if fileInfo.Mode.IsDir() {
// Add os.PathSeparator suffix again for directories as
// filepath.Join would have removed it.
// Add "/" suffix again for directories as
fileInfo.Size = 0
fileInfo.Name += string(os.PathSeparator)
fileInfo.Name += "/"
}
return fileInfo, nil
}

View File

@ -132,7 +132,7 @@ func scandir(dirPath string, filter func(fsDirent) bool, namesOnly bool) ([]fsDi
dirent.name = path.Join(dirPath, dirent.name)
}
if dirent.IsDir() {
dirent.name += string(os.PathSeparator)
dirent.name += "/"
dirent.size = 0
}
if filter == nil || filter(dirent) {

View File

@ -160,7 +160,7 @@ func removeDuplicateVols(volsInfo []VolInfo) []VolInfo {
func getAllUniqueVols(dirPath string) ([]VolInfo, error) {
volumeFn := func(dirent fsDirent) bool {
// Return all directories.
return dirent.IsDir() && isValidVolname(filepath.Clean(dirent.name))
return dirent.IsDir() && isValidVolname(path.Clean(dirent.name))
}
namesOnly := true // Returned are only names.
dirents, err := scandir(dirPath, volumeFn, namesOnly)
@ -173,10 +173,10 @@ func getAllUniqueVols(dirPath string) ([]VolInfo, error) {
}
var volsInfo []VolInfo
for _, dirent := range dirents {
fi, err := os.Stat(filepath.Join(dirPath, dirent.name))
fi, err := os.Stat(pathJoin(dirPath, dirent.name))
if err != nil {
log.WithFields(logrus.Fields{
"path": filepath.Join(dirPath, dirent.name),
"path": pathJoin(dirPath, dirent.name),
}).Debugf("Stat failed with error %s", err)
return nil, err
}
@ -198,7 +198,7 @@ func (s fsStorage) getVolumeDir(volume string) (string, error) {
if !isValidVolname(volume) {
return "", errInvalidArgument
}
volumeDir := filepath.Join(s.diskPath, volume)
volumeDir := pathJoin(s.diskPath, volume)
_, err := os.Stat(volumeDir)
if err == nil {
return volumeDir, nil
@ -210,12 +210,10 @@ func (s fsStorage) getVolumeDir(volume string) (string, error) {
return volumeDir, errVolumeNotFound
}
for _, vol := range volsInfo {
// Verify if lowercase version of
// the volume
// is equal to the incoming volume, then use the proper
// name.
// Verify if lowercase version of the volume is equal to
// the incoming volume, then use the proper name.
if strings.ToLower(vol.Name) == volume {
volumeDir = filepath.Join(s.diskPath, vol.Name)
volumeDir = pathJoin(s.diskPath, vol.Name)
return volumeDir, nil
}
}
@ -544,7 +542,7 @@ func (s fsStorage) ReadFile(volume string, path string, offset int64) (readClose
return nil, err
}
filePath := filepath.Join(volumeDir, filepath.FromSlash(path))
filePath := pathJoin(volumeDir, path)
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
@ -600,7 +598,7 @@ func (s fsStorage) CreateFile(volume, path string) (writeCloser io.WriteCloser,
if err = checkDiskFree(s.diskPath, s.minFreeDisk); err != nil {
return nil, err
}
filePath := filepath.Join(volumeDir, path)
filePath := pathJoin(volumeDir, path)
// Verify if the file already exists and is not of regular type.
var st os.FileInfo
if st, err = os.Stat(filePath); err == nil {
@ -634,7 +632,7 @@ func (s fsStorage) StatFile(volume, path string) (file FileInfo, err error) {
return FileInfo{}, err
}
filePath := filepath.Join(volumeDir, filepath.FromSlash(path))
filePath := pathJoin(volumeDir, path)
st, err := os.Stat(filePath)
if err != nil {
log.WithFields(logrus.Fields{
@ -712,10 +710,10 @@ func deleteFile(basePath, deletePath string) error {
return err
}
// Recursively go down the next path and delete again.
if err := deleteFile(basePath, filepath.Dir(deletePath)); err != nil {
if err := deleteFile(basePath, path.Dir(deletePath)); err != nil {
log.WithFields(logrus.Fields{
"basePath": basePath,
"deleteDir": filepath.Dir(deletePath),
"deleteDir": path.Dir(deletePath),
}).Debugf("deleteFile failed with %s", err)
return err
}
@ -735,10 +733,7 @@ func (s fsStorage) DeleteFile(volume, path string) error {
// Following code is needed so that we retain "/" suffix if any in
// path argument.
filePath := filepath.Join(volumeDir, filepath.FromSlash(path))
if strings.HasSuffix(filepath.FromSlash(path), string(os.PathSeparator)) {
filePath = filePath + string(os.PathSeparator)
}
filePath := pathJoin(volumeDir, path)
// Delete file and delete parent directory as well if its empty.
return deleteFile(volumeDir, filePath)

View File

@ -19,7 +19,6 @@ package main
import (
"errors"
slashpath "path"
"path/filepath"
"github.com/Sirupsen/logrus"
)
@ -145,8 +144,8 @@ func (xl XL) getPartsMetadata(volume, path string) ([]xlMetaV1, []error) {
// Returns collection of errors, indexed in accordance with input
// updateParts order.
// Write lockNS() should be done by caller.
func (xl XL) setPartsMetadata(volume, path string, metadata xlMetaV1, updateParts []bool) []error {
xlMetaV1FilePath := filepath.Join(path, xlMetaV1File)
func (xl XL) updatePartsMetadata(volume, path string, metadata xlMetaV1, updateParts []bool) []error {
xlMetaV1FilePath := pathJoin(path, xlMetaV1File)
errs := make([]error, len(xl.storageDisks))
for index := range updateParts {

View File

@ -206,8 +206,8 @@ func (xl XL) healFile(volume string, path string) error {
writer.Close()
}
// Update the quorum metadata after selfheal.
errs := xl.setPartsMetadata(volume, path, metadata, needsHeal)
// Update the quorum metadata after heal.
errs := xl.updatePartsMetadata(volume, path, metadata, needsHeal)
for index, healNeeded := range needsHeal {
if healNeeded && errs[index] != nil {
return errs[index]