fs.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. )
  10. //Auto detect and execute the correct binary
  11. func autoDetectExecutable() string {
  12. if runtime.GOOS == "windows" {
  13. if fileExists("arozos.exe") {
  14. return "arozos.exe"
  15. }
  16. } else {
  17. if fileExists("arozos") {
  18. return "arozos"
  19. }
  20. }
  21. //Not build from source. Look for release binary names
  22. binaryExecPath := "arozos_" + runtime.GOOS + "_" + runtime.GOARCH
  23. if runtime.GOOS == "windows" {
  24. binaryExecPath += ".exe"
  25. }
  26. if fileExists(binaryExecPath) {
  27. return binaryExecPath
  28. } else {
  29. fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
  30. os.Exit(1)
  31. return ""
  32. }
  33. }
  34. func getUpdateBinaryFilename() (string, error) {
  35. updateFiles, err := filepath.Glob("./updates/*")
  36. if err != nil {
  37. return "", err
  38. }
  39. for _, thisFile := range updateFiles {
  40. if !isDir(thisFile) && filepath.Ext(thisFile) != ".gz" {
  41. //This might be the file
  42. return thisFile, nil
  43. }
  44. }
  45. return "", errors.New("file not found")
  46. }
  47. func copy(src, dst string) (int64, error) {
  48. sourceFileStat, err := os.Stat(src)
  49. if err != nil {
  50. return 0, err
  51. }
  52. if !sourceFileStat.Mode().IsRegular() {
  53. return 0, errors.New("invalid file")
  54. }
  55. source, err := os.Open(src)
  56. if err != nil {
  57. return 0, err
  58. }
  59. defer source.Close()
  60. destination, err := os.Create(dst)
  61. if err != nil {
  62. return 0, err
  63. }
  64. defer destination.Close()
  65. nBytes, err := io.Copy(destination, source)
  66. return nBytes, err
  67. }
  68. func isDir(path string) bool {
  69. fileInfo, err := os.Stat(path)
  70. if err != nil {
  71. return false
  72. }
  73. return fileInfo.IsDir()
  74. }
  75. func fileExists(name string) bool {
  76. _, err := os.Stat(name)
  77. if err == nil {
  78. return true
  79. }
  80. if errors.Is(err, os.ErrNotExist) {
  81. return false
  82. }
  83. return false
  84. }