Skip to content

Commit

Permalink
renames
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliy-leschenko committed Oct 18, 2023
1 parent 9262380 commit 5432f81
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
21 changes: 10 additions & 11 deletions pkg/mounter/refcounter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ func getRootMappingPath(path string) (string, error) {
return strings.ToLower("\\\\" + parts[0] + "\\" + parts[1]), nil
}

// incementRemotePathReferencesCount - adds new reference between mappingPath and remotePath if it doesn't exist.
// incementVolumeIDReferencesCount - adds new reference between mappingPath and remotePath if it doesn't exist.
// How it works:
// 1. MappingPath contains two components: hostname, sharename
// 2. We create directory in basePath related to each mappingPath. It will be used as container for references.
// Example: c:\\csi\\smbmounts\\hostname\\sharename
// 3. Each reference is a file with name based on MD5 of remotePath. For debug it also will contains remotePath in body of the file.
// So, in incementRemotePathReferencesCount we create the file. In decrementRemotePathReferencesCount we remove the file.
// 3. Each reference is a file with name based on MD5 of volumeID. For debug it also will contains remotePath in body of the file.
// So, in incementVolumeIDReferencesCount we create the file. In decrementRemotePathReferencesCount we remove the file.
// Example: c:\\csi\\smbmounts\\hostname\\sharename\\092f1413e6c1d03af8b5da6f44619af8
func incementRemotePathReferencesCount(mappingPath, remotePath string, volumeID string) error {
func incementVolumeIDReferencesCount(mappingPath, remotePath string, volumeID string) error {
remotePath = strings.TrimSuffix(remotePath, "\\")
path := filepath.Join(basePath, strings.TrimPrefix(mappingPath, "\\\\"))
if err := os.MkdirAll(path, os.ModeDir); err != nil {
Expand All @@ -90,10 +90,9 @@ func incementRemotePathReferencesCount(mappingPath, remotePath string, volumeID
return err
}

// decrementRemotePathReferencesCount - removes reference between mappingPath and remotePath.
// See incementRemotePathReferencesCount to understand how references work.
func decrementRemotePathReferencesCount(mappingPath, remotePath string, volumeID string) error {
remotePath = strings.TrimSuffix(remotePath, "\\")
// decrementVolumeIDReferencesCount - removes reference between mappingPath and remotePath.
// See incementVolumeIDReferencesCount to understand how references work.
func decrementVolumeIDReferencesCount(mappingPath, volumeID string) error {
path := filepath.Join(basePath, strings.TrimPrefix(mappingPath, "\\\\"))
if err := os.MkdirAll(path, os.ModeDir); err != nil {
return err
Expand All @@ -102,9 +101,9 @@ func decrementRemotePathReferencesCount(mappingPath, remotePath string, volumeID
return os.Remove(filePath)
}

// getRemotePathReferencesCount - returns count of references between mappingPath and remotePath.
// See incementRemotePathReferencesCount to understand how references work.
func getRemotePathReferencesCount(mappingPath string) int {
// getVolumeIDReferencesCount - returns count of references between mappingPath and remotePath.
// See incementVolumeIDReferencesCount to understand how references work.
func getVolumeIDReferencesCount(mappingPath string) int {
path := filepath.Join(basePath, strings.TrimPrefix(mappingPath, "\\\\"))
if os.MkdirAll(path, os.ModeDir) != nil {
return -1
Expand Down
46 changes: 23 additions & 23 deletions pkg/mounter/refcounter_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ func TestRemotePathReferencesCounter(t *testing.T) {
}()

// by default we have no any files in `mappingPath`. So, `count` should be zero
assert.Zero(t, getRemotePathReferencesCount(mappingPath))
assert.Zero(t, getVolumeIDReferencesCount(mappingPath))
// add reference to `remotePath1`. So, `count` should be equal `1`
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath1, "vol1"))
assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath1, "vol1"))
assert.Equal(t, 1, getVolumeIDReferencesCount(mappingPath))
// add reference to `remotePath2`. So, `count` should be equal `2`
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath2, "vol2"))
assert.Equal(t, 2, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath2, "vol2"))
assert.Equal(t, 2, getVolumeIDReferencesCount(mappingPath))
// remove reference to `remotePath1`. So, `count` should be equal `1`
assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath1, "vol1"))
assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, decrementVolumeIDReferencesCount(mappingPath, "vol1"))
assert.Equal(t, 1, getVolumeIDReferencesCount(mappingPath))
// remove reference to `remotePath2`. So, `count` should be equal `0`
assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath2, "vol2"))
assert.Zero(t, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, decrementVolumeIDReferencesCount(mappingPath, "vol2"))
assert.Zero(t, getVolumeIDReferencesCount(mappingPath))
}

func TestIncementRemotePathReferencesCount(t *testing.T) {
Expand All @@ -147,7 +147,7 @@ func TestIncementRemotePathReferencesCount(t *testing.T) {
os.RemoveAll(basePath)
}()

assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol1"))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol1"))

mappingPathContainer := basePath + "\\servername\\share"
if dir, err := os.Stat(mappingPathContainer); os.IsNotExist(err) || !dir.IsDir() {
Expand All @@ -172,8 +172,8 @@ func TestDecrementRemotePathReferencesCount(t *testing.T) {
os.RemoveAll(basePath)
}()

assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol1"))
assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath, "vol1"))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol1"))
assert.Nil(t, decrementVolumeIDReferencesCount(mappingPath, remotePath, "vol1"))

mappingPathContainer := basePath + "\\servername\\share"
if dir, err := os.Stat(mappingPathContainer); os.IsNotExist(err) || !dir.IsDir() {
Expand All @@ -198,14 +198,14 @@ func TestMultiplyCallsOfIncementRemotePathReferencesCount(t *testing.T) {
os.RemoveAll(basePath)
}()

assert.Zero(t, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Zero(t, getVolumeIDReferencesCount(mappingPath))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
// next calls of `incementMappingPathCount` with the same arguments should be ignored
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.Equal(t, 1, getVolumeIDReferencesCount(mappingPath))
}

func TestMultiplyCallsOfDecrementRemotePathReferencesCount(t *testing.T) {
Expand All @@ -220,8 +220,8 @@ func TestMultiplyCallsOfDecrementRemotePathReferencesCount(t *testing.T) {
os.RemoveAll(basePath)
}()

assert.Zero(t, getRemotePathReferencesCount(mappingPath))
assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.NotNil(t, decrementRemotePathReferencesCount(mappingPath, remotePath, "vol"))
assert.Zero(t, getVolumeIDReferencesCount(mappingPath))
assert.Nil(t, incementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.Nil(t, decrementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
assert.NotNil(t, decrementVolumeIDReferencesCount(mappingPath, remotePath, "vol"))
}
8 changes: 4 additions & 4 deletions pkg/mounter/safe_mounter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (mounter *csiProxyMounter) SMBMount(source, target, fsType string, mountOpt
klog.V(2).Infof("NewSmbGlobalMapping %s on %s successfully", source, normalizedTarget)

if mounter.RemoveSMBMappingDuringUnmount {
if err := incementRemotePathReferencesCount(mappingPath, source, volumeID); err != nil {
if err := incementVolumeIDReferencesCount(mappingPath, source, volumeID); err != nil {
return fmt.Errorf("incementRemotePathReferencesCount(%s, %s, %s) failed with error: %v", mappingPath, source, volumeID, err)
}
}
Expand All @@ -148,10 +148,10 @@ func (mounter *csiProxyMounter) SMBUnmount(target string, volumeID string) error
defer unlock()

if mounter.RemoveSMBMappingDuringUnmount {
if err := decrementRemotePathReferencesCount(mappingPath, remotePath, volumeID); err != nil {
return fmt.Errorf("decrementRemotePathReferencesCount(%s, %s, %s) failed with error: %v", mappingPath, remotePath, volumeID, err)
if err := decrementVolumeIDReferencesCount(mappingPath, volumeID); err != nil {
return fmt.Errorf("decrementRemotePathReferencesCount(%s, %s) failed with error: %v", mappingPath, volumeID, err)
}
count := getRemotePathReferencesCount(mappingPath)
count := getVolumeIDReferencesCount(mappingPath)
if count == 0 {
smbUnmountRequest := &smb.RemoveSmbGlobalMappingRequest{
RemotePath: remotePath,
Expand Down

0 comments on commit 5432f81

Please sign in to comment.