Skip to content

Commit

Permalink
fix: remove parent dir in DeleteVolume
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Sep 15, 2024
1 parent 679857c commit 942e919
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/smb/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err)
}
} else {
klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath)
rootDir := getRootDir(smbVol.subDir)
if rootDir != "" {
rootDir = filepath.Join(getInternalMountPath(d.workingMountDir, smbVol), rootDir)
} else {
rootDir = internalVolumePath
}

klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath)
if err = os.RemoveAll(internalVolumePath); err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,9 @@ func appendMountOptions(mountOptions []string, extraMountOptions map[string]stri
}
return allMountOptions
}

// getRootDir returns the root directory of the given directory
func getRootDir(path string) string {
parts := strings.Split(path, "/")
return parts[0]
}
41 changes: 41 additions & 0 deletions pkg/smb/smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,44 @@ func TestAppendMountOptions(t *testing.T) {
}
}
}

func TestGetRootPath(t *testing.T) {
tests := []struct {
desc string
dir string
expected string
}{
{
desc: "empty path",
dir: "",
expected: "",
},
{
desc: "root path",
dir: "/",
expected: "",
},
{
desc: "subdir path",
dir: "/subdir",
expected: "",
},
{
desc: "subdir path without leading slash",
dir: "subdir",
expected: "subdir",
},
{
desc: "multiple subdir path without leading slash",
dir: "subdir/subdir2",
expected: "subdir",
},
}

for _, test := range tests {
result := getRootDir(test.dir)
if result != test.expected {
t.Errorf("Unexpected result: %s, expected: %s", result, test.expected)
}
}
}

0 comments on commit 942e919

Please sign in to comment.