driver_unix.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +build linux darwin freebsd solaris
  2. package driver
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "sort"
  8. "github.com/containerd/continuity/devices"
  9. "github.com/containerd/continuity/sysx"
  10. )
  11. func (d *driver) Mknod(path string, mode os.FileMode, major, minor int) error {
  12. return devices.Mknod(path, mode, major, minor)
  13. }
  14. func (d *driver) Mkfifo(path string, mode os.FileMode) error {
  15. if mode&os.ModeNamedPipe == 0 {
  16. return errors.New("mode passed to Mkfifo does not have the named pipe bit set")
  17. }
  18. // mknod with a mode that has ModeNamedPipe set creates a fifo, not a
  19. // device.
  20. return devices.Mknod(path, mode, 0, 0)
  21. }
  22. // Getxattr returns all of the extended attributes for the file at path p.
  23. func (d *driver) Getxattr(p string) (map[string][]byte, error) {
  24. xattrs, err := sysx.Listxattr(p)
  25. if err != nil {
  26. return nil, fmt.Errorf("listing %s xattrs: %v", p, err)
  27. }
  28. sort.Strings(xattrs)
  29. m := make(map[string][]byte, len(xattrs))
  30. for _, attr := range xattrs {
  31. value, err := sysx.Getxattr(p, attr)
  32. if err != nil {
  33. return nil, fmt.Errorf("getting %q xattr on %s: %v", attr, p, err)
  34. }
  35. // NOTE(stevvooe): This append/copy tricky relies on unique
  36. // xattrs. Break this out into an alloc/copy if xattrs are no
  37. // longer unique.
  38. m[attr] = append(m[attr], value...)
  39. }
  40. return m, nil
  41. }
  42. // Setxattr sets all of the extended attributes on file at path, following
  43. // any symbolic links, if necessary. All attributes on the target are
  44. // replaced by the values from attr. If the operation fails to set any
  45. // attribute, those already applied will not be rolled back.
  46. func (d *driver) Setxattr(path string, attrMap map[string][]byte) error {
  47. for attr, value := range attrMap {
  48. if err := sysx.Setxattr(path, attr, value, 0); err != nil {
  49. return fmt.Errorf("error setting xattr %q on %s: %v", attr, path, err)
  50. }
  51. }
  52. return nil
  53. }
  54. // LGetxattr returns all of the extended attributes for the file at path p
  55. // not following symbolic links.
  56. func (d *driver) LGetxattr(p string) (map[string][]byte, error) {
  57. xattrs, err := sysx.LListxattr(p)
  58. if err != nil {
  59. return nil, fmt.Errorf("listing %s xattrs: %v", p, err)
  60. }
  61. sort.Strings(xattrs)
  62. m := make(map[string][]byte, len(xattrs))
  63. for _, attr := range xattrs {
  64. value, err := sysx.LGetxattr(p, attr)
  65. if err != nil {
  66. return nil, fmt.Errorf("getting %q xattr on %s: %v", attr, p, err)
  67. }
  68. // NOTE(stevvooe): This append/copy tricky relies on unique
  69. // xattrs. Break this out into an alloc/copy if xattrs are no
  70. // longer unique.
  71. m[attr] = append(m[attr], value...)
  72. }
  73. return m, nil
  74. }
  75. // LSetxattr sets all of the extended attributes on file at path, not
  76. // following any symbolic links. All attributes on the target are
  77. // replaced by the values from attr. If the operation fails to set any
  78. // attribute, those already applied will not be rolled back.
  79. func (d *driver) LSetxattr(path string, attrMap map[string][]byte) error {
  80. for attr, value := range attrMap {
  81. if err := sysx.LSetxattr(path, attr, value, 0); err != nil {
  82. return fmt.Errorf("error setting xattr %q on %s: %v", attr, path, err)
  83. }
  84. }
  85. return nil
  86. }
  87. func (d *driver) DeviceInfo(fi os.FileInfo) (maj uint64, min uint64, err error) {
  88. return devices.DeviceInfo(fi)
  89. }
  90. // Readlink was forked on Windows to fix a Golang bug, use the "os" package here
  91. func (d *driver) Readlink(p string) (string, error) {
  92. return os.Readlink(p)
  93. }