Skip to content

Commit

Permalink
osutil/disks: fix name resolving of devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Meulengracht committed Oct 2, 2024
1 parent a1e1c31 commit c0096f6
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions osutil/disks/mockdisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,36 @@ func MockPartitionDeviceNodeToDiskMapping(mockedDisks map[string]*MockDiskMappin
}

func resolveName(deviceName string) (string, error) {
corrected := path.Join(dirs.GlobalRootDir, deviceName)
if osutil.IsSymlink(corrected) {
resolved, err := filepath.EvalSymlinks(corrected)
if err != nil {
resolve := func(p string) (string, error) {
if !osutil.FileExists(p) {
return "", nil
}
if osutil.IsSymlink(p) {
resolved, err := filepath.EvalSymlinks(p)
if err != nil {
return "", err
}
return resolved, nil
}
return p, nil
}

if res, err := resolve(deviceName); err != nil {
return "", err
} else if res == "" {
// did not exist, try again but with corrected path
if res, err := resolve(path.Join(dirs.GlobalRootDir, deviceName)); err != nil {
return "", err
} else if res == "" {
// did not exist at all, meaning we assume it's the name of
// the device, not a path
return deviceName, nil
} else {
return strings.TrimPrefix(res, dirs.GlobalRootDir), nil
}
corrected = resolved
} else {
return res, nil
}
return strings.TrimPrefix(corrected, dirs.GlobalRootDir), nil
}

// MockDeviceNameToDiskMapping will mock DiskFromDeviceName such that the
Expand Down

0 comments on commit c0096f6

Please sign in to comment.