Skip to content

Commit 16813d9

Browse files
committed
Move PathValid API to use Win32 API
1 parent f60b73a commit 16813d9

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

pkg/os/filesystem/api.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"strings"
87

98
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
109
)
@@ -49,17 +48,6 @@ func (filesystemAPI) PathExists(path string) (bool, error) {
4948
return pathExists(path)
5049
}
5150

52-
func pathValid(path string) (bool, error) {
53-
cmd := `Test-Path $Env:remotepath`
54-
cmdEnv := fmt.Sprintf("remotepath=%s", path)
55-
output, err := utils.RunPowershellCmd(cmd, cmdEnv)
56-
if err != nil {
57-
return false, fmt.Errorf("returned output: %s, error: %v", string(output), err)
58-
}
59-
60-
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
61-
}
62-
6351
// PathValid determines whether all elements of a path exist
6452
//
6553
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-7
@@ -68,7 +56,7 @@ func pathValid(path string) (bool, error) {
6856
//
6957
// e.g. in a SMB server connection, if password is changed, connection will be lost, this func will return false
7058
func (filesystemAPI) PathValid(path string) (bool, error) {
71-
return pathValid(path)
59+
return utils.IsPathValid(path)
7260
}
7361

7462
// Mkdir makes a dir with `os.MkdirAll`.

pkg/os/filesystem/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package filesystem
33
import (
44
"testing"
55

6+
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -25,7 +26,7 @@ func TestPathValid(t *testing.T) {
2526
}
2627

2728
for _, test := range tests {
28-
result, err := pathValid(test.remotepath)
29+
result, err := utils.IsPathValid(test.remotepath)
2930
assert.Equal(t, result, test.expectedResult, "Expect result not equal with pathValid(%s) return: %q, expected: %q, error: %v",
3031
test.remotepath, result, test.expectedResult, err)
3132
if test.expectError {

pkg/utils/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package utils
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"strings"
78

9+
"github.com/pkg/errors"
10+
"golang.org/x/sys/windows"
811
"k8s.io/klog/v2"
912
)
1013

@@ -29,3 +32,23 @@ func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
2932
out, err := cmd.CombinedOutput()
3033
return out, err
3134
}
35+
36+
func IsPathValid(path string) (bool, error) {
37+
pathString, err := windows.UTF16PtrFromString(path)
38+
if err != nil {
39+
return false, fmt.Errorf("invalid path: %w", err)
40+
}
41+
42+
attrs, err := windows.GetFileAttributes(pathString)
43+
if err != nil {
44+
if errors.Is(err, windows.ERROR_PATH_NOT_FOUND) || errors.Is(err, windows.ERROR_FILE_NOT_FOUND) || errors.Is(err, windows.ERROR_INVALID_NAME) {
45+
return false, nil
46+
}
47+
48+
// GetFileAttribute returns user or password incorrect for a disconnected SMB connection after the password is changed
49+
return false, fmt.Errorf("failed to get path %s attribute: %w", path, err)
50+
}
51+
52+
klog.V(6).Infof("Path %s attribute: %d", path, attrs)
53+
return attrs != windows.INVALID_FILE_ATTRIBUTES, nil
54+
}

0 commit comments

Comments
 (0)