Remove unnecessary `filepath.FromSlash` calls (#1458)

## Changes

The prior join call calls `filepath.Join` which returns a cleaned
result.

Path cleaning, in turn, calls `filepath.FromSlash`.

## Tests

* Unit tests.
This commit is contained in:
Pieter Noordhuis 2024-05-29 17:30:26 +02:00 committed by GitHub
parent 13b937cea8
commit b2ea9dd971
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 0 additions and 7 deletions

View File

@ -34,7 +34,6 @@ func (w *LocalClient) Write(ctx context.Context, name string, reader io.Reader,
flags |= os.O_EXCL flags |= os.O_EXCL
} }
absPath = filepath.FromSlash(absPath)
f, err := os.OpenFile(absPath, flags, 0644) f, err := os.OpenFile(absPath, flags, 0644)
if os.IsNotExist(err) && slices.Contains(mode, CreateParentDirectories) { if os.IsNotExist(err) && slices.Contains(mode, CreateParentDirectories) {
// Create parent directories if they don't exist. // Create parent directories if they don't exist.
@ -76,7 +75,6 @@ func (w *LocalClient) Read(ctx context.Context, name string) (io.ReadCloser, err
// This stat call serves two purposes: // This stat call serves two purposes:
// 1. Checks file at path exists, and throws an error if it does not // 1. Checks file at path exists, and throws an error if it does not
// 2. Allows us to error out if the path is a directory // 2. Allows us to error out if the path is a directory
absPath = filepath.FromSlash(absPath)
stat, err := os.Stat(absPath) stat, err := os.Stat(absPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -103,7 +101,6 @@ func (w *LocalClient) Delete(ctx context.Context, name string, mode ...DeleteMod
return CannotDeleteRootError{} return CannotDeleteRootError{}
} }
absPath = filepath.FromSlash(absPath)
err = os.Remove(absPath) err = os.Remove(absPath)
// Return early on success. // Return early on success.
@ -131,7 +128,6 @@ func (w *LocalClient) ReadDir(ctx context.Context, name string) ([]fs.DirEntry,
return nil, err return nil, err
} }
absPath = filepath.FromSlash(absPath)
stat, err := os.Stat(absPath) stat, err := os.Stat(absPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -153,7 +149,6 @@ func (w *LocalClient) Mkdir(ctx context.Context, name string) error {
return err return err
} }
dirPath = filepath.FromSlash(dirPath)
return os.MkdirAll(dirPath, 0755) return os.MkdirAll(dirPath, 0755)
} }
@ -163,7 +158,6 @@ func (w *LocalClient) Stat(ctx context.Context, name string) (fs.FileInfo, error
return nil, err return nil, err
} }
absPath = filepath.FromSlash(absPath)
stat, err := os.Stat(absPath) stat, err := os.Stat(absPath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, FileDoesNotExistError{path: absPath} return nil, FileDoesNotExistError{path: absPath}

View File

@ -19,7 +19,6 @@ func NewLocalRootPath(root string) localRootPath {
func (rp *localRootPath) Join(name string) (string, error) { func (rp *localRootPath) Join(name string) (string, error) {
absPath := filepath.Join(rp.rootPath, name) absPath := filepath.Join(rp.rootPath, name)
if !strings.HasPrefix(absPath, rp.rootPath) { if !strings.HasPrefix(absPath, rp.rootPath) {
return "", fmt.Errorf("relative path escapes root: %s", name) return "", fmt.Errorf("relative path escapes root: %s", name)
} }