device.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package configs
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. const (
  7. Wildcard = -1
  8. )
  9. // TODO Windows: This can be factored out in the future
  10. type Device struct {
  11. // Device type, block, char, etc.
  12. Type rune `json:"type"`
  13. // Path to the device.
  14. Path string `json:"path"`
  15. // Major is the device's major number.
  16. Major int64 `json:"major"`
  17. // Minor is the device's minor number.
  18. Minor int64 `json:"minor"`
  19. // Cgroup permissions format, rwm.
  20. Permissions string `json:"permissions"`
  21. // FileMode permission bits for the device.
  22. FileMode os.FileMode `json:"file_mode"`
  23. // Uid of the device.
  24. Uid uint32 `json:"uid"`
  25. // Gid of the device.
  26. Gid uint32 `json:"gid"`
  27. // Write the file to the allowed list
  28. Allow bool `json:"allow"`
  29. }
  30. func (d *Device) CgroupString() string {
  31. return fmt.Sprintf("%c %s:%s %s", d.Type, deviceNumberString(d.Major), deviceNumberString(d.Minor), d.Permissions)
  32. }
  33. func (d *Device) Mkdev() int {
  34. return int((d.Major << 8) | (d.Minor & 0xff) | ((d.Minor & 0xfff00) << 12))
  35. }
  36. // deviceNumberString converts the device number to a string return result.
  37. func deviceNumberString(number int64) string {
  38. if number == Wildcard {
  39. return "*"
  40. }
  41. return fmt.Sprint(number)
  42. }