required.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package samba
  2. import (
  3. "fmt"
  4. "log"
  5. "os/exec"
  6. "strings"
  7. "imuslab.com/arozos/mod/fileservers"
  8. "imuslab.com/arozos/mod/user"
  9. )
  10. /*
  11. Functions requested by the file server service router
  12. */
  13. func (m *ShareManager) ServerToggle(enabled bool) error {
  14. return SetSmbdEnableState(enabled)
  15. }
  16. func (m *ShareManager) IsEnabled() bool {
  17. smbdRunning, err := checkSmbdRunning()
  18. if err != nil {
  19. log.Println("Unable to get smbd state: " + err.Error())
  20. return false
  21. }
  22. return smbdRunning
  23. }
  24. func (m *ShareManager) GetEndpoints(userInfo *user.User) []*fileservers.Endpoint {
  25. //Get a list of connection endpoint for this user
  26. eps := []*fileservers.Endpoint{}
  27. for _, fsh := range userInfo.GetAllAccessibleFileSystemHandler() {
  28. if fsh.IsNetworkDrive() {
  29. continue
  30. }
  31. fshID := fsh.UUID
  32. if fsh.RequierUserIsolation() {
  33. //User seperated storage. Only mount the user one
  34. fshID = userInfo.Username + "_" + fsh.UUID
  35. }
  36. shareExists, err := m.ShareExists(fshID)
  37. if err != nil || !shareExists {
  38. continue
  39. }
  40. eps = append(eps, &fileservers.Endpoint{
  41. ProtocolName: "//",
  42. Port: 0,
  43. Subpath: "/" + fshID,
  44. })
  45. }
  46. return eps
  47. }
  48. func checkSmbdRunning() (bool, error) {
  49. // Run the system command to check the smbd service status
  50. cmd := exec.Command("systemctl", "is-active", "--quiet", "smbd")
  51. err := cmd.Run()
  52. // If the command exits with status 0, smbd is running
  53. if err == nil {
  54. return true, nil
  55. }
  56. // If the command exits with a non-zero status, smbd is not running
  57. // We can check the error message to be sure it's not another issue
  58. if exitError, ok := err.(*exec.ExitError); ok {
  59. if strings.TrimSpace(string(exitError.Stderr)) == "" {
  60. return false, nil
  61. }
  62. return false, fmt.Errorf("error checking smbd status: %s", exitError.Stderr)
  63. }
  64. return false, fmt.Errorf("unexpected error checking smbd status: %v", err)
  65. }