-
Notifications
You must be signed in to change notification settings - Fork 0
/
wslCommans.go
285 lines (242 loc) · 7.25 KB
/
wslCommans.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
package main
import (
"encoding/json"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/sys/windows/registry"
)
func runCommand(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
return cmd
}
func (a *App) TerminateWsl(name string) {
_, err := runCommand("wsl", "-t", name).Output()
if err != nil {
a.log(2, "Cannot terminate wsl ("+name+"): "+err.Error())
}
}
func removeEmptyChars(str string) string {
newArr := make([]byte, 0)
for _, letter := range str {
if letter == 0 {
continue
}
newArr = append(newArr, byte(letter))
}
return string(newArr)
}
func (a *App) getWslList() string {
type wslLine struct {
Default_ bool
Name string
Status string
Wsl_version int
}
out, _ := runCommand("wsl", "-l", "-v").Output()
commandStr := removeEmptyChars(string(out))
rows := strings.Split(commandStr, "\n")
nameStart := strings.Index(rows[0], "NAME")
stateStart := strings.Index(rows[0], "STATE")
versionStart := strings.Index(rows[0], "VERSION")
// json umwandeln
wslLines := make([]wslLine, 0)
for _, line := range rows[1:] {
if len(line) < 2 {
continue
}
var default_ bool
if string(line[0]) == "*" {
default_ = true
} else {
default_ = false
}
name := strings.Trim(string(line[nameStart:stateStart]), " ")
status := strings.Trim(string(line[stateStart:versionStart]), " ")
wsl_versionStr := strings.Trim(string(line[versionStart:]), " ")
wsl_versionStr = strings.Replace(wsl_versionStr, "\r", "", 1)
wsl_version, _ := strconv.Atoi(wsl_versionStr)
wslLines = append(wslLines, wslLine{default_, name, status, wsl_version})
}
jsonWsl, _ := json.Marshal(wslLines)
return string(jsonWsl)
}
func (a *App) CreateBackupFile(name string, filename string, breaker int) {
backupPath := currentSettings.BackupPath + "\\" + name
backupFile := backupPath + "\\" + filename
if _, err := os.ReadDir(backupPath); err != nil {
a.log(1, "Cannot open backup folder ("+backupPath+"): "+err.Error())
if err := os.MkdirAll(backupPath, os.ModePerm); err != nil {
a.log(2, "Cannot create backup folder ("+backupPath+"): "+err.Error())
}
}
out, err := runCommand("wsl", "--export", "--vhd", name, backupFile).Output()
if out != nil {
a.log(0, "WSL export output ("+backupFile+"): "+string(out))
}
if err != nil {
a.log(2, "WSL export error ("+backupFile+"): "+err.Error())
if breaker >= 1 {
return
} else {
if !a.RequestShutdownWsl() {
return
}
breaker++
a.CreateBackupFile(name, filename, breaker)
}
}
}
type myfile struct {
Name string
ModDate string
ModDateInt int64
}
func (a *App) GetBackupFiles(name string) string {
backupPath := currentSettings.BackupPath + "\\" + name
out, _ := os.ReadDir(backupPath)
backupFiles := make([]myfile, 0)
for _, item := range out {
name := item.Name()
info, _ := item.Info()
modDate := info.ModTime()
fmodDate := modDate.Format("02.01.2006 15:04")
file := myfile{name, fmodDate, modDate.Unix()}
backupFiles = append(backupFiles, file)
}
jsonStr, _ := json.Marshal(backupFiles)
return string(jsonStr)
}
func (a *App) RenameBackupFile(name string, newName string, distroName string) {
backupPath := currentSettings.BackupPath + "\\" + distroName
backupFile := backupPath + "\\" + name
newBackupFile := backupPath + "\\" + newName
if _, err := os.ReadFile(newBackupFile); err == nil {
a.log(2, "("+newBackupFile+")"+" already exists")
return
}
err := os.Rename(backupFile, newBackupFile)
if err != nil {
a.log(2, "Cannot rename ("+backupFile+") to ("+newBackupFile+"): "+err.Error())
return
}
a.log(0, "Renamed: ("+backupFile+") to ("+newBackupFile+")")
}
func (a *App) OpenBackupFolder(distroName string) {
backupPath := currentSettings.BackupPath + "\\" + distroName
_, err := runCommand("explorer", backupPath).Output()
if err != nil {
a.log(2, "Cannot open backup folder ("+backupPath+"): "+err.Error())
}
}
func (a *App) getDistroPath(disroName string) string {
regPath := `Software\Microsoft\Windows\CurrentVersion\Lxss`
lxss, err := registry.OpenKey(registry.CURRENT_USER, regPath, registry.READ)
if err != nil {
a.log(2, "Cannot open registry path ("+regPath+"): "+err.Error())
}
defer lxss.Close()
distros, err := lxss.ReadSubKeyNames(0)
if err != nil {
a.log(2, "Cannot read sub keys ("+regPath+"): "+err.Error())
}
for _, key := range distros {
skey, err := registry.OpenKey(registry.CURRENT_USER, regPath+`\`+key, registry.QUERY_VALUE)
if err != nil {
a.log(2, "Cannot open registry key ("+regPath+`\`+key+"): "+err.Error())
}
defer skey.Close()
name, _, err := skey.GetStringValue("DistributionName")
if err != nil {
a.log(2, "Cannot get DistributionName ("+regPath+`\`+key+"): "+err.Error())
}
if name == disroName {
ext4FileLocation, _, err := skey.GetStringValue("BasePath")
if err != nil {
a.log(2, "Cannot get BasePath ("+regPath+`\`+key+"): "+err.Error())
}
return ext4FileLocation
}
}
return ""
}
func (a *App) RestoreDistro(filename string, disroName string) {
dPath := a.getDistroPath(disroName)
if dPath == "" {
return
}
ext4FileLocation := "'" + dPath + `\` + "ext4.vhdx" + "'"
bakFile := "'" + dPath + `\` + "ext4.vhdx.bak" + "'"
for i := 0; i < 2; i++ {
if i == 1 {
if !a.RequestShutdownWsl() {
return
}
}
_, err := runCommand("powershell", "-Command", "Rename-Item", ext4FileLocation, bakFile).Output()
if err != nil {
a.log(2, "Cannot rename ("+ext4FileLocation+"): "+err.Error())
if i == 1 {
return
}
} else {
break
}
}
restoreFile := "'" + currentSettings.BackupPath + "\\" + disroName + "\\" + filename + "'"
_, err := runCommand("powershell", "-Command", "Copy-Item", restoreFile, ext4FileLocation).Output()
if err != nil {
a.log(2, "Cannot copy ("+restoreFile+") to ("+ext4FileLocation+"): "+err.Error())
return
}
_, err2 := runCommand("powershell", "-Command", "Remove-Item", bakFile).Output()
if err2 != nil {
a.log(2, "Cannot delete ("+bakFile+"): "+err.Error())
}
a.log(0, "Restored: ("+restoreFile+") to ("+ext4FileLocation+")")
}
func (a *App) shutdownWsl() {
a.log(1, "WSL shutting down")
_, err := runCommand("wsl", "--shutdown").Output()
if err != nil {
a.log(2, "WSL shutdown error: "+err.Error())
}
}
func (a *App) openWslShutdownWindow(c chan bool) {
unregisterEvents := func(optionalData ...interface{}) {
runtime.EventsOff(a.ctx, "WslShutdownConfirmed", "WslShutdownCanceled")
}
runtime.EventsOn(a.ctx, "WslShutdownConfirmed", func(optionalData ...interface{}) {
unregisterEvents()
c <- true
})
runtime.EventsOn(a.ctx, "WslShutdownCanceled", func(optionalData ...interface{}) {
unregisterEvents()
c <- false
})
}
func (a *App) RequestShutdownWsl() bool {
a.log(1, "WSL shutdown needed")
runtime.EventsEmit(a.ctx, "openWslShutdown", nil)
c := make(chan bool)
go a.openWslShutdownWindow(c)
res := <-c
if res {
a.log(0, "WSL shutdown confirmed")
a.shutdownWsl()
return true
} else {
a.log(2, "WSL shutdown canceled")
return false
}
}
func (a *App) LaunchDistro(name string) {
_, err := runCommand("cmd", "/c", "start", "wsl", "-d", name, "--cd", "~").Output()
if err != nil {
a.log(2, "Cannot launch ("+name+"): "+err.Error())
}
}