Skip to content

Commit 7b53c3a

Browse files
committed
Move PathValid API to use Win32 API
1 parent 24f2ecd commit 7b53c3a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pkg/os/filesystem/api.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package filesystem
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
7-
"strings"
88

9-
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
9+
"golang.org/x/sys/windows"
1010
)
1111

1212
// Implements the Filesystem OS API calls. All code here should be very simple
@@ -50,14 +50,22 @@ func (filesystemAPI) PathExists(path string) (bool, error) {
5050
}
5151

5252
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)
53+
pathString, err := windows.UTF16PtrFromString(path)
5654
if err != nil {
57-
return false, fmt.Errorf("returned output: %s, error: %v", string(output), err)
55+
return false, fmt.Errorf("invalid path: %w", err)
5856
}
5957

60-
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
58+
attrs, err := windows.GetFileAttributes(pathString)
59+
if err != nil {
60+
if errors.Is(err, windows.ERROR_PATH_NOT_FOUND) || errors.Is(err, windows.ERROR_FILE_NOT_FOUND) || errors.Is(err, windows.ERROR_INVALID_NAME) {
61+
return false, nil
62+
}
63+
64+
// GetFileAttribute returns user or password incorrect for a disconnected SMB connection after the password is changed
65+
return false, fmt.Errorf("failed to get path %s attribute: %w", path, err)
66+
}
67+
68+
return attrs != windows.INVALID_FILE_ATTRIBUTES, nil
6169
}
6270

6371
// PathValid determines whether all elements of a path exist

0 commit comments

Comments
 (0)