Skip to content

Commit b25a643

Browse files
committed
Move constant LongPathPrefix to utils package
1 parent 79fb1d3 commit b25a643

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

pkg/os/smb/api.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"syscall"
77

88
"github.com/kubernetes-csi/csi-proxy/pkg/cim"
9+
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
910
"golang.org/x/sys/windows"
1011
)
1112

1213
const (
1314
credentialDelimiter = ":"
14-
longPathPrefix = `\\?\`
1515
)
1616

1717
type API interface {
@@ -88,14 +88,8 @@ func (*SmbAPI) NewSmbLink(remotePath, localPath string) error {
8888
// so add one if needed.
8989
remotePath = remotePath + "\\"
9090
}
91-
longRemotePath := remotePath
92-
if !strings.HasPrefix(longRemotePath, longPathPrefix) {
93-
longRemotePath = longPathPrefix + longRemotePath
94-
}
95-
longLocalPath := localPath
96-
if !strings.HasPrefix(longLocalPath, longPathPrefix) {
97-
longLocalPath = longPathPrefix + longLocalPath
98-
}
91+
longRemotePath := utils.EnsureLongPath(remotePath)
92+
longLocalPath := utils.EnsureLongPath(localPath)
9993

10094
err := createSymlink(longLocalPath, longRemotePath, true)
10195
if err != nil {

pkg/os/volume/api.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/go-ole/go-ole"
1212
"github.com/kubernetes-csi/csi-proxy/pkg/cim"
13+
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
1314
wmierrors "github.com/microsoft/wmi/pkg/errors"
1415
"github.com/pkg/errors"
1516
"golang.org/x/sys/windows"
@@ -57,8 +58,6 @@ var (
5758
// PS C:\disks> (Get-Disk -Number 1 | Get-Partition | Get-Volume).UniqueId
5859
// \\?\Volume{452e318a-5cde-421e-9831-b9853c521012}\
5960
VolumeRegexp = regexp.MustCompile(`Volume\{[\w-]*\}`)
60-
// longPathPrefix is the prefix of Windows long path
61-
longPathPrefix = "\\\\?\\"
6261

6362
notMountedFolder = errors.New("not a mounted folder")
6463
)
@@ -337,7 +336,7 @@ func getTarget(mount string) (string, error) {
337336
if err != nil {
338337
return "", err
339338
}
340-
targetPath := longPathPrefix + windows.UTF16PtrToString(&outPathBuffer[0])
339+
targetPath := utils.EnsureLongPath(windows.UTF16PtrToString(&outPathBuffer[0]))
341340
if !strings.HasSuffix(targetPath, "\\") {
342341
targetPath += "\\"
343342
}

pkg/utils/utils.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ package utils
33
import (
44
"os"
55
"os/exec"
6+
"strings"
67

78
"k8s.io/klog/v2"
89
)
910

10-
const MaxPathLengthWindows = 260
11+
const (
12+
MaxPathLengthWindows = 260
13+
14+
// LongPathPrefix is the prefix of Windows long path
15+
LongPathPrefix = `\\?\`
16+
)
17+
18+
func EnsureLongPath(path string) string {
19+
if !strings.HasPrefix(path, LongPathPrefix) {
20+
path = LongPathPrefix + path
21+
}
22+
return path
23+
}
1124

1225
func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
1326
cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)

0 commit comments

Comments
 (0)