fs.go 2.9 KB

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