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
27 changes: 10 additions & 17 deletions pkg/filesystem/hostapi/hostapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/kubernetes-csi/csi-proxy/v2/pkg/utils"
)
Expand Down Expand Up @@ -49,17 +48,6 @@ func (filesystemAPI) PathExists(path string) (bool, error) {
return pathExists(path)
}

func pathValid(path string) (bool, error) {
cmd := `Test-Path $Env:remotepath`
cmdEnv := fmt.Sprintf("remotepath=%s", path)
output, err := utils.RunPowershellCmd(cmd, cmdEnv)
if err != nil {
return false, fmt.Errorf("returned output: %s, error: %v", string(output), err)
}

return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
}

// PathValid determines whether all elements of a path exist
//
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-7
Expand All @@ -68,7 +56,7 @@ func pathValid(path string) (bool, error) {
//
// e.g. in a SMB server connection, if password is changed, connection will be lost, this func will return false
func (filesystemAPI) PathValid(path string) (bool, error) {
return pathValid(path)
return utils.IsPathValid(path)
}

// Mkdir makes a dir with `os.MkdirAll`.
Expand Down Expand Up @@ -124,18 +112,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
37 changes: 0 additions & 37 deletions pkg/filesystem/hostapi/hostapi_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/system/hostapi/hostapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
var (
serviceStateCheckInternal = 200 * time.Millisecond
serviceStateCheckTimeout = 30 * time.Second
errTimedOut = errors.New("Timed out")
errTimedOut = errors.New("timed out")
)

type ServiceManager interface {
Expand Down
30 changes: 20 additions & 10 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
Expand All @@ -18,22 +17,33 @@ const (
LongPathPrefix = `\\?\`
)

func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
command = fmt.Sprintf("$global:ProgressPreference = 'SilentlyContinue'; %s", command)
cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
cmd.Env = append(os.Environ(), envs...)
klog.V(8).Infof("Executing command: %q", cmd.String())
out, err := cmd.CombinedOutput()
return out, err
}

func EnsureLongPath(path string) string {
if !strings.HasPrefix(path, LongPathPrefix) {
path = LongPathPrefix + path
}
return path
}

func IsPathValid(path string) (bool, error) {
pathString, err := windows.UTF16PtrFromString(path)
if err != nil {
return false, fmt.Errorf("invalid path: %w", err)
}

attrs, err := windows.GetFileAttributes(pathString)
if err != nil {
if errors.Is(err, windows.ERROR_PATH_NOT_FOUND) || errors.Is(err, windows.ERROR_FILE_NOT_FOUND) || errors.Is(err, windows.ERROR_INVALID_NAME) {
return false, nil
}

// GetFileAttribute returns user or password incorrect for a disconnected SMB connection after the password is changed
return false, fmt.Errorf("failed to get path %s attribute: %w", path, err)
}

klog.V(6).Infof("Path %s attribute: %O", path, attrs)
return attrs != windows.INVALID_FILE_ATTRIBUTES, nil
}

// IsMountedFolder checks whether the `path` is a mounted folder.
func IsMountedFolder(path string) (bool, error) {
// https://learn.microsoft.com/en-us/windows/win32/fileio/determining-whether-a-directory-is-a-volume-mount-point
Expand Down
Loading