@@ -32,6 +32,13 @@ func (m *manager) listSnapshots(ctx context.Context, filter *ListSnapshotsFilter
3232 if err != nil {
3333 return nil , fmt .Errorf ("list snapshots: %w" , err )
3434 }
35+ counts , err := m .snapshotForkCounts ()
36+ if err != nil {
37+ return nil , err
38+ }
39+ for i := range snapshots {
40+ snapshots [i ].RefCount = counts [snapshots [i ].Id ]
41+ }
3542 return snapshots , nil
3643}
3744
@@ -44,6 +51,11 @@ func (m *manager) getSnapshot(ctx context.Context, snapshotID string) (*Snapshot
4451 }
4552 return nil , err
4653 }
54+ refCount , err := m .countSnapshotForks (snapshotID )
55+ if err != nil {
56+ return nil , err
57+ }
58+ snapshot .RefCount = refCount
4759 return snapshot , nil
4860}
4961
@@ -679,26 +691,34 @@ func (m *manager) listSnapshotRecords() ([]snapshotRecord, error) {
679691}
680692
681693func (m * manager ) countSnapshotForks (snapshotID string ) (int , error ) {
682- metaFiles , err := m .listMetadataFiles ()
694+ counts , err := m .snapshotForkCounts ()
683695 if err != nil {
684696 return 0 , err
685697 }
698+ return counts [snapshotID ], nil
699+ }
700+
701+ func (m * manager ) snapshotForkCounts () (map [string ]int , error ) {
702+ metaFiles , err := m .listMetadataFiles ()
703+ if err != nil {
704+ return nil , err
705+ }
686706
687- count := 0
707+ counts := make ( map [ string ] int )
688708 for _ , metaPath := range metaFiles {
689709 content , err := os .ReadFile (metaPath )
690710 if err != nil {
691- return 0 , fmt .Errorf ("read instance metadata %s: %w" , metaPath , err )
711+ return nil , fmt .Errorf ("read instance metadata %s: %w" , metaPath , err )
692712 }
693713 var meta metadata
694714 if err := json .Unmarshal (content , & meta ); err != nil {
695- return 0 , fmt .Errorf ("unmarshal instance metadata %s: %w" , metaPath , err )
715+ return nil , fmt .Errorf ("unmarshal instance metadata %s: %w" , metaPath , err )
696716 }
697- if meta .ForkOfSnapshot == snapshotID {
698- count ++
717+ if meta .ForkOfSnapshot != "" {
718+ counts [ meta . ForkOfSnapshot ] ++
699719 }
700720 }
701- return count , nil
721+ return counts , nil
702722}
703723
704724func snapshotMemHardlinkSource (srcDir string ) (absPath , relSlash string , ok bool ) {
0 commit comments