-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcommon.go
More file actions
326 lines (287 loc) · 9.29 KB
/
common.go
File metadata and controls
326 lines (287 loc) · 9.29 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package system
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
license "github.com/linuxdeepin/go-dbus-factory/system/com.deepin.license"
"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/keyfile"
"github.com/linuxdeepin/go-lib/log"
)
const (
// DeepinImmutableCtlPath is the path of deepin-immutable-ctl
DeepinImmutableCtlPath = "/usr/sbin/deepin-immutable-ctl"
)
type MirrorSource struct {
Id string `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
NameLocale map[string]string `json:"name_locale"`
Weight int `json:"weight"`
Country string `json:"country"`
AdjustDelay int `json:"adjust_delay"` // ms
}
var RepoInfos []RepositoryInfo
var logger = log.NewLogger("lastore/system")
type RepositoryInfo struct {
Name string `json:"name"`
Url string `json:"url"`
Mirror string `json:"mirror"`
}
func DecodeJson(fpath string, d interface{}) error {
// #nosec G304
f, err := os.Open(fpath)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
return json.NewDecoder(f).Decode(&d)
}
func EncodeJson(fpath string, d interface{}) error {
f, err := os.Create(fpath)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
return json.NewEncoder(f).Encode(d)
}
func NormalFileExists(fpath string) bool {
info, err := os.Stat(fpath)
if err != nil {
return false
}
if info.IsDir() {
return false
}
return true
}
// UpgradeStatusAndReason 用于记录整体更新安装的流程状态和原因,dde-session-daemon和回滚界面会根据该配置进行提示
type UpgradeStatusAndReason struct {
Status UpgradeStatus
ReasonCode JobErrorType
}
// UpgradeStatus 整体更新安装的流程状态
type UpgradeStatus string
const (
UpgradeReady UpgradeStatus = "ready"
UpgradeRunning UpgradeStatus = "running"
UpgradeFailed UpgradeStatus = "failed"
)
type JobErrorType string
func (j JobErrorType) String() string {
return string(j)
}
const (
NoError JobErrorType = "NoError"
ErrorUnknown JobErrorType = "ErrorUnknown"
ErrorProgram JobErrorType = "ErrorProgram"
ErrorFetchFailed JobErrorType = "fetchFailed"
ErrorDpkgError JobErrorType = "dpkgError"
ErrorPkgNotFound JobErrorType = "pkgNotFound"
ErrorDpkgInterrupted JobErrorType = "dpkgInterrupted"
ErrorDependenciesBroken JobErrorType = "dependenciesBroken"
ErrorUnmetDependencies JobErrorType = "unmetDependencies"
ErrorNoInstallationCandidate JobErrorType = "noInstallationCandidate"
ErrorInsufficientSpace JobErrorType = "insufficientSpace"
ErrorUnauthenticatedPackages JobErrorType = "unauthenticatedPackages"
ErrorOperationNotPermitted JobErrorType = "operationNotPermitted"
ErrorIndexDownloadFailed JobErrorType = "IndexDownloadFailed"
ErrorIO JobErrorType = "ioError"
ErrorDamagePackage JobErrorType = "damagePackage" // 包损坏,需要删除后重新下载或者安装
ErrorInvalidSourcesList JobErrorType = "invalidSourceList"
ErrorPlatformUnreachable JobErrorType = "platformUnreachable"
ErrorMissCoreFile JobErrorType = "missCoreFile"
ErrorScript JobErrorType = "scriptError"
ErrorProgressCheck JobErrorType = "progressCheckError"
ErrorCheckMetaInfoFile JobErrorType = "ErrorCheckMetaInfoFile"
ErrorPreUpdateCheckScriptsFailed JobErrorType = "ErrorPreUpdateCheckScriptsFailed"
ErrorPostUpdateCheckScriptsFailed JobErrorType = "ErrorPostUpdateCheckScriptsFailed"
ErrorPreDownloadCheckScriptsFailed JobErrorType = "ErrorPreDownloadCheckScriptsFailed"
ErrorPostDownloadCheckScriptsFailed JobErrorType = "ErrorPostDownloadCheckScriptsFailed"
ErrorPreBackupCheckScriptsFailed JobErrorType = "ErrorPreBackupCheckScriptsFailed"
ErrorPostBackupCheckScriptsFailed JobErrorType = "ErrorPostBackupCheckScriptsFailed"
ErrorPreCheckScriptsFailed JobErrorType = "ErrorPreCheckScriptsFailed"
ErrorMidCheckScriptsFailed JobErrorType = "ErrorMidCheckScriptsFailed"
ErrorPostCheckScriptsFailed JobErrorType = "ErrorPostCheckScriptsFailed"
ErrorSysPkgInfoLoad JobErrorType = "ErrorSysPkgInfoLoad"
ErrorCheckToolsDependFailed JobErrorType = "ErrorCheckToolsDependFailed"
ErrorMetaInfoFile JobErrorType = "ErrorMetaInfoFile"
ErrorDpkgVersion JobErrorType = "ErrorDpkgVersion"
ErrorDpkgNotFound JobErrorType = "ErrorDpkgNotFound"
ErrorCheckProgramFailed JobErrorType = "ErrorCheckProgramFailed"
ErrorCheckServiceFailed JobErrorType = "ErrorCheckServiceFailed"
ErrorCheckSysDiskOutSpace JobErrorType = "ErrorCheckSysDiskOutSpace"
ErrorCheckProcessNotRunning JobErrorType = "ErrorCheckProcessNotRunning"
ErrorCheckPkgNotFound JobErrorType = "ErrorCheckPkgNotFound"
ErrorCheckPkgState JobErrorType = "ErrorCheckPkgState"
ErrorCheckPkgVersion JobErrorType = "ErrorCheckPkgVersion"
// running状态
ErrorNeedCheck JobErrorType = "needCheck"
)
func HandleDelayPackage(hold bool, packages []string) {
action := "unhold"
if hold {
action = "hold"
}
args := []string{
action,
}
args = append(args, packages...)
err := exec.Command("apt-mark", args...).Run()
if err != nil {
logger.Warning(err)
}
}
type UpdateModeStatus string
const (
NoUpdate UpdateModeStatus = "noUpdate" // 无更新
NotDownload UpdateModeStatus = "notDownload" // 包含了有更新没下载
IsDownloading UpdateModeStatus = "isDownloading"
DownloadPause UpdateModeStatus = "downloadPause"
DownloadErr UpdateModeStatus = "downloadFailed"
CanUpgrade UpdateModeStatus = "downloaded" // Downloaded
WaitRunUpgrade UpdateModeStatus = "upgradeReady" // 进行备份+更新时,当处于更新未开始状态
Upgrading UpdateModeStatus = "upgrading"
UpgradeErr UpdateModeStatus = "upgradeFailed"
Upgraded UpdateModeStatus = "needReboot" // need reboot
)
type ABStatus string
const (
NotBackup ABStatus = "notBackup"
// NotSupportBackup ABStatus = "notSupportBackup"
BackingUp ABStatus = "backingUp"
BackupFailed ABStatus = "backupFailed"
HasBackedUp ABStatus = "hasBackedUp"
)
type ABErrorType string
const (
NoABError ABErrorType = "noError"
CanNotBackup ABErrorType = "canNotBackup"
OtherError ABErrorType = "otherError"
)
type UiActiveState int32
const (
Unknown UiActiveState = -1 // 未知
Unauthorized UiActiveState = 0 // 未授权
Authorized UiActiveState = 1 // 已授权
AuthorizedLapse UiActiveState = 2 // 授权失效
TrialAuthorized UiActiveState = 3 // 试用期已授权
TrialExpired UiActiveState = 4 // 试用期已过期
)
func IsAuthorized() bool {
edition, err := getEditionName()
if err != nil {
return false
}
// 社区版不需要鉴权
if edition == "Community" {
return true
}
sysBus, err := dbusutil.NewSystemService()
if err != nil {
logger.Warning(err)
return false
}
licenseObj := license.NewLicense(sysBus.Conn())
state, err := licenseObj.AuthorizationState().Get(0)
if err != nil {
logger.Warning(err)
return false
}
if UiActiveState(state) == Authorized || UiActiveState(state) == TrialAuthorized {
return true
}
return false
}
func IsActiveCodeExist() bool {
sysBus, err := dbusutil.NewSystemService()
if err != nil {
logger.Warning(err)
return false
}
licenseObj := license.NewLicense(sysBus.Conn())
code, err := licenseObj.ActiveCode().Get(0)
if err != nil {
logger.Warning(err)
return false
}
return strings.TrimSpace(code) != ""
}
func CheckLock(p string) (string, bool) {
// #nosec G304
file, err := os.Open(p)
if err != nil {
logger.Warningf("error opening %q: %v", p, err)
return "", false
}
defer func() {
_ = file.Close()
}()
flockT := syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: io.SeekStart,
Start: 0,
Len: 0,
Pid: 0,
}
err = syscall.FcntlFlock(file.Fd(), syscall.F_GETLK, &flockT)
if err != nil {
logger.Warningf("unable to check file %q lock status: %s", p, err)
return p, true
}
if flockT.Type == syscall.F_WRLCK {
return p, true
}
return "", false
}
func getEditionName() (string, error) {
kf := keyfile.NewKeyFile()
err := kf.LoadFromFile("/etc/os-version")
if err != nil {
return "", err
}
editionName, err := kf.GetString("Version", "EditionName")
if err != nil {
return "", err
}
return editionName, nil
}
// 单位是K
func GetFreeSpace(diskPath string) (int, error) {
content, err := exec.Command("/usr/bin/df", "-BK", "--output=avail", diskPath).CombinedOutput()
if err == nil {
var contentKb string
scanner := bufio.NewScanner(strings.NewReader(string(content)))
lineCount := 1
for scanner.Scan() {
if lineCount == 2 {
contentKb = scanner.Text()
break
}
lineCount++
}
spaceStr := strings.Replace(contentKb, "K", "", -1)
spaceStr = strings.TrimSpace(spaceStr)
spaceNum, err := strconv.Atoi(spaceStr)
if err == nil {
spaceNum = spaceNum * 1000
return spaceNum, nil
} else {
return 0, err
}
}
return 0, fmt.Errorf("run /usr/bin/df -BK --output=avail %v err: %v", diskPath, string(content))
}