-
Notifications
You must be signed in to change notification settings - Fork 6
/
fs.go
162 lines (138 loc) · 3.29 KB
/
fs.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
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
cp "github.com/otiai10/copy"
)
func restoreConfigs() {
restoreIfExists("system/bridge.json")
restoreIfExists("system/dev.uuid")
restoreIfExists("system/cron.json")
restoreIfExists("system/storage.json")
restoreIfExists("web/SystemAO/vendor/")
//Restore start script
if fileExists("./arozos.old/start.sh") {
copy("./arozos.old/start.sh", "./start.sh")
}
if fileExists("./arozos.old/start.bat") {
copy("./arozos.old/start.bat", "./start.bat")
}
}
func restoreOldArozOS() {
fmt.Println("[LAUNCHER] ArozOS unable to launch. Restoring from backup")
if fileExists("arozos.old") {
backupfiles, err := filepath.Glob("arozos.old/*")
if err != nil {
fmt.Println("[LAUNCHER] Unable to restore backup. Exiting.")
os.Exit(1)
}
for _, thisBackupFile := range backupfiles {
if isDir(thisBackupFile) {
cp.Copy(thisBackupFile, "./"+filepath.Base(thisBackupFile))
} else {
copy(thisBackupFile, "./"+filepath.Base(thisBackupFile))
}
}
} else {
fmt.Println("[LAUNCHER] ArozOS backup not found. Exiting.")
os.Exit(1)
}
}
func restoreIfExists(fileRelPath string) {
if fileExists(filepath.Join("arozos.old", fileRelPath)) {
if !isDir(filepath.Join("arozos.old", fileRelPath)) {
copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
} else {
cp.Copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
}
}
}
// Auto detect and execute the correct binary
func autoDetectExecutable() string {
if runtime.GOOS == "windows" {
if fileExists("arozos.exe") {
return "arozos.exe"
}
} else {
if fileExists("arozos") {
return "./arozos"
}
}
//Not build from source. Look for release binary names
binaryExecPath := "arozos_" + runtime.GOOS + "_" + runtime.GOARCH
if runtime.GOOS == "windows" {
binaryExecPath += ".exe"
}
binaryExecPath = "./" + binaryExecPath
if fileExists(binaryExecPath) {
return binaryExecPath
} else {
//Binary not found. Try download it
err := downloadReleaseBinary()
if err != nil {
fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
os.Exit(1)
return ""
}
if runtime.GOOS == "windows" {
return "./arozos.exe"
} else {
return "./arozos"
}
}
}
func getUpdateBinaryFilename() (string, error) {
updateFiles, err := filepath.Glob("./updates/*")
if err != nil {
return "", err
}
for _, thisFile := range updateFiles {
if !isDir(thisFile) && filepath.Ext(thisFile) != ".gz" {
//This might be the file
return thisFile, nil
}
}
return "", errors.New("file not found")
}
func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, errors.New("invalid file")
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func isDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
func fileExists(name string) bool {
_, err := os.Stat(name)
if err == nil {
return true
}
if errors.Is(err, os.ErrNotExist) {
return false
}
return false
}