@@ -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+
2848func 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}
0 commit comments