-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
217 lines (176 loc) · 5.09 KB
/
cleaner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"errors"
"os"
"sort"
"strings"
"time"
)
// ShowCleaningItem is used to display current cleaning package information.
type ShowCleaningItem func(pack *PackageInfo)
// CleanResult stores processing result of cleaning.
type CleanResult struct {
FileCount int
CleanCount int
CleanSize int64
SoftwareCount int
ScoopPath string
BackupPath string
Action ActionType
CleanPackages Packages
}
// PackageInfo stores the information of software installation file.
type PackageInfo struct {
Name string
Version string
Size int64
FileName string
}
// Packages use to implement sort.interface()
type Packages []*PackageInfo
func (p Packages) Len() int {
return len(p)
}
func (p Packages) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Packages) Less(i, j int) bool {
name_i := strings.ToLower(p[i].Name)
name_j := strings.ToLower(p[j].Name)
version_i := strings.ToLower(p[i].Version)
version_j := strings.ToLower(p[j].Version)
return name_i < name_j || name_i == name_j && version_i < version_j
}
// ActionType defines the types of action that can be executed.
type ActionType int
const (
List ActionType = iota
Backup
Delete
)
// ActionInfo stores the clean action information.
type ActionInfo struct {
Action ActionType
ScoopPath string
}
// GetScoopPath gets the formal path string from the command parameter
// or environment variable. At last, ensure the path exists.
func GetScoopPath(param string) (string, error) {
var s string
var err error
if param == "" {
scoop := os.Getenv("SCOOP")
if scoop == "" {
return "", errors.New("environment variable SCOOP not found")
}
s, err = JoinFileName(scoop, "cache")
} else {
s, err = FormatFileName(param)
}
if err != nil {
return "", err
}
if FileExists(s) {
return s, nil
} else {
return "", errors.New("Scoop cache path [" + s + "] does not exist")
}
}
// CleanScoopCache moves outdated installation files to the backup directory.
func CleanScoopCache(result *CleanResult, showItem ShowCleaningItem) error {
if result.CleanCount > 0 {
if result.Action == List {
for _, p := range result.CleanPackages {
showItem(p)
}
} else if result.Action == Backup {
var err error
result.BackupPath, err = prepareBackupPath(result.ScoopPath)
if err != nil {
return err
}
for _, p := range result.CleanPackages {
old, _ := JoinFileName(result.ScoopPath, p.FileName)
new, _ := JoinFileName(result.BackupPath, p.FileName)
if err := os.Rename(old, new); err != nil {
return err
}
showItem(p)
}
} else if result.Action == Delete {
for _, p := range result.CleanPackages {
old, _ := JoinFileName(result.ScoopPath, p.FileName)
if err := os.Remove(old); err != nil {
return err
}
showItem(p)
}
}
}
return nil
}
// FindObsoletePackages finds obsolete packages in specified path.
func FindObsoletePackages(action *ActionInfo) (*CleanResult, error) {
f, err := os.Open(action.ScoopPath)
if err != nil {
return nil, err
}
// get the list of files in the specified directory.
// file names are in alphabetical ascending order.
// so the latest package of each software is relatively behind the file list.
files, err := f.Readdir(0)
if err != nil {
return nil, err
}
result := &CleanResult{0, 0, 0, 0, action.ScoopPath, "", action.Action, make([]*PackageInfo, 0)}
count := len(files)
newestPackage := PackageInfo{"", "", 0, ""}
// process files in the list in reverse order.
// then first package is the newest one.
for i := count - 1; i >= 0; i-- {
file := files[i]
// skip directories.
if file.IsDir() {
continue
}
result.FileCount++
// skip none scoop installation files by checking isPackage.
if currentPackage, isPackage := getPackageInfo(file.Name()); isPackage {
if !strings.EqualFold(currentPackage.Name, newestPackage.Name) {
// found a new package, it is the newest one.
result.SoftwareCount++
newestPackage.Name = currentPackage.Name
newestPackage.Version = currentPackage.Version
} else if !strings.EqualFold(currentPackage.Version, newestPackage.Version) {
// found old version package.
result.CleanCount++
result.CleanSize += file.Size()
result.CleanPackages = append(result.CleanPackages, currentPackage)
currentPackage.Size = file.Size()
currentPackage.FileName = file.Name()
}
}
}
sort.Sort(result.CleanPackages)
return result, nil
}
// prepareBackupPath creates the backup directory when necessary.
func prepareBackupPath(scoopPath string) (string, error) {
s, err := JoinFileName(scoopPath, time.Now().Format("bak_2006-01-02T15-04-05"))
if err == nil && !FileExists(s) {
if err = os.Mkdir(s, 0777); err != nil {
return "", err
}
}
return s, err
}
// getPackageInfo returns the package information from file name.
func getPackageInfo(fileName string) (*PackageInfo, bool) {
// the installation file name in scoop format is "name#version#other-information".
parts := strings.Split(fileName, "#")
// ignore invalid file name.
if len(parts) != 3 {
return nil, false
}
return &PackageInfo{parts[0], parts[1], 0, ""}, true
}