Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions pkg/os/filesystem/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ func New() API {
return filesystemAPI{}
}

func pathExists(path string) (bool, error) {
_, err := os.Lstat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func (filesystemAPI) PathExists(path string) (bool, error) {
return pathExists(path)
return utils.PathExists(path)
}

// PathValid determines whether all elements of a path exist
Expand Down Expand Up @@ -112,18 +101,23 @@ func (filesystemAPI) IsSymlink(tgt string) (bool, error) {
// This code is similar to k8s.io/kubernetes/pkg/util/mount except the pathExists usage.
// Also in a remote call environment the os error cannot be passed directly back, hence the callers
// are expected to perform the isExists check before calling this call in CSI proxy.
stat, err := os.Lstat(tgt)
isSymlink, err := utils.IsPathSymlink(tgt)
if err != nil {
return false, err
}

// mounted folder created by SetVolumeMountPoint may still report ModeSymlink == 0
mountedFolder, err := utils.IsMountedFolder(tgt)
if err != nil {
return false, err
}

// If its a link and it points to an existing file then its a mount point.
if stat.Mode()&os.ModeSymlink != 0 {
if isSymlink || mountedFolder {
target, err := os.Readlink(tgt)
if err != nil {
return false, fmt.Errorf("readlink error: %v", err)
}
exists, err := pathExists(target)
exists, err := utils.PathExists(target)
if err != nil {
return false, err
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ func CreateSymlink(link, target string, isDir bool) error {
)
return err
}

// PathExists checks whether the given `path` exists.
func PathExists(path string) (bool, error) {
_, err := os.Lstat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
Loading