Skip to content

Commit b9a9f3f

Browse files
committed
feat(apt-clean): skip immutable clean and merge cache sizes in print-json mode
When running with --print-json, lastore-apt-clean no longer executes deepin-immutable-ctl upgrade clean. Instead it queries cache sizes via --print-cache-sizes and appends immutable debtree cache info to the JSON output. - Add binImmutableCtl constant to replace hardcoded command name - Add queryImmutableCacheSizes() and supporting types - Merge immutable debtree cache into archivesInfos output - Guard JSON marshaling with options.printJSON check
1 parent 2418992 commit b9a9f3f

1 file changed

Lines changed: 84 additions & 34 deletions

File tree

src/lastore-apt-clean/main.go

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
const maxElapsed = time.Hour * 24 * 6 // 6 days
30+
const binImmutableCtl = "deepin-immutable-ctl"
3031

3132
var (
3233
binDpkg string
@@ -76,12 +77,21 @@ func main() {
7677
}
7778
findBins()
7879

79-
// 如果是增量更新,则调用deepin-immutable-ctl upgrade cleanup命令清理immutable系统的缓存deb包和ostree包分支
80+
// Handle immutable system cache if incremental update is enabled
81+
var immutableCache *immutableCacheOutput
8082
cfg := config.NewConfig(path.Join("/var/lib/lastore", "config.json"))
8183
if cfg.UseIncrementalUpdate() {
82-
err := exec.Command("deepin-immutable-ctl", "upgrade", "clean").Run()
83-
if err != nil {
84-
logger.Debugf("failed to clean upgrade cache: %v", err)
84+
if options.printJSON {
85+
var err error
86+
immutableCache, err = queryImmutableCacheSizes()
87+
if err != nil {
88+
logger.Debugf("failed to query immutable cache sizes: %v", err)
89+
}
90+
} else {
91+
err := exec.Command(binImmutableCtl, "upgrade", "clean").Run()
92+
if err != nil {
93+
logger.Debugf("failed to clean immutable cache: %v", err)
94+
}
8595
}
8696
}
8797

@@ -92,6 +102,27 @@ func main() {
92102
Files: make(map[string][]*archiveInfo),
93103
TotalSize: 0,
94104
}
105+
106+
// Append immutable debtree cache files to the output
107+
if immutableCache != nil {
108+
const immutableDebtreeCacheDir = "/persistent/ostree/debtree/cache"
109+
if files, ok := immutableCache.Files[immutableDebtreeCacheDir]; ok {
110+
archiveFiles := make([]*archiveInfo, 0, len(files))
111+
var dirTotalSize uint64
112+
for _, f := range files {
113+
archiveFiles = append(archiveFiles, &archiveInfo{
114+
Name: f.Name,
115+
Size: f.Size,
116+
})
117+
dirTotalSize += uint64(f.Size)
118+
}
119+
if len(archiveFiles) > 0 {
120+
archivesInfos.Files[immutableDebtreeCacheDir] = archiveFiles
121+
archivesInfos.TotalSize += dirTotalSize
122+
}
123+
}
124+
}
125+
95126
for _, dirInfo := range _archivesDirInfos {
96127
logger.Infof("dirInfo: %v", dirInfo)
97128
var archivesInfo *archivesInfo
@@ -109,8 +140,6 @@ func main() {
109140
logger.Fatal(err)
110141
}
111142

112-
// var testAgainDebInfoList []*debInfo
113-
114143
for _, entry := range fileInfoList {
115144
logger.Infof("entry: %v", entry)
116145
fileInfo, err := entry.Info()
@@ -133,46 +162,27 @@ func main() {
133162
delPolicy = DeleteImmediately
134163
} else {
135164
logger.Debugf("debInfo: %#v\n", debInfo)
136-
// var testAgain bool
137165
delPolicy, _ = shouldDelete(debInfo, cache)
138-
// if testAgain {
139-
// // 需要更多地判断
140-
// debInfo.fileInfo = fileInfo
141-
// testAgainDebInfoList = append(testAgainDebInfoList, debInfo)
142-
// continue
143-
// }
144166
}
145167
actWithPolicy(delPolicy, fileInfo, filename, archivesInfo)
146168
}
147169

148-
// 更新治理管控之后,不一定从本地仓库更新,所以不能在通过这种方式获取备选版本和校验,防止包被误删
149-
// t := time.Now()
150-
// err = loadCandidateVersions(testAgainDebInfoList, dirInfo.configPath)
151-
// logger.Debug("loadCandidateVersions cost:", time.Since(t))
152-
// if err != nil {
153-
// logger.Fatal("load candidate versions failed:", err)
154-
// }
155-
156-
// for _, info := range testAgainDebInfoList {
157-
// logger.Debug(">> ", info.fileInfo.Name())
158-
// delPolicy := shouldDeleteTestAgain(info)
159-
// actWithPolicy(delPolicy, info.fileInfo, info.filename, archivesInfo)
160-
// }
161170
if archivesInfo != nil {
162171
archivesInfos.Files[archivesInfo.dir] = archivesInfo.Files
163172
archivesInfos.TotalSize += archivesInfo.TotalSize
164173
}
165174
}
166175

167-
data, err := json.Marshal(archivesInfos)
168-
if err != nil {
169-
logger.Fatal(err)
170-
}
171-
_, err = os.Stdout.Write(data)
172-
if err != nil {
173-
logger.Fatal(err)
176+
if options.printJSON {
177+
data, err := json.Marshal(archivesInfos)
178+
if err != nil {
179+
logger.Fatal(err)
180+
}
181+
_, err = os.Stdout.Write(data)
182+
if err != nil {
183+
logger.Fatal(err)
184+
}
174185
}
175-
176186
}
177187

178188
type archivesInfo struct {
@@ -206,6 +216,46 @@ type archiveInfo struct {
206216
Size int64 `json:"size"`
207217
}
208218

219+
// immutableCacheOutput parses the output of deepin-immutable-ctl upgrade clean --print-cache-sizes
220+
//
221+
// Example output:
222+
//
223+
// {
224+
// "files": {
225+
// "/persistent/ostree/debtree/cache": [
226+
// {"name": "game-pp_5.3.2272.521_i386.deb", "size": 154281314},
227+
// {"name": "io.qt.qtcreator_8.0.2+szbt5_amd64.deb", "size": 26607790}
228+
// ],
229+
// "/persistent/ostree/repo": [],
230+
// "/sysroot/ostree/repo": []
231+
// },
232+
// "total": 180889104
233+
// }
234+
type immutableCacheOutput struct {
235+
Files map[string][]*immutableFileInfo `json:"files"`
236+
Total uint64 `json:"total"`
237+
}
238+
239+
type immutableFileInfo struct {
240+
Name string `json:"name"`
241+
Size int64 `json:"size"`
242+
}
243+
244+
// queryImmutableCacheSizes queries deb package size info in the immutable system cache
245+
func queryImmutableCacheSizes() (*immutableCacheOutput, error) {
246+
output, err := exec.Command(binImmutableCtl, "upgrade", "clean", "--print-cache-sizes").Output()
247+
if err != nil {
248+
return nil, err
249+
}
250+
251+
var result immutableCacheOutput
252+
err = json.Unmarshal(output, &result)
253+
if err != nil {
254+
return nil, fmt.Errorf("failed to parse immutable cache output: %v", err)
255+
}
256+
return &result, nil
257+
}
258+
209259
func actWithPolicy(deletePolicy DeletePolicy, fileInfo os.FileInfo, filename string, archivesInfo *archivesInfo) {
210260
needDelete := false
211261
switch deletePolicy {

0 commit comments

Comments
 (0)