| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package discover |
| 4 | |
| 5 | import ( |
| 6 | "time" |
| 7 | |
| 8 | "github.com/vmware/govmomi/vim25/types" |
| 9 | ) |
| 10 | |
| 11 | const maxSnapshotTreeDepth = 64 |
| 12 | |
| 13 | type snapshotSummary struct { |
| 14 | count int64 |
| 15 | maxChainDepth int64 |
| 16 | oldestCreateTime time.Time |
| 17 | } |
| 18 | |
| 19 | func summarizeSnapshotInfo(info *types.VirtualMachineSnapshotInfo) snapshotSummary { |
| 20 | if info == nil { |
| 21 | return snapshotSummary{} |
| 22 | } |
| 23 | |
| 24 | var summary snapshotSummary |
| 25 | for i := range info.RootSnapshotList { |
| 26 | walkSnapshotTree(info.RootSnapshotList[i], 1, &summary) |
| 27 | } |
| 28 | return summary |
| 29 | } |
| 30 | |
| 31 | func walkSnapshotTree(node types.VirtualMachineSnapshotTree, depth int64, summary *snapshotSummary) { |
| 32 | if depth > maxSnapshotTreeDepth { |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | summary.count++ |
| 37 | if depth > summary.maxChainDepth { |
| 38 | summary.maxChainDepth = depth |
| 39 | } |
| 40 | if !node.CreateTime.IsZero() && (summary.oldestCreateTime.IsZero() || node.CreateTime.Before(summary.oldestCreateTime)) { |
| 41 | summary.oldestCreateTime = node.CreateTime |
| 42 | } |
| 43 | |
| 44 | for i := range node.ChildSnapshotList { |
| 45 | walkSnapshotTree(node.ChildSnapshotList[i], depth+1, summary) |
| 46 | } |
| 47 | } |