Browse code

daemon: remove intermediate vars when collecting diskUsage

Set values directly on the DiskUsage objects instead of using some
intermediate vars, some of which were named slightly confusing due
to them being used both for "totalSize" and "reclaimableSize".

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/11/07 22:43:32
Showing 1 changed files
... ...
@@ -26,30 +26,27 @@ func (daemon *Daemon) containerDiskUsage(ctx context.Context, verbose bool) (*ba
26 26
 			return nil, fmt.Errorf("failed to retrieve container list: %v", err)
27 27
 		}
28 28
 
29
-		// Remove image manifest descriptor from the result as it should not be included.
30
-		// https://github.com/moby/moby/pull/49407#discussion_r1954396666
31
-		for _, c := range containers {
32
-			c.ImageManifestDescriptor = nil
33
-		}
34
-
35 29
 		isActive := func(ctr *container.Summary) bool {
36 30
 			return ctr.State == container.StateRunning ||
37 31
 				ctr.State == container.StatePaused ||
38 32
 				ctr.State == container.StateRestarting
39 33
 		}
40 34
 
41
-		activeCount := int64(len(containers))
42
-
43
-		du := &backend.ContainerDiskUsage{TotalCount: activeCount}
35
+		du := &backend.ContainerDiskUsage{
36
+			ActiveCount: int64(len(containers)),
37
+			TotalCount:  int64(len(containers)),
38
+		}
44 39
 		for _, ctr := range containers {
45 40
 			du.TotalSize += ctr.SizeRw
46 41
 			if !isActive(ctr) {
47 42
 				du.Reclaimable += ctr.SizeRw
48
-				activeCount--
43
+				du.ActiveCount--
49 44
 			}
50
-		}
51 45
 
52
-		du.ActiveCount = activeCount
46
+			// Remove image manifest descriptor from the result as it should not be included.
47
+			// https://github.com/moby/moby/pull/49407#discussion_r1954396666
48
+			ctr.ImageManifestDescriptor = nil
49
+		}
53 50
 
54 51
 		if verbose {
55 52
 			du.Items = sliceutil.Deref(containers)
... ...
@@ -73,29 +70,29 @@ func (daemon *Daemon) imageDiskUsage(ctx context.Context, verbose bool) (*backen
73 73
 			return nil, errors.Wrap(err, "failed to retrieve image list")
74 74
 		}
75 75
 
76
-		reclaimable, _, err := daemon.usageLayer.Do(ctx, struct{}{}, func(ctx context.Context) (int64, error) {
76
+		totalSize, _, err := daemon.usageLayer.Do(ctx, struct{}{}, func(ctx context.Context) (int64, error) {
77 77
 			return daemon.imageService.ImageDiskUsage(ctx)
78 78
 		})
79 79
 		if err != nil {
80 80
 			return nil, errors.Wrap(err, "failed to calculate image disk usage")
81 81
 		}
82 82
 
83
-		activeCount := int64(len(images))
84
-
85
-		du := &backend.ImageDiskUsage{TotalCount: activeCount, TotalSize: reclaimable}
83
+		du := &backend.ImageDiskUsage{
84
+			ActiveCount: int64(len(images)),
85
+			Reclaimable: totalSize,
86
+			TotalCount:  int64(len(images)),
87
+			TotalSize:   totalSize,
88
+		}
86 89
 		for _, i := range images {
87 90
 			if i.Containers == 0 {
88
-				activeCount--
91
+				du.ActiveCount--
89 92
 				if i.Size == -1 || i.SharedSize == -1 {
90 93
 					continue
91 94
 				}
92
-				reclaimable -= i.Size - i.SharedSize
95
+				du.Reclaimable -= i.Size - i.SharedSize
93 96
 			}
94 97
 		}
95 98
 
96
-		du.Reclaimable = reclaimable
97
-		du.ActiveCount = activeCount
98
-
99 99
 		if verbose {
100 100
 			du.Items = sliceutil.Deref(images)
101 101
 		}
... ...
@@ -115,21 +112,20 @@ func (daemon *Daemon) localVolumesSize(ctx context.Context, verbose bool) (*back
115 115
 			return nil, err
116 116
 		}
117 117
 
118
-		activeCount := int64(len(volumes))
119
-
120
-		du := &backend.VolumeDiskUsage{TotalCount: activeCount}
118
+		du := &backend.VolumeDiskUsage{
119
+			ActiveCount: int64(len(volumes)),
120
+			TotalCount:  int64(len(volumes)),
121
+		}
121 122
 		for _, v := range volumes {
122 123
 			if v.UsageData.Size != -1 {
124
+				du.TotalSize += v.UsageData.Size
123 125
 				if v.UsageData.RefCount == 0 {
124 126
 					du.Reclaimable += v.UsageData.Size
125
-					activeCount--
127
+					du.ActiveCount--
126 128
 				}
127
-				du.TotalSize += v.UsageData.Size
128 129
 			}
129 130
 		}
130 131
 
131
-		du.ActiveCount = activeCount
132
-
133 132
 		if verbose {
134 133
 			du.Items = sliceutil.Deref(volumes)
135 134
 		}