Skip to content

Commit

Permalink
Remove the unnecessary 'terminal' variable
Browse files Browse the repository at this point in the history
We can simply compare 'remainder' against nil. Doing this makes it more
explicit that we don't need to return the remainder in the GotSymlink
case.
  • Loading branch information
EdSchouten committed Jun 22, 2024
1 parent cfde925 commit c56dbb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
29 changes: 12 additions & 17 deletions pkg/filesystem/path/unix_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ type unixRelativeParser struct {

func (rp unixRelativeParser) ParseFirstComponent(componentWalker ComponentWalker, mustBeDirectory bool) (next GotDirectoryOrSymlink, remainder RelativeParser, err error) {
var name string
terminal := false
if slash := strings.IndexByte(rp.path, '/'); slash == -1 {
// Path no longer contains a slash. Consume it entirely.
terminal = true
name = rp.path
remainder = nil
} else {
Expand All @@ -71,10 +69,9 @@ func (rp unixRelativeParser) ParseFirstComponent(componentWalker ComponentWalker

switch name {
case "", ".":
// An explicit "." entry, or an empty component.
// Empty components can occur if paths end with
// one or more slashes. Treat "foo/" as identical
// to "foo/."
// An explicit "." entry, or an empty component. Empty
// components can occur if paths end with one or more
// slashes. Treat "foo/" as identical to "foo/."
return GotDirectory{Child: componentWalker}, remainder, nil
case "..":
// Traverse to the parent directory.
Expand All @@ -85,12 +82,10 @@ func (rp unixRelativeParser) ParseFirstComponent(componentWalker ComponentWalker
return GotDirectory{Child: parent}, remainder, nil
}

// A filename that was followed by a
// slash, or we are symlink expanding
// one or more paths that are followed
// by a slash. This component must yield
// a directory or symlink.
if mustBeDirectory || !terminal {
// A filename that was followed by a slash, or we are symlink
// expanding one or more paths that are followed by a slash.
// This component must yield a directory or symlink.
if mustBeDirectory || remainder != nil {
r, err := componentWalker.OnDirectory(Component{
name: name,
})
Expand All @@ -105,15 +100,15 @@ func (rp unixRelativeParser) ParseFirstComponent(componentWalker ComponentWalker
name: name,
})
if err != nil || r == nil {
// Path resolution ended with
// any file other than a symlink.
// Path resolution ended with any file other than a
// symlink.
return nil, nil, err
}

// Observed a symlink at the end of a
// path. We should continue to run.
// Observed a symlink at the end of a path. We should continue
// to run.
return GotSymlink{
Parent: r.Parent,
Target: r.Target,
}, remainder, nil
}, nil, nil
}
6 changes: 2 additions & 4 deletions pkg/filesystem/path/windows_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ type windowsRelativeParser struct {

func (rp windowsRelativeParser) ParseFirstComponent(componentWalker ComponentWalker, mustBeDirectory bool) (next GotDirectoryOrSymlink, remainder RelativeParser, err error) {
var name string
terminal := false
if separator := strings.IndexAny(rp.path, "/\\"); separator == -1 {
// Path no longer contains a separator. Consume it entirely.
terminal = true
name = rp.path
remainder = nil
} else {
Expand Down Expand Up @@ -91,7 +89,7 @@ func (rp windowsRelativeParser) ParseFirstComponent(componentWalker ComponentWal
// A filename that was followed by a separator, or we are
// symlink expanding one or more paths that are followed by a
// separator. This component must yield a directory or symlink.
if mustBeDirectory || !terminal {
if mustBeDirectory || remainder != nil {
r, err := componentWalker.OnDirectory(Component{
name: name,
})
Expand All @@ -116,5 +114,5 @@ func (rp windowsRelativeParser) ParseFirstComponent(componentWalker ComponentWal
return GotSymlink{
Parent: r.Parent,
Target: r.Target,
}, remainder, nil
}, nil, nil
}

0 comments on commit c56dbb3

Please sign in to comment.