Skip to content

Commit f39bd14

Browse files
committed
Failing closed after maximum retry is achieved to avoid inifite recursion
1 parent 07be14d commit f39bd14

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

pkg/os/volume/api.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454
// PS C:\disks> (Get-Disk -Number 1 | Get-Partition | Get-Volume).UniqueId
5555
// \\?\Volume{452e318a-5cde-421e-9831-b9853c521012}\
5656
VolumeRegexp = regexp.MustCompile(`Volume\{[\w-]*\}`)
57+
5758
)
5859

5960
// New - Construct a new Volume API Implementation.
@@ -268,7 +269,7 @@ func (VolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
268269

269270
// GetVolumeIDFromTargetPath - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out
270271
func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
271-
volumeString, err := getTarget(mount)
272+
volumeString, err := getTarget(mount, 0)
272273

273274
if err != nil {
274275
return "", fmt.Errorf("error getting the volume for the mount %s, internal error %v", mount, err)
@@ -277,22 +278,28 @@ func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
277278
return volumeString, nil
278279
}
279280

280-
func getTarget(mount string) (string, error) {
281+
func getTarget(mount string, retry int) (string, error) {
281282
cmd := `(Get-Item -Path $Env:mountpath).Target`
282283
cmdEnv := fmt.Sprintf("mountpath=%s", mount)
283284
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
284-
if err != nil || len(out) == 0 {
285+
if err != nil || len(out) == 0 {
285286
return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
286287
}
288+
if retry >= 256 {
289+
return "", fmt.Errorf("maximum recursion reached, cmd: %s, output: %s, :retry %d", cmd, string(out), retry)
290+
}
291+
287292
volumeString := strings.TrimSpace(string(out))
293+
klog.V(8).Infof("retry: %d, volumeString: %s", retry, volumeString)
294+
288295
if !strings.HasPrefix(volumeString, "Volume") {
289-
return getTarget(volumeString)
296+
return getTarget(volumeString, retry+1)
290297
}
291298

292299
return ensureVolumePrefix(volumeString), nil
293300
}
294301

295-
// GetVolumeIDFromTargetPath returns the volume id of a given target path.
302+
// GetClosestVolumeIDFromTargetPath returns the volume id of a given target path.
296303
func (VolumeAPI) GetClosestVolumeIDFromTargetPath(targetPath string) (string, error) {
297304
volumeString, err := findClosestVolume(targetPath)
298305

pkg/os/volume/api_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package volume
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestGetTarget(t *testing.T) {
9+
tests := []struct {
10+
mountpath string
11+
expectedResult string
12+
expectError bool
13+
counter int
14+
}{
15+
{
16+
"c:\\",
17+
"",
18+
true,
19+
1,
20+
},
21+
}
22+
for _, test := range tests {
23+
target, err := getTarget(test.mountpath, test.counter)
24+
if test.expectError {
25+
assert.NotNil(t, err, "Expect error during getTarget(%s)", test.mountpath)
26+
} else {
27+
assert.Nil(t, err, "Expect error is nil during getTarget(%s)", test.mountpath)
28+
}
29+
assert.Equal(t, target, test.expectedResult, "Expect result not equal with getTarget(%s) return: %q, expected: %s, error: %v",
30+
test.mountpath, target, test.expectedResult, err)
31+
}
32+
}

0 commit comments

Comments
 (0)