fs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. cp "github.com/otiai10/copy"
  10. )
  11. func restoreConfigs() {
  12. restoreIfExists("system/bridge.json")
  13. restoreIfExists("system/dev.uuid")
  14. restoreIfExists("system/cron.json")
  15. restoreIfExists("system/storage.json")
  16. restoreIfExists("web/SystemAO/vendor/")
  17. //Restore start script
  18. if fileExists("./arozos.old/start.sh") {
  19. copy("./arozos.old/start.sh", "./start.sh")
  20. }
  21. if fileExists("./arozos.old/start.bat") {
  22. copy("./arozos.old/start.bat", "./start.bat")
  23. }
  24. }
  25. func restoreOldArozOS() {
  26. fmt.Println("[LAUNCHER] ArozOS unable to launch. Restoring from backup")
  27. if fileExists("arozos.old") {
  28. backupfiles, err := filepath.Glob("arozos.old/*")
  29. if err != nil {
  30. fmt.Println("[LAUNCHER] Unable to restore backup. Exiting.")
  31. os.Exit(1)
  32. }
  33. for _, thisBackupFile := range backupfiles {
  34. if isDir(thisBackupFile) {
  35. cp.Copy(thisBackupFile, "./"+filepath.Base(thisBackupFile))
  36. } else {
  37. copy(thisBackupFile, "./"+filepath.Base(thisBackupFile))
  38. }
  39. }
  40. } else {
  41. fmt.Println("[LAUNCHER] ArozOS backup not found. Exiting.")
  42. os.Exit(1)
  43. }
  44. }
  45. func restoreIfExists(fileRelPath string) {
  46. if fileExists(filepath.Join("arozos.old", fileRelPath)) {
  47. if !isDir(filepath.Join("arozos.old", fileRelPath)) {
  48. copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
  49. } else {
  50. cp.Copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
  51. }
  52. }
  53. }
  54. //Auto detect and execute the correct binary
  55. func autoDetectExecutable() string {
  56. if runtime.GOOS == "windows" {
  57. if fileExists("arozos.exe") {
  58. return "arozos.exe"
  59. }
  60. } else {
  61. if fileExists("arozos") {
  62. return "./arozos"
  63. }
  64. }
  65. //Not build from source. Look for release binary names
  66. binaryExecPath := "arozos_" + runtime.GOOS + "_" + runtime.GOARCH
  67. if runtime.GOOS == "windows" {
  68. binaryExecPath = "./" + binaryExecPath + ".exe"
  69. } else {
  70. binaryExecPath = "./" + binaryExecPath
  71. }
  72. if fileExists(binaryExecPath) {
  73. return binaryExecPath
  74. } else {
  75. fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
  76. os.Exit(1)
  77. return ""
  78. }
  79. }
  80. func getUpdateBinaryFilename() (string, error) {
  81. updateFiles, err := filepath.Glob("./updates/*")
  82. if err != nil {
  83. return "", err
  84. }
  85. for _, thisFile := range updateFiles {
  86. if !isDir(thisFile) && filepath.Ext(thisFile) != ".gz" {
  87. //This might be the file
  88. return thisFile, nil
  89. }
  90. }
  91. return "", errors.New("file not found")
  92. }
  93. func copy(src, dst string) (int64, error) {
  94. sourceFileStat, err := os.Stat(src)
  95. if err != nil {
  96. return 0, err
  97. }
  98. if !sourceFileStat.Mode().IsRegular() {
  99. return 0, errors.New("invalid file")
  100. }
  101. source, err := os.Open(src)
  102. if err != nil {
  103. return 0, err
  104. }
  105. defer source.Close()
  106. destination, err := os.Create(dst)
  107. if err != nil {
  108. return 0, err
  109. }
  110. defer destination.Close()
  111. nBytes, err := io.Copy(destination, source)
  112. return nBytes, err
  113. }
  114. func isDir(path string) bool {
  115. fileInfo, err := os.Stat(path)
  116. if err != nil {
  117. return false
  118. }
  119. return fileInfo.IsDir()
  120. }
  121. func fileExists(name string) bool {
  122. _, err := os.Stat(name)
  123. if err == nil {
  124. return true
  125. }
  126. if errors.Is(err, os.ErrNotExist) {
  127. return false
  128. }
  129. return false
  130. }