fs.go 2.3 KB

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