fs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  63. if fileExists(binaryExecPath) {
  64. return binaryExecPath
  65. } else {
  66. fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
  67. os.Exit(1)
  68. return ""
  69. }
  70. }
  71. func getUpdateBinaryFilename() (string, error) {
  72. updateFiles, err := filepath.Glob("./updates/*")
  73. if err != nil {
  74. return "", err
  75. }
  76. for _, thisFile := range updateFiles {
  77. if !isDir(thisFile) && filepath.Ext(thisFile) != ".gz" {
  78. //This might be the file
  79. return thisFile, nil
  80. }
  81. }
  82. return "", errors.New("file not found")
  83. }
  84. func copy(src, dst string) (int64, error) {
  85. sourceFileStat, err := os.Stat(src)
  86. if err != nil {
  87. return 0, err
  88. }
  89. if !sourceFileStat.Mode().IsRegular() {
  90. return 0, errors.New("invalid file")
  91. }
  92. source, err := os.Open(src)
  93. if err != nil {
  94. return 0, err
  95. }
  96. defer source.Close()
  97. destination, err := os.Create(dst)
  98. if err != nil {
  99. return 0, err
  100. }
  101. defer destination.Close()
  102. nBytes, err := io.Copy(destination, source)
  103. return nBytes, err
  104. }
  105. func isDir(path string) bool {
  106. fileInfo, err := os.Stat(path)
  107. if err != nil {
  108. return false
  109. }
  110. return fileInfo.IsDir()
  111. }
  112. func fileExists(name string) bool {
  113. _, err := os.Stat(name)
  114. if err == nil {
  115. return true
  116. }
  117. if errors.Is(err, os.ErrNotExist) {
  118. return false
  119. }
  120. return false
  121. }