Skip to content

Commit 8563980

Browse files
Fix path for a smb global mapping to allow mount the share more that once
1 parent 26b63f8 commit 8563980

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ func normalizeWindowsPath(path string) string {
2525
return normalizedPath
2626
}
2727

28+
func normalizeMappingPath(path string) string {
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+
return strings.ToLower("\\\\" + parts[0] + "\\" + parts[1])
40+
}
41+
2842
func NewServer(hostAPI smb.API, fsServer *fsserver.Server) (*Server, error) {
2943
return &Server{
3044
hostAPI: hostAPI,
@@ -43,38 +57,45 @@ func (s *Server) NewSmbGlobalMapping(context context.Context, request *internal.
4357
return response, fmt.Errorf("remote path is empty")
4458
}
4559

46-
isMapped, err := s.hostAPI.IsSmbMapped(remotePath)
60+
mappingPath := normalizeMappingPath(remotePath)
61+
62+
isMapped, err := s.hostAPI.IsSmbMapped(mappingPath)
4763
if err != nil {
4864
isMapped = false
4965
}
5066

5167
if isMapped {
52-
valid, err := s.fsServer.PathValid(context, remotePath)
68+
klog.V(4).Infof("Remote %s already mapped. Validating...", mappingPath)
69+
70+
valid, err := s.fsServer.PathValid(context, mappingPath)
5371
if err != nil {
54-
klog.Warningf("PathValid(%s) failed with %v, ignore error", remotePath, err)
72+
klog.Warningf("PathValid(%s) failed with %v, ignore error", mappingPath, err)
5573
}
5674

5775
if !valid {
58-
klog.V(4).Infof("RemotePath %s is not valid, removing now", remotePath)
59-
err := s.hostAPI.RemoveSmbGlobalMapping(remotePath)
76+
klog.V(4).Infof("RemotePath %s is not valid, removing now", mappingPath)
77+
err := s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
6078
if err != nil {
61-
klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", remotePath, err)
79+
klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", mappingPath, err)
6280
return response, err
6381
}
6482
isMapped = false
83+
} else {
84+
klog.V(4).Infof("RemotePath %s is valid", mappingPath)
6585
}
6686
}
6787

6888
if !isMapped {
69-
klog.V(4).Infof("Remote %s not mapped. Mapping now!", remotePath)
70-
err := s.hostAPI.NewSmbGlobalMapping(remotePath, request.Username, request.Password)
89+
klog.V(4).Infof("Remote %s not mapped. Mapping now!", mappingPath)
90+
err := s.hostAPI.NewSmbGlobalMapping(mappingPath, request.Username, request.Password)
7191
if err != nil {
7292
klog.Errorf("failed NewSmbGlobalMapping %v", err)
7393
return response, err
7494
}
7595
}
7696

7797
if len(localPath) != 0 {
98+
klog.V(4).Infof("ValidatePluginPath: '%s'", localPath)
7899
err = s.fsServer.ValidatePluginPath(localPath)
79100
if err != nil {
80101
klog.Errorf("failed validate plugin path %v", err)
@@ -101,11 +122,13 @@ func (s *Server) RemoveSmbGlobalMapping(context context.Context, request *intern
101122
return response, fmt.Errorf("remote path is empty")
102123
}
103124

104-
err := s.hostAPI.RemoveSmbGlobalMapping(remotePath)
125+
mappingPath := normalizeMappingPath(remotePath)
126+
err := s.hostAPI.RemoveSmbGlobalMapping(mappingPath)
105127
if err != nil {
106128
klog.Errorf("failed RemoveSmbGlobalMapping %v", err)
107129
return response, err
108130
}
131+
109132
klog.V(2).Infof("RemoveSmbGlobalMapping on remote path %q is completed", request.RemotePath)
110133
return response, nil
111134
}

0 commit comments

Comments
 (0)