flags_linux.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package mount
  2. import (
  3. "strings"
  4. "syscall"
  5. )
  6. // Parse fstab type mount options into mount() flags
  7. // and device specific data
  8. func parseOptions(options string) (int, string) {
  9. var (
  10. flag int
  11. data []string
  12. )
  13. flags := map[string]struct {
  14. clear bool
  15. flag int
  16. }{
  17. "defaults": {false, 0},
  18. "ro": {false, syscall.MS_RDONLY},
  19. "rw": {true, syscall.MS_RDONLY},
  20. "suid": {true, syscall.MS_NOSUID},
  21. "nosuid": {false, syscall.MS_NOSUID},
  22. "dev": {true, syscall.MS_NODEV},
  23. "nodev": {false, syscall.MS_NODEV},
  24. "exec": {true, syscall.MS_NOEXEC},
  25. "noexec": {false, syscall.MS_NOEXEC},
  26. "sync": {false, syscall.MS_SYNCHRONOUS},
  27. "async": {true, syscall.MS_SYNCHRONOUS},
  28. "dirsync": {false, syscall.MS_DIRSYNC},
  29. "remount": {false, syscall.MS_REMOUNT},
  30. "mand": {false, syscall.MS_MANDLOCK},
  31. "nomand": {true, syscall.MS_MANDLOCK},
  32. "atime": {true, syscall.MS_NOATIME},
  33. "noatime": {false, syscall.MS_NOATIME},
  34. "diratime": {true, syscall.MS_NODIRATIME},
  35. "nodiratime": {false, syscall.MS_NODIRATIME},
  36. "bind": {false, syscall.MS_BIND},
  37. "rbind": {false, syscall.MS_BIND | syscall.MS_REC},
  38. "relatime": {false, syscall.MS_RELATIME},
  39. "norelatime": {true, syscall.MS_RELATIME},
  40. "strictatime": {false, syscall.MS_STRICTATIME},
  41. "nostrictatime": {true, syscall.MS_STRICTATIME},
  42. }
  43. for _, o := range strings.Split(options, ",") {
  44. // If the option does not exist in the flags table then it is a
  45. // data value for a specific fs type
  46. if f, exists := flags[o]; exists {
  47. if f.clear {
  48. flag &= ^f.flag
  49. } else {
  50. flag |= f.flag
  51. }
  52. } else {
  53. data = append(data, o)
  54. }
  55. }
  56. return flag, strings.Join(data, ",")
  57. }