Skip to content

Commit fedd9da

Browse files
authored
Merge pull request #210 from vitaliy-leschenko/fix_smb_mount
Try to fix issue with 'Multiple connections to a server'
2 parents 26b63f8 + 0b7cc3d commit fedd9da

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/bin/
22
settings.json
33
integrationtests/integrationtests.test.exe
4+
.vs/

pkg/server/smb/server.go

+45-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ func normalizeWindowsPath(path string) string {
2525
return normalizedPath
2626
}
2727

28+
func getRootMappingPath(path string) (string, error) {
29+
items := strings.Split(path, "\\")
30+
parts := []string{}
31+
for _, s := range items {
32+
if len(s) > 0 {
33+
parts = append(parts, s)
34+
if len(parts) == 2 {
35+
break
36+
}
37+
}
38+
}
39+
if len(parts) != 2 {
40+
klog.Errorf("remote path (%s) is invalid", path)
41+
return "", fmt.Errorf("remote path (%s) is invalid", path)
42+
}
43+
// parts[0] is a smb host name
44+
// parts[1] is a smb share name
45+
return strings.ToLower("\\\\" + parts[0] + "\\" + parts[1]), nil
46+
}
47+
2848
func NewServer(hostAPI smb.API, fsServer *fsserver.Server) (*Server, error) {
2949
return &Server{
3050
hostAPI: hostAPI,
@@ -43,38 +63,48 @@ func (s *Server) NewSmbGlobalMapping(context context.Context, request *internal.
4363
return response, fmt.Errorf("remote path is empty")
4464
}
4565

46-
isMapped, err := s.hostAPI.IsSmbMapped(remotePath)
66+
mappingPath, err := getRootMappingPath(remotePath)
67+
if err != nil {
68+
return response, err
69+
}
70+
71+
isMapped, err := s.hostAPI.IsSmbMapped(mappingPath)
4772
if err != nil {
4873
isMapped = false
4974
}
5075

5176
if isMapped {
52-
valid, err := s.fsServer.PathValid(context, remotePath)
77+
klog.V(4).Infof("Remote %s already mapped. Validating...", mappingPath)
78+
79+
valid, err := s.fsServer.PathValid(context, mappingPath)
5380
if err != nil {
54-
klog.Warningf("PathValid(%s) failed with %v, ignore error", remotePath, err)
81+
klog.Warningf("PathValid(%s) failed with %v, ignore error", mappingPath, err)
5582
}
5683

5784
if !valid {
58-
klog.V(4).Infof("RemotePath %s is not valid, removing now", remotePath)
59-
err := s.hostAPI.RemoveSmbGlobalMapping(remotePath)
85+
klog.V(4).Infof("RemotePath %s is not valid, removing now", mappingPath)
86+
err := s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
6087
if err != nil {
61-
klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", remotePath, err)
88+
klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", mappingPath, err)
6289
return response, err
6390
}
6491
isMapped = false
92+
} else {
93+
klog.V(4).Infof("RemotePath %s is valid", mappingPath)
6594
}
6695
}
6796

6897
if !isMapped {
69-
klog.V(4).Infof("Remote %s not mapped. Mapping now!", remotePath)
70-
err := s.hostAPI.NewSmbGlobalMapping(remotePath, request.Username, request.Password)
98+
klog.V(4).Infof("Remote %s not mapped. Mapping now!", mappingPath)
99+
err := s.hostAPI.NewSmbGlobalMapping(mappingPath, request.Username, request.Password)
71100
if err != nil {
72101
klog.Errorf("failed NewSmbGlobalMapping %v", err)
73102
return response, err
74103
}
75104
}
76105

77106
if len(localPath) != 0 {
107+
klog.V(4).Infof("ValidatePluginPath: '%s'", localPath)
78108
err = s.fsServer.ValidatePluginPath(localPath)
79109
if err != nil {
80110
klog.Errorf("failed validate plugin path %v", err)
@@ -101,11 +131,17 @@ func (s *Server) RemoveSmbGlobalMapping(context context.Context, request *intern
101131
return response, fmt.Errorf("remote path is empty")
102132
}
103133

104-
err := s.hostAPI.RemoveSmbGlobalMapping(remotePath)
134+
mappingPath, err := getRootMappingPath(remotePath)
135+
if err != nil {
136+
return response, err
137+
}
138+
139+
err = s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
105140
if err != nil {
106141
klog.Errorf("failed RemoveSmbGlobalMapping %v", err)
107142
return response, err
108143
}
144+
109145
klog.V(2).Infof("RemoveSmbGlobalMapping on remote path %q is completed", request.RemotePath)
110146
return response, nil
111147
}

pkg/server/smb/server_test.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestNewSmbGlobalMapping(t *testing.T) {
7979
expectError: true,
8080
},
8181
{
82-
remote: "\\test\\path",
82+
remote: "\\\\hostname\\path",
8383
username: "",
8484
password: "",
8585
version: v1,
@@ -111,3 +111,51 @@ func TestNewSmbGlobalMapping(t *testing.T) {
111111
}
112112
}
113113
}
114+
115+
func TestGetRootMappingPath(t *testing.T) {
116+
testCases := []struct {
117+
remote string
118+
expectResult string
119+
expectError bool
120+
}{
121+
{
122+
remote: "",
123+
expectResult: "",
124+
expectError: true,
125+
},
126+
{
127+
remote: "hostname",
128+
expectResult: "",
129+
expectError: true,
130+
},
131+
{
132+
remote: "\\\\hostname\\path",
133+
expectResult: "\\\\hostname\\path",
134+
expectError: false,
135+
},
136+
{
137+
remote: "\\\\hostname\\path\\",
138+
expectResult: "\\\\hostname\\path",
139+
expectError: false,
140+
},
141+
{
142+
remote: "\\\\hostname\\path\\subpath",
143+
expectResult: "\\\\hostname\\path",
144+
expectError: false,
145+
},
146+
}
147+
for _, tc := range testCases {
148+
result, err := getRootMappingPath(tc.remote)
149+
if tc.expectError && err == nil {
150+
t.Errorf("Expected error but getRootMappingPath returned a nil error")
151+
}
152+
if !tc.expectError {
153+
if err != nil {
154+
t.Errorf("Expected no errors but getRootMappingPath returned error: %v", err)
155+
}
156+
if tc.expectResult != result {
157+
t.Errorf("Expected (%s) but getRootMappingPath returned (%s)", tc.expectResult, result)
158+
}
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)