From 2ccfaa0b08461ff95849a263953833dd4ef74a54 Mon Sep 17 00:00:00 2001 From: Maxim Patlasov Date: Thu, 9 May 2024 17:23:10 -0700 Subject: [PATCH] Remove stale archived volume copy before Rename The user might put non-unique spec for subdir: ``` subDir: ${pvc.metadata.namespace}/${pvc.metadata.name} ``` Then, `os.Rename(internalVolumePath, archivedInternalVolumePath)` can fail becaue the destination (`archivedInternalVolumePath`) already exists. It's safer to remove it before `os.Rename()`. --- pkg/smb/controllerserver.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/smb/controllerserver.go b/pkg/smb/controllerserver.go index 4865274d24d..0786b0d547e 100644 --- a/pkg/smb/controllerserver.go +++ b/pkg/smb/controllerserver.go @@ -189,8 +189,11 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) } } - // archive subdirectory under base-dir + // archive subdirectory under base-dir. Remove stale archived copy if exists. klog.V(2).Infof("archiving subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath) + if err = os.RemoveAll(archivedInternalVolumePath); err != nil { + return nil, status.Errorf(codes.Internal, "failed to delete archived subdirectory %s: %v", archivedInternalVolumePath, err.Error()) + } if err = os.Rename(internalVolumePath, archivedInternalVolumePath); err != nil { return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err.Error()) }