Skip to content

feat: Migrate PathValid API to use Win32 API to reduce PowerShell overhead #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 1 addition & 13 deletions pkg/os/filesystem/api.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/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
23 changes: 23 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package utils

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
"golang.org/x/sys/windows"
"k8s.io/klog/v2"
)

Expand All @@ -29,3 +32,23 @@ func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
out, err := cmd.CombinedOutput()
return out, err
}

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: %d", path, attrs)
return attrs != windows.INVALID_FILE_ATTRIBUTES, nil
}