forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
285 lines (246 loc) · 6.13 KB
/
utils.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
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
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
dbus "github.com/godbus/dbus"
"github.com/linuxdeepin/go-gir/gio-2.0"
"github.com/linuxdeepin/go-lib/appinfo/desktopappinfo"
"github.com/linuxdeepin/go-lib/keyfile"
"github.com/linuxdeepin/go-lib/xdg/basedir"
)
func Exist(name string) bool {
_, err := os.Stat(name)
return err == nil || os.IsExist(err)
}
type CopyFlag int
const (
CopyFileNone CopyFlag = 1 << iota
CopyFileNotKeepSymlink
CopyFileOverWrite
)
func copyFileAux(src, dst string, copyFlag CopyFlag) error {
srcStat, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("Error os.Lstat src %s: %s", src, err)
}
if (copyFlag&CopyFileOverWrite) != CopyFileOverWrite && Exist(dst) {
return fmt.Errorf("error dst file is already exist")
}
os.Remove(dst)
if (copyFlag&CopyFileNotKeepSymlink) != CopyFileNotKeepSymlink &&
(srcStat.Mode()&os.ModeSymlink) == os.ModeSymlink {
readlink, err := os.Readlink(src)
if err != nil {
return fmt.Errorf("error read symlink %s: %s", src,
err)
}
err = os.Symlink(readlink, dst)
if err != nil {
return fmt.Errorf("error creating symlink %s to %s: %s",
readlink, dst, err)
}
return nil
}
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("error opening src file %s: %s", src, err)
}
defer srcFile.Close()
dstFile, err := os.OpenFile(
dst,
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
srcStat.Mode(),
)
if err != nil {
return fmt.Errorf("error opening dst file %s: %s", dst, err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("error in copy from %s to %s: %s", src, dst,
err)
}
return nil
}
func copyFile(src, dst string, copyFlag CopyFlag) error {
srcStat, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("error os.Stat src %s: %s", src, err)
}
if srcStat.IsDir() {
return fmt.Errorf("error src is a directory: %s", src)
}
if Exist(dst) {
dstStat, err := os.Lstat(dst)
if err != nil {
return fmt.Errorf("error os.Lstat dst %s: %s", dst, err)
}
if dstStat.IsDir() {
dst = path.Join(dst, path.Base(src))
} else {
if (copyFlag & CopyFileOverWrite) == 0 {
return fmt.Errorf("error dst %s is alreadly exist", dst)
}
}
}
return copyFileAux(src, dst, copyFlag)
}
func syncFile(filename string) error {
fh, err := os.Open(filename)
if err != nil {
return err
}
defer fh.Close()
return fh.Sync()
}
func getDelayTime(desktopFile string) (time.Duration, error) {
dai, err := desktopappinfo.NewDesktopAppInfoFromFile(desktopFile)
if err != nil {
return 0, err
}
num, _ := dai.GetInt(desktopappinfo.MainSection, KeyXGnomeAutostartDelay)
return time.Second * time.Duration(num), nil
}
func showDDEWelcome() error {
systemBus, err := dbus.SystemBus()
if err != nil {
return err
}
obj := systemBus.Object("com.deepin.ABRecovery", "/com/deepin/ABRecovery")
var canRestore bool
err = obj.Call("com.deepin.ABRecovery.CanRestore", 0).Store(&canRestore)
if err != nil {
return err
}
if canRestore {
return nil
}
cmd := exec.Command("/usr/lib/deepin-daemon/dde-welcome")
err = cmd.Start()
if err != nil {
return err
}
go func() {
err := cmd.Wait()
if err != nil {
logger.Warning(err)
}
}()
return nil
}
const (
AppDirName = "applications"
desktopExt = ".desktop"
)
func getAppDirs() []string {
dataDirs := basedir.GetSystemDataDirs()
dataDirs = append(dataDirs, basedir.GetUserDataDir())
var dirs []string
for _, dir := range dataDirs {
dirs = append(dirs, path.Join(dir, AppDirName))
}
return dirs
}
func getAppIdByFilePath(file string, appDirs []string) string {
file = filepath.Clean(file)
var desktopId string
for _, dir := range appDirs {
if strings.HasPrefix(file, dir) {
desktopId, _ = filepath.Rel(dir, file)
break
}
}
if desktopId == "" {
return ""
}
return strings.TrimSuffix(desktopId, desktopExt)
}
type GSettingsConfig struct {
autoStartDelay int32
iowaitEnabled bool
memcheckerEnabled bool
swapSchedEnabled bool
wmCmd string
needQuickBlackScreen bool
loginReminder bool
}
func getGSettingsConfig() *GSettingsConfig {
gs := gio.NewSettings("com.deepin.dde.startdde")
cfg := &GSettingsConfig{
autoStartDelay: gs.GetInt("autostart-delay"),
iowaitEnabled: gs.GetBoolean("iowait-enabled"),
memcheckerEnabled: gs.GetBoolean("memchecker-enabled"),
swapSchedEnabled: gs.GetBoolean("swap-sched-enabled"),
wmCmd: gs.GetString("wm-cmd"),
needQuickBlackScreen: gs.GetBoolean("quick-black-screen"),
loginReminder: gs.GetBoolean("login-reminder"),
}
gs.Unref()
return cfg
}
func initGSettingsConfig() {
if _gSettingsConfig == nil {
_gSettingsConfig = getGSettingsConfig()
}
}
func isOSDRunning() (bool, error) {
sessionBus, err := dbus.SessionBus()
if err != nil {
return false, err
}
var has bool
err = sessionBus.BusObject().Call("org.freedesktop.DBus.NameHasOwner", 0,
"com.deepin.dde.osd").Store(&has)
if err != nil {
return false, err
}
return has, nil
}
func isNotificationsOwned() (bool, error) {
sessionBus, err := dbus.SessionBus()
if err != nil {
return false, err
}
var has bool
err = sessionBus.BusObject().Call("org.freedesktop.DBus.NameHasOwner", 0,
"org.freedesktop.Notifications").Store(&has)
if err != nil {
return false, err
}
return has, nil
}
func getLightDMAutoLoginUser() (string, error) {
kf := keyfile.NewKeyFile()
err := kf.LoadFromFile("/etc/lightdm/lightdm.conf")
if err != nil {
return "", err
}
v, err := kf.GetString("Seat:*", "autologin-user")
return v, err
}
// dont collect experience message if edition is community
func isCommunity() bool {
kf := keyfile.NewKeyFile()
err := kf.LoadFromFile("/etc/os-version")
// 为避免收集数据的风险,读不到此文件,或者Edition文件不存在也不收集数据
if err != nil {
return true
}
edition, err := kf.GetString("Version", "EditionName")
if err != nil {
return true
}
if edition == "Community" {
return true
}
return false
}