Skip to content

Commit 5c6afc6

Browse files
committed
Move PathValid function to utils package
1 parent 11033a9 commit 5c6afc6

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

pkg/os/filesystem/api.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package filesystem
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76
"path/filepath"
87

9-
"golang.org/x/sys/windows"
10-
"k8s.io/klog/v2"
8+
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
119
)
1210

1311
// Implements the Filesystem OS API calls. All code here should be very simple
@@ -50,26 +48,6 @@ func (filesystemAPI) PathExists(path string) (bool, error) {
5048
return pathExists(path)
5149
}
5250

53-
func pathValid(path string) (bool, error) {
54-
pathString, err := windows.UTF16PtrFromString(path)
55-
if err != nil {
56-
return false, fmt.Errorf("invalid path: %w", err)
57-
}
58-
59-
attrs, err := windows.GetFileAttributes(pathString)
60-
if err != nil {
61-
if errors.Is(err, windows.ERROR_PATH_NOT_FOUND) || errors.Is(err, windows.ERROR_FILE_NOT_FOUND) || errors.Is(err, windows.ERROR_INVALID_NAME) {
62-
return false, nil
63-
}
64-
65-
// GetFileAttribute returns user or password incorrect for a disconnected SMB connection after the password is changed
66-
return false, fmt.Errorf("failed to get path %s attribute: %w", path, err)
67-
}
68-
69-
klog.V(6).Infof("Path %s attribute: %d", path, attrs)
70-
return attrs != windows.INVALID_FILE_ATTRIBUTES, nil
71-
}
72-
7351
// PathValid determines whether all elements of a path exist
7452
//
7553
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-7
@@ -78,7 +56,7 @@ func pathValid(path string) (bool, error) {
7856
//
7957
// e.g. in a SMB server connection, if password is changed, connection will be lost, this func will return false
8058
func (filesystemAPI) PathValid(path string) (bool, error) {
81-
return pathValid(path)
59+
return utils.IsPathValid(path)
8260
}
8361

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

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)